某公司有老板Boss、雇员Employee、小时工HourlyWorker和营销人员CommWorker,他们的薪金计算方法如下:老板实行年薪制,一年15万;雇员按月计酬,方法是基本工资+奖金;小时工按工作时间计算报酬,方法是工作小时*每小时单价;营销人员按月计酬,方法是基本工资+销售利润*5%。
每类人员都有姓名、职工编号、年龄、性别、工资等数据。设计计算各类人员报酬的程序,用虚函数getPay()计算各类人员的应得报酬,用虚函数print()打印输出各位工作人员的基本数据。
提示:将各类人员都共有的属性和行为抽象在类Person中,包括姓名、职工编号、年龄、性别等,以及函数getPay()和print()。getPay()设计为纯虚函数,print()设计为一般虚函数,其余类从Person类派生,各类再定义getPay()和print()的实现方法。此外,每个类需要根据实际情况定义相应的成员函数。
实现代码:
#include<iostream>
#include<string>
using namespace std;
class person
{
public:
string name;
string num;
int age;
string sex;
person() {};
person(string name_, string num_, int age_, string sex_) :name(name_), num(num_), age(age_), sex(sex_)
{
}
virtual double Getpay() = 0;
virtual void print() = 0;
~person()
{
}
};
class Boss :public person
{
public:
Boss(string name_, string num_, int age_, string sex_) :person(name_, num_, age_, sex_) {}
virtual double Getpay()
{
return 150000;
}
virtual void print()
{
cout << name << endl;
cout << num << endl;
cout << age << endl;
cout << sex << endl;
}
};
class Employee :public person
{
public:
double basepay, bonus;
Employee(string name_, string num_, int age_, string sex_, double basepay_, double bonus_) :person(name_, num_, age_, sex_)
{
basepay = basepay_;
bonus = bonus_;
}
virtual double Getpay()
{
return basepay + bonus;
}
virtual void print()
{
cout << name << endl;
cout << num << endl;
cout << age << endl;
cout << sex << endl;
}
};
class HourlyWorker :public person
{
public:
double hour, price;
HourlyWorker(string name_, string num_, int age_, string sex_, double hour_, double price_) :person(name_, num_, age_, sex_)
{
hour = hour_;
price = price_;
}
virtual double Getpay()
{
return hour * price;
}
virtual void print()
{
cout << name << endl;
cout << num << endl;
cout << age << endl;
cout << sex << endl;
}
};
class CommWorker :public person
{
public:
double basepay, profit;
CommWorker(string name_, string num_, int age_, string sex_, double basepay_, double profit_) :person(name_, num_, age_, sex_)
{
basepay = basepay_;
profit = profit_;
}
virtual double Getpay()
{
return basepay + profit * 0.05;
}
virtual void print()
{
cout << name << endl;
cout << num << endl;
cout << age << endl;
cout << sex << endl;
}
};
int main()
{
Boss A("大龙", "001", 55, "女");
Employee B("张三", "002", 28, "男", 4000, 15000);
HourlyWorker C("李四", "003", 32, "男", 7, 31);
CommWorker D("王五", "004", 47, "女", 6000, 8000);
Boss* p1;
Employee* p2;
HourlyWorker* p3;
CommWorker* p4;
p1 = &A;
p1->print();
cout << p1->Getpay() << endl;
cout << "-------------------------------------------" << endl;
p2 = &B;
p2->print();
cout << p2->Getpay() << endl;
cout << "-------------------------------------------" << endl;
p3 = &C;
p3->print();
cout << p3->Getpay() << endl;
cout << "-------------------------------------------" << endl;
p4 = &D;
p4->print();
cout << p4->Getpay() << endl;
return 0;
}