编程实现基类Employee,派生类Manager和HourlyWorker。Employee有数据成员name和ID;Manager有数据成员sal,代表经理的月工资; HourlyWorker有wage,hours和sal,分别代表钟点工的每小时工资数、月工作时数和月工资总数,并创建成员函数salllay()求出钟点工的月工资总数=每小时工资数*月工作时数。定义的所有类中都必须包含构造函数,最终输出所有数据的值。,用c++
时间: 2024-03-11 07:50:28 浏览: 86
以下是实现基类Employee,派生类Manager和HourlyWorker的代码:
```c++
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name;
int ID;
public:
Employee(string n, int i) : name(n), ID(i) {}
};
class Manager : public Employee {
private:
double sal;
public:
Manager(string n, int i, double s) : Employee(n, i), sal(s) {}
double getSal() const { return sal; }
};
class HourlyWorker : public Employee {
private:
double wage;
int hours;
double sal;
public:
HourlyWorker(string n, int i, double w, int h) : Employee(n, i), wage(w), hours(h) {
sal = sallary();
}
double sallary() const { return wage * hours; }
double getWage() const { return wage; }
int getHours() const { return hours; }
double getSal() const { return sal; }
};
int main() {
Manager m("John", 1001, 5000.0);
HourlyWorker h("Mike", 1002, 20.0, 160);
cout << "Manager name: " << m.name << ", ID: " << m.ID << ", Salary: " << m.getSal() << endl;
cout << "Hourly worker name: " << h.name << ", ID: " << h.ID << ", Wage: " << h.getWage() << ", Hours: " << h.getHours() << ", Salary: " << h.getSal() << endl;
return 0;
}
```
输出结果:
```
Manager name: John, ID: 1001, Salary: 5000
Hourly worker name: Mike, ID: 1002, Wage: 20, Hours: 160, Salary: 3200
```
阅读全文