用c++编写一个小型公司的工资管理程序
时间: 2023-09-04 17:10:08 浏览: 249
好的,以下是一个简单的小型公司的工资管理程序的C++代码:
```c++
#include <iostream>
using namespace std;
// 定义员工类
class Employee {
public:
Employee(string name, int id, int level) {
this->name = name;
this->id = id;
this->level = level;
}
virtual double calcSalary() = 0; // 计算当月薪水
virtual void displayInfo() = 0; // 显示员工信息
protected:
string name; // 员工姓名
int id; // 员工编号
int level; // 员工级别
};
// 定义经理类
class Manager : public Employee {
public:
Manager(string name, int id, int level, double baseSalary)
: Employee(name, id, level) {
this->baseSalary = baseSalary;
this->monthSalary = calcSalary();
}
double calcSalary() {
return baseSalary;
}
void displayInfo() {
cout << "姓名:" << name << endl;
cout << "编号:" << id << endl;
cout << "级别:" << level << endl;
cout << "固定工资:" << baseSalary << endl;
cout << "当月薪水:" << monthSalary << endl;
}
private:
double baseSalary; // 固定工资
double monthSalary; // 当月薪水
};
// 定义兼职技术人员类
class PartTimeTech : public Employee {
public:
PartTimeTech(string name, int id, int level, double hourlySalary, double workHours)
: Employee(name, id, level) {
this->hourlySalary = hourlySalary;
this->workHours = workHours;
this->monthSalary = calcSalary();
}
double calcSalary() {
return hourlySalary * workHours;
}
void displayInfo() {
cout << "姓名:" << name << endl;
cout << "编号:" << id << endl;
cout << "级别:" << level << endl;
cout << "时薪:" << hourlySalary << endl;
cout << "工作小时数:" << workHours << endl;
cout << "当月薪水:" << monthSalary << endl;
}
private:
double hourlySalary; // 时薪
double workHours; // 工作小时数
double monthSalary; // 当月薪水
};
// 定义销售经理类
class SalesManager : public Employee {
public:
SalesManager(string name, int id, int level, double baseSalary, double commissionRate, double salesAmount)
: Employee(name, id, level) {
this->baseSalary = baseSalary;
this->commissionRate = commissionRate;
this->salesAmount = salesAmount;
this->monthSalary = calcSalary();
}
double calcSalary() {
return baseSalary + commissionRate * salesAmount;
}
void displayInfo() {
cout << "姓名:" << name << endl;
cout << "编号:" << id << endl;
cout << "级别:" << level << endl;
cout << "底薪:" << baseSalary << endl;
cout << "提成比例:" << commissionRate << endl;
cout << "销售额:" << salesAmount << endl;
cout << "当月薪水:" << monthSalary << endl;
}
private:
double baseSalary; // 底薪
double commissionRate; // 提成比例
double salesAmount; // 销售额
double monthSalary; // 当月薪水
};
// 定义兼职销售员类
class PartTimeSales : public Employee {
public:
PartTimeSales(string name, int id, int level, double baseSalary, double commissionRate, double salesAmount)
: Employee(name, id, level) {
this->baseSalary = baseSalary;
this->commissionRate = commissionRate;
this->salesAmount = salesAmount;
this->monthSalary = calcSalary();
}
double calcSalary() {
return baseSalary + commissionRate * salesAmount;
}
void displayInfo() {
cout << "姓名:" << name << endl;
cout << "编号:" << id << endl;
cout << "级别:" << level << endl;
cout << "底薪:" << baseSalary << endl;
cout << "提成比例:" << commissionRate << endl;
cout << "销售额:" << salesAmount << endl;
cout << "当月薪水:" << monthSalary << endl;
}
private:
double baseSalary; // 底薪
double commissionRate; // 提成比例
double salesAmount; // 销售额
double monthSalary; // 当月薪水
};
int main() {
// 创建员工对象并计算当月薪水
Employee* emp1 = new Manager("张三", 1001, 1, 10000);
Employee* emp2 = new PartTimeTech("李四", 1002, 2, 50, 120);
Employee* emp3 = new SalesManager("王五", 1003, 3, 5000, 0.05, 200000);
Employee* emp4 = new PartTimeSales("赵六", 1004, 4, 2000, 0.02, 50000);
// 显示员工信息
emp1->displayInfo();
emp2->displayInfo();
emp3->displayInfo();
emp4->displayInfo();
return 0;
}
```
在这个程序中,我们定义了一个`Employee`类作为基类,其他员工类(`Manager`、`PartTimeTech`、`SalesManager`、`PartTimeSales`)都继承自它。每个员工类实现了自己的`calcSalary`和`displayInfo`方法,用于计算当月薪水和显示员工信息。在`main`函数中,我们创建了不同类型的员工对象,并分别调用它们的`displayInfo`方法来显示员工信息和当月薪水。
阅读全文