第四次上机:在雇员基本信息中增加雇员的生日(birthDate),并设计日期类(Date)来表示生日。在主函数中创建一个容器用来管理公司各种雇员对象,多态地计算并输出每个雇员的工资。如果雇员的生日在本月,就奖给该雇员100元。同时,在本次工资发放阶段,公司决定奖励带薪佣金雇员,把他们的基本工资提高10%。
设计提示:
- 使用抽象类。
- 如果一个类的全部成员和另一个类的部分成员相同,逻辑上是is-a的关系,考虑将两个类设计成继承关系。
- 实现“本次工资发放奖励带薪佣金雇员”时,可考虑采用动态类型转换。
例如:Base* pb;Derived *pd=dynamic_cast<Derived *>(pb); 若pd!=0则pd指向的是Derived 对象。
第一次(第三次实验课)请参看:
blog.sagiri-web.com/wordpress/2019/06/12/%E9%9B%87%E5%91%98%E5%B7%A5%E8%B5%84%E5%8F%91%E6%94%BE%E7%B3%BB%E7%BB%9F-1/
//Employee.h-类定义头文件
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include<iostream>
using namespace std;
class Date{
public:
Date();
int year;
int month;
int day;
};
class Employee{
public:
virtual void output_income() = 0;
protected:
Date birth;
char name[20];
char type[30];
int id;
static float week_salary;
static float hour_salary;
static float unit_price;
static float basic_salary;
};
class SalariedEmployee:public Employee{
public:
SalariedEmployee();
void output_income();
};
class HourlyEmployee:public Employee{
public:
HourlyEmployee();
void output_income();
private:
int work_hours;
};
class CommissionEmployee:public Employee{
public:
CommissionEmployee();
void output_income();
private:
int sales_volume;
};
class BasePlusCommissionEmployee:public Employee{
public:
BasePlusCommissionEmployee();
void judge_add_basic_salary(bool judge);
void output_income();
private:
int sales_volume;
};
#endif
//Employee.cpp-类函数定义
#include "Employee.h"
#include <string.h>
Date current_time;
float Employee::week_salary=1000;
float Employee::hour_salary=20;
float Employee::unit_price=10;
float Employee::basic_salary=4000;
Date::Date(){
/*
默认今日日期为2019-06-12
*/
current_time.year=2019;
current_time.month=6;
current_time.day=12;
}
SalariedEmployee::SalariedEmployee(){
strcpy(type,"SalariedEmployee");
cout<<"please input employee's name:";
cin>>name;
cout<<"please input employee's id:";
cin>>id;
cout<<"please input employee's birth(yyyy mm dd):";
cin>>birth.year;
cin>>birth.month;
cin>>birth.day;
}
void SalariedEmployee::output_income(){
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
cout<<" name:"<<name<<endl;
cout<<" id:"<<id<<endl;
cout<<" birth:"<<birth.year<<'-';
if(birth.month<10)
cout<<'0';
cout<<birth.month<<'-';
if(birth.day<10)
cout<<'0';
cout<<birth.day<<endl;
cout<<" type:SalariedEmployee"<<endl;
cout<<" week_salary:"<<week_salary<<endl;
if(current_time.month==birth.month)
cout<<" weekly income:"<<week_salary+100<<endl;
else
cout<<" weekly income:"<<week_salary<<endl;
cout<<" details:Deserved salary:week_salary*1="<<week_salary<<"*1="<<week_salary<<endl;
if(current_time.month==birth.month)
cout<<" extra salary:100(Happy birthday!)"<<endl;
else
cout<<" extra salary:none"<<endl;
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
}
HourlyEmployee::HourlyEmployee(){
strcpy(type,"HourlyEmployee");
cout<<"please input employee's name:";
cin>>name;
cout<<"please input employee's id:";
cin>>id;
cout<<"please input employee's birth(yyyy mm dd):";
cin>>birth.year;
cin>>birth.month;
cin>>birth.day;
cout<<"please input employee's work time:";
cin>>work_hours;
}
void HourlyEmployee::output_income(){
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
cout<<" name:"<<name<<endl;
cout<<" id:"<<id<<endl;
cout<<" birth:"<<birth.year<<'-';
if(birth.month<10)
cout<<'0';
cout<<birth.month<<'-';
if(birth.day<10)
cout<<'0';
cout<<birth.day<<endl;
cout<<" type:HourlyEmployee"<<endl;
cout<<" hour_salary:"<<hour_salary<<endl;
cout<<" work_hours:"<<work_hours<<endl;
if(work_hours<=40)
{
if(current_time.month==birth.month)
cout<<" weekly income:"<<hour_salary*work_hours+100<<endl;
else
cout<<" weekly income:"<<hour_salary*work_hours<<endl;
cout<<" deatils:Deserved salary:work_hours*hour_salary="<<work_hours<<'*'<<hour_salary<<'='<<hour_salary*work_hours<<endl;
if(current_time.month==birth.month)
cout<<" extra salary:100(Happy birthday!)"<<endl;
else
cout<<" extra salary:none"<<endl;
}
else
{
if(current_time.month==birth.month)
cout<<" weekly income:"<<40*hour_salary+(work_hours-40)*hour_salary*1.5+100<<endl;
else
cout<<" weekly income:"<<40*hour_salary+(work_hours-40)*hour_salary*1.5<<endl;
cout<<" details:Deserved salary:40*hour_salary+(work_hours-40)*hour_salary*150%=40*"<<hour_salary<<"+("<<work_hours<<"-40)*"<<hour_salary<<"*150%="<<40*hour_salary+(work_hours-40)*hour_salary*1.5<<endl;
if(current_time.month==birth.month)
cout<<" extra salary:100(Happy birthday!)"<<endl;
else
cout<<" extra salary:none"<<endl;
}
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
}
CommissionEmployee::CommissionEmployee(){
strcpy(type,"CommissionEmployee");
cout<<"please input employee's name:";
cin>>name;
cout<<"please input employee's id:";
cin>>id;
cout<<"please input employee's birth(yyyy mm dd):";
cin>>birth.year;
cin>>birth.month;
cin>>birth.day;
cout<<"please input employee's sales volume:";
cin>>sales_volume;
}
void CommissionEmployee::output_income(){
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
cout<<" name:"<<name<<endl;
cout<<" id:"<<id<<endl;
cout<<" birth:"<<birth.year<<'-';
if(birth.month<10)
cout<<'0';
cout<<birth.month<<'-';
if(birth.day<10)
cout<<'0';
cout<<birth.day<<endl;
cout<<" type:CommissionEmployee"<<endl;
cout<<" sales_volume:"<<sales_volume<<endl;
cout<<" unit_price:"<<unit_price<<endl;
if(current_time.month==birth.month)
cout<<" weekly income:"<<sales_volume*unit_price+100<<endl;
else
cout<<" weekly income:"<<sales_volume*unit_price<<endl;
cout<<" details:Deserved salary:sales_volume*unit_price="<<sales_volume<<'*'<<unit_price<<'='<<sales_volume*unit_price<<endl;
if(current_time.month==birth.month)
cout<<" extra salary:100(Happy birthday!)"<<endl;
else
cout<<" extra salary:none"<<endl;
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
}
BasePlusCommissionEmployee::BasePlusCommissionEmployee(){
strcpy(type,"BasePlusCommissionEmployee");
cout<<"please input employee's name:";
cin>>name;
cout<<"please input employee's id:";
cin>>id;
cout<<"please input employee's birth(yyyy mm dd):";
cin>>birth.year;
cin>>birth.month;
cin>>birth.day;
cout<<"please input employee's sales volume:";
cin>>sales_volume;
}
void BasePlusCommissionEmployee::output_income(){
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
cout<<" name:"<<name<<endl;
cout<<" id:"<<id<<endl;
cout<<" birth:"<<birth.year<<'-';
if(birth.month<10)
cout<<'0';
cout<<birth.month<<'-';
if(birth.day<10)
cout<<'0';
cout<<birth.day<<endl;
cout<<" type:BasePlusCommissionEmployee"<<endl;
cout<<" basic_salary:"<<basic_salary<<endl;
cout<<" sales_volume:"<<sales_volume<<endl;
cout<<" unit_price:"<<unit_price<<endl;
if(current_time.month==birth.month)
cout<<" weekly income:"<<basic_salary+sales_volume*unit_price+100<<endl;
else
cout<<" weekly income:"<<basic_salary+sales_volume*unit_price<<endl;
cout<<" details:Deserved salary:basic_salary+sales_volume*unit_price="<<basic_salary<<"+"<<sales_volume<<'*'<<unit_price<<'='<<basic_salary+sales_volume*unit_price<<endl;
if(current_time.month==birth.month)
cout<<" extra salary:100(Happy birthday!)"<<endl;
else
cout<<" extra salary:none"<<endl;
cout<<"----------------------------------------------------------------------------------------------------------"<<endl;
}
void BasePlusCommissionEmployee::judge_add_basic_salary(bool judge){
if(judge)
basic_salary*=1.1;
}
//main.cpp-主函数(测试)
#include <iostream>
#include <vector>
#include "Employee.h"
int main() {
HourlyEmployee A;
// A.output_income();
CommissionEmployee B;
// B.output_income();
SalariedEmployee C;
// C.output_income();
BasePlusCommissionEmployee D;
// D.output_income();
vector<Employee *> p;
p.push_back(&A);
p.push_back(&B);
p.push_back(&C);
p.push_back(&D);
BasePlusCommissionEmployee *q=dynamic_cast<BasePlusCommissionEmployee *>(p[3]);
p[0]->output_income();
p[1]->output_income();
p[2]->output_income();
q->judge_add_basic_salary(true);
p[3]->output_income();
}



