C++语言程序设计

| 例11-37

#include "employee.h"
//普通雇员类
class Employee
{
public:
//构造函数
Employee(string theName, float thePayRate):
name(theName),payRate(thePayRate) {}
//取雇员姓名
string getName() const {return name; }
//取雇员薪水等级
float getPayRate() const { return payRate; }
//计算雇员薪水
virtual float pay(float hoursWorked) const { return hoursWorked*payRate; }

protected:
string name; //雇员名称
float payRate; //薪水等级
};

//管理人员类
//继承普通雇员类
class Manager : public Employee
{
public:
//构造函数
//isSalaried标识管理人员类的付薪方式
//true 按阶段付薪（固定工资）
//false 按小时付薪
Manager(string theName, float thePayRate, bool isSalaried):
Employee(theName,thePayRate),salaried(isSalaried) {}
//取付薪方式
bool getSalaried() const { return salaried; }
//计算薪水
virtual float pay(float hoursWorked) const ;
protected:
bool salaried;
};
float Manager ::pay(float hoursWorked) const
{
if (salaried) //固定付薪方式
return payRate;
else //按小时付薪
return hoursWorked * payRate;
}

//主管人员类
class Supervisor : public Manager
{
public:
//构造函数
Supervisor（string theName, float thePayRate, float theBouns）:
Manager(theName, thePayRate, true), bouns(theBouns) {}
//取奖金数额
float getBouns() const { return bouns; }
//计算薪水
virtual float pay(float hoursWorked) const
{
return payRate+bouns;
}
protected:
float bouns;
}