定义一个交通工具vehicle,将他作为基类派生校车类car,卡车类truck和轮船类boat,定义类并定义虚函数来显示各类信息。
二、代码实现#includeusing namespace std; class vehicle { protected: double speed; //速度,公里/小时 int wheels; //轮子数 double weight; //重量 public: vehicle(double speed=80,int wheels=4,double weight=1000); virtual void show(void)=0; }; vehicle::vehicle(double speed,int wheels,double weight) { this->speed=speed,this->wheels=wheels,this->weight=weight; } class car : public vehicle { int passenger_load; public: car(double speed=80,int wheels=4,double weight=1000,int passenger_load=4); virtual void show(void); }; car::car(double speed,int wheels,double weight,int passenger_load):vehicle(speed,wheels,weight) { this->passenger_load=passenger_load; } void car::show(void) { cout << "Car messagen"; cout< double rated_load; //额定载重 public: truck(double speed=80,int wheels=4,double weight=2500,double rated_load=3000); virtual void show(void); }; truck::truck(double speed,int wheels,double weight,double rated_load):vehicle(speed,wheels,weight) { this->rated_load=rated_load; } void truck::show(void) { cout<<"truck messagen"; cout< char kind; //轮船类别,如客轮为'k' public: boat(double speed=30,int wheels=0,double weight=12000,char kind='k'); virtual void show(void); }; boat::boat(double speed,int wheels,double weight,char kind):vehicle(speed,wheels,weight) { this->kind=kind; } void boat::show(void) { cout<<"boat messagen"; cout< vehicle *unicycle; car *BMW; unicycle = new car; unicycle ->show(); BMW= (car *) unicycle; BMW ->show(); delete unicycle; unicycle = new truck; unicycle ->show(); delete unicycle; unicycle = new boat; unicycle ->show(); delete unicycle; return 0; }



