为某公司设计雇员工资发放系统。 每个雇员的基本信息包括:姓名(name),工号(id)。 雇员的收入取决于雇员的类型。该公司共有四类雇员: 周薪雇员(SalariedEmployee):收入=固定周薪。 时薪雇员(HourlyEmployee):若工作40小时以下,收入=小时数*每小时薪水;若工作40小时以上,收入=40*每小时薪水+(小时数-40)*每小时薪水*150%。 佣金雇员(CommissionEmployee):收入=销售量*每个商品的销售佣金 带底薪佣金雇员(BasePlusCommissionEmployee):收入=底薪+销售量*每个商品的销售佣金 要求:建立雇员继承层次,每个类包含计算工资和显示输出的功能,可以计算和显示输出公司雇员(Employee)的每周收入。输出时要显示该类雇员的所有信息。(包括雇员类型、姓名、工号、工资各项明细),写出主函数测试各类。在雇员基本信息中增加雇员的生日(birthDate),并设计日期类(Date)来表示生日。在主函数中创建一个容器用来管理公司各种雇员对象,多态地计算并输出每个雇员的工资。如果雇员的生日在本月,就奖给该雇员100元。同时,在本次工资发放阶段,公司决定奖励带薪佣金雇员,把他们的基本工资提高10%。 设计提示:使用抽象类。
时间: 2023-06-24 07:08:35 浏览: 1714
南京邮电大学JAVA第二次课程实验
以下是针对该问题的代码实现:
```cpp
#include<iostream>
#include<vector>
#include<ctime>
using namespace std;
class Date { //日期类
private:
int year, month, day;
public:
Date(int y, int m, int d) : year(y), month(m), day(d) {}
int getMonth() const { return month; }
};
class Employee { //雇员类(抽象类)
private:
string name;
int id;
Date birthDate;
public:
Employee(string n, int i, Date b) : name(n), id(i), birthDate(b) {}
virtual double earnings() const = 0; //纯虚函数,计算工资
virtual void print() const = 0; //纯虚函数,输出信息
int getID() const { return id; }
string getName() const { return name; }
Date getBirthDate() const { return birthDate; }
};
class SalariedEmployee : public Employee { //周薪雇员类
private:
double weeklySalary;
public:
SalariedEmployee(string n, int i, Date b, double w) : Employee(n, i, b), weeklySalary(w) {}
double earnings() const override {
return weeklySalary;
}
void print() const override {
cout << "Salaried Employee: " << getName() << " (ID: " << getID() << ")" << endl;
cout << "Birth Date: " << getBirthDate().getMonth() << "/xx/xxxx" << endl;
cout << "Weekly Salary: $" << earnings() << endl;
}
};
class HourlyEmployee : public Employee { //时薪雇员类
private:
double wage;
double hours;
public:
HourlyEmployee(string n, int i, Date b, double w, double h) : Employee(n, i, b), wage(w), hours(h) {}
double earnings() const override {
if (hours <= 40)
return wage * hours;
else
return 40 * wage + (hours - 40) * wage * 1.5;
}
void print() const override {
cout << "Hourly Employee: " << getName() << " (ID: " << getID() << ")" << endl;
cout << "Birth Date: " << getBirthDate().getMonth() << "/xx/xxxx" << endl;
cout << "Hours Worked: " << hours << endl;
cout << "Hourly Wage: $" << wage << endl;
cout << "Total Earnings: $" << earnings() << endl;
}
};
class CommissionEmployee : public Employee { //佣金雇员类
private:
double commission;
int sales;
public:
CommissionEmployee(string n, int i, Date b, double c, int s) : Employee(n, i, b), commission(c), sales(s) {}
double earnings() const override {
return commission * sales;
}
void print() const override {
cout << "Commission Employee: " << getName() << " (ID: " << getID() << ")" << endl;
cout << "Birth Date: " << getBirthDate().getMonth() << "/xx/xxxx" << endl;
cout << "Sales: " << sales << endl;
cout << "Commission: $" << commission << endl;
cout << "Total Earnings: $" << earnings() << endl;
}
};
class BasePlusCommissionEmployee : public CommissionEmployee { //带底薪佣金雇员类
private:
double baseSalary;
public:
BasePlusCommissionEmployee(string n, int i, Date b, double c, int s, double bs) : CommissionEmployee(n, i, b, c, s), baseSalary(bs) {}
double earnings() const override {
return baseSalary + CommissionEmployee::earnings();
}
void print() const override {
cout << "Base Plus Commission Employee: " << getName() << " (ID: " << getID() << ")" << endl;
cout << "Birth Date: " << getBirthDate().getMonth() << "/xx/xxxx" << endl;
cout << "Sales: " << CommissionEmployee::sales << endl;
cout << "Commission: $" << CommissionEmployee::commission << endl;
cout << "Base Salary: $" << baseSalary << endl;
cout << "Total Earnings: $" << earnings() << endl;
}
};
int main() {
vector<Employee*> employees; //创建雇员指针容器
employees.push_back(new SalariedEmployee("John Doe", 1, Date(2000, 1, 1), 1000));
employees.push_back(new HourlyEmployee("Jane Smith", 2, Date(1995, 5, 15), 20, 45));
employees.push_back(new CommissionEmployee("Bob Johnson", 3, Date(1980, 10, 27), 0.05, 1000));
employees.push_back(new BasePlusCommissionEmployee("Mike Brown", 4, Date(1978, 2, 3), 0.03, 2000, 2000));
time_t now = time(0);
tm* local_time = localtime(&now);
int month = local_time->tm_mon + 1; //获取当前月份
for (Employee* e : employees) {
e->print();
if (e->getBirthDate().getMonth() == month)
cout << "Birthday Bonus: $100" << endl;
if (dynamic_cast<BasePlusCommissionEmployee*>(e))
dynamic_cast<BasePlusCommissionEmployee*>(e)->earnings() *= 1.1; //提高带底薪佣金雇员的基本工资
cout << endl;
}
for (Employee* e : employees)
delete e; //释放内存
return 0;
}
```
上述代码中,我们定义了一个抽象类 `Employee`,它包含了雇员的基本信息(姓名、工号、生日)以及两个纯虚函数 `earnings()` 和 `print()`。不同类型的雇员分别继承了 `Employee` 类,并实现了自己的计算工资和输出信息的函数。主函数中创建了一个雇员指针容器 `employees`,并向其中添加不同类型的雇员对象。在输出每个雇员的信息时,如果该雇员的生日在本月,就奖励100元。同时,对于带底薪佣金雇员,将其基本工资提高10%。最后,释放内存并结束程序。
阅读全文