雇员类employee的声明如下:
class employee{
private:
string name;
int id;
public:
......
......set(.....); //设置数据成员
double earnings(){return 0;}
void print();//输出雇员的姓名和id
};
类commissionEmployee是指总收入是"销售额*提成比例"的雇员
class commissionEmployee:public employee{
private:
double grossSales;//销售额
double commissionRate; //提成比例
public:
......
......set(.....); //设置数据成员
double earnings();//返回总收入
void print();//输出雇员的姓名,id和总收入
};
类basePlusCommissionEmployee是指总收入是"基本工资+销售额*提成比例"的雇员
class basePlusCommissionEmployee:public commissionEmployee{
private:
double baseSalary; //基本工资
public:
......
......set(.....); //设置数据成员
double earnings();//返回总收入
void print();//输出雇员的姓名,id和总收入
};
完成以上三个类的定义,编写主函数进行验证。
void main()
{
basePlusCommissionEmployee ce;
ce.set("wang", 123, 30000, 0.2, 500);
ce.print();
}
运行结果如下:
wang123
6500
实现代码:
#include<iostream>
#include<string>
using namespace std;
class employee{
private:
string name;
int id;
public:
void set(const string &name_,int id_)
{
name=name_;
id=id_;
}
double earnings(){return 0;}
void print()
{
cout<<name<<id<<endl;
}
};
class commissionEmployee:public employee{
private:
double grossSales;
double commissionRate;
public:
void set(double grossSales_,double commissionRate_)
{
grossSales=grossSales_;
commissionRate=commissionRate_;
}
double earnings(double baseSalary_)
{
return grossSales*commissionRate+baseSalary_;
}
void print(double baseSalary_)
{
cout<<grossSales*commissionRate+baseSalary_;
}
};
class basePlusCommissionEmployee:public commissionEmployee{
private:
double baseSalary;
public:
void set(const string& name_,int id_,double grossSales_,double commissionRate_,double baseSalary_ )
{
baseSalary=baseSalary_;
employee::set(name_,id_);
commissionEmployee::set(grossSales_,commissionRate_);
};
double earnings()
{
}
void print()
{
employee::print();
commissionEmployee::print(baseSalary);
}
};
void main()
{
basePlusCommissionEmployee ce;
ce.set("wang",123, 30000, 0.2, 500);
ce.print();
cout<<endl;
}