在C++分别定义Person类、Employee类、Student类以及在职研 究生类PartTime_Graduate_Student,并且画出相应的继承层 次图。
时间: 2024-01-22 16:20:59 浏览: 35
以下是对应的代码和继承层次图:
```cpp
// Person类
class Person {
protected:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
// Employee类,继承自Person类
class Employee : public Person {
protected:
string company;
double salary;
public:
Employee(string n, int a, string c, double s) : Person(n, a), company(c), salary(s) {}
void display() {
Person::display();
cout << "Company: " << company << endl;
cout << "Salary: " << salary << endl;
}
};
// Student类,继承自Person类
class Student : public Person {
protected:
string major;
double gpa;
public:
Student(string n, int a, string m, double g) : Person(n, a), major(m), gpa(g) {}
void display() {
Person::display();
cout << "Major: " << major << endl;
cout << "GPA: " << gpa << endl;
}
};
// PartTime_Graduate_Student类,继承自Employee和Student类
class PartTime_Graduate_Student : public Employee, public Student {
protected:
string supervisor;
public:
PartTime_Graduate_Student(string n, int a, string c, double s, string m, double g, string sup) : Employee(n, a, c, s), Student(n, a, m, g), supervisor(sup) {}
void display() {
Employee::display();
cout << "Major: " << major << endl;
cout << "GPA: " << gpa << endl;
cout << "Supervisor: " << supervisor << endl;
}
};
```
继承层次图如下:
```
+------> Employee ----------+
| |
Person -->+ +--> PartTime_Graduate_Student
| |
+------> Student ----------+
```