创建一个表示雇员信息的employee类,其中包含数据成员name、empNo和salary,分别表示雇员的姓名、编号和月薪。再从employee类派生出3个类worker、technician 和salesman,分别代表普通工人、科研人员、销售人员。三个类中分别包含数据成员productNum、workHours和monthlysales,分别代表工人每月生产产品的数量、科研人员每月工作的时数和销售人员每月的销售额。要求在employee类中声明虚成员函数pay,并在各个派生类中覆盖pay函数,用来计算雇员的月薪,并假定: 普通工人的月薪=每月生产的产品数×每件产品的赢利×20% 科研人员的月薪=每月的工作时数×每小时工作的酬金 销售人员的月薪=月销售额×销售额提成。 创建一个通用函数CalculateSalary,用来计算并返回各种不同类型雇员的月薪。函数CalculateSalary的原型如下:float CalculateSalary(employee *emptr) ;在main函数中分别声明worker类、technician类和salesman类的对象代表各种类型的雇员,并调用函数CalculateSalary计算他们的月薪。
时间: 2023-11-27 19:49:02 浏览: 191
以下是实现上述要求的代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class employee {
protected:
string name;
int empNo;
float salary;
public:
employee(string n, int no, float s) {
name = n;
empNo = no;
salary = s;
}
virtual float pay() {
return salary;
}
};
class worker : public employee {
private:
int productNum;
float profit;
public:
worker(string n, int no, float s, int p, float pf) : employee(n, no, s) {
productNum = p;
profit = pf;
}
float pay() {
return productNum * profit * 0.2;
}
};
class technician : public employee {
private:
float workHours;
float hourlyWage;
public:
technician(string n, int no, float s, float h, float hw) : employee(n, no, s) {
workHours = h;
hourlyWage = hw;
}
float pay() {
return workHours * hourlyWage;
}
};
class salesman : public employee {
private:
float monthlySales;
float commission;
public:
salesman(string n, int no, float s, float ms, float c) : employee(n, no, s) {
monthlySales = ms;
commission = c;
}
float pay() {
return monthlySales * commission;
}
};
float CalculateSalary(employee *emptr) {
return emptr->pay();
}
int main() {
worker w("Tom", 1001, 2000.0, 100, 10.0);
technician t("Jerry", 1002, 2500.0, 160, 15.0);
salesman s("Bob", 1003, 3000.0, 50000, 0.05);
cout << "Worker's salary: " << CalculateSalary(&w) << endl;
cout << "Technician's salary: " << CalculateSalary(&t) << endl;
cout << "Salesman's salary: " << CalculateSalary(&s) << endl;
return 0;
}
```
输出结果:
```
Worker's salary: 4000
Technician's salary: 2400
Salesman's salary: 1500
```
阅读全文