代码如下:

#include<iostream>
#include<string>
using namespace std;

class Peso {
protected:
    string name;    // 姓名
    string id;      // 职工编号
    int age;        // 年龄
    char sex;       // 性别
public:
    Peso(string n, string i, int a, char s) :name(n), id(i), age(a), sex(s) {}
    virtual double getPay() = 0;        // 纯虚函数
    virtual void print() {             // 虚函数
        cout << "姓名:" << name << endl;
        cout << "职工编号:" << id << endl;
        cout << "年龄:" << age << endl;
        cout << "性别:" << sex << endl;
    }
};

// 老板类
class Boss :public Peso {
private:
    double salary;      // 年薪
public:
    Boss(string n, string i, int a, char s, double sal) :Peso(n, i, a, s), salary(sal) {}
    double getPay() { return salary; }  // 实现纯虚函数
    void print() {                      // 重载虚函数
        Peso::print();                  // 调用父类的print函数
        cout << "年薪:" << salary << endl;
    }
};

// 雇员类
class Employee :public Peso {
private:
    double basicPay;    // 基本工资
    double bonus;       // 奖金
public:
    Employee(string n, string i, int a, char s, double bp, double b) :Peso(n, i, a, s), basicPay(bp), bonus(b) {}
    double getPay() { return basicPay + bonus; }   // 实现纯虚函数
    void print() {                                  // 重载虚函数
        Peso::print();                              // 调用父类的print函数
        cout << "基本工资:" << basicPay << endl;
        cout << "奖金:" << bonus << endl;
    }
};

// 小时工类
class HourlyWorker :public Peso {
private:
    double hourPay;     // 每小时单价
    double workHour;    // 工作时间
public:
    HourlyWorker(string n, string i, int a, char s, double hp, double wh) :Peso(n, i, a, s), hourPay(hp), workHour(wh) {}
    double getPay() { return hourPay * workHour; } // 实现纯虚函数
    void print() {                                  // 重载虚函数
        Peso::print();                              // 调用父类的print函数
        cout << "每小时单价:" << hourPay << endl;
        cout << "工作时间:" << workHour << endl;
    }
};

// 营销人员类
class CommWorker :public Peso {
private:
    double basicPay;    // 基本工资
    double saleProfit;  // 销售利润
public:
    CommWorker(string n, string i, int a, char s, double bp, double sp) :Peso(n, i, a, s), basicPay(bp), saleProfit(sp) {}
    double getPay() { return basicPay + saleProfit * 0.05; } // 实现纯虚函数
    void print() {                                          // 重载虚函数
        Peso::print();                                      // 调用父类的print函数
        cout << "基本工资:" << basicPay << endl;
        cout << "销售利润:" << saleProfit << endl;
    }
};

int main() {
    Peso* p[4];     // 定义基类指针数组
    p[0] = new Boss("张三", "001", 40, 'M', 150000);    // 实例化各个类的对象
    p[1] = new Employee("李四", "002", 25, 'F', 5000, 1000);
    p[2] = new HourlyWorker("王五", "003", 30, 'M', 100, 160);
    p[3] = new CommWorker("赵六", "004", 28, 'F', 8000, 10000);

    for (int i = 0; i < 4; i++) {   // 循环输出各个对象的数据
        p[i]->print();
        cout << "工资:" << p[i]->getPay() << endl;
        cout << "--------------------------" << endl;
    }

    return 0;
}
``
7某公司有老板Boss、雇员Employee、小时工Hourly Worker和营销人员CommWorker他们的薪金计算方法如下:老板实行年薪制如一年15万:雇员按月计酬方法是基本工资+奖金:小时工按工作时间计算报酬方法是工作小时×每小时单价;营销人员按月计酬方法是基本工资+销售利润×5。每类人员都有姓名、职工编号、年龄、性别、工资等数据。设计计算各类人员报酬的程序用虚函数getPayO计算各类

原文地址: http://www.cveoy.top/t/topic/eRg3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录