c++ - 如何使继承依赖

标签 c++

我正在尝试做这个练习:

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

此外,您可能希望将 namesurname 作为成员变量包括在内,并在构造函数中初始化它们。要添加到之前的构造函数,它可能看起来像这样。

Employees(Supervisor *s, string sName, string sSurname){
    name = sName;
    surname = sSurname;
    supervisor = s;
    workerid += 1;
}

关于c++ - 如何使继承依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30782583/

相关文章:

c++ - C++ 是否有标准方法对两个 std::sets、 vector 等进行三态比较?

c++ - BlackBerry 10 Cascades - 如何导航到另一个 CPP 类

c++ - 求提高微软印章库计算效率的方法

c++ - C++11 可以判断 std::thread 是否处于事件状态吗?

c# - 如何在 C# 中释放在 C++ 中分配的内存

c++ - CAtlMap 使用 CString 作为键

c++ - 让 explorer.exe 从启动时加载我的扩展

c++ - 为模板类重载运算符<<。即使使用 friend 关键字也无法获得私有(private)成员(member)

c++ - 计算相差 K 的数字的总数

Raspberry Pi 1 B 上的 c++ bass 和 rapidXml