4001:工资调整:虚基类应用
Problem Description 学校员工有三类:教师、行政人员和双肩挑人员(既担任行政职务又教书的员工)。现对员工工资进行调整,确定增幅规则如下:
教师按职称确定增幅:Lecturer(10%),AssociateProfessor(8%),Professor(6%); 行政人员按级别确定增幅:keji(9%),fuchu(7%),chuji(5%);
双肩挑人员按职称和级别中较高的增幅确定。如职称为AssociateProfessor级别为keji人员按9%增加工资,而职称为AssociateProfessor级别为fuchu按8%增加工资。 类的部分定义如下,请将继承关系及各个类的成员函数补充完整但不能增加新的数据成员,使之能通过main()函数测试。
class Employee
{
protected:
string name
;
double salary
;
};
class Teacher
{
protected:
string title
;
};
class Staff
{
protected:
string level
;
};
class StaffTeacher
{
};
int main()
{
int flag
;
string name
;
string title
;
string level
;
double salary
;
while(cin
>>flag
)
{
if(flag
==0)
{
cin
>> name
>> title
>> salary
;
Teacher
t(name
,title
,salary
);
t
.show();
}
else if(flag
==1)
{
cin
>> name
>> level
>> salary
;
Staff
s(name
,level
,salary
);
s
.show();
}
else
{
cin
>> name
>> title
>> level
>> salary
;
StaffTeacher
st(name
,title
,level
,salary
);
st
.show();
}
}
}
Input 输入数据有多行。每行代表一个员工的信息。 每行第一个整数代表员工类别,0:教师,1:行政人员,2:双肩挑人员 教师的信息依次为:姓名、职称、工资;职称名如题目中所描述的英文; 行政人员的信息依次为:姓名、级别、工资;级别名如题目中所描述的英文 双肩挑人员的信息依次为:姓名、职称、级别、工资。 工资均为实数。具体格式见示例。
output 输出各类人员增加工资后的信息,以空格隔开。 教师的信息依次为:姓名、职称、工资; 行政人员的信息依次为:姓名、级别、工资; 双肩挑人员的信息依次为:姓名、职称、级别、工资。 具体格式见示例。
Sample input
0 zhangsan Lecturer 3000
1 linqing fuchu 4000
2 hechong Professor keji 5000
Sample output
zhangsan Lecturer 3300
linqing fuchu 4280
hechong Professor keji 5450
心得
这个题目没什么难度,只要按照题目的要求往下打就可以了(毕竟老师都贴心的帮你把类的大概内容给打出来了),注意继承时要加上关键字virtual。
答案
#include <iostream>
#include <string>
using namespace std
;
class Employee
{
protected:
string name
;
double salary
;
public:
Employee(string name
,double salary
):name(name
),salary(salary
) {}
};
class Teacher:virtual public Employee
{
protected:
string title
;
public:
Teacher(string name
,string title
,double salary
):
Employee(name
,salary
),title(title
) {}
void show()
{
if (title
== "Lecturer") salary
*= 1.1;
else if (title
== "AssociateProfessor") salary
*= 1.08;
else salary
*= 1.06;
cout
<< name
<< " " << title
<< " " << salary
<< endl
;
}
};
class Staff :virtual public Employee
{
protected:
string level
;
public:
Staff(string name
,string level
,double salary
):
Employee(name
,salary
),level(level
) {}
void show()
{
if(level
=="keji")salary
*=1.09;
else if(level
=="fuchu")salary
*=1.07;
else salary
*=1.05;
cout
<<name
<<" "<<level
<<" "<<salary
<<endl
;
}
};
class StaffTeacher:public Teacher
,public Staff
{
private:
double count
;
public:
StaffTeacher(string name
,string title
,string level
,double salary
):
Employee(name
,salary
),Teacher(name
,title
,salary
),Staff(name
,level
,salary
) {}
void show()
{
if(title
=="Lecturer")count
=1.1;
else if(title
=="AssociateProfessor")count
=1.08;
else count
=1.06;
if(level
=="keji")count
=(count
>1.09?count
:1.09);
else if(level
=="fuchu")count
=(count
>1.07)?count
:1.07;
else count
=(count
>1.05?count
:1.05);
salary
*=count
;
cout
<<name
<<" "<<title
<<" "<<level
<<" "<<salary
<<endl
;
}
};
int main()
{
int flag
;
string name
;
string title
;
string level
;
double salary
;
while(cin
>>flag
)
{
if(flag
==0)
{
cin
>>name
>>title
>>salary
;
Teacher
t(name
,title
,salary
);
t
.show();
}
else if(flag
==1)
{
cin
>>name
>>level
>>salary
;
Staff
s(name
,level
,salary
);
s
.show();
}
else
{
cin
>>name
>>title
>>level
>>salary
;
StaffTeacher
st(name
,title
,level
,salary
);
st
.show();
}
}
}