完善代码#include<iostream.h> void main() {class Person: def __init__(self, name, gender, age, profession): self.name = name self.gender = gender self.age = age self.profession = profession def set_name(self, name): self.name = name def set_gender(self, gender): self.gender = gender def set_age(self, age): self.age = age def set_profession(self, profession): self.profession = profession def display_name(self): print("Name:", self.name) def display_gender(self): print("Gender:", self.gender) def display_age(self): print("Age:", self.age) def display_profession(self): print("Profession:", self.profession) def display_all(self): self.display_name() self.display_gender() self.display_age() self.display_profession() class Student(Person): def __init__(self, name, gender, age, profession, school, major, grade): super().__init__(name, gender, age, profession) self.school = school self.major = major self.grade = grade def set_school(self, school): self.school = school def set_major(self, major): self.major = major def set_grade(self, grade): self.grade = grade def display_school(self): print("School:", self.school) def display_major(self): print("Major:", self.major) def display_grade(self): print("Grade:", self.grade) def display_all(self): super().display_all() self.display_school() self.display_major() self.display_grade() person1 = Person("Alice", "Female", 25, "Engineer") person1.display_all() student1 = Student("Bob", "Male", 20, "Student", "XYZ University", "Computer Science", "Sophomore") student1.display_all() }
时间: 2024-01-01 13:06:47 浏览: 170
抱歉,这段代码不是 C++ 代码,而是 Python 代码。可以参考下面的完整代码:
```python
class Person:
def __init__(self, name, gender, age, profession):
self.name = name
self.gender = gender
self.age = age
self.profession = profession
def set_name(self, name):
self.name = name
def set_gender(self, gender):
self.gender = gender
def set_age(self, age):
self.age = age
def set_profession(self, profession):
self.profession = profession
def display_name(self):
print("Name:", self.name)
def display_gender(self):
print("Gender:", self.gender)
def display_age(self):
print("Age:", self.age)
def display_profession(self):
print("Profession:", self.profession)
def display_all(self):
self.display_name()
self.display_gender()
self.display_age()
self.display_profession()
class Student(Person):
def __init__(self, name, gender, age, profession, school, major, grade):
super().__init__(name, gender, age, profession)
self.school = school
self.major = major
self.grade = grade
def set_school(self, school):
self.school = school
def set_major(self, major):
self.major = major
def set_grade(self, grade):
self.grade = grade
def display_school(self):
print("School:", self.school)
def display_major(self):
print("Major:", self.major)
def display_grade(self):
print("Grade:", self.grade)
def display_all(self):
super().display_all()
self.display_school()
self.display_major()
self.display_grade()
person1 = Person("Alice", "Female", 25, "Engineer")
person1.display_all()
student1 = Student("Bob", "Male", 20, "Student", "XYZ University", "Computer Science", "Sophomore")
student1.display_all()
```
这个代码定义了两个类:`Person` 和 `Student`,其中 `Student` 是 `Person` 的子类。`Person` 类有四个属性:姓名、性别、年龄、职业,以及相应的 getter 和 setter 方法。`Student` 类在 `Person` 的基础上增加了三个属性:学校、专业、年级,也有相应的 getter 和 setter 方法。同时,`Student` 类重写了 `display_all()` 方法,打印出所有属性,包括父类属性和子类属性。
在代码的最后,创建了一个 `Person` 对象和一个 `Student` 对象,分别调用了它们的 `display_all()` 方法,打印出所有属性。
阅读全文