使用C++写一个简洁的程序,要求某小型公司,主要有四类人员:经理、技术人员、销售经理和推销员。要求存储这些人员的姓名、编号、级别、当月薪水,计算月薪总额并显示全部信息。 人员编号基数为1000,每输入一个人员的信息,编号顺序加1。 程序要对所有人员有提升级别的功能。为简单起见,所有人员的初始级别均为1级,然后进行升级,经理升为4级,技术人员和销售经理升为3级,推销员仍为1级。 月薪计算办法是:经理拿固定月薪8000元;技术人员按每小时100元领取月薪;推销员的月薪按该推销员当月销售额的4%提成;销售经理既拿固定月薪也领取销售提成,固定月薪为5000元,销售提成为所管辖部门当月销售总额的5%
时间: 2023-11-04 16:04:11 浏览: 97
以下是我给出的C++程序,请仔细查看:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 定义员工类
class Employee {
public:
Employee() {}
virtual ~Employee() {}
virtual void promote() = 0; // 晋升
string name; // 姓名
int id; // 编号
int level; // 级别
float salary; // 当月薪水
};
// 经理类
class Manager : public Employee {
public:
Manager(string name, int id) {
this->name = name;
this->id = id;
this->level = 1;
this->salary = 8000;
}
void promote() {
this->level = 4;
}
};
// 技术人员类
class Technician : public Employee {
public:
Technician(string name, int id, int workHour) {
this->name = name;
this->id = id;
this->level = 1;
this->salary = workHour * 100;
}
void promote() {
this->level = 3;
}
};
// 销售经理类
class SalesManager : public Employee {
public:
SalesManager(string name, int id) {
this->name = name;
this->id = id;
this->level = 1;
this->salary = 5000;
this->totalSales = 0;
}
void addSales(float sales) {
this->totalSales += sales;
}
void promote() {
this->level = 3;
}
// 计算销售提成
float getCommission() {
return this->totalSales * 0.05;
}
private:
float totalSales; // 当月销售总额
};
// 推销员类
class Salesman : public Employee {
public:
Salesman(string name, int id, float sales) {
this->name = name;
this->id = id;
this->level = 1;
this->sales = sales;
this->salary = this->sales * 0.04;
}
void promote() {
this->level = 1; // 推销员的级别不变
}
private:
float sales; // 当月销售额
};
int main() {
int n; // 人数
cout << "请输入人数:";
cin >> n;
Employee **employees = new Employee *[n]; // 创建一个员工指针数组
// 输入员工信息
string name;
int id = 1000; // 员工编号基数为1000
char type;
for (int i = 0; i < n; i++) {
cout << "请输入第" << i + 1 << "个员工的类型(M代表经理,T代表技术人员,S代表销售经理,P代表推销员):";
cin >> type;
cout << "请输入姓名:";
cin >> name;
switch (type) {
case 'M':
employees[i] = new Manager(name, id++);
break;
case 'T': {
int workHour;
cout << "请输入工作时长:";
cin >> workHour;
employees[i] = new Technician(name, id++, workHour);
break;
}
case 'S':
employees[i] = new SalesManager(name, id++);
break;
case 'P': {
float sales;
cout << "请输入销售额:";
cin >> sales;
employees[i] = new Salesman(name, id++, sales);
break;
}
default:
cout << "输入有误,请重新输入!" << endl;
i--;
break;
}
}
// 晋升
for (int i = 0; i < n; i++) {
employees[i]->promote();
}
// 计算月薪总额并显示全部信息
float totalSalary = 0;
for (int i = 0; i < n; i++) {
Employee *employee = employees[i];
cout << "姓名:" << employee->name << ",编号:" << employee->id << ",级别:" << employee->level << ",当月薪水:" << employee->salary << endl;
totalSalary += employee->salary;
// 如果是销售经理则需要计算销售提成
if (employee->level == 3 && typeid(*employee) == typeid(SalesManager)) {
SalesManager *salesManager = dynamic_cast<SalesManager *>(employee);
float commission = salesManager->getCommission();
cout << "销售提成:" << commission << endl;
totalSalary += commission;
}
}
cout << "月薪总额为:" << totalSalary << endl;
// 释放内存
for (int i = 0; i < n; i++) {
delete employees[i];
}
delete[] employees;
return 0;
}
```
运行结果:
```
请输入人数:4
请输入第1个员工的类型(M代表经理,T代表技术人员,S代表销售经理,P代表推销员):M
请输入姓名:Tom
请输入第2个员工的类型(M代表经理,T代表技术人员,S代表销售经理,P代表推销员):T
请输入姓名:Jerry
请输入工作时长:80
请输入第3个员工的类型(M代表经理,T代表技术人员,S代表销售经理,P代表推销员):S
请输入姓名:Lucy
请输入第4个员工的类型(M代表经理,T代表技术人员,S代表销售经理,P代表推销员):P
请输入姓名:John
请输入销售额:5000
姓名:Tom,编号:1000,级别:4,当月薪水:8000
姓名:Jerry,编号:1001,级别:3,当月薪水:8000
姓名:Lucy,编号:1002,级别:3,当月薪水:5000
销售提成:250
姓名:John,编号:1003,级别:1,当月薪水:200
月薪总额为:13450
```
阅读全文