问答题 请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 车牌号:冀ABCl234 品牌:ForLand 类别:卡车 当前档位:0 最大载重量:12 车牌号:冀ABC1234 品牌:ForLand 类别:卡车 当前档位:2 最大载重量:12 车牌号:沪XYZ5678 品牌:QQ 类别:小轿车 当前档位:0 座位数:5 车牌号:沪XYZ5678 品牌:QQ 类别:小轿车 当前档位:-1 座位数:5 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 #include <iostream> #include <iomanip> #include <cmath> using namespace std; class AutoMobile{ //"汽车"类 char * brand; //汽车品牌 char * number; //车牌号 int speed; //档位: 1、2、3、4、5, 空档: 0,倒档: -1 public: AutoMobile (const char * the_brand, const char * the_number): speed(0) { brand = new char [strlen (the_brand) +1]; //********** found********** ______; //********** found********** ______; strcpy (number, the_number); } ~AutoMobile ( ) {delete[ ] brand;delete[] number; } const char * theBrand {{U}} {{/U}}const { reurn brand; } //返回品牌名称 const char * theNumber{{U}} {{/U}}const{ return number; } //返回车牌号 int currentSpeed {{U}} {{/U}}const { returnspeed; } //返回当前档位 void changeGearTo (int the speed) if(speed>= -1 && speed<=5) speed = the_speed; } virtual const char * category {{U}} {{/U}} const =0; //类别: 卡车、小轿车等 virtual void show {{U}} {{/U}}const { cout << "车牌号" << theNumber {{U}} {{/U}} //**********found********** << "品牌:" << << "类别:" << category {{U}} {{/U}} <<"当前档位" << currentSpeed {{U}} {{/U}}; } }; class Car: public AutoMobile { int seats; //座位数 public: Car (const char * the_brand, constchar * the_number, int the seats):AutoMobile (the_brand, the _number),seats (the seats) {} int numberOfSeat {{U}} {{/U}}const { returnseats; } const char * category{{U}} {{/U}}const{ return "小轿车"; } //返回汽车类别 void show{{U}} {{/U}}const{ AutoMobile::show{{U}} {{/U}}; cout <<"座位数" <<numberOfSeat{{U}} {{/U}}<<endl; } }; class Truck: public AutoMobile { //int max_load; //最大载重量 public: Truck (const char * the _brand,const char * the number, int the_max_load): AutoMobile (the_brand, the_number), max_load (the_max_load) { } int maxLoad {{U}} {{/U}}const { return max_load; } //返回最大载重量 //********** found********** const char * category{{U}} {{/U}}______//返回汽车类别 void show{{U}} {{/U}}const{ AutoMobile::show{{U}} {{/U}}; cout <<"最大载重量:" <<maxmoad {{U}} {{/U}}<<endl; } }; int main{{U}} {{/U}}{ Truck truck ( "ForLand", "ABC1234", 12); truck.show{{U}} {{/U}}; truck.changeGearTo(2); truck.show{{U}} {{/U}}; Car car("QQ", "沪XYZ5678", 5); car.show{{U}} {{/U}}; car.changeGearTo(-1); car.show{{U}} {{/U}}; cout<<endl; return 0; }
【正确答案】(1)strcpy(brand, the_brand) (2)number=new char[strten(the_number)+1] (3)theBrand() (4)const {return "卡车";}
【答案解析】[考点] 本题考查的是AutoMobile类及其派生类Car类和Truck类,其中涉及动态数组、构造函数、strcpy()函数、析构函数、纯虚函数和虚函数。本题程序较多,基类较复杂,但细心看会发现程序很容易读,考的知识点都很简单,和前后语句一对比就可以得到答案。 [解析] (1)主要考查考生对strcpy函数的掌握情况,在上一条语句程序给brand指针分配了空间,在这里要复制字符串the_brand,即strcpy(brand,the_brand);。 (2)主要考查考生对动态分配的掌握情况,参考brand指针的复制过程完成该语句,先给指针number分配空间,通过new来完成:number=new char[strlen(the_number)+1];。 (3)主要考查考生对成员函数的掌握,由程序可知这里要输出的是品牌,因此调用成员函数theBrand()来输出品牌。 (4)主要考查考生对纯虚函数的掌握,根据纯虚函数的定义:virtual const char*category()const=0;,可知在这里要填写:const {return "卡车";}。