某学校有3类员工:教师、行政人员、教师兼行政人员,共有的信息包括编号、姓名、性别和职工类别。工资计算方法如下。 教师:基本工资+课时数×课时补贴。 行政人员:基本工资+行政补贴。 教师兼行政人员:基本工资+课时数×课时补贴+行政补贴。 分析以上信息,定义人员抽象类,派生不同类型的员工,并完成工资的计算。
测试输入:
请输入教师的信息:
输入编号:01
输入姓名:李明
输入性别(f或m):m
基本工资:2000
课 时 数:60
课时补贴:100
请输入行政人员的信息:
输入编号:03
输入姓名:王强
输入性别(f或m):m
基本工资:3000
行政补贴:200
请输入教师兼行政人员的信息:
输入编号:05
输入姓名:张蕾
输入性别(f或m):f
基本工资:2500
课 时 数:80
课时补贴:200
行政补贴:100
预期输出:
********教师信息*********
编 号--1
姓 名--李明
性 别--女
类 别--教师
基本工资--2000
课 时 数--60
课时补贴--100
本月工资--8000
********行政人员信息*********
编 号–3
姓 名–王强
性 别–女
类 别–行政人员
基本工资–3000
行政补贴–200
本月工资–3200
********教师兼行政人员信息*********
编 号--5
姓 名--张蕾
性 别--女
类 别--教师兼行政人员
基本工资--2500
课 时 数--80
课时补贴--200
行政补贴--100
本月工资--18600
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(int n = 0, string nm = "", char s = ‘f‘, string t = "", double w = 0):num(n), name(nm), sex(s), title(t), wage(w) {}
virtual void Input();
virtual void Display();
virtual double Pay() = 0; //计算工资
protected:
int num;
string name;
char sex;
string title;
double wage; //基本工资
};
void Person::Input()
{
cout << "输入编号:"; cin >> num;
if(num<10)cout<<"0"<<num<<endl;
else cout<<num<<endl;
cout << "输入姓名:"; cin >> name;
cout<<name<<endl;
cout << "输入性别(f或m):"; cin >> sex;
cout<<sex<<endl;
cout << "基本工资:"; cin >> wage;
cout<<wage<<endl;
}
void Person::Display()
{
cout << "编 号--" << num << endl;
cout << "姓 名--" << name << endl;
cout << "性 别--" << (sex == ‘f‘ || ‘F‘ ? "女" : "男") << endl;
cout << "类 别--" << title << endl;
cout << "基本工资--" << wage << endl;
}
class Teacher :virtual public Person
{
public:
Teacher(int n = 0, string nm = "", char s = ‘f‘, double w = 0, int h = 0, double hr = 0)
:Person(n, nm, s, "教师", w), hours(h), hourlyrate(hr) {}
void T_Input();
virtual void Input();
virtual double Pay();
void T_Display();
virtual void Display();
protected:
int hours; //课时数
double hourlyrate; //课时补贴
};
void Teacher::T_Input()
{
cout << "课 时 数:"; cin>>hours;
cout<<hours<<endl;
cout << "课时补贴:"; cin>>hourlyrate;
cout<<hourlyrate<<endl;
}
void Teacher::Input()
{
cout << "请输入教师的信息:" << endl;
}
double Teacher::Pay()
{
return this->wage + this->hours * this->hourlyrate;
}
void Teacher::T_Display()
{
cout << "课 时 数--" << hours << endl;
cout << "课时补贴--" << hourlyrate << endl;
}
void Teacher::Display()
{
cout << "********教师信息*********" << endl;
}
class Staff :virtual public Person
{
public:
Staff(int n = 0, string nm = "", char s = ‘f‘, double w = 0, double all = 0)
:Person(n, nm, s, "行政人员", w), allowance(all) {}
void S_Input();
virtual void Input();
virtual double Pay();
void S_Display();
virtual void Display();
protected:
double allowance; //行政补贴
};
void Staff::S_Input()
{
cout << "行政补贴:"; cin>>allowance;
cout<<allowance<<endl;
}
void Staff::Input()
{
cout << "请输入行政人员的信息:" << endl;
}
double Staff::Pay()
{
return this->wage + this->allowance;
}
void Staff::S_Display()
{
cout << "行政补贴--" << allowance << endl;
}
void Staff::Display()
{
cout << "********行政人员信息*********" << endl;
}
class Tea_St :public Teacher, public Staff
{
public:
Tea_St(int n = 0, string nm = "", char s = ‘f‘, double w = 0, int h = 0, double hr = 0, double all = 0)
:Person(n, nm, s, "教师兼行政人员", w), Teacher(n, nm, s, w, h, hr),
Staff(n, nm, s, w, all) {}
virtual void Input();
virtual double Pay();
virtual void Display();
};
void Tea_St::Input()
{
cout << "请输入教师兼行政人员的信息:" << endl;
}
double Tea_St::Pay()
{
return this->wage + this->allowance + this->hours * this->hourlyrate;
}
void Tea_St::Display()
{
cout << "********教师兼行政人员信息*********" << endl;
}
int main()
{
int n,h;
string nm;
char s;
double w,hr;
//输入教师信息
Teacher tc;
tc.Input();
tc.Person::Input();
tc.T_Input();
cout<<endl;
//输入行政人员信息
Staff sf;
sf.Input();
sf.Person::Input();
sf.S_Input();
cout<<endl;
//输入教师及行政人员信息
Tea_St ts;
ts.Input();
ts.Person::Input();
ts.Teacher::T_Input();
ts.Staff::S_Input();
cout<<endl;
//输出教师信息
tc.Display();
tc.Person::Display();
tc.T_Display();
cout << "本月工资--" << tc.Pay() << endl;
cout<<endl;
//输出行政人员信息
sf.Display();
sf.Person::Display();
sf.S_Display();
cout << "本月工资--" << sf.Pay() << endl;
cout<<endl;
// 输出教师及行政人员信息
ts.Display();
ts.Person::Display();
ts.T_Display();
ts.S_Display();
cout << "本月工资--" << ts.Pay()<< endl;
cout<<endl;
return 0;
}
原文:https://www.cnblogs.com/lightice/p/12911040.html