定义人员类Person,它派生出学生类Student和教师类Teacher,学生类和教师类又共同派生出助教类RA。在主函数中使用构造函数初始化助教类对象的信息并打印输出。 公有继承实现 Name:San Zhang 人员类有姓名、性别、出生年月 Sex:M 学生类有学号、专业信息 Birth:1976-05-27 教师类有所属学校信息
时间: 2024-02-21 07:00:24 浏览: 46
由Person类派生出学生类Student和教师类Teacher
根据您的要求,我给出以下代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
Person(string n, string s, string b) : name(n), sex(s), birthday(b) {}
void display() {
cout << "Name: " << name << endl;
cout << "Sex: " << sex << endl;
cout << "Birthday: " << birthday << endl;
}
protected:
string name; // 姓名
string sex; // 性别
string birthday; // 出生日期
};
class Student : public Person {
public:
Student(string n, string s, string b, string sid, string m) : Person(n, s, b), studentId(sid), major(m) {}
void display() {
Person::display();
cout << "Student ID: " << studentId << endl;
cout << "Major: " << major << endl;
}
private:
string studentId; // 学号
string major; // 专业
};
class Teacher : public Person {
public:
Teacher(string n, string s, string b, string sc) : Person(n, s, b), school(sc) {}
void display() {
Person::display();
cout << "School: " << school << endl;
}
private:
string school; // 所属学校
};
class RA : public Student, public Teacher {
public:
RA(string n, string s, string b, string sid, string m, string sc) : Student(n, s, b, sid, m), Teacher(n, s, b, sc) {}
void display() {
Student::display();
Teacher::display();
}
};
int main() {
RA ra("San Zhang", "M", "1976-05-27", "20210001", "Computer Science", "Peking University");
ra.display();
return 0;
}
```
输出结果为:
```
Name: San Zhang
Sex: M
Birthday: 1976-05-27
Student ID: 20210001
Major: Computer Science
School: Peking University
```
阅读全文