用C++ (多重继承) 声明一个教师类(Teacher): 保护数据成员:string name; //姓名 int age; //年龄 string title; //职称 公有成员西数:Teacher(string n,int a, string t):/构造函数 void display();1/输出教师的有关数据(姓名、年龄、职称) 声明一个学生类(Student): 保护数据成员:string name; /姓名 char sex; //性别 float score; //成绩 公有成员西数:Student(string n, int s, float c):/构造函数 void display();/输出学生的有关数据(姓名、性别、成绩) 声明一个在职研究生(Graduate),公有继承教师类(Teacher)和学生类(Student 私有数据成员:foat wage; 1/工资 公有成员西数:Graduate (string n, char s, int a, string t, nloat c, nloat w);/构造函数void show(;/输出研究生的有关数据(姓名、性别、年龄、职称、成绩、工资) 编写一个程序测试该类,测试数据及要求如下: 主西数中定义在职研究生类对象:Graduate g(“LiMing',’r,24,“助教”,89.5,3000); 本程序的执行结果如下: name: LiMing sex: f age: 24 title:助教 score: 89.5 wage: 3000
时间: 2024-02-05 08:12:04 浏览: 95
分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:①在两个基类中都包含
```cpp
#include <iostream>
#include <string>
using namespace std;
class Teacher {
protected:
string name;
int age;
string title;
public:
Teacher(string n, int a, string t) : name(n), age(a), title(t){}
void display() {
cout << "name: " << name << endl;
cout << "age: " << age << endl;
cout << "title: " << title << endl;
}
};
class Student {
protected:
string name;
char sex;
float score;
public:
Student(string n, char s, float c) : name(n), sex(s), score(c){}
void display() {
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
cout << "score: " << score << endl;
}
};
class Graduate : public Teacher, public Student {
private:
float wage;
public:
Graduate(string n, char s, int a, string t, float c, float w) : Teacher(n, a, t), Student(n, s, c), wage(w){}
void show() {
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
cout << "age: " << age << endl;
cout << "title: " << title << endl;
cout << "score: " << score << endl;
cout << "wage: " << wage << endl;
}
};
int main() {
Graduate g("LiMing", 'f', 24, "助教", 89.5, 3000);
g.show();
return 0;
}
```
输出结果:
```
name: LiMing
sex: f
age: 24
title: 助教
score: 89.5
wage: 3000
```
阅读全文