主函数
salary.cpp
#include <iostream>
#include <assert.h>
#include <iomanip>
#include "Employ.h"
#include "Manager.h"
#include "CommissionWorker.h"
#include "HourlyWorker.h"
#include "PieceWorker.h"
using namespace std;
int main(void)
{
cout.precision(2);
cout.setf(ios::fixed);
Employee *ptr; //基类指针
Manager m("张", "奇", 800.00);
ptr = &m;
ptr->print(); //动态连编
cout << "的工资是:" << ptr->earnings() << "元"; //动态连编
m.print(); //静态连编
cout << "的工资是:" << m.earnings() << "元" << endl;//静态连编
CommissionWorker c("张", "明", 200.0, 3.0, 150);
ptr = &c;
ptr->print(); //动态连编
cout << "的工资是:" << ptr->earnings() << "元"; //动态连编
c.print(); //静态连编
cout << "的工资是:" << c.earnings() << "元" << endl;//静态连编
HourlyWorker h("周", "玲", 20.0, 40);
ptr = &h;
ptr->print(); //动态连编
cout << "的工资是:" << ptr->earnings() << "元"; //动态连编
c.print(); //静态连编
cout << "的工资是:" << c.earnings() << "元" << endl;//静态连编
PieceWorker p("孔", "杰", 5, 300);
ptr = &p;
ptr->print(); //动态连编
cout << "的工资是:" << ptr->earnings() << "元"; //动态连编
p.print();
cout << "的工资是:" << c.earnings() << "元"; //静态连编
return 0;
}
所有工作人员公用的数据与函数
Employ.h
#ifndef employ_h
#define employ_h
//抽象基类Employee
class Employee
{
public:
//默认构造函数
Employee(const char *first=" ", const char *last = " ");
~Employee(); //析构函数
const char *getFirstName() const; //读取并返回姓
const char *getLastName() const; //读取并返回名
//纯虚函数使类Employee成为抽象基类
virtual float earnings() const = 0; //纯虚函数
virtual void print() const = 0; //纯虚函数
private:
char *firstName; //姓
char *lastName; //名
};
#endif
Employ.cpp
#include <iostream>
#include <assert.h>
#include <string.h>
#include "Employ.h"
using namespace std;
//注:纯虚函数没有定义
//构造函数动态为姓和名分配内存,并用函数strcpy把姓和名拷贝到对象中
/*
assert的作用是现计算表达式 expression ,如果其值为假(即为0),
那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行
*/
Employee::Employee(const char *first, const char *last)
{
firstName = new char[strlen(first) + 1];
assert(firstName != 0); //测试内存分配是否成功
strcpy(firstName, first);
lastName = new char[strlen(last) + 1];
assert(lastName != 0); //测试内存分配是否成功
strcpy(lastName, last);
}
//析构函数释放动态分配的内存
Employee::~Employee()
{
delete []firstName;
delete []lastName;
}
//返回指向雇员姓的指针
const char *Employee::getFirstName() const
{
return firstName; //调用者必须自己释放内存
}
//返回指向雇员名的指针
const char *Employee::getLastName() const
{
return lastName; //调用者必须自己释放内存
}
经理的对象
Manager.h
#ifndef manager_h
#define manager_h
class Manager : public Employee
{
public:
Manager(const char*, const char*, float = 0.0);
void setWeeklySalary(float);
virtual float earnings() const;
virtual void print() const;
private:
float weeklySalary; //每周固定薪水
};
#endif
Manager.cpp
#include <iostream>
#include <assert.h>
#include "employ.h"
#include "Manager.h"
using namespace std;
//类Manager的构造函数
Manager::Manager(const char *first, const char *last, float s)
: Employee(first, last)
{
weeklySalary = s;
}
//设置经理的固定月薪
void Manager::setWeeklySalary(float s)
{
weeklySalary = s;
}
//返回经理的薪水
float Manager::earnings() const
{
return weeklySalary;
}
//打印经理的信息
void Manager::print() const
{
cout << "\n经理:" << getFirstName() << ' ' << getLastName();
}
合同工对象
CommissionWorker.h
#ifndef commisworker_h
#define commisworker_h
#include "Employ.h"
class CommissionWorker : public Employee
{
public :
CommissionWorker(const char*, const char*, float = 0.0, float = 0.0, int = 0);
void setSalary(float); //设置基本工资
void setCommission(float); //设置每件产品的回扣额
void setQuantity(int); //设置每一周的销售额
virtual float earnings() const; //计算雇员的薪水
virtual void print() const; //打印雇员的基本信息
private:
float salary; //基本工资
float commission; //每年产品的回扣额
int quantity; //一周的销售额
};
#endif
CommissionWorker.cpp
#include <iostream>
#include "assert.h"
#include "CommissionWorker.h"
using namespace std;
//类CommissionWorker的构造函数
CommissionWorker::CommissionWorker(const char *first, const char *last, float s, float c, int q)
: Employee(first, last) //调用基类的构造函数
{
salary = s;
commission = c > 0 ? c : 0;
quantity = q > 0 ? q : 0;
}
//设置销售员每周的基本工资
void CommissionWorker::setSalary(float s)
{
salary = s > 0 ? s : 0;
}
//设置每件产品的回扣额
void CommissionWorker::setCommission(float c)
{
commission = c > 0 ? c : 0;
}
//计算雇员CommissionWorker的薪水
float CommissionWorker::earnings() const
{
return salary + commission * quantity;
}
//打印雇员CommissionWorker的信息
void CommissionWorker::print() const
{
cout << "\n合同工:" << getFirstName() << ' ' << getLastName();
}
计时工对象
HourlyWorker.h
#ifndef hourlyworker_h
#define hourlyworker_h
#include "Employ.h"
class HourlyWorker : public Employee
{
public:
HourlyWorker(const char*, const char*, float = 0.0, float = 0.0);
void setWage(float);
void setHours(float);
virtual float earnings() const;
virtual void print() const;
private:
float wage; //每小时的报酬
float hours; //一周工作小时数
};
#endif
HourlyWorker.cpp
#include <iostream>
#include <assert.h>
#include "HourlyWorker.h"
using namespace std;
//类HourlyWorker的构造函数
HourlyWorker::HourlyWorker(const char *first, const char *last, float w, float h)
: Employee(first, last) //调用基类的构造函数
{
wage = w > 0 ? w : 0;
hours = h > 0 && h < 168 ? h : 0;
}
// 设置每小时的报酬
void HourlyWorker::setWage(float w)
{
wage = w;
}
//设置每周工作的小时数
void HourlyWorker::setHours(float h)
{
hours = h > 0 && h < 168 ? h : 0;
}
//计算并返回HourlyWorker的薪水
float HourlyWorker::earnings() const
{
return wage * hours;
}
//打印HourlyWorker的信息
void HourlyWorker::print() const
{
cout << "\n小时工:" << getFirstName() << ' ' << getLastName();
}
计件工对象
PieceWorker.h
#ifndef pieceworker_h
#define pieceworker_h
#include "Employ.h"
class PieceWorker : public Employee
{
public:
PieceWorker(const char*, const char*, float w, int q);
void setWage(float);
void setQuantity(int);
virtual float earnings() const;
virtual void print() const;
private:
float wagePerPiece; //没见产品的报酬
int quantity; //一周生产的产品数量
};
#endif
PieceWorker.cpp
#include <iostream>
#include <assert.h>
#include "PieceWorker.h"
using namespace std;
//类PieceWorker的构造函数
PieceWorker::PieceWorker(const char *first, const char *last, float w, int q)
: Employee(first, last) //调用基类的构造函数
{
wagePerPiece = w > 0 ? w : 0;
quantity = q > 0 ? q : 0;
}
//设置没见产品的回扣额
void PieceWorker::setWage(float w)
{
wagePerPiece = w > 0 ? w : 0;
}
//设置每周的销售量
void PieceWorker::setQuantity(int q)
{
quantity = q > 0 ? q : 0;
}
//计算并返回PieceWorker每周的薪水
float PieceWorker::earnings() const
{
return wagePerPiece * quantity;
}
//打印PieceWorker的信息
void PieceWorker::print() const
{
cout << "\n计件工:" << getFirstName() << ' ' << getLastName();
}