【发布时间】:2020-04-13 22:42:16
【问题描述】:
我正在为员工管理系统构建一个简单的面向对象的继承类,但是我发现一个问题,如果某些员工获得晋升,那么我们如何处理这种情况。
此代码使用继承关系表示员工类层次结构。
#include <iostream>
using namespace std;
/* This is Employee Class */
class Employee
{
private:
string empname;
int empid;
float salary;
public:
int getSalary();
};
/* This is Manager Class Derived from Employee */
class Manager: public Employee
{
public:
int getSalary(); // This will return salary of manager
};
/* This is Clerk Class Derived from Employee */
class Clerk: public Employee
{
public:
int getSalary(); // This will return salary of clerk
};
/* This is Accountant Class Derived from Employee */
class Accountant : public Employee
{
public:
int getSalary(); // This will return salary of Accountant
};
/* This is Accountant Class Derived from Employee */
class Developer: public Employee
{
public:
int getSalary(); // This will return salary of Developer
};
现在如果有员工升职降职,我该如何处理呢?
假设有一天开发人员成为经理。我将如何将这些更改应用到 C++ 类中?
【问题讨论】:
-
“我发现了一个问题” 什么样的问题?一个“问题”可能意味着很多事情。
-
您应该有一个带有(指向)
Employee对象的容器?然后一种可能的解决方案是用指向新“提升”对象的指针替换指针,并使用复制构造来复制所需的所有细节。与all_employees[some_developer_index] = new Manager(*all_employees[some_developer_index])一样(请注意,我简化了示例,因为它当前会泄漏内存)。
标签: c++ class oop object design-patterns