C++这道题应该怎么做?

编写一个程序设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的公有派生类其中包含载人数passenger_load。卡车类truck是vehicle的私有派生类其中包含载人数passenger_load和载重量payload,每个类都有相关数据的输出方法。构造和析构方法

#include<iostream>

#include<cstring>

using namespace std;

class vehicle {

int wheels, weight;

public:vehicle() {

wheels = 5;

weight = 5;

}

   void set(int wh, int wei) {

   wheels = wh;

   weight = wei;

   }

   void showme() {

   cout << "wheels = " << wheels << endl;

   cout << "weight=" << weight << endl;

   }

  ~vehicle() {


   cout << "Now destroying the instance of vehicle\n";

}

};

class car :public vehicle {

int passenger_load;

public: car() {

passenger_load = 5;

}void set(int a, int b, int c) {

vehicle::set(a, b);

passenger_load = c;

}

void showme() {

vehicle::showme();

cout << "passenger_load=" << passenger_load << endl;

}

~car() {

cout << "Now destroying the instance of car\n";


}

};

class truck :public car{

int payload;

public:truck() {

payload = 5;

}

   void set(int a, int b, int c, int d) {

   car::set(a, b, c);

   payload = d;

   }

   void showme() {

   car::showme();

   cout << "payload=" << payload << endl;

   }

   ~truck() {

   cout << "Now destroying the instance of truck\n";

   }

};

int main() {

truck a;

a.showme();

return 0;

}

温馨提示:答案为网友推荐,仅供参考