题目:
定义Staff(员工)类,由Staff分别派生出Saleman(销售员)类和Manager(经理)类,再由Saleman(销售员)类和Manager(经理)类采用多重继承方式派生出新类SaleManager(销售经理)类。
要求:
(1)在Staff类中包含的数据成员有编号(num)、姓名(name)、出勤率(rateOfAttend)、基本工资(basicSal)和奖金(prize)。在Saleman类中还包含数据成员销售员提成比例(deductRate)和个人销售额(personAmount),在Manager类中还包含数据成员经理提成比例(totalDeductRate)和总销售额(totalAmount)。在SaleManager类中不包含其它数据成员。
(2)各类人员的实发工资公式如下:
员工实发工资 = 基本工资 + 奖金 * 出勤率
销售员实发工资 = 基本工资 + 奖金 * 出勤率 + 个人销售额 * 销售员提成比例
经理实发工资 = 基本工资 + 奖金 * 出勤率 + 总销售额 * 经理提成比例
销售经理实发工资 =基本工资 + 奖金 * 出勤率 + 个人销售额 * 销售员提成比例+ 总销售额 * 经理提成比例
(3)每个类都有构造函数、输出基本信息函数(Output)和输出实发工资函数(OutputWage)。
本题目属于很简单的有关C++继承的问题,感兴趣的小伙伴可以做一下这个题目。本题答案为原创内容。
我们可以很清晰的从题目中得到我们需要声明定义的类,博主在第一次写代码的时候忘记了虚基类这个很重要的知识点,导致在写SaleManager类的时候遇到了很多麻烦,不过好在最后都解决了,这个答案没有什么注释,但是每行代码都很好理解(其实是懒),希望对初学C++的小伙伴们能有所帮助!
#include <iostream>
#include<string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class Staff{
public:
int num;
string name;
float rateOfAttend;
double basicSal;
double prize;
Staff(int a,string b,float c,double d,double e ){
num=a;
name=b;
rateOfAttend=c;
basicSal=d;
prize=e;
}
void Output(){
cout<<"编号:"<<num<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"出勤率:"<<rateOfAttend<<endl;
cout<<"基本工资:"<<basicSal<<endl;
cout<<"奖金:"<<prize<<endl;
}
float OutputWage(float n){
n=basicSal+rateOfAttend*prize;
cout<<n<<endl;
return n;
}
};
/*使用虚基类*/
class Saleman:virtual public Staff{
public:
/*int num;
string name;
float rateOfAttend;
double basicSal;
double prize;*/
float deducRate;
double personAmount;
Saleman(int a,string b,float c,double d,double e ,float x,double y):Staff(a,b,c,d,e ){
deducRate=x;
personAmount=y;
}
float Outputwage(float n){
n=basicSal+rateOfAttend*prize+deducRate*personAmount;
cout<<n<<endl;
return n;
}
void Output(){
cout<<"编号:"<<num<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"出勤率:"<<rateOfAttend<<endl;
cout<<"基本工资:"<<basicSal<<endl;
cout<<"奖金:"<<prize<<endl;
cout<<"提成比例:"<<deducRate<<endl;
cout<<"个人销售额"<<personAmount<<endl;
}
};
class Manager:virtual public Staff{
public :
float totalDeductRate;
double totalAmount;
Manager(int a,string b,float c,double d,double e ,float p,double q):Staff(a,b,c,d,e ){
totalDeductRate=p;
totalAmount=q;
}
void Output(){
cout<<"编号:"<<num<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"出勤率:"<<rateOfAttend<<endl;
cout<<"基本工资:"<<basicSal<<endl;
cout<<"奖金:"<<prize<<endl;
cout<<"提成比例:"<<totalDeductRate<<endl;
cout<<"总销售金额:"<<totalAmount<<endl;
}
float Outputwage(float n){
n=basicSal+rateOfAttend*prize+totalDeductRate*totalAmount;
cout<<n<<endl;
return n;
}
};
class SaleManager:public Saleman,public Manager{
public:
SaleManager(int a,string b,float c,double d,double e ,float x,double y,float q,double p):Staff(a,b,c,d,e),Saleman(a,b,c,d,e,x,y),Manager(a,b,c,d,e,p,q)
{
}
//SaleManager(){};
void Output(){
cout<<"编号:"<<num<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"出勤率:"<<rateOfAttend<<endl;
cout<<"基本工资:"<<basicSal<<endl;
cout<<"奖金:"<<prize<<endl;
}
float Outputwage(float n){
n=basicSal+rateOfAttend*prize+totalDeductRate*totalAmount+deducRate*personAmount;
cout<<n<<endl;
return n;
}
};
int main(int argc, char** argv) {
float n=0;
Staff a(1,"yi",0.9,10000,2000);
a.Output();
cout<<"员工实发工资为:";
a.OutputWage(n);
cout<<"\n------------------\n";
Saleman b(2,"er",0.9,20000,2000,0.9,10000);
b.Output();
cout<<"销售员实发工资为:";
b.Outputwage(n);
cout<<"\n------------------\n";
Manager c(3,"san",0.9,20000,2000,0.9,10000);
c.Output();
cout<<"经理实发工资为:";
c.Outputwage(n);
cout<<"\n------------------\n";
SaleManager d(4,"si",0.9,30000,2000,0.9,10000,0.9,10000);
//SaleManager d();
d.Output();
cout<<"销售经理实发工资为:";
d.Outputwage(n);
cout<<"\n------------------\n";
return 0;
}