使用c++分别定义Person类、Employee类、Student类以及在职研究生类PartTime Graduate Student,并且画出相应的继承层次图。 ..
时间: 2024-02-01 15:15:55 浏览: 105
Person、Student、Employee、Faculty和Staff等含类的继承应用
4星 · 用户满意度95%
以下是C++代码实现:
```c++
class Person {
protected:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
virtual void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
class Employee : public Person {
protected:
int salary;
public:
Employee(string n, int a, int s) : Person(n, a), salary(s) {}
void display() {
Person::display();
cout << "Salary: " << salary << endl;
}
};
class Student : public Person {
protected:
string major;
public:
Student(string n, int a, string m) : Person(n, a), major(m) {}
void display() {
Person::display();
cout << "Major: " << major << endl;
}
};
class PartTimeGraduateStudent : public Employee, public Student {
protected:
string researchTopic;
public:
PartTimeGraduateStudent(string n, int a, int s, string m, string r) : Employee(n, a, s), Student(n, a, m), researchTopic(r) {}
void display() {
Employee::display();
Student::display();
cout << "Research Topic: " << researchTopic << endl;
}
};
```
以下是继承层次图:
```
+-------+
| Person|
+-------+
/ \
/ \
/ \
+---------+ +--------+
|Student | |Employee|
+---------+ +--------+
| |
| |
| |
+---------------------------+
|PartTimeGraduateStudent |
+---------------------------+
```
阅读全文