练习: 某公司有三种员工: 第一种,工资固定 第二种,工资靠提成 第三种,底薪+提成 在某次发工资时,公司决定调整第三种员工的底薪,请写一个C++程序,实现以上功能。
时间: 2024-12-17 13:54:06 浏览: 3
在这个练习中,我们可以创建一个Employee基类和两个派生类:FixedSalaryEmployee和CommissionBasedEmployee,再加上BonusEmployee类。当需要调整底薪时,我们可以在BonusEmployee类中添加一个函数来设置新的底薪。
```cpp
#include <iostream>
// 基类 Employee
class Employee {
protected:
double baseSalary;
public:
Employee(double baseSalary) : baseSalary(baseSalary) {}
virtual ~Employee() {}
// 获取底薪
double getBaseSalary() const { return baseSalary; }
};
// 固定工资员工 FixedSalaryEmployee
class FixedSalaryEmployee : public Employee {
private:
public:
FixedSalaryEmployee(double baseSalary) : Employee(baseSalary) {} // 使用基类构造函数
void setBaseSalary(double newSalary) override { // 不能直接修改固定工资
std::cout << "固定工资员工的底薪不能更改" << std::endl;
}
};
// 提成工资员工 CommissionBasedEmployee
class CommissionBasedEmployee : public Employee {
private:
public:
CommissionBasedEmployee(double baseSalary) : Employee(baseSalary) {}
void setBaseSalary(double newSalary) override { // 底薪可以调整
baseSalary = newSalary;
}
};
// 薪酬结构 BonusEmployee (包含底薪和提成)
class BonusEmployee : public CommissionBasedEmployee {
private:
double commissionRate;
public:
BonusEmployee(double baseSalary, double commissionRate) : CommissionBasedEmployee(baseSalary), commissionRate(commissionRate) {}
void setBaseSalary(double newSalary) override {
baseSalary = newSalary;
// 如果需要,还可以更新提成比例或其他相关的变量
}
};
int main() {
// 创建一个薪酬结构 BonusEmployee 实例
BonusEmployee employee(5000, 0.1); // 原始底薪 5000,提成率 10%
// 调整底薪
employee.setBaseSalary(6000);
// 打印当前底薪
std::cout << "调整后的底薪: " << employee.getBaseSalary() << std::endl;
return 0;
}
```
阅读全文