编写程序声明一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车 (motorcar)类。从bicycle和motorcar派生出摩托车(motorcycle)类。他们都有Run、Stop等成员函数。在main( )函数中声明vehicle、bicycle、motorcar、motorcycle的对象,分别调用其Run( )、Stop( )函数,观察其执行情况。再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run、Stop声明为虚函数,再试试看。观察虚函数的作用。 输入 输出 vehicle is running... vehicle is stopped... bicycle is running... bicycle is stopped... motorcar is running... motorcar is stopped... motorcycle is running... motorcycle is stopped... --------------------------------------- vehicle is running... vehicle is stopped... bicycle is running... bicycle is stopped... motorcar is running... motorcar is stopped... motorcycle is running... motorcycle is stopped... #include <iostream> using namespace std; class vehicle { ...... }; class bicycle...... { ...... }; class motorcar..... { ....... }; class motorcycle....... { ...... }; int main() { vehicle v,*pv; bicycle by; motorcar mc; motorcycle mcy; //此处通过对象名来调用各成员函数 ......... cout<<"---------------------------------------\n"; pv=&v; pv->Run(); pv->Stop(); ........ pv->Run(); pv->Stop(); ....... pv->Run(); pv->Stop(); ....... pv->Run(); pv->Stop(); return 0; }
时间: 2024-02-21 18:00:37 浏览: 26
这是一个 C++ 的面向对象编程题目,你可以按照如下代码进行编写:
```cpp
#include <iostream>
using namespace std;
class vehicle {
public:
virtual void Run() {
cout << "vehicle is running..." << endl;
}
virtual void Stop() {
cout << "vehicle is stopped..." << endl;
}
};
class bicycle : public vehicle {
public:
void Run() {
cout << "bicycle is running..." << endl;
}
void Stop() {
cout << "bicycle is stopped..." << endl;
}
};
class motorcar : public vehicle {
public:
void Run() {
cout << "motorcar is running..." << endl;
}
void Stop() {
cout << "motorcar is stopped..." << endl;
}
};
class motorcycle : public bicycle, public motorcar {
public:
void Run() {
cout << "motorcycle is running..." << endl;
}
void Stop() {
cout << "motorcycle is stopped..." << endl;
}
};
int main() {
vehicle v, *pv;
bicycle by;
motorcar mc;
motorcycle mcy;
// 通过对象名来调用各成员函数
v.Run();
v.Stop();
by.Run();
by.Stop();
mc.Run();
mc.Stop();
mcy.Run();
mcy.Stop();
cout << "---------------------------------------" << endl;
// 通过指针来调用各成员函数
pv = &v;
pv->Run();
pv->Stop();
pv = &by;
pv->Run();
pv->Stop();
pv = &mc;
pv->Run();
pv->Stop();
pv = м
pv->Run();
pv->Stop();
return 0;
}
```
上述代码中,我们定义了 `vehicle`、`bicycle`、`motorcar`、`motorcycle` 四个类,其中 `vehicle` 是基类,其他三个类都是从 `vehicle` 类派生而来的子类。
在 `vehicle` 类中,我们定义了 `Run` 和 `Stop` 两个成员函数,并且将它们声明为虚函数。这是因为在派生类中,我们需要重写这两个成员函数,所以需要将它们声明为虚函数,以便实现多态性。
在 `bicycle`、`motorcar`、`motorcycle` 类中,我们分别重写了 `Run` 和 `Stop` 成员函数,并且在 `motorcycle` 类中,我们还继承了 `bicycle` 和 `motorcar` 两个类,从而实现了多重继承。
在 `main` 函数中,我们分别创建了 `vehicle`、`bicycle`、`motorcar`、`motorcycle` 四个对象,并且通过对象名来调用各成员函数,从而观察其执行情况。同时,我们还声明了一个指向 `vehicle` 类的指针 `pv`,通过指针来调用各成员函数,以观察虚函数的作用。最后,我们输出了一条分割线,以区分不同部分的输出结果。
相关推荐
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)