1、二级 C+-46及答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 proj1,此工程中包含程序文件main.cpp,其中有 ElectricFan(“电风扇”)类和主函数 main的定义。程序中位于每个“/ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 品牌:清风牌,电源:关,风速:0 品牌:清风牌,电源:开,风速:3 品牌:清风牌,电源:关,风速:0 注意:只修改每个“/ERRO*found*”下的那一行,不要改动程序中的其他内容
2、。 #includeiostream using namespace std; class ElectricFan /“电扇“类 char * brand; int intensity; /风速: 0-关机, 1-弱, 2-中, 3-强 public: ElectricFan (const char * the_brand): intensity(0) brand = new char strlen (the_brand) +1; strcpy(brand, the_brand); ElectricFan() delete brand; /ERROR *found* const char *
3、theBrand()const return* brand; /返回电扇品牌 int theIntensity () const returnintensity; /返回风速 bool isOn()const return intensity0; /ERROR *found* void turnOff() intensity=1; /关电扇 void setIntensity (int inten) /ERROR *found* if (intensity=i void show () cout “品牌:“theBrand () “牌“ “,电源:“ (isOn ()? “开“:“关“) “,
4、风速:“ theIntensity () endl; ; int main() ElectricFan fan (“清风“); fan. show () ; fan. setIntensity (3); fan. show (); fan. turnOff (); fan. show (); return 0; (分数:30.00)_二、B简单应用题/B(总题数:1,分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 proj2,该工程中包含一个程序文件main.cpp,其中有类 AutoMobile(“汽车”)及其派生类 Car(“小轿车”)、Truck(“
5、卡车”)的定义,还有主函数 main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 车牌号:冀 ABCl234 品牌:ForLand 类别:卡车 当前档位:0 最大载重量:12 车牌号:冀ABC1234 品牌:ForLand 类别:卡车 当前档位:2 最大载重量:12 车牌号:沪 XYZ5678 品牌:QQ 类别:小轿车 当前档位:0 座位数:5 车牌号:沪 XYZ5678 品牌:QQ 类别:小轿车 当前档位:-1 座位数:5 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/*found*”。 #include iost
6、ream #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* _; /* fou
7、nd* _; strcpy (number, the_number); AutoMobile ( ) delete brand;delete number; const char * theBrand U /Uconst reurn brand; /返回品牌名称 const char * theNumberU /Uconst return number; /返回车牌号 int currentSpeed U /Uconst returnspeed; /返回当前档位 void changeGearTo (int the speed) if(speed= -1 virtual const char
8、* category U /U const =0; /类别: 卡车、小轿车等 virtual void show U /Uconst 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,
9、the _number),seats (the seats) int numberOfSeat U /Uconst returnseats; const char * categoryU /Uconst return “小轿车“; /返回汽车类别 void showU /Uconst AutoMobile:showU /U; cout “座位数“ numberOfSeatU /Uendl; ; class Truck: public AutoMobile /int max_load; /最大载重量 public: Truck (const char * the _brand,const cha
10、r * the number, int the_max_load): AutoMobile (the_brand, the_number), max_load (the_max_load) int maxLoad U /Uconst return max_load; /返回最大载重量 /* found* const char * categoryU /U_/返回汽车类别 void showU /Uconst AutoMobile:showU /U; cout “最大载重量:“ maxmoad U /Uendl; ; int mainU /U Truck truck ( “ForLand“, “
11、ABC1234“, 12); truck.showU /U; truck.changeGearTo(2); truck.showU /U; Car car(“QQ“, “沪 XYZ5678“, 5); car.showU /U; car.changeGearTo(-1); car.showU /U; coutendl; return 0; (分数:30.00)_三、B综合应用题/B(总题数:1,分数:40.00)3.请使用 VC6或使用答题菜单打开考生文件夹 proj3下的工程 proj3,其中使用友元函数访问类的私有数据成员,求出两个数据成员的大于 1的最小公因子。请编写友员函数 FriFu
12、n,使其输出结果为: Common denominator is 2 要求:补充编制的内容写在“/*333*”与“/*666*”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数 writeToFile已经编译为 obj文件,并且在本程序中调用。 /proj3.h class FriFunClass int a, b; public: FriFunClass(int i, int j) a=i; b=j; friend int FriFun (FriFunClass x); /友元函数 ; void writeToFile(const char *); /
13、proj3.cpp #include iostream using namespace std; #include “prj3.h“ int FriFun (FriFunClass x) /* 333* /由于函数 FriFun()是类 FriFunClass /* 666* int main ( ) FriFunClass n(10, 20); if (FriFun (n) cout “Common denominator is “FriFun(n)“/n“; else cout “No common denominator./n“; writeToFile(“); return 0; (分
14、数:40.00)_二级 C+-46答案解析(总分:100.00,做题时间:90 分钟)一、B基本操作题/B(总题数:1,分数:30.00)1.请使用 VC6或使用答题菜单打开考生文件夹 proj1下的工程 proj1,此工程中包含程序文件main.cpp,其中有 ElectricFan(“电风扇”)类和主函数 main的定义。程序中位于每个“/ERROR*found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 品牌:清风牌,电源:关,风速:0 品牌:清风牌,电源:开,风速:3 品牌:清风牌,电源:关,风速:0 注意:只修改每个“/ERRO*found*”下的那一行,不要改动
15、程序中的其他内容。 #includeiostream using namespace std; class ElectricFan /“电扇“类 char * brand; int intensity; /风速: 0-关机, 1-弱, 2-中, 3-强 public: ElectricFan (const char * the_brand): intensity(0) brand = new char strlen (the_brand) +1; strcpy(brand, the_brand); ElectricFan() delete brand; /ERROR *found* const
16、 char * theBrand()const return* brand; /返回电扇品牌 int theIntensity () const returnintensity; /返回风速 bool isOn()const return intensity0; /ERROR *found* void turnOff() intensity=1; /关电扇 void setIntensity (int inten) /ERROR *found* if (intensity=i void show () cout “品牌:“theBrand () “牌“ “,电源:“ (isOn ()? “开“
17、:“关“) “,风速:“ theIntensity () endl; ; int main() ElectricFan fan (“清风“); fan. show () ; fan. setIntensity (3); fan. show (); fan. turnOff (); fan. show (); return 0; (分数:30.00)_正确答案:(1)const chin*theBrand()conmreturn brand; /返回电扇品牌 (2)void turnOff()intenmtyr=0; /关电扇 (3)if(inten=1 )解析:考点 本题考查的是 Electr
18、icFan类,其中涉及动态数组、构造函数、strcpy()函数、析构函数、const函数和 bool函数。ElectricFan 是一个“电扇”类,动态数组存储的是品牌名称,即字符串。 解析 (1)主要考查考生对指针的掌握情况,因为 brand是一个动态指针,*brand 表示字符串的首个字符,brand表示动态数组,这里要返回动态数组存储的品牌名称。 (2)主要考查考生对成员函数的掌握情况,根据题目中类的定义中私有成员的定义:int intensity;/风速:0-关机,1-弱,2-中,3-强,可知本函数要关电扇,因此在这里 intensity=0;。 (3)主要考查考生对成员函数的掌握,根
19、据题目中函数的注释:开电扇并设置风速,可知 if语句里要判断的应该是形参 inten而不是 intensity。 主要考查考生对指针和成员函数的掌握,成员函数主要考查考生的逻辑思维,尤其要注意类的定义。二、B简单应用题/B(总题数:1,分数:30.00)2.请使用 VC6或使用答题菜单打开考生文件夹 proj2下的工程 proj2,该工程中包含一个程序文件main.cpp,其中有类 AutoMobile(“汽车”)及其派生类 Car(“小轿车”)、Truck(“卡车”)的定义,还有主函数 main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 车牌号:
20、冀 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 Aut
21、oMobile /“汽车“类 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;del
22、ete number; const char * theBrand U /Uconst reurn brand; /返回品牌名称 const char * theNumberU /Uconst return number; /返回车牌号 int currentSpeed U /Uconst returnspeed; /返回当前档位 void changeGearTo (int the speed) if(speed= -1 virtual const char * category U /U const =0; /类别: 卡车、小轿车等 virtual void show U /Uconst
23、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 /Uconst returnsea
24、ts; const char * categoryU /Uconst return “小轿车“; /返回汽车类别 void showU /Uconst AutoMobile:showU /U; cout “座位数“ numberOfSeatU /Uendl; ; 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_numbe
25、r), max_load (the_max_load) int maxLoad U /Uconst return max_load; /返回最大载重量 /* found* const char * categoryU /U_/返回汽车类别 void showU /Uconst AutoMobile:showU /U; cout “最大载重量:“ maxmoad U /Uendl; ; int mainU /U Truck truck ( “ForLand“, “ABC1234“, 12); truck.showU /U; truck.changeGearTo(2); truck.showU /
26、U; Car car(“QQ“, “沪 XYZ5678“, 5); car.showU /U; car.changeGearTo(-1); car.showU /U; coutendl; return 0; (分数:30.00)_正确答案:(1)strcpy(brand, the_brand) (2)number=new charstrten(the_number)+1 (3)theBrand() (4)const return “卡车“;)解析:考点 本题考查的是 AutoMobile类及其派生类 Car类和 Truck类,其中涉及动态数组、构造函数、strcpy()函数、析构函数、纯虚函数
27、和虚函数。本题程序较多,基类较复杂,但细心看会发现程序很容易读,考的知识点都很简单,和前后语句一对比就可以得到答案。 解析 (1)主要考查考生对 strcpy函数的掌握情况,在上一条语句程序给 brand指针分配了空间,在这里要复制字符串 the_brand,即strcpy(brand,the_brand);。 (2)主要考查考生对动态分配的掌握情况,参考 brand指针的复制过程完成该语句,先给指针 number分配空间,通过 new来完成:number=new charstrlen(the_number)+1;。 (3)主要考查考生对成员函数的掌握,由程序可知这里要输出的是品牌,因此调用成
28、员函数 theBrand()来输出品牌。 (4)主要考查考生对纯虚函数的掌握,根据纯虚函数的定义:virtual const char*category()const=0;,可知在这里要填写:const return “卡车“;。三、B综合应用题/B(总题数:1,分数:40.00)3.请使用 VC6或使用答题菜单打开考生文件夹 proj3下的工程 proj3,其中使用友元函数访问类的私有数据成员,求出两个数据成员的大于 1的最小公因子。请编写友员函数 FriFun,使其输出结果为: Common denominator is 2 要求:补充编制的内容写在“/*333*”与“/*666*”之间,
29、不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数 writeToFile已经编译为 obj文件,并且在本程序中调用。 /proj3.h class FriFunClass int a, b; public: FriFunClass(int i, int j) a=i; b=j; friend int FriFun (FriFunClass x); /友元函数 ; void writeToFile(const char *); /proj3.cpp #include iostream using namespace std; #include “prj3.h“
30、int FriFun (FriFunClass x) /* 333* /由于函数 FriFun()是类 FriFunClass /* 666* int main ( ) FriFunClass n(10, 20); if (FriFun (n) cout “Common denominator is “FriFun(n)“/n“; else cout “No common denominator./n“; writeToFile(“); return 0; (分数:40.00)_正确答案:(int min=x.ax.b?x.a:x.b; /此处为取出 x.a与 x.b中的最小值 for(int i=2; i=min; i+) /从 i到 min遍历数组 if(x.a% i=0 return-1;)解析:考点 本题考查的是 FriFunClass类,其中涉及构造函数和友元函数。题目要求完成友元函数,在函数体内可以任意调用 FriFunClass类的私有成员。 解析 主要考查考生对友元函数的掌握情况,友元函数可以访问类的私有数据成员,题目要求函数求出两个数据成员的大于 1的最小公因子,从 2开始往上算,因此要同时可以被两个私有成员整除,这里用取余符号完成,取余为 0即为整除。 主要考查考生对友元函数的掌握情况,友元函数可以访问类的私有数据成员。