问答题
已知交通工具类定义如下。
要求:(1)实现这个类;(2)定义并实现一个小车类car,是它的公有派生类,小车本身的私有属性有载人数,小车的函数有init(设置车轮数,重量和载人数),getpassenger(获取载人数),print(打印车轮数,重量和载人数)。
class vehicle
{ protected:
int wheels;//车轮数
float weight;//重量
public:
void init( int wheels, float weight);
int get_wheels();
float get_weight ();
void print();
};
void vehicle::init( int wheels, float weight)
{ this - > wheels = wheels;
this - > weight = weight;
cout << wheels << endl;
}
int vehicle::get_wheels()
{ return wheels; }
float vehicle::get_weight ()
{ return weight;}
void vehicle::print()
{ cout << "车轮数:" << wheels << "," << "重量:" << weight << endl; }
【参考答案】
class car:public vehicle
{private:int passengers;
......
(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)