C++.Homework.Polymorphism.02

============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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值