我正在尝试做这个练习:
There are different types of employees: regular workers,supervisors,board members which are specific types of supervisors
Track the number of objects of type regular worker.
Each employee has its name and surname and can introduce himself.
Each employee object has its own worker id which is assigned to him while it is created
The salary is calculated is specific way depending on the type of the employee.
Each employee can have its direct supervisor which can be any supervisor(it means that for example a board member can be a supervisor of regular worker)
现在我已经说完了所有要点,排除最后一点:
class Employees
{
public:
static int workerid;
int salary;
Employees(){
workerid += 1;
}
void introduce(){
cout << "the name is:" << name << "and surname" << surname << endl;
}
};
int Employees::workerid = 0;
class Supervisors :public Employees{
public:
Supervisors(){
salary = 1000;
}
};
class BoardMembers : public Supervisors{
public:
BoardMembers(){
salary = 1200;
}
};
class RegularWorkers :public Employees{
public:
static int number;
Supervisors *supervisor;
RegularWorkers(Supervisors supervisor){
this->supervisor = &supervisor;
number += 1;
salary = 600;
}
};
int RegularWorkers::number = 0;
(我认为直到最后一点都可以),但对于最后一点,我需要一个主管的 ID,但是如何让主管或董事会成员分配给正式员工?
谢谢你和最好的问候
最佳答案
修改员工类,添加Supervisor
Supervisor *supervisor;
然后重载 Employee 构造函数以接受类型 Supervisor
并将它们设置为彼此相等。
Employees(Supervisor *s){
supervisor = s;
workerid += 1;
}
然后你可以通过使用访问主管的ID
supervisor->ID
此外,您可能希望将 name
和 surname
作为成员变量包括在内,并在构造函数中初始化它们。要添加到之前的构造函数,它可能看起来像这样。
Employees(Supervisor *s, string sName, string sSurname){
name = sName;
surname = sSurname;
supervisor = s;
workerid += 1;
}
关于c++ - 如何使继承依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30782583/