1、二级 C+机试-77 及答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test42_1,此工程包含一个源程序文件 test42_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为:rect area: 12rectb area: 30源程序文件 test42_1.cpp 清单如下:#include iostream.hclass CRectangle/* found */int *width, height;public:CRectangle (int,int);CRectangl
2、e ();int area (void) return (*width * *height);CRectangle:CRectangle (int a, int b)width = new int;height = new int;/* found */width = a;*height = b;CRectangle:CRectangle ()delete width;delete height;/* found */void main ()CRectangle rect (3,4), rectb (5,6);cout “rect area: “ rect.area() endl;cout “
3、rectb area: “ rectb.area() endl;return 0;(分数:30.00)填空项 1:_二、2简单应用题(总题数:1,分数:40.00)2.请编写一个函数 maxofarray(atype*p,int count),该函数从一个数组中找出其中的最大元素,并且数组中可以存放多种数据类型的元素。注意:部分源程序己存在文件 test42_2.cpp 中。请勿修改主函数 main 和其他函数中的任何内容,仅在函数 maxofarray 的花括号中填写若干语句。文件 test42_2.cpp 清单如下:#includeiostream.h#includestring.h#in
4、cludeconio.htemplateclass atypevoid maxofarray(atype* p,int count)void main ()int len=5;char *p1;cout“the char type array and its length is 5:/n“;cout“the array element is a b c d e/n“;p1=new charlen;for (int i=0;ilen;i+)p1i=a+i;maxofarray(p1,len);(分数:40.00)_三、3综合应用题(总题数:1,分数:30.00)3.使用 VC6 打开考生文件夹下
5、的工程 test42_3。此工程包含个 test42_3.cpp,其中定义了类Cpolygon、COutput 和 CTriangle,其中 CTriangle 类由 Cpolygon 和 COutput 类 public 派生,但三个类的定义并不完整。请按要求完成下列操作,将程序补充完整。(1)定义类 CPolygon 的保护数据成员 width 和 height,它们都是 int 型的数据。请在注释“/*1*”之后添加适当的语句。(2)完成类 CPolygon 的成员函数 set_values(int a,int b),使参数 a 和 b 分别赋值给保护数据成员width 和 height
6、,请在注释“/*2*”之后添加适当的语句。(3)完成类 Coutput 的成员函数 output(int)的定义,将传入的参数为血型的 i 输出到屏幕并换行,请在注释“/*3*”之后添加适当的语句。(4)完成派生类 CTriangle 的声明,它由 Cpolygon 和 COutput 类 public 派生,请在注释“/*4*”之后添加适当的语句。源程序文件 test42_3.cpp 清单如下:#include iostream.hclass CPolygonprotected:/ * 1 *public:void set_values(int a, int b)/ * 2 *;class
7、COutputpublic:void output(int i);void COutput:output(int i)/ * 3 */ * 4 *public:int area (void)return (width * height / 2);int main ()CTriangle trgl;trgl.set_values (4,5);trgl.output (trgl.area();return 0;(分数:30.00)_二级 C+机试-77 答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test
8、42_1,此工程包含一个源程序文件 test42_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为:rect area: 12rectb area: 30源程序文件 test42_1.cpp 清单如下:#include iostream.hclass CRectangle/* found */int *width, height;public:CRectangle (int,int);CRectangle ();int area (void) return (*width * *height);CRectangle:CRectangle (int a, int b)wi
9、dth = new int;height = new int;/* found */width = a;*height = b;CRectangle:CRectangle ()delete width;delete height;/* found */void main ()CRectangle rect (3,4), rectb (5,6);cout “rect area: “ rect.area() endl;cout “rectb area: “ rectb.area() endl;return 0;(分数:30.00)填空项 1:_ (正确答案:(1) 错误:int*width,hei
10、ght;正确:int*width,*height;(2) 错误:width=a;正确:*width=a;(3) 错误:void main()正确;int main())解析:解析(1)根据后面类中构造函数的定义可以看出只有定义成指针的变量才能动态申请空间,所以本题的错误在于没有把变量 height 定义成指针类型;(2)变量 width 是指针类型的变量,直接给它赋值相当于让它指向一个新的地址,而要改变它指向的变量的值,应该使用取内容符号“*”:(3)根据主函数最后的返回值情况,可知该主函数是需要定义成带返回值的函数,本题的错误在于,把主函数定义成了 void,改成 int 即可,考生一定要注
11、意函数定义的返回值和最后实际的返回情况是否一一对应。二、2简单应用题(总题数:1,分数:40.00)2.请编写一个函数 maxofarray(atype*p,int count),该函数从一个数组中找出其中的最大元素,并且数组中可以存放多种数据类型的元素。注意:部分源程序己存在文件 test42_2.cpp 中。请勿修改主函数 main 和其他函数中的任何内容,仅在函数 maxofarray 的花括号中填写若干语句。文件 test42_2.cpp 清单如下:#includeiostream.h#includestring.h#includeconio.htemplateclass atypev
12、oid maxofarray(atype* p,int count)void main ()int len=5;char *p1;cout“the char type array and its length is 5:/n“;cout“the array element is a b c d e/n“;p1=new charlen;for (int i=0;ilen;i+)p1i=a+i;maxofarray(p1,len);(分数:40.00)_正确答案:(void maxofarray(atype*p,int count)for (int j=0;jcount-1;j+)for (int
13、 k=0;kcount-1-j;k+)if(pkpk+1)atype temp;temp=pk;pk=pk+1;pk+1=temp;cout“/nthe max element of this array is: “pcount-1endl;)解析:解析 本题考查的是考生对模板函数和简单的排序方法的综合应用。为了对于任何数据类型都能进行比较,应该使用模板类进行函数的参数的定义,而函数内部则使用了冒泡排序法得到最大的元素,实际上只需要一次两两比较就可以得到正确的答案了,考生可以自己试试看。三、3综合应用题(总题数:1,分数:30.00)3.使用 VC6 打开考生文件夹下的工程 test42_3。
14、此工程包含个 test42_3.cpp,其中定义了类Cpolygon、COutput 和 CTriangle,其中 CTriangle 类由 Cpolygon 和 COutput 类 public 派生,但三个类的定义并不完整。请按要求完成下列操作,将程序补充完整。(1)定义类 CPolygon 的保护数据成员 width 和 height,它们都是 int 型的数据。请在注释“/*1*”之后添加适当的语句。(2)完成类 CPolygon 的成员函数 set_values(int a,int b),使参数 a 和 b 分别赋值给保护数据成员width 和 height,请在注释“/*2*”之后
15、添加适当的语句。(3)完成类 Coutput 的成员函数 output(int)的定义,将传入的参数为血型的 i 输出到屏幕并换行,请在注释“/*3*”之后添加适当的语句。(4)完成派生类 CTriangle 的声明,它由 Cpolygon 和 COutput 类 public 派生,请在注释“/*4*”之后添加适当的语句。源程序文件 test42_3.cpp 清单如下:#include iostream.hclass CPolygonprotected:/ * 1 *public:void set_values(int a, int b)/ * 2 *;class COutputpublic
16、:void output(int i);void COutput:output(int i)/ * 3 */ * 4 *public:int area (void)return (width * height / 2);int main ()CTriangle trgl;trgl.set_values (4,5);trgl.output (trgl.area();return 0;(分数:30.00)_正确答案:(1)int width, height;(2)width=a; height=b;(3)cout i endl;(4)class CTriangle:public CPolygon, public COutput)解析:解析 主要考查考生对于类的定义和派生类声明的掌握,其中(4)中对于多继承的声明按照如下的格式:关键字 class 派生类名:继承方式继承类名,继承方式继承类名,在多继承中,一定要注意防止程序的二义性,同时要善于利用基类中已经定义的方式,才能达到代码复用的作用。