###前提・実現したいこと
引数付きのコンストラクタへ、ポインタ型のplanを渡したいのですが、
実行結果がplanがうまく表示されません。
どのように記述したら、この問題が解決されるのでしょうか。
###発生している問題・エラーメッセージ
this is constrcuter
----------p2.ShowPhone();-------------------
i = 0
plan =
pInfo = nokia
----------p3.ShowPhone();-------------------
i = 0
plan =
pInfo = nokia
i = 0
plan = normal
pInfo = nokia
This is ShowData
###ソースコード
<メイン文> #include <iostream> #include <string.h> #include "Phone.h" using namespace std; Phone::Phone(){ plan = new char[100]; cout << "this is constrcuter "<< "\n"; this->i = false; strcpy(this->plan, "normal"); this->pInfo = "nokia"; } Phone::Phone(bool i, char* plan, string pInfo){ plan = new char[100]; this->i = i; strcpy(this->plan, plan); this->pInfo = "nokia"; } Phone::~Phone(){ } void Phone::ShowPhone(){ cout << " i = " << this->i << "\n"; cout << " plan = " << this->plan <<"\n"; cout << " pInfo = " << this->pInfo <<"\n"; } void Phone::ShowData(){ cout << " This is ShowData " << "\n"; } int main(){ Phone p1; Phone p2(false, "p2","noraml2"); Phone p3(false, "p3","noraml3"); cout << "----------p2.ShowPhone();-------------------" << "\n"; p2.ShowPhone(); cout << "----------p3.ShowPhone();-------------------" << "\n"; p3.ShowPhone(); cout << "-----------------------------" << "\n"; p1.i = false; strcpy(p1.plan,"normal"); p1.pInfo = "nokia"; p1.ShowPhone(); cout << "-----------------------------" << "\n"; p2.ShowData(); return 0; } <ヘッダー文> #include <string.h> class Phone{ public: Phone(); Phone(bool i, char* plan, std::string pInfo); ~Phone(); bool i; char* plan; std::string pInfo; void ShowPhone(); virtual void ShowData(); };
###補足情報(言語/FW/ツール等のバージョンなど)
paiza.io
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/22 02:06 編集