1、二级 C+-40及答案解析(总分:32.00,做题时间:90 分钟)一、B1改错题/B(总题数:1,分数:30.00)使用 VC+6.0打开考生文件夹下的源程序文件 1.cpp,该程序运行时有错,请改正程序中的错误,使程序输出的结果为Number=7Number=12注意:错误的语句在/*error*的下面,修改该语句即可。试题程序:#includeiostream.hclass TCpublic:/*error*TC(int i):Number=i/*error*return Number;void set(int i)Number=i;void display()cout“Number=“
2、Numberend1;private:int Number;void main()/*error*TC *p=new TC;pdisplay();pset(12);pdisplay();return;(分数:30.00)填空项 1:_填空项 1:_填空项 1:_二、B2简单应用题/B(总题数:1,分数:1.00)1.请编写一个函数 printdate(int year,int month,int day),该函数实现将输入的 3个数字转换成英语数字纪年输出的功能,如输入 March9,1978,则输出 1978 3 9。注意:使用 switch结构实现该函数的基本功能并应该能够判断错误的输入。
3、部分源程序已存在文件 test40_2.cpp中。请勿修改主函数 main和其他函数中的任何内容,仅在函数 printdate的花括号中填写若干语句。 源程序文件 rest40_2.cpp清单如下: #includeiostream.h void printdate(int year, int month, int day) void main() printdate(1978,3,9); (分数:1.00)_三、B3综合应用题/B(总题数:1,分数:1.00)2.使用 VC6打开考生文件夹下的工程 test21_3,此工程包含一个源程序文件 test21_3.cpp,其中定义了用于表示长方形
4、的类 CRectangle,但类 CRectangle的定义并不完整。请按要求完成下列操作,将类CRectangle的定义补充完整。 (1)定义 CRectangle的构造函数,函数含参数 dx,dy,da 和 db,它们都是 double型的数据,请将类数据成员 x,y, a 和 b初始化,并输出“CRectangle Constructed.”(另起一行输出该文字)。请在注释“/*1*之后添加适当的语句。 (2)完成类 CRectangle的成员函数getperimeter()的定义,将以 a和 b为边的矩形周长的值返回,请在注释“/*2*”之后添加适当的语句。 (3)完成类 CRecta
5、ngle的成员函数 getarea()的定义,将以 a和 b为边的矩形面积的值返回,请在注释“/*3*”之后添加适当的语句。 (4)完成类 CRectangle的友元函数 friend double dist(CRectangle double y; double a; double b; public: CRectangle() cout“/nCRectangle Constructed.“endl; CRectangle(double dx, double dy, double da, double db) /*1* a=da; b=db; cout“/nCRectangle Constr
6、ucted.“endl; CRectangle ( ) cout“CRectangle Destructed.“endl; void putxy(double dx, double dy) x=dx; y=dy; void putab(double da, double db)( a=da; b=db; double getx() return x; double gety() return y; double geta() return a; double getb() return b; double getperimeter() /*2* double getarea() /*3* fr
7、iend double dist(CRectangle ; double dist(CRectangle return sqrt(tx*tx+ty*ty); void main() CRectangle rect; rect.putxy(100.0, 50.0); rect.putab(1200.0, 700.0); cout“Down_Left corner point is: (“rect.getx() “, “rect.gety()“)“ endl; cout“a= “rect.geta()“, b=“rect.getb() endl; cout“Perimeter of this re
8、ctangle is: “rect.getperimeter()endl; cout“Area of this rectangle is:“rect.getarea()endl; cout“The Distance is:“dist(rect)endl; CRectangle recta(200,150,2000,800); cout“Down_Left corner point is:(“recta.getx()“,“recta.gety()“)“endl; cout“a=“recta.geta()“, b=“recta.getb()endl; cout“Perimeter of this
9、rectangle is: “recta.getperimeter()endl; cout“Area of this rectangle is: “recta.getarea()endl; cout“The Distance is :“dist(recta)endl; (分数:1.00)_二级 C+-40答案解析(总分:32.00,做题时间:90 分钟)一、B1改错题/B(总题数:1,分数:30.00)使用 VC+6.0打开考生文件夹下的源程序文件 1.cpp,该程序运行时有错,请改正程序中的错误,使程序输出的结果为Number=7Number=12注意:错误的语句在/*error*的下面,修
10、改该语句即可。试题程序:#includeiostream.hclass TCpublic:/*error*TC(int i):Number=i/*error*return Number;void set(int i)Number=i;void display()cout“Number=“Numberend1;private:int Number;void main()/*error*TC *p=new TC;pdisplay();pset(12);pdisplay();return;(分数:30.00)填空项 1:_ (正确答案:应改为“TC(int i):Number(i)”。)解析:填空项
11、 1:_ (正确答案:应删除“return Number;”。)解析:填空项 1:_ (正确答案:应改为“TC *p=new TC(7);”。)解析:解析 本题第 1处是声明构造函数,并使用初始化列表完成成员变量的初始化,可知第 1处的初始化列表错误,正确的应该是“TC(im i):Number(i)”。构造函数不能有返回值,不能用 return来返回值,故第 2处应将“return Number;”删除。类实例在不指定构造函数的情况下,调用的是默认无参数的构造函数,此时成员变量 Number是不确定的,在定义对象时,应使用已定义的构造函数,根据输出结果可知,P 指向的对象的 Number初始
12、化值为 7,故第 3处应改为“TC *p=new TC(7);”。二、B2简单应用题/B(总题数:1,分数:1.00)1.请编写一个函数 printdate(int year,int month,int day),该函数实现将输入的 3个数字转换成英语数字纪年输出的功能,如输入 March9,1978,则输出 1978 3 9。注意:使用 switch结构实现该函数的基本功能并应该能够判断错误的输入。部分源程序已存在文件 test40_2.cpp中。请勿修改主函数 main和其他函数中的任何内容,仅在函数 printdate的花括号中填写若干语句。 源程序文件 rest40_2.cpp清单如下
13、: #includeiostream.h void printdate(int year, int month, int day) void main() printdate(1978,3,9); (分数:1.00)_正确答案:(void printdate(int year, int month, int day) if(year0|month1|month12|day1|day31) cout“ERROR“; return; switch(month) case 1:cout“January“;break; case 2:cout“February“;break; case 3:cout“
14、March“;break; case 4:eout“April“;break; case 5:cout“May“;break; case 6:cout“June“;break; case 7:cout“July“;break; case 8:cout“Auguest“;break; case 9:cout“September“;break; case 10:cout“October“;break; case 11:cout“November“;break; case 12:cout“December“;break; cout“ “day“,“yearendl; )解析:解析 本题考查的是考生对
15、 switch结构的应用。switch 分支结构也是常用的选择结构,对于每个 case结构,只有遇到 break才会中止并且跳出 switch结构,否则会一直执行到下一个 break或者switch的结尾,而对于参数的预处理应该是程序健壮性的基本要求。三、B3综合应用题/B(总题数:1,分数:1.00)2.使用 VC6打开考生文件夹下的工程 test21_3,此工程包含一个源程序文件 test21_3.cpp,其中定义了用于表示长方形的类 CRectangle,但类 CRectangle的定义并不完整。请按要求完成下列操作,将类CRectangle的定义补充完整。 (1)定义 CRectang
16、le的构造函数,函数含参数 dx,dy,da 和 db,它们都是 double型的数据,请将类数据成员 x,y, a 和 b初始化,并输出“CRectangle Constructed.”(另起一行输出该文字)。请在注释“/*1*之后添加适当的语句。 (2)完成类 CRectangle的成员函数getperimeter()的定义,将以 a和 b为边的矩形周长的值返回,请在注释“/*2*”之后添加适当的语句。 (3)完成类 CRectangle的成员函数 getarea()的定义,将以 a和 b为边的矩形面积的值返回,请在注释“/*3*”之后添加适当的语句。 (4)完成类 CRectangle的
17、友元函数 friend double dist(CRectangle double y; double a; double b; public: CRectangle() cout“/nCRectangle Constructed.“endl; CRectangle(double dx, double dy, double da, double db) /*1* a=da; b=db; cout“/nCRectangle Constructed.“endl; CRectangle ( ) cout“CRectangle Destructed.“endl; void putxy(double d
18、x, double dy) x=dx; y=dy; void putab(double da, double db)( a=da; b=db; double getx() return x; double gety() return y; double geta() return a; double getb() return b; double getperimeter() /*2* double getarea() /*3* friend double dist(CRectangle ; double dist(CRectangle return sqrt(tx*tx+ty*ty); vo
19、id main() CRectangle rect; rect.putxy(100.0, 50.0); rect.putab(1200.0, 700.0); cout“Down_Left corner point is: (“rect.getx() “, “rect.gety()“)“ endl; cout“a= “rect.geta()“, b=“rect.getb() endl; cout“Perimeter of this rectangle is: “rect.getperimeter()endl; cout“Area of this rectangle is:“rect.getare
20、a()endl; cout“The Distance is:“dist(rect)endl; CRectangle recta(200,150,2000,800); cout“Down_Left corner point is:(“recta.getx()“,“recta.gety()“)“endl; cout“a=“recta.geta()“, b=“recta.getb()endl; cout“Perimeter of this rectangle is: “recta.getperimeter()endl; cout“Area of this rectangle is: “recta.getarea()endl; cout“The Distance is :“dist(recta)endl; (分数:1.00)_正确答案:(1) x=dx; y=dy; (2) return2*(a+b); (3) return a*b; (4) double tx,ty; tx=rt.x+rt.a/2.0;)解析:解析 本题主要考查考生对于类的定义和友元函数的定义的理解。注意(4)中使用了求开平方的数学函数 sqrt。