题目如下:
任务描述:
某公司的雇员分为以下若干类:
1、抽象类 Employee :这是所有员工总的父类;
数据成员:员工的姓名,员工的生日月份。
成员函数:string getname(),来获取员工姓名;float getSalary(int month)根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。
2、SalariedEmployee : Employee 的子类,拿固定工资的员工。
数据成员:月薪。
3、HourlyEmploycee : Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。
数据成员:每小时的工资、每月工作的小时数。
4、SalesEmployee : Employee 的子类,销售人员,工资由月销售额和提成率决定。
数据成员:月销售额、提成率。
写一个程序把若干各种类型的员工放在一个Employee 数组里,写一个函数,打印出某月每个员工的工资数额。
实验要求和目的:
1.熟练派生类的定义方法。
2.掌握使用虚函数实现多态性。
#include "iostream"
#include "string"
#include"iomanip"
using namespace std;
class Employee //Employee抽象基类
{
public:
Employee(char *na,int bm);
virtual float getSalary(int month)=0;
//各类中实现方式不同,故定义为虚函数,以便后续调用
string getname();
protected:
char name[20];
float brithmonth;
};
Employee::Employee(char *na,int bm){strcpy(name,na);brithmonth=bm;}
string Employee::getname(){return name;}
class SalariedEmployee:public Employee //固定工资的员工类
{
public:
SalariedEmployee(char *na,int bm,float s);
float getSalary(int month); //虚函数需要重新定义
protected:
float salary;
};
SalariedEmployee::SalariedEmployee(char *na,int bm,float s):Employee(na,bm),salary(s){}
float SalariedEmployee::getSalary(int month)
{
if(month==brithmonth)
salary+=100;
return salary;
}
class HourlyEmployee:public Employee //按小时收费的员工类
{
public:
HourlyEmployee(char *na,int bm,float hs,int hour);
float getSalary(int month);
protected:
int hour;
float hoursalary;
};
HourlyEmployee::HourlyEmployee(char *na,int bm,float hs,int h):Employee(na,bm),hoursalary(hs),hour(h){}
float HourlyEmployee::getSalary(int month)
{
if (hour>160)
hoursalary=(hour-160)*1.5*hoursalary;
else
hoursalary=hour*hoursalary;
if(month==brithmonth)
hoursalary+=100;
return hoursalary;
}
class SalesEmployee:public Employee //按销售提成收费的员工类
{
public:
SalesEmployee(char *na,int bm,float sn,float tc);
float getSalary(int month);
protected:
float sellnum;
float ticheng;
float salary;
};
SalesEmployee::SalesEmployee(char *na,int bm,float sn,float tc):Employee(na,bm),sellnum(sn),ticheng(tc){}
float SalesEmployee::getSalary(int month)
{
salary=sellnum*ticheng;
if(month==brithmonth)
salary+=100;
return salary;
}
int main ()
{
int month,i;
char name[20];
Employee *e[5]=
{
&SalariedEmployee ("Mike",3,2789),
&HourlyEmployee ("Jake",7,30,100),
&SalesEmployee("Rotus",2,1.5e7,0.12),
&HourlyEmployee ("Lin",12,235,100),
&SalariedEmployee ("Liu",12,3122)
};
cout<<"plase input the month:";
cin>>month;
cout<<setiosflags(ios::fixed)<<setprecision(2);
for (i=0;i<5;i++)
cout<<(*e[i]).getname()<<":"<<(*e[i]).getSalary(month)<<endl;
return 0;
}