员工类
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
class Person{
public:
Person(string Name,string Id,int Age,bool Sex):name(Name),id(Id),age(Age),sex(Sex){}
virtual double getPay()=0;
virtual void print(){
cout<<"职工编号:"<<id<<endl;
cout<<"职位:"<<typeid(*this).name()<<endl;
cout<<"姓名:"<<name<<endl;
cout<<"年龄:"<<age<<endl;
cout<<"性别:"<<(sex?"男":"女")<<endl;
}
private:
string name,id;
int age;
bool sex;
};
class Boss:public Person{
public:
Boss(string Name,string Id,int Age,bool Sex,double YearPay):Person(Name,Id,Age,Sex),yearPay(YearPay){}
double getPay(){return yearPay;};
void print(){
Person::print();
cout<<"年薪:"<<getPay()<<endl;
}
private:
double yearPay;
};
class Employee:public Person{
public:
Employee(string Name,string Id,int Age,bool Sex,double BasePay,double Reward):Person(Name,Id,Age,Sex),basePay(BasePay),reward(Reward){}
double getPay(){return basePay+reward;};
void print(){
Person::print();
cout<<"基本工资:"<<basePay<<endl;
cout<<"奖金:"<<reward<<endl;
cout<<"月薪:"<<getPay()<<endl;
}
private:
double basePay;
double reward;
};
class HourlyWorker:public Person{
public:
HourlyWorker(string Name,string Id,int Age,bool Sex,double Hour,double Price):Person(Name,Id,Age,Sex),hour(Hour),price(Price){}
double getPay(){return hour*price;};
void print(){
Person::print();
cout<<"工作时长:"<<hour<<endl;
cout<<"单价/时:"<<price<<endl;
cout<<"工资:"<<getPay()<<endl;
}
private:
double hour;
double price;
};
class CommWorker:public Person{
public:
CommWorker(string Name,string Id,int Age,bool Sex,double BasePay,double Profit):Person(Name,Id,Age,Sex),basePay(BasePay),profit(Profit){}
double getPay(){return basePay+0.05*profit;};
void print(){
Person::print();
cout<<"基本工资:"<<basePay<<endl;
cout<<"销售利润:"<<profit<<endl;
cout<<"月薪:"<<getPay()<<endl;
}
private:
double basePay;
double profit;
};
int main(){
Person *p;
Boss boss("A","001",50,1,150000);
Employee employee("B","002",40,0,2000,1000);
HourlyWorker hourlyWorker("C","003",30,1,50,30);
CommWorker commWorker("D","004",20,0,2500,600000);
p=&boss;
p->print();
cout<<"----------------"<<endl;
p=&employee;
p->print();
cout<<"----------------"<<endl;
p=&hourlyWorker;
p->print();
cout<<"----------------"<<endl;
p=&commWorker;
p->print();
}