在主函数中创建一个容器用来管理公司各种雇员对象,多态地计算并输出每个雇员的工资。
时间: 2023-06-20 17:10:27 浏览: 96
通过容器类HR管理系统中的部门、雇员管理功能
好的,我可以帮你写出代码实现这个功能。以下是一个示例程序:
```C++
#include <iostream>
#include <vector>
#include "employee.h"
#include "manager.h"
#include "engineer.h"
#include "salesman.h"
int main() {
std::vector<Employee*> employees;
Manager* manager1 = new Manager("John Smith", 10000, 0.05);
Manager* manager2 = new Manager("Jane Doe", 12000, 0.06);
Engineer* engineer1 = new Engineer("Bob Johnson", 8000, 1000);
Engineer* engineer2 = new Engineer("Alice Brown", 9000, 1500);
Salesman* salesman1 = new Salesman("Mike Davis", 6000, 0.1, 50000);
Salesman* salesman2 = new Salesman("Sara Parker", 7000, 0.12, 60000);
employees.push_back(manager1);
employees.push_back(manager2);
employees.push_back(engineer1);
employees.push_back(engineer2);
employees.push_back(salesman1);
employees.push_back(salesman2);
for (Employee* emp : employees) {
std::cout << "Employee " << emp->getName() << " earned $" << emp->calculateSalary() << std::endl;
}
for (Employee* emp : employees) {
delete emp;
}
return 0;
}
```
在这个示例程序中,我们创建了一个 `std::vector<Employee*>` 类型的容器 `employees` 来存储公司各种雇员对象。我们向这个容器中添加了两个经理对象 `manager1` 和 `manager2`,两个工程师对象 `engineer1` 和 `engineer2`,以及两个销售员对象 `salesman1` 和 `salesman2`。
然后,我们使用 for 循环遍历容器中的每个雇员对象,并且通过多态的方式调用每个对象的 `calculateSalary()` 函数来计算工资,并输出到屏幕上。最后,我们再次使用 for 循环遍历容器中的每个雇员对象,并且使用 `delete` 关键字释放这些对象所占用的内存。
在这个示例程序中,我们还需要编写 `Employee`、`Manager`、`Engineer` 和 `Salesman` 四个类的定义文件,以及实现每个类的成员函数。下面是一个可能的实现:
```C++
// employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
class Employee {
public:
Employee(const std::string& name, double baseSalary);
virtual ~Employee();
virtual double calculateSalary() const;
const std::string& getName() const;
double getBaseSalary() const;
private:
std::string name;
double baseSalary;
};
#endif // EMPLOYEE_H
// employee.cpp
#include "employee.h"
Employee::Employee(const std::string& name, double baseSalary)
: name(name), baseSalary(baseSalary) {}
Employee::~Employee() {}
double Employee::calculateSalary() const {
return baseSalary;
}
const std::string& Employee::getName() const {
return name;
}
double Employee::getBaseSalary() const {
return baseSalary;
}
// manager.h
#ifndef MANAGER_H
#define MANAGER_H
#include "employee.h"
class Manager : public Employee {
public:
Manager(const std::string& name, double baseSalary, double bonusRate);
double calculateSalary() const override;
private:
double bonusRate;
};
#endif // MANAGER_H
// manager.cpp
#include "manager.h"
Manager::Manager(const std::string& name, double baseSalary, double bonusRate)
: Employee(name, baseSalary), bonusRate(bonusRate) {}
double Manager::calculateSalary() const {
return getBaseSalary() * (1 + bonusRate);
}
// engineer.h
#ifndef ENGINEER_H
#define ENGINEER_H
#include "employee.h"
class Engineer : public Employee {
public:
Engineer(const std::string& name, double baseSalary, double bonus);
double calculateSalary() const override;
private:
double bonus;
};
#endif // ENGINEER_H
// engineer.cpp
#include "engineer.h"
Engineer::Engineer(const std::string& name, double baseSalary, double bonus)
: Employee(name, baseSalary), bonus(bonus) {}
double Engineer::calculateSalary() const {
return getBaseSalary() + bonus;
}
// salesman.h
#ifndef SALESMAN_H
#define SALESMAN_H
#include "employee.h"
class Salesman : public Employee {
public:
Salesman(const std::string& name, double baseSalary, double commissionRate, double sales);
double calculateSalary() const override;
private:
double commissionRate;
double sales;
};
#endif // SALESMAN_H
// salesman.cpp
#include "salesman.h"
Salesman::Salesman(const std::string& name, double baseSalary, double commissionRate, double sales)
: Employee(name, baseSalary), commissionRate(commissionRate), sales(sales) {}
double Salesman::calculateSalary() const {
return getBaseSalary() + commissionRate * sales;
}
```
在这个实现中,我们定义了 `Employee` 类作为基类,其他三个类分别继承自 `Employee` 类,实现了各自的工资计算方法。注意到 `calculateSalary()` 函数被声明为 `virtual`,这就允许我们通过指向基类的指针来调用派生类的函数,实现了多态的效果。
阅读全文