C++中派生类的问题

来源:百度知道 编辑:UC知道 时间:2024/05/19 18:35:21
1.设计一个汽车类Vehicle,包含私有的数据成员车轮个数wheels和车重weight,公有的成员函数start、move、stop;小车类Car是Vehicle的派生类,其中包含载人数passengerLoad;为其提供start、move、stop的不同实现;卡车类Truck是Vehicle的派生类,其中包含载人数passengerLoad和载重量payload;为其提供start、move、stop的不同实现;为每个每个类设计相关数据的输出方法。
2.编写程序测试你设计的类。
3.在汽车类Vehicle中分别以虚函数和非虚函数两种不同的方式实现start、move、stop,观察其派生类的成员函数不同。
用C++语言写

class Vehicle{
private:
int wheels;
float weight;
public:
Vehicle();
~Vehicle(){};
~void start() = 0;
~void move() = 0;
~void stop() = 0;
}

class Car::public Vehicle
{
private:
int passegerLoad;
public:
void start(){...};
void move(){...};
void stop(){...};
void print(){ cout << passengerLoad << endl;}
}

class Truck::public Vehicle
{
private:
int passengerLoad;
float payload;
public:
void start(){...};
void move(){...};
void stop(){...};
void print(){ cout << passengerLoad << '/n' << payload;}
}
and so on