某公司雇员(employee)包括经理(manager),技术人员(technician)和销售员(salesman)。开发部经理(developermanger),既是经理也是技术人员。销售部经理(salesmanager),既是经理也是销售员。
以employ类为虚基类派生出manager,technician和salesman类;再进一步派生出developermanager和salesmanager类。
employee类的属性包括姓名、职工号、工资级别,月薪(实发基本工资加业绩工资)。操作包括月薪计算函数(pay()),该函数要求输入请假天数,扣去应扣工资后,得出实发基本工资。
technician类派生的属性有每小时附加酬金和当月工作时数,及研究完成进度系数。业绩工资为三者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。
salesman类派生的属性有当月销售额和酬金提取百分比,业绩工资为两者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。
manager类派生属性有固定奖金额和业绩系数,业绩工资为两者之积。工资总额也为基本工资加业绩工资。
而developermanager类,pay()函数是将作为经理和作为技术人员业绩工资之和的一半作为业绩工资。
salesamanager类,pay()函数则是经理的固定奖金额的一半,加上部门总销售额与提成比例之积,这是业绩工资。
编程实现工资管理。特别注意pay()的定义和调用方法:先用同名覆盖,再用运行时多态。
#include<iostream>
#include <string>
using namespace std;
static int Grades[]={500,600,750,1000,1400,2000,2800,4000};
class employee{
protected:
string name;//姓名
int ID;//职工号
int grade;//工资级别
double salary;//月
double base_salary;//基本月薪
double career_salary;//业绩工资
public:
employee(string="",int=0,int=0);
employee(employee &); //拷贝构造函数
virtual void pay(); //月薪计算函数
void show();
double getsalary(){return salary;}
double getbase_salary(){return base_salary;}
double getcareer_salary(){return career_salary;}
};
employee::employee(string nn,int id,int gr){
name=nn;
ID = id;
grade = gr;
salary=0; //月薪
base_salary=0; //基本月薪
career_salary=0; //业绩工资
}
employee::employee(employee &emp){ //拷贝构造函数
name=emp.name;
ID=emp.ID;
grade =emp.grade;
salary=emp.salary; //月薪
base_salary=emp.base_salary; //基本月薪
career_salary=emp.career_salary;//业绩工资
}
void employee::show(){
cout<<name<<'\t'<<ID<<'\t'<<salary<<endl;
}
void employee::pay(){
int days;
cout<<"请输入请假天数:\n";
cin>>days;
base_salary = Grades[grade]*(23 - days)/23;
career_salary = base_salary/2; //普通员工业绩工资为基本工资的一半
salary = base_salary+career_salary;
}
class manager:virtual public employee{//虚基类
protected:
double prize;//固定奖金额
double factor;//业绩系数
public:
manager(string="",int=0,int=0,double=0);
manager(manager&mag); //拷贝构造函数
void pay();
};
manager::manager(manager&mag):employee(mag){//按赋值兼容规则mag可为employee的实参
prize=mag.prize;
factor=mag.factor;
}
manager::manager(string nn,int id,int gr,double pr):employee(nn,id,gr){
prize = pr;//固定奖金额
factor = 0;
}
void manager::pay(){
int days;
cout<<"请输入请假天数:\n";
cin>>days;
cout<<"请输入业绩系数:\n";
cin>>factor;
base_salary = Grades[grade]*(23 - days)/23;
career_salary = prize*factor*(23 - days)/23;
salary = base_salary + career_salary ;
}
class technician:virtual public employee{
protected:
double hours;//月工作时数
double perhour;//每小时附加酬金
double shfactor;//研究进度系数
public:
technician(string="",int=0,int=0,double=0);
technician(technician &); //拷贝构造函数
void pay();
};
technician::technician(technician& tech):employee(tech){ //拷贝构造函数
hours=tech.hours;//月工作时数
perhour=tech.perhour;//每小时附加酬金
shfactor=tech.shfactor;//研究进度系数
}
technician::technician(string nn,int id,int gr,double phr):employee(nn,id,gr){
hours = 0;
perhour = phr;//每小时附加酬金
shfactor = 0;
}
void technician::pay(){
int days;
cout<<"请输入请假天数:\n";
cin>>days;
cout<<"请输入研究进度系数:\n";
hours=8*(23 - days);
cin>>shfactor;
base_salary = Grades[grade]*(23 - days)/23;
career_salary = perhour*hours*shfactor*(23 - days)/23;
salary= base_salary + career_salary ;
}
class salesman:virtual public employee{
protected:
double amount;//销售额
double slfactor;//提成比例
public:
salesman(string="",int=0,int=0,double=0);
salesman(salesman &); //拷贝构造函数
void pay();
};
salesman::salesman(string nn,int id,int gr,double slfac):employee(nn,id,gr){
amount = 0;
slfactor = slfac;//提成比例
}
salesman::salesman(salesman &sale):employee(sale){ //拷贝构造函数
amount=sale.amount;//销售额
slfactor=sale.slfactor;//提成比例
}
void salesman::pay(){
int days;
cout<<"请输入请假天数:\n";
cin>>days;
cout<<"请输入销售额:\n";
cin>>amount;
base_salary = Grades[grade]*(23 - days)/23;
career_salary = amount*slfactor;
salary = base_salary + career_salary ;
}
class developermanager:public manager,public technician {
public:
developermanager(string="",int id=0,int gr=0,double pr=0, double phr=0);
developermanager(developermanager &); //拷贝构造函数
void pay();
};
developermanager::developermanager(string nn,int id,int gr,double pr,double phr)
:manager(nn,id,gr,pr),technician(nn,id,gr,phr),employee(nn,id,gr){}
developermanager::developermanager(developermanager &deman)
:manager(deman),technician(deman),employee(deman){} //拷贝构造函数
void developermanager::pay(){
int days;
cout<<"请输入请假天数:\n";
cin>>days;
cout<<"请输入业绩系数:\n";
cin>>factor;
cout<<"请输入研究进度系数:\n";
cin>>shfactor;
hours=8*(23 - days);
base_salary = Grades[grade]*(23 - days)/23;//基本工资
career_salary = perhour*hours*shfactor*(23 - days)/23;//技术人员奖金
career_salary += prize*factor*(23 - days)/23;//加经理业绩工资
career_salary /=2;
salary = base_salary + career_salary ;
}
class salesmanager:public manager,public salesman{
public:
salesmanager(string="",int=0,int=0,double pr=0, double slfac=0);
salesmanager(salesmanager &); //拷贝构造函数
void pay();
};
salesmanager::salesmanager(string nn,int id,int gr,double pr,double slfac)
:manager(nn,id,gr,pr),salesman(nn,id,gr,slfac),employee(nn,id,gr){}
salesmanager::salesmanager(salesmanager& salman)
:manager(salman),salesman(salman),employee(salman){} //拷贝构造函数
void salesmanager::pay(){
int days;
cout<<"请输入请假天数:\n";
cin>>days;
cout<<"请输入业绩系数:\n";
cin>>factor;
cout<<"请输入销售额:\n";
cin>>amount;
base_salary = Grades[grade]*(23 - days)/23;//基本工资
career_salary = prize*factor*(23 - days)/23;//经理业绩工资
career_salary /=2;
career_salary += amount*slfactor;//加销售业绩工资
salary = base_salary + career_salary ;
}
int main(){
employee eml1("张伟",10012,0),*emlp;
manager mag1("姚婕",20005,4,1000);
technician tec1("王茜",30047,5,10);
salesman sal1("朱明",40038,2,0.05);
developermanager dem1("沈俊",50069,6,1500,12);
salesmanager sam1("况钟",60007,3,1000,0.05);
eml1.pay();
eml1.show();
mag1.pay();
mag1.show();
tec1.pay();
tec1.show();
sal1.pay();
sal1.show();
emlp=&dem1;
emlp->pay();
emlp->show();
emlp=&sam1;
emlp->pay();
emlp->show();
cout<<endl;
employee eml0(eml1);
manager mag0(mag1);
technician tec0(tec1);
salesman sal0(sal1);
developermanager dem0(dem1);
salesmanager sam0(sam1);
cout<<"输出拷贝结果:"<<endl;
eml0.show();
mag0.show();
tec0.show();
sal0.show();
emlp=&dem0;
emlp->show();
emlp=&sam0;
emlp->show();
return 0;
}
![运行结果!!!](https://img-blog.csdnimg.cn/20190104190509680.png)