【C++OJ多重继承与虚拟继承】OOP教师与干部(多重继承)

【C++OJ多重继承与虚拟继承】OOP教师与干部(多重继承)

B. OOP教师与干部(多重继承)

题目描述

分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)类。要求:
(1) 在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
(2) 在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务)。在Teacher_Cadre类中还包含数据成员wages(工资)。
(3) 对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
(4) 在类体中声明成员函数,在类外定义成员函数。
(5) 在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。

输入

依次输入姓名、年龄、性别、职称、职务、地址、电话、工资

输出

依次输出姓名、年龄、性别、职称、职务、地址、电话、工资

输入样例

Wang-li 50 f prof. president
135 Beijing Road,Shanghai
(021)61234567 1534.5

输出样例

name:Wang-li
age:50
sex:f
title:prof.
address:135 Beijing Road,Shanghai
tel:(021)61234567
post:president
wages:1534.5

参考代码

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

class Teacher // Teacher(教师)类
{
public:
    Teacher(string nam, int a, char s, string tit, string ad, string tele); //构造函数,初始化参数列表
    void display();                                                         // Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话
    ~Teacher();                                                             //析构函数
protected:                                                                  // 两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
    string name;
    int age;
    char sex;
    string address;
    string telephone;
    string title; // 在Teacher类中还包含数据成员title(职称)
};
//在类体中声明成员函数,在类外定义成员函数。
Teacher::Teacher(string nam, int a, char s, string tit, string ad, string tele) : name(nam), age(a), sex(s), title(tit), address(ad), telephone(tele) {}
void Teacher::display()
{
    cout << "name:" << name << endl;
    cout << "age:" << age << endl;
    cout << "sex:" << sex << endl;
    cout << "title:" << title << endl;
    cout << "address:" << address << endl;
    cout << "tel:" << telephone << endl;
}
Teacher::~Teacher() {}

class Cadre // Cadre(干部)类
{
public:
    Cadre(string nam, int a, char s, string ad, string p, string tele); //构造函数,初始化参数列表
    ~Cadre();                                                           //析构函数
protected:                                                              // 两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
    string name;
    int age;
    char sex;
    string address;
    string telephone;
    string post; //在Cadre类中还包含数据成员post(职务)
};
Cadre::Cadre(string nam, int a, char s, string ad, string p, string tele) : name(nam), age(a), sex(s), address(ad), post(p), telephone(tele) {}
Cadre::~Cadre() {}

//采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)类
class Teacher_Cadre : public Teacher, public Cadre
{
public:                                                                                              //基类数据成员使用相同的名字,在引用这些数据成员时,指定作用域。
    Teacher_Cadre(string nam, int a, char s, string ad, string tit, string p, string tele, float w); //构造函数,初始化参数列表
    void show();                                                                                     // show中调用Teacher类中的display函数,用cout语句输出职务与工资。
    ~Teacher_Cadre();                                                                                //析构函数
private:
    float wages; // 在Teacher_Cadre类中还包含数据成员wages(工资)。
};
Teacher_Cadre::Teacher_Cadre(string nam, int a, char s, string ad, string tit, string p, string tele, float w) : Teacher(nam, a, s, ad, tit, tele), Cadre(nam, a, s, ad, p, tele), wages(w) {}
void Teacher_Cadre::show()
{
    Teacher::display();
    cout << "post:" << Cadre::post << endl;
    cout << "wages:" << wages << endl;
}
Teacher_Cadre::~Teacher_Cadre(){};

int main()
{
    //依次输入姓名、年龄、性别、职称、职务、地址、电话、工资
    string name;
    int age;
    char sex;
    string title;
    string post;
    string address;
    string telephone;
    float wages;

    cin >> name >> age >> sex >> title >> post;
    cin.ignore();         // cin.ignore()将输入流里面的换行符舍弃掉,输入完成以后还可以进行第二次输入
    getline(cin, address); //读取一行数据
    cin >> telephone >> wages;

    Teacher_Cadre t(name, age, sex, title, address, post, telephone, wages);
    t.show();

    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ferry_xie

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

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

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

打赏作者

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

抵扣说明:

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

余额充值