分别定义教师类Teacher 和干部类Cadre,采用多重继承的方式由这两个类派生出新类Teacher_Cadre(教师兼干部类)。要求: (1)在两个基类中都包括姓名、年龄、性别、地址、电话作为数据成员; (2)在Teacher类中还包含数据成员:职称title,在Cadre类中还包含数据成员:职务post,在Teacher_Cadre类中还包含数据成员:工资wage;(3)对两个基类中的姓名、年龄、性别、地址、电话用相同的名字,在访问者类数据成员时,请指定作用域; (4)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、地址、电话和职称,然后再调用cout语句输出职务和工资;
时间: 2023-11-28 20:47:42 浏览: 139
分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:①在两个基类中都包含
```cpp
#include<iostream>
#include<string>
using namespace std;
class Teacher {
protected:
string name;
int age;
char sex;
string address;
string phone;
string title;
public:
void display() {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "性别:" << sex << endl;
cout << "地址:" << address << endl;
cout << "电话:" << phone << endl;
cout << "职称:" << title << endl;
}
};
class Cadre {
protected:
string name;
int age;
char sex;
string address;
string phone;
string post;
public:
void display() {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "性别:" << sex << endl;
cout << "地址:" << address << endl;
cout << "电话:" << phone << endl;
cout << "职务:" << post << endl;
}
};
class Teacher_Cadre : public Teacher, public Cadre {
private:
double wage;
public:
Teacher_Cadre(string n, int a, char s, string add, string ph, string t, string p, double w) {
name = n;
age = a;
sex = s;
address = add;
phone = ph;
title = t;
post = p;
wage = w;
}
void show() {
Teacher::display();
cout << "职务:" << post << endl;
cout << "工资:" << wage << endl;
}
};
int main() {
Teacher_Cadre tc("张三", 35, 'M', "北京市", "123456789", "高级教师", "校长", 8000);
tc.show();
return 0;
}
```
输出结果为:
```
姓名:张三
年龄:35
性别:M
地址:北京市
电话:123456789
职称:高级教师
职务:校长
工资:8000
```
阅读全文