2024年3月14日发(作者:09年的福特嘉年华值多少钱)

编辑一个C++ 设计一个程序,定义一个汽车类Vehicle,它具有一个需要传递参

数的构造函数,类中的数据包括:车轮个数wheel和车重weight,两者作为

保护成员;小车类Car是它的私有派生类,其中包括承载人数passengers;卡

车类Truck是Vehicle的私有派生类,其中包括承载人数passengers和载重

量payload。每个类都有相关数据的输出方法。

class Vehicle

{

protected:

int wheels;

float weight;

public:

Vehicle(int wh, float we): wheels(wh), weight(we) {}

void display();

int getWheels();

float getWeight();

};

class Car: private Vehicle

{

private:

int passengers;

public:

Car(int wh, float we, int pa): Vehicle(wh, we), passengers(pa) {}

int getPassengers();

void display();

};

void Car::display()

{

cout << \"wheels: \" << wheels << endl;

cout << \"weight: \" << weight << endl;

cout << \"passengers: \" << passengers << endl;

}

class Trunk: private Vehicle

{

private:

float payload;

int passengers;

public:

Trunk(int wh, float we, int pay, int pas): Vehicle(wh, we), payload(pay), passengers(pas) {}

float getPayload();

int getPassengers();

void display();

};

更多推荐

包括,定义,数据,汽车