员工工资(虚函数与多态)

题目描述

某公司员工的属性有:姓名、职位、级别、工作年限,级别和年限都是非负整数,否则显示错误。包含方法有:构造函数,计算工资的方法(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;
        }
    }
}

  • 14
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

szuzhan.gy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值