2016-04-12 12 views
0

だから今私は "従業員"の一部であるクラスに基づいて特定のプリント機能を呼び出す必要があります。親子クラスの関数を結合する方法は?

"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つの子クラスもあります。

ありがとうございました。

+0

[仮想関数継承](http://stackoverflow.com/questions/17842594/virtual-function-inheritance)の可能性の重複。これを欺瞞と呼ぶことは少し物を伸ばすことですが、これはあなたがしたいことであり、もう一度これを繰り返す必要はありません。 – user4581301

+0

もっと便利な情報はここにあります:https://isocpp.org/wiki/faq/virtual-functions – user4581301

+0

印刷はメンバー機能であってはなりません。 – Thomas

答えて

0

仮想メソッドについて学ぶ必要があります。そして、純粋な仮想メソッド。これを見て、これが役に立ったら教えてください! http://www.thegeekstuff.com/2013/06/cpp-virtual-functions/

http://www.cplusplus.com/doc/tutorial/polymorphism/
+0

これは実際には答えではありません。これは質問のコメントとして残された方が良いでしょう。 – callyalater

+0

パーフェクト、これはまさに私が探していたものです!ありがとう、それは素晴らしい作品! – KBro

+0

@callyalaterのコメントに同意します。 OPの問題を具体的に解決するコードを含めることによって、投稿を拡張する必要があります。 –