用C++ 虛基类 声明一个人员类(Person): 保护数据成员:string name; //姓名 char sex; //性别 int age; //年龄 公有成员西数:Teacher(string n, char s, inta):/构造函数 声明一个教师类(Teacher):公有继承人员类(Person),井声明Person为虚基类保护数据成员:string title; //职称 公有成员函数:Teacher(string n, char s, int a, string t);1构造函数 声明一个学生类(Student):公有继承人员类(Person),并声明Person为虚基类保护数据成员:1oat score;1/成绩 公有成员函数:Student(string n, chars, int a, nloat c):/构造函数 声明一个在职研究生(Graduate), 公有继承教师类(Teacher)和学生类(Student 私有数据成员:noat wage; //工资 公有成员西数:Graduate (string n, char s, int a, string t, float c, float w):/构造西数void showO;1/输出研究生的有关数据(姓名、性别、年龄、职称、成绩、工资) 编写一个程序测试该类,测试数据及要求如下: 主函数中定义在职研究生类对象:Graduate g('LiMing',’f,24,“助教:,89.5,3000); 本程序的执行结果如下: name: LiMing sex: f age:24 title:助教 score: 89.5 wage: 3000
时间: 2024-02-05 12:05:22 浏览: 119
以下是代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
char sex;
int age;
public:
Person(string n, char s, int a): name(n), sex(s), age(a) {}
};
class Teacher : virtual public Person {
protected:
string title;
public:
Teacher(string n, char s, int a, string t): Person(n, s, a), title(t) {}
};
class Student : virtual public Person {
protected:
float score;
public:
Student(string n, char s, int a, float c): Person(n, s, a), score(c) {}
};
class Graduate : public Teacher, public Student {
private:
float wage;
public:
Graduate(string n, char s, int a, string t, float c, float w): Person(n, s, a), Teacher(n, s, a, t), Student(n, s, a, 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
```
阅读全文