写一份 工资管理系统 源码
时间: 2024-03-08 14:29:51 浏览: 95
抱歉,作为AI语言模型,我目前无法编写完整的源代码。但是,我可以给出一个简单的工资管理系统的示例代码,供您参考。
示例代码:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Employee {
public:
Employee(string name, int baseSalary) : m_name(name), m_baseSalary(baseSalary) {}
virtual ~Employee() {}
virtual int getSalary() const = 0;
string getName() const {
return m_name;
}
protected:
string m_name;
int m_baseSalary;
};
class HourlyEmployee : public Employee {
public:
HourlyEmployee(string name, int baseSalary, int hourlyRate, int hoursWorked) : Employee(name, baseSalary), m_hourlyRate(hourlyRate), m_hoursWorked(hoursWorked) {}
virtual ~HourlyEmployee() {}
virtual int getSalary() const {
return m_baseSalary + (m_hourlyRate * m_hoursWorked);
}
private:
int m_hourlyRate;
int m_hoursWorked;
};
class SalariedEmployee : public Employee {
public:
SalariedEmployee(string name, int baseSalary) : Employee(name, baseSalary) {}
virtual ~SalariedEmployee() {}
virtual int getSalary() const {
return m_baseSalary;
}
};
class CommissionedEmployee : public Employee {
public:
CommissionedEmployee(string name, int baseSalary, int commissionRate, int sales) : Employee(name, baseSalary), m_commissionRate(commissionRate), m_sales(sales) {}
virtual ~CommissionedEmployee() {}
virtual int getSalary() const {
return m_baseSalary + (m_commissionRate * m_sales);
}
private:
int m_commissionRate;
int m_sales;
};
class PayrollSystem {
public:
PayrollSystem() {}
void addEmployee(Employee* employee) {
m_employees.push_back(employee);
}
void removeEmployee(Employee* employee) {
for (int i = 0; i < m_employees.size(); i++) {
if (m_employees[i] == employee) {
m_employees.erase(m_employees.begin() + i);
break;
}
}
}
int calculateTotalPayroll() const {
int totalPayroll = 0;
for (int i = 0; i < m_employees.size(); i++) {
totalPayroll += m_employees[i]->getSalary();
}
return totalPayroll;
}
private:
vector<Employee*> m_employees;
};
int main() {
PayrollSystem payroll;
HourlyEmployee* hourlyEmployee1 = new HourlyEmployee("John Doe", 1000, 20, 40);
HourlyEmployee* hourlyEmployee2 = new HourlyEmployee("Jane Smith", 1200, 25, 35);
SalariedEmployee* salariedEmployee1 = new SalariedEmployee("Bob Johnson", 2000);
CommissionedEmployee* commissionedEmployee1 = new CommissionedEmployee("Alice Brown", 1500, 10, 5000);
payroll.addEmployee(hourlyEmployee1);
payroll.addEmployee(hourlyEmployee2);
payroll.addEmployee(salariedEmployee1);
payroll.addEmployee(commissionedEmployee1);
cout << "Total payroll: " << payroll.calculateTotalPayroll() << endl;
payroll.removeEmployee(hourlyEmployee2);
cout << "Total payroll after removing an employee: " << payroll.calculateTotalPayroll() << endl;
delete hourlyEmployee1;
delete hourlyEmployee2;
delete salariedEmployee1;
delete commissionedEmployee1;
return 0;
}
阅读全文