Worker.cpp で Phoneクラスのオブジェクトをコンポジションとして持っています。
その後、WorkerクラスをSalesクラスで継承しています。
Workerクラスでは、Phoneコンストラクタでメンバイニシャライザをしており、Salesクラスでイニシャライザを行っています。
###前提・実現したいこと
Phoneクラスを仮想関数(Virtual ShowData())にして、Workerクラスでその関数を実装する場合( ShowData())、通常のオーバーライドとどう違うのでしょうか。
皆様は、具体的にどう使い分けているのでしょうか。
###ソースコード
<Sales.h> #include <string.h> #include "Worker.h" class Sales:public Worker{ public: double profit; char* client; std::string addtionalInfo; Sales(); Sales(double profit, char* client, std::string addtionalInfo, int number, char* name, double salary, bool i, char* plan, std::string pInfo); Sales(const Sales &obj); ~Sales(); void ShowData(Sales s1); }; <Sales.cpp> #include <iostream> #include <string.h> #include "Sales.h" using namespace std; Sales::Sales(double profit, char* client, std::string addtionalInfo, int number, char* name, double salary, bool i, char* plan, std::string pInfo):Worker(number, name, salary, i, plan, pInfo){ this->profit = profit; this->client = new char[100]; strcpy(this->client, client); this->addtionalInfo = addtionalInfo; } Sales::Sales(const Sales &obj){ client = new char[100]; strcpy(client,obj.name); this->profit =obj.profit; this->addtionalInfo=obj.addtionalInfo; } Sales::~Sales(){ } Sales::Sales():Worker(){ this->profit = 100; this->client = new char[100]; strcpy(this->client, "Undifined_client"); this->addtionalInfo="undifined_additionalInfo"; } void Sales::ShowData(Sales s1){ cout << "s1.name = " << s1.name << "\n"; cout << "s1.pInfo = " << s1.phone.pInfo << "\n"; cout << "s1.profit = " << s1.profit << "\n"; cout << "s1.client = " << s1.client << "\n"; cout << " s1.addtionalInfo = " << s1.addtionalInfo << "\n"; cout << " s1.addtionalInfo " << s1.addtionalInfo << "\n"; } int main(){ Sales s1; return 0; } <Worker.h> #include <string.h> #include "Phone.h" class Worker{ public: int number; char* name; double salary; Phone phone; Worker(); Worker(int number, char* name, double salary, bool i, char* plan, std::string pInfo); Worker(const Worker &obj); ~Worker(); }; <Worker.cpp> #include <iostream> #include "Worker.h" using namespace std; Worker::Worker(){ name = new char[80]; strcpy(name, "undifined in Worker con"); number = 0; salary = 0; } Worker::Worker(int number, char* name, double salary,bool i, char* plan, string pInfo):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[80]; strcpy(this->name, name); this->salary = salary; } Worker::Worker(const Worker &obj){ name = new char[80]; strcpy(name,obj.name); this->number =obj.number; this->salary = obj.salary; }; Worker::~Worker(){ delete[] name; } /* int main(){ Worker w1; strcpy(w1.name,"Takayuki"); //Worker::Worker(int number, char* name, double salary,bool i, char* plan, string pInfo): w1.number =10; w1.salary = 200; ShowData(w1); //Worker::Worker(int number, char* name, double salary,bool i, char* plan, string pInfo): Worker w2(1000, "name_w2", 1000, false, "plan_w2", "pInfo_w2"); ShowData(w2); return 0; } */ <Worker.h> #include <string.h> #include "Phone.h" class Worker{ public: int number; char* name; double salary; Phone phone; Worker(); Worker(int number, char* name, double salary, bool i, char* plan, std::string pInfo); Worker(const Worker &obj); ~Worker(); }; <Phone.cpp> #include <iostream> #include <string.h> #include "Phone.h" using namespace std; Phone::Phone(){ plan = new char[100]; 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(){ delete[] plan; } void Phone::ShowPhone(){ cout << " i = " << this->i << "\n"; cout << " plan = " << this->plan <<"\n"; cout << " pInfo = " << this->pInfo <<"\n"; } void Phone::ShowData(){ } /* 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; } */ <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(); };
###補足情報(言語/FW/ツール等のバージョンなど)
Paiza.io
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/27 09:29