根据给定的汽车类vehicle(包含的数据成员有车轮个数wheels和车重weight)声明,完成其中成员函数的定义,之后再定义其派生类并完成测试。 小车类car是它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。
时间: 2024-02-17 12:03:12 浏览: 22
好的,我了解了你的问题。以下是对应的代码实现:
```cpp
#include <iostream>
using namespace std;
// 定义父类vehicle
class vehicle {
protected:
int wheels;
int weight;
public:
vehicle(int w, int wt) {
wheels = w;
weight = wt;
}
void display() {
cout << "车轮个数:" << wheels << endl;
cout << "车重:" << weight << endl;
}
};
// 定义子类car
class car : public vehicle {
int passenger_load;
public:
car(int w, int wt, int pl) : vehicle(w, wt) {
passenger_load = pl;
}
void display() {
vehicle::display();
cout << "载人数:" << passenger_load << endl;
}
};
int main() {
vehicle v(4, 1000);
car c(4, 800, 5);
cout << "vehicle类的数据成员:" << endl;
v.display();
cout << "\ncar类的数据成员:" << endl;
c.display();
return 0;
}
```
输出结果:
```
vehicle类的数据成员:
车轮个数:4
车重:1000
car类的数据成员:
车轮个数:4
车重:800
载人数:5
```
其中,vehicle类作为父类,包含车轮个数和车重两个数据成员,以及一个显示车辆信息的display()函数;car类作为子类,继承了vehicle类,并增加了载人数这个数据成员,同时也重写了display()函数。在main函数中分别创建了vehicle对象和car对象,并输出它们的数据成员。