いつもお世話になっております。
C++の学習を初めて1週間なので、質問が多くて申し訳ありません。
質問なのですが、コンポジションの場合、仮想関数をオーバーライドすることはできないのでしょうか。
*修正 : 回答を元に、エラー文、Workerクラスのプログラムを修正を行いました。
###前提・実現したいこと
下記のコードでは、phoneクラスを使って、Workerクラスでコンポジションを実装しています。
なぜか下記コードを実装したところ、[phoneクラスがすでに実装されている]と言われ、イニシャライザがうまく実装できません。
構文もあっていると思っているので、エラーの解決方法が分からないので、教えて頂けると幸いです。
###発生している問題・エラーメッセージ
Phone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char ' [-Wwritable-strings]
Phone p2(false, "p2","noraml2");
^
Phone.cpp:41:21: warning: ISO C++11 does not allow conversion from string literal to 'char ' [-Wwritable-strings]
Phone p3(false, "p3","noraml3");
^
2 warnings generated.
In file included from Worker.cpp:2:
./Worker.h:8:4: error: unknown type name 'Phone'
Phone phone;
^
Worker.cpp:16:61: error: use of undeclared identifier 'i'
Worker::Worker(int number, char name, double salary):Phone(i,plan,pInfo){
^
Worker.cpp:16:63: error: use of undeclared identifier 'plan'
Worker::Worker(int number, char name, double salary):Phone(i,plan,pInfo){
^
Worker.cpp:16:68: error: use of undeclared identifier 'pInfo'
Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
^
Worker.cpp:32:9: error: exception specification in declaration does not match previous declaration
Worker::~Worker(){
^
./Worker.h:12:4: note: previous declaration is here
~Worker();
^
5 errors generated.
###ソースコード
<Phone.h> #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(); }; <Phone.cpp> #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){ this->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; } <Worker.h> class Worker{ public: int number; char* name; double salary; Phone phone; Worker(); Worker(int number, char* name, double salary); Worker(const Worker &obj); ~Worker(); }; #include <iostream> #include "Worker.h" #include "Phone.h" using namespace std; Worker::Worker(){ name = new char[80]; cout<< " This is Constructor " << "\n"; strcpy(name, "undifined"); number = 0; salary = 0; } Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){ cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n"; this->number = number; this->name = new char[100]; strcpy(this->name, name); this->salary = salary; } Worker::Worker(const Worker &obj){ cout<< " This is Copy Constructor " << "\n"; name = new char[80]; strcpy(name,obj.name); this->number =obj.number; this->salary = obj.salary; }; Worker::~Worker(){ delete[] name; cout << "デコンストラクタ" << "\n"; } /* void ShowData(Worker w2){ cout << "name = " << w2.name << "\n"; strcpy(w2.name,"AVD"); cout << "name = " << w2.name << "\n"; cout << "number = " << w2.number << "\n"; cout << "salary = " << w2.salary << "\n"; } */ int main(){ Worker w1; strcpy(w1.name,"Takayuki"); w1.number =10; w1.salary = 200; cout << " Before shwodata" << "\n"; //ShowData(w1); cout << " After shwodata" << "\n"; return 0; }
###補足情報(言語/FW/ツール等のバージョンなど)
paiza.io

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/22 03:53
2016/01/22 04:13
2016/01/22 04:21
2016/01/22 04:25
2016/01/22 05:00