第三次上机实验:为某公司设计雇员工资发放系统。
每个雇员的基本信息包括:姓名(name),工号(id)。
雇员的收入取决于雇员的类型。该公司共有四类雇员:
周薪雇员(SalariedEmployee):收入=固定周薪。
时薪雇员(HourlyEmployee):若工作40小时以下,收入=小时数*每小时薪水;若工作40小时以上,收入=40*每小时薪水+(小时数-40)*每小时薪水*150%。
佣金雇员(CommissionEmployee):收入=销售量*每个商品的销售佣金
带底薪佣金雇员(BasePlusCommissionEmployee):收入=底薪+销售量*每个商品的销售佣金
要求:建立雇员继承层次,每个类包含计算工资和显示输出的功能,可以计算和显示输出公司雇员(Employee)的每周收入。输出时要显示该类雇员的所有信息。(包括雇员类型、姓名、工号、工资各项明细),写出主函数测试各类。
//Employee.h-类定义头文件
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include<iostream>
using namespace std;
class Employee{
public:
virtual void output_income() = 0;
protected:
char name[20];
char type[30];
int id;
static float week_salary;
static float hour_salary;
static float unit_price;
static float basic_salary;
};
class SalariedEmployee:public Employee{
public:
SalariedEmployee();
void output_income();
};
class HourlyEmployee:public Employee{
public:
HourlyEmployee();
void output_income();
private:
int work_hours;
};
class CommissionEmployee:public Employee{
public:
CommissionEmployee();
void output_income();
private:
int sales_volume;
};
class BasePlusCommissionEmployee:public Employee{
public:
BasePlusCommissionEmployee();
void output_income();
private:
int sales_volume;
};
#endif
//Employee.cpp-类函数定义
#include "Employee.h"
#include <string.h>
float Employee::week_salary=1000;
float Employee::hour_salary=20;
float Employee::unit_price=10;
float Employee::basic_salary=4000;
SalariedEmployee::SalariedEmployee(){
strcpy(type,"SalariedEmployee");
cout<<"please input employee's name:";
cin>>name;
cout<<"please input employee's id:";
cin>>id;
}
void SalariedEmployee::output_income(){
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
cout<<" name:"<<name<<endl;
cout<<" id: "<<id<<endl;
cout<<" type:SalariedEmployee"<<endl;
cout<<" week_salary:"<<week_salary<<endl;
cout<<" weekly income:"<<week_salary<<endl;
cout<<" details:salary=week_salary*1="<<week_salary<<"*1="<<week_salary<<endl;
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
}
HourlyEmployee::HourlyEmployee(){
strcpy(type,"HourlyEmployee");
cout<<"please input employee's name:";
cin>>name;
cout<<"please input employee's id:";
cin>>id;
cout<<"please input employee's work time:";
cin>>work_hours;
}
void HourlyEmployee::output_income(){
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
cout<<" name:"<<name<<endl;
cout<<" id: "<<id<<endl;
cout<<" type:HourlyEmployee"<<endl;
cout<<" hour_salary:"<<hour_salary<<endl;
cout<<" work_hours:"<<work_hours<<endl;
if(work_hours<=40)
{
cout<<" weekly income:"<<hour_salary*work_hours<<endl;
cout<<" deatils:salary=work_hours*hour_salary="<<work_hours<<'*'<<hour_salary<<'='<<hour_salary*work_hours<<endl;
}
else
{
cout<<" weekly income:"<<40*hour_salary+(work_hours-40)*hour_salary*1.5<<endl;
cout<<" details:salary=40*hour_salary+(work_hours-40)*hour_salary*150%=40*"<<hour_salary<<"+("<<work_hours<<"-40)*"<<hour_salary<<"*150%="<<40*hour_salary+(work_hours-40)*hour_salary*1.5<<endl;
}
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
}
CommissionEmployee::CommissionEmployee(){
strcpy(type,"CommissionEmployee");
cout<<"please input employee's name:";
cin>>name;
cout<<"please input employee's id:";
cin>>id;
cout<<"please input employee's sales volume:";
cin>>sales_volume;
}
void CommissionEmployee::output_income(){
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
cout<<" name:"<<name<<endl;
cout<<" id: "<<id<<endl;
cout<<" type:CommissionEmployee"<<endl;
cout<<" sales_volume:"<<sales_volume<<endl;
cout<<" unit_price:"<<unit_price<<endl;
cout<<" weekly income:"<<sales_volume*unit_price<<endl;
cout<<" details:salary=sales_volume*unit_price="<<sales_volume<<'*'<<unit_price<<'='<<sales_volume*unit_price<<endl;
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
}
BasePlusCommissionEmployee::BasePlusCommissionEmployee(){
strcpy(type,"BasePlusCommissionEmployee");
cout<<"please input employee's name:";
cin>>name;
cout<<"please input employee's id:";
cin>>id;
cout<<"please input employee's sales volume:";
cin>>sales_volume;
}
void BasePlusCommissionEmployee::output_income(){
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
cout<<" name:"<<name<<endl;
cout<<" id: "<<id<<endl;
cout<<" type:BasePlusCommissionEmployee"<<endl;
cout<<" basic_salary:"<<basic_salary<<endl;
cout<<" sales_volume:"<<sales_volume<<endl;
cout<<" unit_price:"<<unit_price<<endl;
cout<<" weekly income:"<<basic_salary+sales_volume*unit_price<<endl;
cout<<" details:salary=basic_salary+sales_volume*unit_price="<<basic_salary<<'+'<<sales_volume<<'*'<<unit_price<<'='<<basic_salary+sales_volume*unit_price<<endl;
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
}
//main.cpp-主函数(测试)
#include <iostream>
#include "Employee.h"
int main() {
HourlyEmployee A;
A.output_income();
CommissionEmployee B;
B.output_income();
BasePlusCommissionEmployee C;
C.output_income();
SalariedEmployee D;
D.output_income();
}