============Person.h==============
#pragma once
#include<iostream>
#include<string>
using namespace std;
#define NameSize 20
#define NoSize 5
#define SexSize 10
class Person{
char name[NameSize],sex[SexSize],No[NoSize];
int age;
public:
Person(char*,char*,int,char*);
virtual double getPay()=0;
virtual void print();
};
Person::Person(char *name,char *No,int age,char *sex){
strcpy(this->name,name);
strcpy(this->No,No);
this->age=age;
strcpy(this->sex,sex);
}
void Person::print(){
cout<<"Name:"<<name<<endl<<"No:"<<No<<endl<<"Age:"<<age<<endl<<"Sex:"<<sex<<endl;
}
==============Boss.h==============
#include "Person.h"
class Boss:public Person{
double AnnualPay;
int year;
public:
Boss(char*,char*,int,char*,double,int);
double getPay();
void print();
};
Boss::Boss(char *name,char *No,int age,char *sex,double AnnualPay,int year):Person(name,No,age,sex){
this->AnnualPay=AnnualPay;
this->year=year;
}
double Boss::getPay(){
return AnnualPay*year;
}
void Boss::print(){
Person::print();
cout<<"Annual pay:"<<AnnualPay<<endl<<"Working Year:"<<year<<endl;
}
=============Employee.h===============
#include"Person.h"
class Employee:public Person{
double MonthlyPay,month,bonus;
public:
Employee(char*,char*,int,char*,double,double,double);
double getPay();
void print();
};
Employee::Employee(char *name,char *No,int age,char *sex,double MonthlyPay,double month,double bonus):Person(name,No,age,sex){
this->MonthlyPay=MonthlyPay;
this->month=month;
this->bonus=bonus;
}
double Employee::getPay(){
return (MonthlyPay+bonus)*month;
}
void Employee::print(){
Person::print();
cout<<"Monthly pay:"<<MonthlyPay<<endl<<"Working month:"<<month<<endl<<"Bonus:"<<bonus<<endl;
}
=============HourlyWorker.h============
#include"Person.h"
class HourlyWorker:public Person{
double HourlyPay,hour;
public:
HourlyWorker(char*,char*,int,char*,double,double);
double getPay();
void print();
};
HourlyWorker::HourlyWorker(char *name,char *No,int age,char *sex,double HourlyPay,double hour):Person(name,No,age,sex){
this->HourlyPay=HourlyPay;
this->hour=hour;
}
double HourlyWorker::getPay(){
return (HourlyPay*hour);
}
void HourlyWorker::print(){
Person::print();
cout<<"Hourly pay:"<<HourlyPay<<endl<<"Working hour:"<<hour<<endl;
}
============CommWorker.h=============
#include"Person.h"
class CommWorker:public Person{
double BasePay,month,profit;
public:
CommWorker(char*,char*,int,char*,double,double,double);
double getPay();
void print();
};
CommWorker::CommWorker(char *name,char *No,int age,char *sex,double BasePay,double month,double profit):Person(name,No,age,sex){
this->BasePay=BasePay;
this->month=month;
this->profit=profit;
}
double CommWorker::getPay(){
return (BasePay+(profit*0.05))*month;
}
void CommWorker::print(){
Person::print();
cout<<"Base pay:"<<BasePay<<endl<<"Working month:"<<month<<endl<<"Profit:"<<profit<<endl;
}
================main.cpp================
#include "Boss.h"
#include "CommWorker.h"
#include "Employee.h"
#include "HourlyWorker.h"
void main(){
Boss B("Boss Li","No.1",40,"male",150000,1);
B.print();
cout<<"Total pay:"<<B.getPay()<<endl;
cout<<"==================================================="<<endl;
Employee E("Employee Wang","No.2",30,"female",3000,1,1000);
E.print();
cout<<"Total pay:"<<E.getPay()<<endl;
cout<<"==================================================="<<endl;
HourlyWorker H("HourlyWorker Wu","No.3",35,"male",8,10);
H.print();
cout<<"Total pay:"<<H.getPay()<<endl;
cout<<"==================================================="<<endl;
CommWorker C("CommWorker Zhang","No.4",25,"female",5000,1,2000);
C.print();
cout<<"Total pay:"<<C.getPay()<<endl;
}