1、二级 C+机试-58 及答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test36_1,此工程包含一个源程序文件 test36_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为:2源程序文件 test36_1.cpp 清单如下:#includeiostream.hclass amount;class coins/* found */enum units penny, nickel, dime, quarter, half_dollar/* found */class amount
2、;class amount/* found */coins:units money;public:void setm();int germ();void amount:setm()money = coins:dime;int amount:getm()return money;int main ( )amount ob;ob.setm();cout ob.getm()end1;return 0;(分数:30.00)填空项 1:_二、2简单应用题(总题数:1,分数:40.00)2.coutsi/tsj/tend1;(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.使用 V
3、C6 打开考生文件夹下的工程 test36_3。此工程包含一个 test36_3.cpp,其中定义了类CRectangle,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。(1)定义类 CRectangle 的数据成员 width 和 height,它们都是 int 型的数据。请在注释“/*1*”之后添加适当的语句。(2)添加类 CRectangle 的友元函数 duplicate()的声明,其返回值类型和参数类型均为 Crectangle 的,请在注释“/*2*”之后添加适当的语句。(3)完成类 Crectangle 的成员函数 set_values 的定义,把传入的参数 a 和
4、 b 分别赋值为数据成员 width和 height,请在注释“/*3*”之后添加适当的语句。(4)完成派生类 Crectangle 的友元函数 duplicate 的定义,把函数中的临时对象 rectres 的值返回主函数,请在注释“/* 4*”之后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。源程序文件 test36_3.cpp 清单如下:#include iostream.hclass CRectangle/ * 1 *public:void set_values (int, int);int area (void) return (width * heig
5、ht);/ * 2 *;void CRectangle:set_values (int a, int b)/ * 3 *CRectangle duplicate (CRectangle rectparam)CRectangle rectres;rectres.width = rectparam.width*2;rectres.height = rectparam.height*2;/ * 4 *void main ( )CRectangle rect, rectb;rect.set_values (2,3);rectb = duplicate (rect);cout rectb.area()e
6、nd1;(分数:30.00)_二级 C+机试-58 答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test36_1,此工程包含一个源程序文件 test36_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为:2源程序文件 test36_1.cpp 清单如下:#includeiostream.hclass amount;class coins/* found */enum units penny, nickel, dime, quarter, half_dollar/* found
7、*/class amount;class amount/* found */coins:units money;public:void setm();int germ();void amount:setm()money = coins:dime;int amount:getm()return money;int main ( )amount ob;ob.setm();cout ob.getm()end1;return 0;(分数:30.00)填空项 1:_ (正确答案:(1) 错误;enum unitspenny,nickel,dime,quarter,half_dollar正确:enum u
8、nits penny,nickel,dime,quarter,half_dollar;(2) 错误:class amount;正确:friend class amount;(3) 错误:coins:units money;正确:coins:units money;)解析:解析 (1)enum 是枚举类型,units 是本题中该类型的一个变量,后面为枚举表,大括号中为枚举符,枚举变量的值实际上是一个有名字的常量,所以它的定义结束时也需要一个分号;(2)类 amount 是类 coins 中定义的成员,要是想要访问 coins 类中的私有成员,把 amount 定义成普通的数据成员是不能做到的,只
9、能把它定义成友元类,即可以访问 coins 类中的所有成员(提示:units 是使用的默认定义,即私有成员);(3)units 是类 coins 中的成员,在它的友元类中访问它时应该使用作用域符“:”,本题的错误是使用了冒号。二、2简单应用题(总题数:1,分数:40.00)2.coutsi/tsj/tend1;(分数:40.00)_正确答案:(int prim(int num)int half,flag;flag =0;half=num/2;for (int i=2;i=half;i+)if(num%i=O)flag=1;elsecontinue;if (flag=1)return 1;els
10、ereturn 0;)解析:解析 本题考查的是考生对一般应用的综合考查,主要是对于 for 函数使用的应用。其基本算法如下:从 2 开始到该数的一半进行穷举,每个数都对参数 nam 进行整除,如果发现有任何一个数能够整除num,则标志变量 flag 变为 1,最后返回的时候,返回值根据标志 flag 分别返回能整除 flag 为 1,返回“不能整除 flag 为 0,返回 0。三、3综合应用题(总题数:1,分数:30.00)3.使用 VC6 打开考生文件夹下的工程 test36_3。此工程包含一个 test36_3.cpp,其中定义了类CRectangle,但该类的定义并不完整。请按要求完成下
11、列操作,将程序补充完整。(1)定义类 CRectangle 的数据成员 width 和 height,它们都是 int 型的数据。请在注释“/*1*”之后添加适当的语句。(2)添加类 CRectangle 的友元函数 duplicate()的声明,其返回值类型和参数类型均为 Crectangle 的,请在注释“/*2*”之后添加适当的语句。(3)完成类 Crectangle 的成员函数 set_values 的定义,把传入的参数 a 和 b 分别赋值为数据成员 width和 height,请在注释“/*3*”之后添加适当的语句。(4)完成派生类 Crectangle 的友元函数 duplica
12、te 的定义,把函数中的临时对象 rectres 的值返回主函数,请在注释“/* 4*”之后添加适当的语句。注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。源程序文件 test36_3.cpp 清单如下:#include iostream.hclass CRectangle/ * 1 *public:void set_values (int, int);int area (void) return (width * height);/ * 2 *;void CRectangle:set_values (int a, int b)/ * 3 *CRectangle duplicate
13、 (CRectangle rectparam)CRectangle rectres;rectres.width = rectparam.width*2;rectres.height = rectparam.height*2;/ * 4 *void main ( )CRectangle rect, rectb;rect.set_values (2,3);rectb = duplicate (rect);cout rectb.area()end1;(分数:30.00)_正确答案:(1)int width,height;(2)friend CRectangle duplicate(CRectangle);(3)widtha;heightb;(4)return(rectres);)解析:解析 主要考查考生对于类的定义和友元函数的掌握,其中(2)中 friend 是定义友元的关键字,注意声明之后的分号不能缺少(4)中从子函数中返回应该使用关键字 return,只要符合返回类型的要求就可以直接使用 return 返回。