だから今私は "従業員"の一部であるクラスに基づいて特定のプリント機能を呼び出す必要があります。親子クラスの関数を結合する方法は?
"Employees.cpp" で私の親クラスの印刷機能:
と子クラスの印刷functpionの例は、私の "Chef.cpp" 印刷機能である:
void Chef::printChef() const{
cout<<"Name: "<<employeeName<<endl;
cout<<"Salary: $"<<calculateSalary()<<endl;
cout<<"Employee ID: "<<employeeID<<endl;
cout<<"Employee Class: "<<employeeClass<<endl;
cout<<"Share Perecent: "<<chefSharePercent<<"%"<<endl;
cout<<"Chef's Specialty: "<<chefSpecialty<<endl<<endl;
}
「Chef.cpp」の機能は非常にのように見える「Chef.h」にdefiniedされています
#ifndef CHEF_H_
#define CHEF_H_
#include <string>
#include "employee.h"
using namespace std;
class Chef : public Employees{
public:
Chef();
Chef(double percent, string specailty);
void setPercent(double percent);
void setSpecialty(string specailty);
double getPerecent() const;
string getSpecialty() const;
double calculateSalary() const;
void printChef() const;
private:
double chefSharePercent;
string chefSpecialty;
};
#endif
持ってする方法はあります各クラスの適切なメンバを印刷する1つの印刷機能。たとえば、「シェフの子クラス」クラスには、親「従業員」と同じ属性がありますが、「共有パーセント」と「シェフの専門性」メンバもあります。クラス固有のメンバーを持つ他の2つの子クラスもあります。
ありがとうございました。
[仮想関数継承](http://stackoverflow.com/questions/17842594/virtual-function-inheritance)の可能性の重複。これを欺瞞と呼ぶことは少し物を伸ばすことですが、これはあなたがしたいことであり、もう一度これを繰り返す必要はありません。 – user4581301
もっと便利な情報はここにあります:https://isocpp.org/wiki/faq/virtual-functions – user4581301
印刷はメンバー機能であってはなりません。 – Thomas