c++城西:分别定义Person类、Employee类、Student类以及在职研 究生类PartTime_Graduate_Student,并且画出相应的继承层 次图
时间: 2024-02-01 16:14:20 浏览: 81
Person、Student、Employee、Faculty和Staff等含类的继承应用
4星 · 用户满意度95%
以下是相应的代码和继承层次图:
```cpp
class Person {
protected:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
virtual void show() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
class Employee : public Person {
protected:
double salary;
public:
Employee(string n, int a, double s) : Person(n, a), salary(s) {}
void show() {
Person::show();
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 show() {
Person::show();
cout << "Major: " << major << endl;
}
};
class PartTime_Graduate_Student : public Employee, public Student {
protected:
string research;
public:
PartTime_Graduate_Student(string n, int a, double s, string m, string r) : Employee(n, a, s), Student(n, a, m), research(r) {}
void show() {
Employee::show();
cout << "Major: " << major << endl;
cout << "Research: " << research << endl;
}
};
```
继承层次图:
```
+-----------+
| Person |
+-----------+
|
|
+-----------+
| Student |
+-----------+
|
|
+-----------+
| Employee |
+-----------+
|
|
+---------------------------+
| PartTime_Graduate_Student |
+---------------------------+
```
阅读全文