帮我写一段C++代码,要求如下:某软件公司员工(Employee)可以分为现有两类人员:行政管理人员(Manager)和项目开发人员(Developer)。现在需要对公司的人员信息进行统一管理,存储人员的编号(number)、姓名(name)、职务级别(position)、每月固定奖金(monthBonus)、每月月收入(monthIncome)和计算每月月收入(void calculateMonthIncome()),并且能够显示员工全部信息(void showInfo())。 行政管理人员和项目开发人员均划分成三个等级,行政管理人员分为总经理(级别为1)、部门经理(级别为2)和小组长(级别为3);行政管理人员月收入的计算公式为12000*(3-级别+1)/3+每月固定奖金,固定资金为3500元/月。 项目开发人员分为工作时间不满一年的(级别为3)、工作时间超过一年不到三年的(级别为2)和工龄在三年以上的(级别为1);项目开发人员月收入的计算公式为6000*(3-级别+1)/3+奖金,奖金为其加班的小时数*40元/小时+每月固定奖金500元。 试编程实现上述人员管理,用虚函数实现体现出多态性。将Employee类的声明放在employee.h文件中、Employee类的实现放在employee.cpp文件中;将Manager类的声明放在manager.h文件中、Manager类的实现放在manager.cpp文件中;将Developer类的声明放在developer.h文件中、Developer类的实现放在developer.cpp文件中;主函数放在main.cpp文件中。
时间: 2024-03-07 16:49:47 浏览: 217
employee.h:
```cpp
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class Employee {
protected:
int number;
string name;
int position;
double monthBonus;
double monthIncome;
public:
Employee(int number, string name, int position, double monthBonus);
virtual ~Employee();
virtual void calculateMonthIncome() = 0;
void showInfo();
};
#endif
```
employee.cpp:
```cpp
#include "employee.h"
#include <iostream>
using namespace std;
Employee::Employee(int number, string name, int position, double monthBonus) {
this->number = number;
this->name = name;
this->position = position;
this->monthBonus = monthBonus;
this->monthIncome = 0;
}
Employee::~Employee() {}
void Employee::showInfo() {
cout << "编号:" << number << endl;
cout << "姓名:" << name << endl;
cout << "职务级别:" << position << endl;
cout << "每月固定奖金:" << monthBonus << endl;
cout << "每月月收入:" << monthIncome << endl;
}
```
manager.h:
```cpp
#ifndef MANAGER_H
#define MANAGER_H
#include "employee.h"
class Manager : public Employee {
public:
Manager(int number, string name, int position, double monthBonus);
~Manager();
virtual void calculateMonthIncome();
};
#endif
```
manager.cpp:
```cpp
#include "manager.h"
Manager::Manager(int number, string name, int position, double monthBonus) : Employee(number, name, position, monthBonus) {}
Manager::~Manager() {}
void Manager::calculateMonthIncome() {
monthIncome = 12000 * (3 - position + 1) / 3 + monthBonus + 3500;
}
```
developer.h:
```cpp
#ifndef DEVELOPER_H
#define DEVELOPER_H
#include "employee.h"
class Developer : public Employee {
private:
int workHours;
public:
Developer(int number, string name, int position, double monthBonus, int workHours);
~Developer();
virtual void calculateMonthIncome();
};
#endif
```
developer.cpp:
```cpp
#include "developer.h"
Developer::Developer(int number, string name, int position, double monthBonus, int workHours) : Employee(number, name, position, monthBonus) {
this->workHours = workHours;
}
Developer::~Developer() {}
void Developer::calculateMonthIncome() {
if (position == 1) {
monthIncome = 6000 * (3 - position + 1) / 3 + monthBonus + workHours * 40 + 500;
} else if (position == 2) {
monthIncome = 6000 * (3 - position + 1) / 3 + monthBonus + workHours * 40;
} else {
monthIncome = 6000 * (3 - position + 1) / 3 + monthBonus;
}
}
```
main.cpp:
```cpp
#include "employee.h"
#include "manager.h"
#include "developer.h"
#include <iostream>
using namespace std;
int main() {
Employee *e1 = new Manager(1001, "张三", 1, 5000);
Employee *e2 = new Manager(1002, "李四", 2, 3000);
Employee *e3 = new Developer(2001, "王五", 3, 2000, 80);
Employee *e4 = new Developer(2002, "赵六", 1, 1000, 120);
e1->calculateMonthIncome();
e2->calculateMonthIncome();
e3->calculateMonthIncome();
e4->calculateMonthIncome();
e1->showInfo();
e2->showInfo();
e3->showInfo();
e4->showInfo();
delete e1;
delete e2;
delete e3;
delete e4;
return 0;
}
```
阅读全文