qq_39677327 2022-06-27 15:58 采纳率: 94.9%
浏览 85
已结题

构造一个Staff员工类为虚基类,多重派生继承问题

如下图所示,构造一个Staff员工类为虚基类含有数据成员姓名、年龄、性别、地址、电话,派生出教师类Teacher增加数据成员职称和干部类Cader增加数据成员职务,派生出教师干部类Teacher_Cader增加数据成员工资,要求:
(1)在类中设计void input()和void output()函数实现数据的录入和输出。
(2)设计主程序显示教师干部信息。

img

已有部分代码,请按题修改一下

#include<iostream>
#include<string>
using namespace std;
class Teacher
{
    public:
        Teacher(string name1,int age1,string sex1,string title1,string addr1,string tel1)
        {
            name=name1;
            age=age1;
            sex=sex1;
            title=title1;
            addr=addr1;
            tel=tel1;
        }
        void display();
        protected:
            string name;
            int age;
            string sex;
            string title,addr,tel;
};
void Teacher::display()
{
    cout<<"姓名:"<<name<<endl;
    cout<<"年龄:"<<age<<endl;
    cout<<"性别:"<<sex<<endl;
    cout<<"职称:"<<title<<endl;
    cout<<"地址:"<<addr<<endl;
    cout<<"电话:"<<tel<<endl;
}
class Cadre
{
    public:
        Cadre(string name1,int age1,string sex1,string post1,string addr1,string tel1);
        void display();
    protected:
        string name;
        int age;
        string sex;
        string post;
        string addr;
        string tel;
};

Cadre::Cadre(string name1,int age1,string sex1,string post1,string addr1,string tel1):name(name1),age(age1),sex(sex1),post(post1),addr(addr1),tel(tel1)
{
    
}

void Cadre::display()
{
    cout<<"姓名:"<<name<<endl;
    cout<<"年龄:"<<age<<endl;
    cout<<"性别:"<<sex<<endl;
    cout<<"职务:"<<post<<endl;
    cout<<"地址:"<<addr<<endl;
    cout<<"电话:"<<tel<<endl;
}
class Person:public Teacher,public Cadre
{
    public:
        Person(string name1,int age1,string sex1,string title1,string post1,string address1,string tel1,float wages1);
        void show()
        {
            Teacher::display();
            cout<<"职务:"<<Cadre::post<<endl;
            cout<<"工资:"<<wages<<endl;
        }
        protected:
            float wages;
};

Person::Person(string name1,int age1,string sex1,string title1,string post1,string address1,string tel1,float wages1):Teacher(name1,age1,sex1,title1,address1,tel1),Cadre(name1,age1,sex1,post1,address1,tel1),wages(wages1)
{
    
}

int main()
{
    Person person1("小明",18,"男","教授","部长","资阳","18080594922",950000);
    person1.show();
    system("pause");
    return 0;
}

  • 写回答

2条回答 默认 最新

  • 关注

    代码如下:

    #include<iostream>
    #include<string>
    using namespace std;
    
    class Staff
    {
    protected:
        string name;  //姓名
        int age; //年龄
        string sex; //性别
        string addr;
        string tel;
    
    public:
        void input()
        {
            cout << "请输入姓名:";
            cin >> name;
            cout << "请输入年龄:";
            cin >> age;
            cout << "请输入性别:";
            cin >> sex;
            cout << "请输入地址:";
            cin >> addr;
            cout << "请输入电话:";
            cin >> tel;
        }
        void output()
        {
            cout << "姓名:" << name << endl;
            cout << "年龄:" << age << endl;
            cout << "性别:" << sex << endl;
            cout << "地址:" << addr << endl;
            cout << "电话:" << tel << endl;
        }
    };
    
    
    
    
    class Teacher : public virtual Staff
    {
    protected:
        string title; //职称
    public:
        Teacher(string name1, int age1, string sex1, string addr1, string tel1, string title1)
        {
            name = name1;
            age = age1;
            sex = sex1;
            title = title1;
            addr = addr1;
            tel = tel1;
        }
        void output();
        void input();
    
    };
    
    void Teacher::input()
    {
        cout << "请输入姓名:";
        cin >> name;
        cout << "请输入年龄:";
        cin >> age;
        cout << "请输入性别:";
        cin >> sex;
        cout << "请输入职称:";
        cin >> title;
        cout << "请输入地址:";
        cin >> addr;
        cout << "请输入电话:";
        cin >> tel;
    }
    void Teacher::output()
    {
        cout << "姓名:" << name << endl;
        cout << "年龄:" << age << endl;
        cout << "性别:" << sex << endl;
        cout << "职称:" << title << endl;
        cout << "地址:" << addr << endl;
        cout << "电话:" << tel << endl;
    }
    class Cadre :public virtual Staff
    {
    protected:
        string post; //职务
    public:
        Cadre(string name1, int age1, string sex1, string addr1, string tel1,string post1);
        void display();
        void input();
    
    };
    
    Cadre::Cadre(string name1, int age1, string sex1, string addr1, string tel1,string post1)
    {
        name = name1;
        age = age1;
        sex = sex1;
        addr = addr1;
        tel = tel1;
        post = post1;
    }
    
    void Cadre::input()
    {
        cout << "请输入姓名:";
        cin >> name;
        cout << "请输入年龄:";
        cin >> age;
        cout << "请输入性别:";
        cin >> sex;
        cout << "请输入职务:";
        cin >> post;
        cout << "请输入地址:";
        cin >> addr;
        cout << "请输入电话:";
        cin >> tel;
    }
    
    
    void Cadre::display()
    {
        cout << "姓名:" << name << endl;
        cout << "年龄:" << age << endl;
        cout << "性别:" << sex << endl;
        cout << "职务:" << post << endl;
        cout << "地址:" << addr << endl;
        cout << "电话:" << tel << endl;
    }
    class Teacher_Cader :public Teacher, public Cadre
    {
    public:
        Teacher_Cader(string name1 = "", int age1 = 0, string sex1 = "", string title1 = "", string post1 = "", string address1 = "", string tel1 = "", float wages1 = 0);
        void input()
        {
            Teacher::input();
            cout << "请输入职务:"; 
            cin >> post;
            cout << "请输入工资:";
            cin>> wages;
        }
        void output()
        {
            Teacher::output();
            cout << "职务:" << post << endl;
            cout << "工资:" << wages << endl;
        }
    protected:
        float wages;
    };
    
    Teacher_Cader::Teacher_Cader(string name1, int age1, string sex1, string title1, string post1, string address1, string tel1, float wages1) :Teacher(name1, age1, sex1, title1, address1, tel1), Cadre(name1, age1, sex1, post1, address1, tel1), wages(wages1)
    {
    
    }
    
    int main()
    {
        Teacher_Cader person1;
        person1.input(); //输入数据
        person1.output();
        system("pause");
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月6日
  • 已采纳回答 6月28日
  • 创建了问题 6月27日

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加