所以现在我必须根据“员工”所属的类来调用特定的打印功能。
我在“Employees.cpp”中的父类打印功能:
void Employees::printEmployee() const{
cout<<"Name: "<<employeeName<<endl;
cout<<"Salary:"<<employeeSalary<<endl;
cout<<"Employee ID: "<<employeeID<<endl;
cout<<"Employee Class: "<<employeeClass<<endl<<endl;
}
和子类打印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”中定义,如下所示:
#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
有没有办法只有一个打印功能,可以打印每个类的相应成员。例如,我的chef子类具有与父“Employees”相同的所有属性,但另外还有“Share Percent”和“Chef Specialty”成员。我还有另外两个有特定班级成员的子课程。
感谢。
答案 0 :(得分:-1)
您应该了解虚拟方法。和纯虚拟方法。看看这个,让我知道这是否有帮助! http://www.thegeekstuff.com/2013/06/cpp-virtual-functions/ http://www.cplusplus.com/doc/tutorial/polymorphism/