为某公司设计雇员工资发放系统。
每个雇员的基本信息包括:姓名(name),工号(id)。
雇员的收入取决于雇员的类型。该公司共有四类雇员:
周薪雇员(SalariedEmployee):收入=固定周薪。
时薪雇员(HourlyEmployee):若工作40小时以下,收入=小时数每小时薪水;若工作40小时以上,收入=40每小时薪水+(小时数-40)每小时薪水150%。
佣金雇员(CommissionEmployee):收入=销售量每个商品的销售佣金
带底薪佣金雇员(BasePlusCommissionEmployee):收入=底薪+销售量每个商品的销售佣金
要求:建立雇员继承层次,每个类包含计算工资和显示输出的功能,可以计算和显示输出公司雇员(Employee)的每周收入。输出时要显示该类雇员的所有信息。(包括雇员类型、姓名、工号、工资各项明细),写出主函数测试各类。在雇员基本信息中增加雇员的生日(birthDate),并设计日期类(Date)来表示生日。在主函数中创建一个容器用来管理公司各种雇员对象,多态地计算并输出每个雇员的工资。如果雇员的生日在本月,就奖给该雇员100元。同时,在本次工资发放阶段,公司决定奖励带薪佣金雇员,把他们的基本工资提高10%。
设计提示:使用抽象类。
基于new bing的参考编写,实现你题目要求,有帮助记得采纳一下哦!:
#include <iostream>
#include <vector>
using namespace std;
// 定义日期类
class Date {
public:
Date(int year, int month, int day) : year_(year), month_(month), day_(day) {} // 构造函数
int getYear() const { return year_; } // 获取年份
int getMonth() const { return month_; } // 获取月份
int getDay() const { return day_; } // 获取日期
private:
int year_; // 年份
int month_; // 月份
int day_; // 日期
};
// 声明Employee类
class Employee {
public:
// 构造函数
Employee(const string& name, const string& id, const Date& birthDate);
// 析构函数
virtual ~Employee() {}
// 纯虚函数,用来计算收入
virtual double earnings() const = 0;
// 打印员工信息
virtual void print() const;
// 获取生日月份
int birthdayMonth() const;
private:
string name_; // 姓名
string id_; // 编号
Date birthDate_; // 出生日期
};
// 实现Employee类
Employee::Employee(const string& name, const string& id, const Date& birthDate)
: name_(name), id_(id), birthDate_(birthDate) {}
void Employee::print() const {
cout << "Employee Type: " << typeid(*this).name() << endl; // 输出类名
cout << "Name: " << name_ << endl; // 输出姓名
cout << "ID: " << id_ << endl; // 输出编号
cout << "Birth Date: " << birthDate_.getYear()
<< "-" << birthDate_.getMonth()
<< "-" << birthDate_.getDay() << endl; // 输出出生日期
}
int Employee::birthdayMonth() const {
return birthDate_.getMonth(); // 返回出生月份
}
// 声明SalariedEmployee类,继承自Employee类
class SalariedEmployee : public Employee {
public:
// 构造函数
SalariedEmployee(const string& name, const string& id, const Date& birthDate, double weeklySalary);
// 计算并返回收入
virtual double earnings() const override;
// 打印员工信息
virtual void print() const override;
private:
double weeklySalary_; // 周薪
};
// 实现SalariedEmployee类
SalariedEmployee::SalariedEmployee(const string& name, const string& id, const Date& birthDate, double weeklySalary)
: Employee(name, id, birthDate), weeklySalary_(weeklySalary) {}
double SalariedEmployee::earnings() const {
return weeklySalary_; // 返回周薪
}
void SalariedEmployee::print() const {
Employee::print(); // 调用基类的打印函数
cout << "Weekly Salary: " << weeklySalary_ << endl; // 输出周薪
}
// 声明HourlyEmployee类,继承自Employee类
class HourlyEmployee : public Employee {
public:
// 构造函数
HourlyEmployee(const string& name, const string& id, const Date& birthDate, double wage, double hours);
// 计算并返回收入
virtual double earnings() const override;
// 打印员工信息
virtual void print() const override;
private:
double wage_; // 每小时工资
double hours_; // 工作小时数
};
// 实现HourlyEmployee类
HourlyEmployee::HourlyEmployee(const string& name, const string& id, const Date& birthDate, double wage, double hours)
: Employee(name, id, birthDate), wage_(wage), hours_(hours) {}
double HourlyEmployee::earnings() const {
if (hours_ <= 40) { // 工作小时数小于等于40
return wage_ * hours_; // 直接计算工资
} else {
return 40 * wage_ + (hours_ - 40) * 1.5 * wage_; // 超过40小时按照加班工资计算工资
}
}
void HourlyEmployee::print() const {
Employee::print(); // 调用基类的打印函数
cout << "Hourly Wage: " << wage_ << endl; // 输出每小时工资
cout << "Working Hours: " << hours_ << endl; // 输出工作小时数
}
// 声明CommissionEmployee类,继承自Employee类
class CommissionEmployee : public Employee {
public:
// 构造函数
CommissionEmployee(const string& name, const string& id, const Date& birthDate, double grossSales, double commissionRate);
// 计算并返回收入
virtual double earnings() const override;
// 打印员工信息
virtual void print() const override;
private:
double grossSales_; // 销售总额
double commissionRate_; // 提成比率
};
// 实现CommissionEmployee类
CommissionEmployee::CommissionEmployee(const string& name, const string& id, const Date& birthDate, double grossSales, double commissionRate)
: Employee(name, id, birthDate), grossSales_(grossSales), commissionRate_(commissionRate) {}
double CommissionEmployee::earnings() const {
return grossSales_ * commissionRate_; // 计算并返回收入
}
void CommissionEmployee::print() const {
Employee::print(); // 调用基类的打印函数
cout << "Gross Sales: " << grossSales_ << endl; // 输出销售总额
cout << "Commission Rate: " << commissionRate_ << endl; // 输出提成比率
}
// 声明BasePlusCommissionEmployee类,继承自CommissionEmployee类
class BasePlusCommissionEmployee : public CommissionEmployee {
public:
// 构造函数
BasePlusCommissionEmployee(const string& name, const string& id, const Date& birthDate, double grossSales, double commissionRate, double baseSalary);
// 计算并返回收入
virtual double earnings() const override;
// 打印员工信息
virtual void print() const override;
private:
double baseSalary_; // 底薪
};
// 实现BasePlusCommissionEmployee类
BasePlusCommissionEmployee::BasePlusCommissionEmployee(const string& name, const string& id, const Date& birthDate, double grossSales, double commissionRate, double baseSalary)
: CommissionEmployee(name, id, birthDate, grossSales, commissionRate), baseSalary_(baseSalary) {}
double BasePlusCommissionEmployee::earnings() const {
return baseSalary_ + CommissionEmployee::earnings(); // 计算并返回收入
}
void BasePlusCommissionEmployee::print() const {
CommissionEmployee::print(); // 调用基类的打印函数
cout << "Base Salary: " << baseSalary_ << endl; // 输出底薪
}
// 检查员工是否生日,如果是则奖励100元
void checkBirthday(Employee* employee) {
Date now(2023, 5, 1);
if (employee->birthdayMonth() == now.getMonth()) { // 如果员工生日月份等于当前月份
cout << "This employee's birthday is in this month. Bonus 100 yuan!" << endl; // 输出奖励信息
}
}
// 主函数
int main() {
vector<Employee*> employees; // 存放雇员对象的容器
// 创建4个不同类型的雇员对象,并加入到容器中
employees.push_back(new SalariedEmployee("Jack", "0001", Date(1990, 1, 1), 2000));
employees.push_back(new HourlyEmployee("Tom", "0002", Date(1993, 2, 14), 20, 45));
employees.push_back(new CommissionEmployee("Lily", "0003", Date(1994, 4, 20), 30000, 0.1));
employees.push_back(new BasePlusCommissionEmployee("Lucy", "0004", Date(1995, 8, 29), 25000, 0.08, 1500));
// 计算并输出各个雇员的收入
for (auto e : employees) { // 遍历容器中的每个雇员
e->print(); // 输出员工信息
checkBirthday(e); // 检查是否生日并奖励
if (dynamic_cast<BasePlusCommissionEmployee*>(e)) { // 如果是BasePlusCommissionEmployee类的对象
BasePlusCommissionEmployee* bpce = dynamic_cast<BasePlusCommissionEmployee*>(e);
bpce->print(); // 输出底薪
double newSalary = bpce->earnings() * 1.1; // 计算经过奖励后的新收入
cout << "New salary after the bonus: " << newSalary << endl; // 输出新收入
}
else {
cout << "Earnings: " << e->earnings() << endl; // 输出原先的收入
}
cout << "------------------------------------------" << endl; // 输出分隔线
}
// 释放内存
for (auto e : employees) {
delete e; // 删除对象
}
employees.clear(); // 清空容器
return 0;
}
了解到多态了,这个code可行:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Date {
public:
Date(int month, int day, int year) : month(month), day(day), year(year) {}
int getMonth() const {
return month;
}
private:
int month;
int day;
int year;
};
class Employee {
public:
Employee(string name, int id, Date birthDate) : name(name), id(id), birthDate(birthDate) {}
virtual double calculatePay() = 0;
virtual void display() = 0;
protected:
string name;
int id;
Date birthDate;
public:
Date getBirthDate() const {
return birthDate;
}
};
class SalariedEmployee : public Employee {
public:
SalariedEmployee(string name, int id, Date birthDate, double salary) : Employee(name, id, birthDate), salary(salary) {}
double calculatePay() override {
return salary;
}
void display() override {
cout << "SalariedEmployee: " << name << ", id: " << id << ", birth month: " << birthDate.getMonth() << ", salary: " << salary << endl;
}
private:
double salary;
};
class HourlyEmployee : public Employee {
public:
HourlyEmployee(string name, int id, Date birthDate, double wage, double hours) : Employee(name, id, birthDate), wage(wage), hours(hours) {}
double calculatePay() override {
if (hours <= 40) {
return wage * hours;
} else {
return 40 * wage + (hours - 40) * wage * 1.5;
}
}
void display() override {
cout << "HourlyEmployee: " << name << ", id: " << id << ", birth month: " << birthDate.getMonth() << ", hours: " << hours << ", wage: " << wage << endl;
}
private:
double wage;
double hours;
};
class CommissionEmployee : public Employee {
public:
CommissionEmployee(string name, int id, Date birthDate, double commissionRate, double sales) : Employee(name, id, birthDate), commissionRate(commissionRate), sales(sales) {}
double calculatePay() override {
return commissionRate * sales;
}
void display() override {
cout << "CommissionEmployee: " << name << ", id: " << id << ", birth month: " << birthDate.getMonth() << ", commission rate: " << commissionRate << ", sales: " << sales << endl;
}
public:
double getCommissionRate() const {
return commissionRate;
}
double getSales() const {
return sales;
}
private:
double commissionRate;
double sales;
};
class BasePlusCommissionEmployee : public CommissionEmployee {
public:
BasePlusCommissionEmployee(string name, int id, Date birthDate, double commissionRate, double sales, double baseSalary) : CommissionEmployee(name, id, birthDate, commissionRate, sales), baseSalary(baseSalary) {}
double calculatePay() override {
return baseSalary + CommissionEmployee::calculatePay();
}
void display() override {
cout << "BasePlusCommissionEmployee: " << name << ", id: " << id << ", birth month: " << getBirthDate().getMonth() << ", commission rate: " << getCommissionRate() << ", sales: " << getSales() << ", base salary: " << baseSalary << endl;
}
private:
double baseSalary;
};
int main() {
vector<Employee*> employees;
employees.push_back(new SalariedEmployee("John", 1, Date(4, 1, 1980), 5000));
employees.push_back(new HourlyEmployee("Mary", 2, Date(5, 2, 1981), 25, 45));
employees.push_back(new CommissionEmployee("Tom", 3, Date(6, 3, 1982), 0.1, 20000));
employees.push_back(new BasePlusCommissionEmployee("Jane", 4, Date(7, 4, 1983), 0.15, 30000, 5000));
for (auto employee : employees) {
double pay = employee->calculatePay();
employee->display();
cout << "Weekly pay: " << pay << endl;
if (employee->getBirthDate().getMonth() == 9) {
cout << "Happy birthday! You get a $100 bonus!" << endl;
pay += 100;
cout << "New weekly pay: " << pay << endl;
}
if (dynamic_cast<BasePlusCommissionEmployee*>(employee) != nullptr) {
cout << "Congratulations! You get a 10% raise!" << endl;
pay *= 1.1;
cout << "New weekly pay: " << pay << endl;
}
cout << endl;
}
for (auto employee : employees) {
delete employee;
}
return 0;
}