分别定义Person类、Employee类、Student类以及在职研究生类PartTime_Graduate_Student,并且画出相应的继承层次图。
时间: 2024-02-01 10:16:30 浏览: 129
以下是相应类的定义和继承层次图:
```
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Employee(Person):
def __init__(self, name, age, company):
super().__init__(name, age)
self.company = company
class Student(Person):
def __init__(self, name, age, school):
super().__init__(name, age)
self.school = school
class PartTime_Graduate_Student(Student, Employee):
def __init__(self, name, age, school, company):
Student.__init__(self, name, age, school)
Employee.__init__(self, name, age, company)
```
继承层次图如下:
```
Person
|
Employee
|
PartTime_Graduate_Student
|
Student
```
相关问题
分别定义Person类、Employee类、Student类以及在职研 究生类PartTime_Graduate_Student,并且画出相应的继承层 次图
Person类是一个基类,用于描述一个人的基本信息,如姓名、年龄、性别等。Employee类和Student类都继承自Person类,分别用于描述一个职员和一个学生的信息。PartTime_Graduate_Student类继承自Employee类和Student类,表示一个在职研究生的信息。以下是各个类的定义以及继承关系图:
```
class Person:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class Employee(Person):
def __init__(self, name, age, gender, company):
super().__init__(name, age, gender)
self.company = company
class Student(Person):
def __init__(self, name, age, gender, school):
super().__init__(name, age, gender)
self.school = school
class PartTime_Graduate_Student(Employee, Student):
def __init__(self, name, age, gender, company, school):
Employee.__init__(self, name, age, gender, company)
Student.__init__(self, name, age, gender, school)
```
继承关系图:
```
+------------+
| Person |
+-----+------+
|
|
+-----+------+
| Student |
+-----+------+
|
|
+-----+------+
| Employee |
+-----+------+
|
|
+-----+------+
| PartTime |
| Grad. |
+-----------+
```
在C++分别定义Person类、Employee类、Student类以及在职研 究生类PartTime_Graduate_Student,并且画出相应的继承层 次图。
以下是对应的代码和继承层次图:
```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 ----------+
```
阅读全文