• 自考新教材-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;
    }

    运行结果:

  • 相关阅读:
    多线程与Socket编程
    正则表达式
    委托事件泛型
    C#基础加强
    随笔
    不设置JAVA_HOME运行eclipse
    CentOS7.x系统中使用Docker时,在存储方面需要注意的问题
    【转】关于高可用负载均衡的探索-基于Rancher和Traefic
    Rancher 容器管理平台-免费视频培训-链接及内容-第三季
    使用Rancher的RKE快速部署Kubernetes集群
  • 原文地址:https://www.cnblogs.com/duanqibo/p/12259824.html
Copyright © 2020-2023  润新知