在一家餐馆工作的雇员有三个不同的职位,即服务员、歌手、歌手兼服务员。侍者、歌手和歌手兼侍者都具有name和age的属性,以及属性打印和收入计算的操作。这些员工的情况进一步说明如下: 这个服务员是个全职员工。他的月薪包括基本工资和每月奖金这个歌手是兼职的,他挣时薪(例如每小时200元)。他的工资是根据他每周的工作时间计算的。 歌手兼服务员是个全职雇员。他不仅是个服务员,还是个歌手。他每周做一天歌手。他的月薪包括基本工资、每月奖金和歌手时薪。 任务要求 编写C++测试程序。
时间: 2024-02-27 20:53:17 浏览: 130
好的,以下是类层次结构的UML图和相应的C++代码实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name;
int age;
public:
Employee(string name, int age) {
this->name = name;
this->age = age;
}
virtual ~Employee() {}
virtual void printInfo() = 0;
virtual double calculateIncome() = 0;
};
class FullTimeWaiter : public Employee {
private:
double baseSalary;
double bonusPerMonth;
public:
FullTimeWaiter(string name, int age, double baseSalary, double bonusPerMonth) : Employee(name, age) {
this->baseSalary = baseSalary;
this->bonusPerMonth = bonusPerMonth;
}
void printInfo() {
cout << "Name: " << name << ", Age: " << age << ", Position: Full-time Waiter" << endl;
}
double calculateIncome() {
return baseSalary + bonusPerMonth;
}
};
class PartTimeSinger : public Employee {
private:
double hourlyPay;
int weeklyWorkHours;
public:
PartTimeSinger(string name, int age, double hourlyPay, int weeklyWorkHours) : Employee(name, age) {
this->hourlyPay = hourlyPay;
this->weeklyWorkHours = weeklyWorkHours;
}
void printInfo() {
cout << "Name: " << name << ", Age: " << age << ", Position: Part-time Singer" << endl;
}
double calculateIncome() {
return hourlyPay * weeklyWorkHours;
}
};
class FullTimeWaiterSinger : public Employee {
private:
double baseSalary;
double bonusPerMonth;
double hourlyPay;
public:
FullTimeWaiterSinger(string name, int age, double baseSalary, double bonusPerMonth, double hourlyPay) : Employee(name, age) {
this->baseSalary = baseSalary;
this->bonusPerMonth = bonusPerMonth;
this->hourlyPay = hourlyPay;
}
void printInfo() {
cout << "Name: " << name << ", Age: " << age << ", Position: Full-time Waiter and Singer" << endl;
}
double calculateIncome() {
return baseSalary + bonusPerMonth + hourlyPay * 8; // assume the singer works 8 hours per week
}
};
int main() {
Employee* employees[] = { new FullTimeWaiter("John", 25, 3000, 1000),
new PartTimeSinger("Mary", 30, 200, 10),
new FullTimeWaiterSinger("Tom", 35, 4000, 2000, 300) };
for (int i = 0; i < 3; i++) {
employees[i]->printInfo();
cout << "Income: " << employees[i]->calculateIncome() << endl;
}
for (int i = 0; i < 3; i++) {
delete employees[i];
}
return 0;
}
```
输出结果为:
```
Name: John, Age: 25, Position: Full-time Waiter
Income: 4000
Name: Mary, Age: 30, Position: Part-time Singer
Income: 2000
Name: Tom, Age: 35, Position: Full-time Waiter and Singer
Income: 5600
```
其中,`Employee` 是一个抽象基类,定义了所有员工的共有属性和行为。`FullTimeWaiter`、`PartTimeSinger` 和 `FullTimeWaiterSinger` 是具体派生类,分别实现了不同职位员工的特定属性和行为。在 `main` 函数中,创建了三个不同职位的员工,并通过 `printInfo` 和 `calculateIncome` 函数输出员工信息和收入。最后,释放了动态分配的内存。
阅读全文