声明一个教师(Teacher)类和一个学生(Student)类,用多重继承的方式声明执行派生类 小在职研究生(Graduate)派生类(在职教师攻读研究生)。教师类中包括数据成员和子对象进 (姓名)、age(年龄)、title(职称)。学生类中包括数据成员 namel(姓名)、age(性.sore(成绩)。在定义派生类对象时给出初始化的数据,然后输出这些数据。在程序中使用组合和继承。在定义Professor类对象prof1时给出所有数据的初值,然后修改prof1的生日数据,最后输出prof1的全部最新数据。
时间: 2024-02-29 20:51:54 浏览: 36
下面是具体的实现代码:
```
#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) {}
};
// 声明学生类
class Student {
protected:
string name;
int age;
float score;
public:
Student(string n, int a, float s) : name(n), age(a), score(s) {}
};
// 声明小在职研究生类,继承自教师类和学生类
class Graduate : public Teacher, public Student {
public:
Graduate(string n, int a, string t, float s)
: Teacher(n, a, t), Student(n, a, s) {}
void show() {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "职称:" << title << endl;
cout << "成绩:" << score << endl;
}
};
// 测试
int main() {
Graduate g("张三", 30, "教授", 90.5);
g.show();
return 0;
}
```
在上面的代码中,我们声明了教师类 `Teacher` 和学生类 `Student`,分别包含了各自的数据成员和构造函数。然后我们声明了小在职研究生类 `Graduate`,继承自教师类和学生类,并在构造函数中分别调用了父类的构造函数来初始化数据成员。
最后在 `main` 函数中创建了 `Graduate` 对象 `g`,并调用了它的 `show` 函数来输出各个数据成员的值。
需要注意的是,由于 `Graduate` 类继承了 `Teacher` 和 `Student` 类,因此在创建 `Graduate` 对象时需要同时传入这两个父类的构造函数参数。同时,由于 `Graduate` 类中包含了与 `Teacher` 和 `Student` 类相同的数据成员 `name` 和 `age`,因此在初始化这两个数据成员时需要注意区分。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)