1、【编程题】某公司的雇员分为以下若干类: (1) Employee:这是所有员工总的父类。 ① 属性:员工的姓名,员工的生日月份 ② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。 (2) SalariedEmployee:Employee 的子类,拿固定工资的员工。 ① 属性:月薪。 (3)HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。 ① 属性:每小时的工资、每月工作的小时数。 (4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。 ① 属性:月销售额、提成率。 (5) BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。 ① 属性:底薪。 要求: 创建一个Employee 数组,分别创建若干不同的Employee对象,并打印某个月的工资。 注意:要求把每个类都做成完全封装,不允许非私有化属性。
时间: 2024-03-27 15:38:58 浏览: 381
类的运用 某公司雇员(employee)
4星 · 用户满意度95%
以下是代码实现:
```
class Employee {
private:
string name;
int birthMonth;
public:
Employee(string name, int birthMonth) {
this->name = name;
this->birthMonth = birthMonth;
}
virtual double getSalary(int month) {
double salary = 0;
//根据月份计算工资
return salary;
}
};
class SalariedEmployee : public Employee {
private:
double monthlySalary;
public:
SalariedEmployee(string name, int birthMonth, double monthlySalary) : Employee(name, birthMonth) {
this->monthlySalary = monthlySalary;
}
double getSalary(int month) {
double salary = monthlySalary;
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
};
class HourlyEmployee : public Employee {
private:
double hourlyWage;
int monthlyWorkHours;
public:
HourlyEmployee(string name, int birthMonth, double hourlyWage, int monthlyWorkHours) : Employee(name, birthMonth) {
this->hourlyWage = hourlyWage;
this->monthlyWorkHours = monthlyWorkHours;
}
double getSalary(int month) {
double salary = hourlyWage * monthlyWorkHours;
if (monthlyWorkHours > 160) {
salary += hourlyWage * 1.5 * (monthlyWorkHours - 160);
}
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
};
class SalesEmployee : public Employee {
private:
double monthlySales;
double commissionRate;
public:
SalesEmployee(string name, int birthMonth, double monthlySales, double commissionRate) : Employee(name, birthMonth) {
this->monthlySales = monthlySales;
this->commissionRate = commissionRate;
}
double getSalary(int month) {
double salary = monthlySales * commissionRate;
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
};
class BasePlusSalesEmployee : public SalesEmployee {
private:
double baseSalary;
public:
BasePlusSalesEmployee(string name, int birthMonth, double monthlySales, double commissionRate, double baseSalary) : SalesEmployee(name, birthMonth, monthlySales, commissionRate) {
this->baseSalary = baseSalary;
}
double getSalary(int month) {
double salary = baseSalary + SalesEmployee::getSalary(month);
if (month == getBirthMonth()) {
salary += 100;
}
return salary;
}
};
int main() {
Employee* employees[] = {
new SalariedEmployee("Alice", 5, 5000),
new HourlyEmployee("Bob", 10, 50, 180),
new SalesEmployee("Charlie", 6, 100000, 0.05),
new BasePlusSalesEmployee("David", 8, 80000, 0.1, 3000)
};
int month = 5;
for (int i = 0; i < 4; i++) {
cout << employees[i]->getSalary(month) << endl;
}
return 0;
}
```
这里使用了虚函数和多态的特性,通过创建不同的子类对象,实现了不同类型员工的计算工资方式。在main函数中创建了一个Employee类型的指针数组,指向四个不同类型的员工对象,并通过循环打印出某个月的工资。
阅读全文