• 自考新教材-p189


    源程序:

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

    class employee //类employee将作为其他几个类的基类
    {
    short age;
    float salary;
    protected:
    string name;
    public:
    employee(short ag,float sa,string na)
    {
    age=age;
    salary=sa;
    name=na;
    };
    void print()
    {
    cout<<" "<<name<<": ";
    cout<<age<<": ";
    cout<<salary;
    }
    ~employee()
    {
    }
    };

    class manager:public employee //派生类
    {
    int level;
    public:
    manager(short ag,float sa,string na,int lev):employee(ag,sa,na) //对基类初始化
    {
    level = lev;
    }
    void print()
    {
    employee::print(); //调用基类print()显示“共性”数据
    cout<<" level:"<<endl;
    }
    };

    class engineer:public employee
    {
    char speciality,adegree;
    public:
    engineer(short ag,float sa,string na,char spe,char ade):employee(ag,sa,na) //对基类初始化
    {
    speciality=spe;
    adegree=ade;
    }
    void print()
    {
    employee::print(); //调用基类print()显示“共性”数据
    cout<<" speciality:"<<speciality;
    cout<<" adegree:"<<adegree;
    }
    };
    enum ptitle{PS,GM,VPS,VGM};
    class director:public manager
    {
    ptitle post;
    public:
    director(short ag,float sa,string na,int le,ptitle po):manager(ag,sa,na,le) //对基类初始化
    {
    post=po;
    }
    void print()
    {
    manager::print(); //调用基类print()显示“共性”数据
    cout<<" post:"<<post<<endl;
    }
    };

    int main()
    {
    employee emp1(23,610.5,"wang"),emp2(27,824.75,"li");
    manager man1(32,812.45,"zhang",11),man2(34,1200.5,"chen",7);
    engineer eng(26,1420.10,"sun",'E','M');
    director dir(38,1800.2,"liu",2,VPS);
    emp1.print();
    emp2.print();
    man1.print();
    man2.employee::print(); //调用基类的print()
    eng.print();
    dir.print();
    return 0;
    }

    运行结果:

  • 相关阅读:
    docker 删除所有的 docker ps -a 记录
    使用ES6的Promise完美解决回调地狱
    linux查看历史命令history
    linux命令补全 忘记命令只记得开头
    linux更改shell
    java信号量PV操作 解决生产者-消费者问题
    eclipse代码自动提示功能设置
    linux下mysql修改数据库账户root密码
    IntelliJ Idea12 破解码与中文乱码配置
    linux usermod修改用户所在组方法
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12259824.html
Copyright © 2020-2023  润新知