//employee.h 类的定义
class employee
{
protected:
char name[20];
int individualEmpNo; //个人编号
int grade; //级别
float accumPay; //月薪
static int employeeNo; //本公司职员编号目前最大值
public:
employee();
~employee();
virtual void pay()=0; //计算月薪函数(纯虚函数)
void SetName(char *); //设置姓名函数
char * GetName(); //提取姓名函数
virtual void promote(int increment=0); //升级函数(虚函数)
int GetindividualEmpNo(); //提取编号函数
int Getgrade(); //提取级别函数
float GetaccumPay(); //提取月薪函数
};
class technician:public employee //兼职技术人员
{
private:
float hourlyRate; //每小时酬金
int workHours; //当月工作时数
public:
technician();
void SetworkHours(int wh); //设置工作时数
void pay(); //计算月薪函数
void promote(int);
};
class salesman:virtual public employee //兼职推销员类
{
protected:
float CommonRate; //按销售额提取酬金的百分比
float sales; //当月销售额
public:
salesman();
void Setsales(float); //设置销售额
void pay(); //计算月薪函数
void promote(int);
};
class manager:virtual public employee //经理类
{
protected:
float monthlyPay; //固定月薪数
public:
manager();
void pay(); //计算月薪函数
void promote(int);
};
class salesmanager:public manager,public salesman
{
public:
salesmanager();
void pay();
void promote(int);
};
//employee.cpp 类的实现
int employee::employeeNo=1000; //员工编号级数为1000
employee::employee()
{
individualEmpNo=employeeNo++; //员工编号目前最大编号为1000
grade=1; //级别初值为1
accumPay=0.0; //月薪总额初值为0
}
employee::~employee()
{}
void employee::pay()
{}
void employee::SetName(char * names) //设置姓名
{
strcpy(name,names);
}
char* employee::GetName() //提取姓名
{
return name;
}
void employee::promote(int increment)
{
grade+=increment; //升级,提升的级数由increment指定
}
int employee::GetindividualEmpNo()
{
return individualEmpNo; //获取成员编号
}
int employee::Getgrade()
{
return grade; //获取级别
}
float employee::GetaccumPay()
{
return accumPay; //获取月薪
}
technician::technician()
{
hourlyRate=1000;
}
void technician::SetworkHours(int wh)
{
workHours=wh;
}
void technician::pay()
{
accumPay=hourlyRate*workHours;
}
void technician::promote(int)
{
employee::promote(2);
}
salesman::salesman()
{
CommonRate=0.04f;
}
void salesman::Setsales(float sl)
{
sales=sl;
}
void salesman::pay()
{
accumPay=sales*CommonRate;
}
void salesman::promote(int)
{
employee::promote(0);
}
manager::manager()
{
monthlyPay=8000;
}
void manager::pay()
{
accumPay=monthlyPay;
}
void manager::promote(int)
{
employee::promote(3);
}
salesmanager::salesmanager()
{
monthlyPay=5000;
CommonRate=0.005;
}
void salesmanager::pay()
{
accumPay=monthlyPay+CommonRate*sales;
}
void salesmanager::promote(int)
{
employee::promote(2);
}
//main
#include<iostream>
#include<vector>
#include "employee.h"
using namespace std;
int main()
{
manager m1;
technician t1;
salesmanager sm1;
salesman ss1;
char namestr[20];
vector<employee *> vchar;
vchar.push_back(&m1);
vchar.push_back(&t1);
vchar.push_back(&sm1);
vchar.push_back(&ss1);
int i;
for(i=0;i<4;i++)
{
cout<<"请输入下一个雇员的姓名:";
cin>>namestr;
vchar[i]->SetName(namestr); //设置每个成员姓名
vchar[i]->promote(); //升级,通过基类指针访问各派生类函数
}
cout<<"请输入兼职技术人员"<<t1.GetName()<<"本月的工作时数:";
int ww;
cin>>ww;
t1.SetworkHours(ww);
cout<<"请输入销售经理"<<sm1.GetName()<<"所管辖部门本月的销售总额:";
float s1;
cin>>s1;
sm1.Setsales(s1);
cout<<"请输入推销员"<<ss1.GetName()<<"本月的销售额:";
cin>>s1;
ss1.Setsales(s1);
for(i=0;i<4;i++)
{
vchar[i]->pay(); //计算月薪,通过基类指针访问各派生类函数
cout<<vchar[i]->GetName()<<"编号"<<vchar[i]->GetindividualEmpNo()<<"级别为"<<vchar[i]->Getgrade()
<<"级,本月工资"<<vchar[i]->GetaccumPay()<<endl;
}
}