题目描述
某公司员工的属性有:姓名、职位、级别、工作年限,级别和年限都是非负整数,否则显示错误。包含方法有:构造函数,计算工资的方法(salary())。
员工职位分为三种:Employee、Teamleader、Manager,其他职位类型显示错误。
三种职位员工的区别在于计算工资的方法不同:
1. Employee的每月工资 = 1000 + 500*级别 + 50*工作年限
2. Teamleader的每月工资 = 3000 + 800*级别 + 100*工作年限
3. Manager的每月工资 = 5000 + 1000 * (级别+工作年限)
计算工资的方法返回每个员工的工资数。
要求:以普通员工为基类,组长和经理继承基类,程序中只能使用基类指针指向对象与调用对象的方法。
输入
测试案例的个数 t
每行输入一个员工的信息:包括姓名、职位、级别、工作年限
输出
输出相应员工的信息
如有错误信息,则输出错误信息,若职位信息与级别和年限信息同时出错,仅输出职位错误信息
IO模式
本题IO模式为标准输入/输出(Standard IO),你需要从标准输入流中读入数据,并将答案输出至标准输出流中。
//
输入示例
5
zhangsan Employee 4 5
lisi Teamleader 4 5
Wangwu Manager 4 5
chenliu Precident 4 5
xiaoxiao Manager -1 5
输出示例
zhangsan:Employee,Salary:3250
lisi:Teamleader,Salary:6700
Wangwu:Manager,Salary:14000
error position.
error grade or year.
AC代码:
#include <bits/stdc++.h>
using namespace std;
class Employee
{
protected:
string name;
string position;
int level;
int yearsOfWork;
public:
Employee() {}
Employee(string name1, string position1, int level1, int yearsOfWork1)
: name(name1), position(position1), level(level1), yearsOfWork(yearsOfWork1) {}
virtual int salary() const
{
int sum = 1000 + 500 * level + 50 * yearsOfWork;
return sum;
};
void set(string name1, string position1, int level1, int yearsOfWork1)
{
name = name1;
position = position1;
level = level1;
yearsOfWork = yearsOfWork1;
}
virtual void display() const
{
if (level < 0 || yearsOfWork < 0)
{
cout << "error grade or year." << endl;
}
else
cout << name << ":" << position << ",Salary:" << salary() << endl;
}
};
class Teamleader : public Employee
{
public:
Teamleader(string name, int level, int yearsOfWork)
: Employee(name, "Teamleader", level, yearsOfWork) {}
virtual int salary() const override
{
return 3000 + 800 * level + 100 * yearsOfWork;
}
};
class Manager : public Employee
{
public:
Manager(string name1, int level, int yearsOfWork)
: Employee(name1, "Manager", level, yearsOfWork) {}
virtual int salary() const override
{
return 5000 + 1000 * (level + yearsOfWork);
}
};
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t;
cin >> t;
getchar();
for (int i = 0; i < t; i++)
{
string name, position;
int level, yearsOfWork;
cin >> name >> position >> level >> yearsOfWork;
Employee *emp;
if (position == "Employee")
{
emp = new Employee(name, position, level, yearsOfWork);
emp->display();
}
else if (position == "Teamleader")
{
emp = new Teamleader(name, level, yearsOfWork);
emp->display();
}
else if (position == "Manager")
{
emp = new Manager(name, level, yearsOfWork);
emp->display();
}
else
{
cout << "error position." << endl;
continue;
}
}
}