设计两个父类Father和Mother,以及一个Boy子类,Boy类从Father和Mother类中继承相应的方法,并覆盖父类的方法。 如Father类中RepairTV方法 Mother类中SingASong方法
时间: 2024-02-12 07:03:30 浏览: 20
好的,以下是实现:
```cpp
#include <iostream>
using namespace std;
class Father {
public:
virtual void RepairTV() {
cout << "Father is repairing TV." << endl;
}
};
class Mother {
public:
virtual void SingASong() {
cout << "Mother is singing a song." << endl;
}
};
class Boy : public Father, public Mother {
public:
void RepairTV() override {
cout << "Boy is repairing TV." << endl;
}
void SingASong() override {
cout << "Boy is singing a song." << endl;
}
};
int main() {
Boy b;
Father* f = &b;
Mother* m = &b;
f->RepairTV();
m->SingASong();
return 0;
}
```
以上代码定义了两个父类 `Father` 和 `Mother`,以及一个子类 `Boy`。`Boy` 类从 `Father` 和 `Mother` 类中继承了 `RepairTV` 和 `SingASong` 两个方法,并覆盖了父类的方法。在 `main()` 函数中,我们创建了一个 `Boy` 对象,并使用指针调用了 `Father` 和 `Mother` 类的方法,用于测试。