[問題] constructor的問題
遇到的問題: (題意請描述清楚)
Cell_Phone::Cell_Phone(7);
//為何不會影響Cell_Phone_Pay內容? 7寫到哪裡去了?
希望得到的正確結果:
7
程式跑出來的錯誤結果:
4
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
VC++
有問題的code: (請善用置底文標色功能)
#include <iostream>
using namespace std;
class Cell_Phone
{
public:
Cell_Phone(int cost){Cell_Phone_Pay=cost;}
void set_data(int cost){Cell_Phone_Pay=cost;}
void show_Pay(){cout<< "手機話費NT:"<<Cell_Phone_Pay << endl;}
int Cell_Phone_Pay;
};
class Pay:public Cell_Phone
{
public:
Pay(int x):Cell_Phone(x){
// this->Cell_Phone::Cell_Phone(5);//ok
// this->Cell_Phone::Cell_Phone(6);//ok
Cell_Phone::Cell_Phone(7);
//為何不會影響Cell_Phone_Pay內容? 7寫到哪裡去了?
// Cell_Phone::set_data(8);//ok
}
};
void main(void)
{
Pay Obj(4);
Obj.show_Pay();
}
補充說明:
板上很多利害人物, 如果問題太蠢, 會盡快自刪.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.209.4
→
08/26 02:24, , 1F
08/26 02:24, 1F
→
08/26 02:26, , 2F
08/26 02:26, 2F
→
08/26 02:26, , 3F
08/26 02:26, 3F
推
08/26 02:28, , 4F
08/26 02:28, 4F
→
08/26 02:28, , 5F
08/26 02:28, 5F
→
08/26 02:29, , 6F
08/26 02:29, 6F
→
08/26 02:29, , 7F
08/26 02:29, 7F
→
08/26 02:29, , 8F
08/26 02:29, 8F
→
08/26 02:30, , 9F
08/26 02:30, 9F
→
08/26 02:34, , 10F
08/26 02:34, 10F
推
08/26 02:34, , 11F
08/26 02:34, 11F
→
08/26 02:34, , 12F
08/26 02:34, 12F
→
08/26 05:16, , 13F
08/26 05:16, 13F
※ 編輯: pttone 來自: 140.113.209.4 (06/17 05:00)
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):