1、二级 C+机试 27 及答案解析(总分:100.00,做题时间:90 分钟)一、B1改错题/B(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test39_1,此工程包含一个源程序文件 test39_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为; This is static a: 1 This is non-static b: 1 This is static a: 2 This is non-static b: 2 This is static a: 2 This is non-static b: 1 Press any key to
2、continue 源程序文件 test39_1.cpp 清单如下; #includeiostream.h class shared static int a; int b; public: /* found */ void set(int i=0, int j) a=i; b=j; void show(); ; /* found */ void shared:show() cout “This is static a: “ a; cout “/nThis is non-static b: “ b; /* found */ cout “/n“; void main () shared x, y;
3、 x.set (1, 1); x.show ( ); y.set (2, 2); y.show ( ); x.show ( ); (分数:30.00)二、B2简单应用题/B(总题数:1,分数:40.00)2.请编写函数 fun(),该函数的功能是将 M 行 N 列的二维数组中的数据,按列的顺序依次放到一维数组中。例如:二维数组中的数据为 33333333 44444444 55555555 则一维数组中的内容应是 334455334455334455334455。 注意:部分源程序以存在文件 test_2.cpp 中。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括
4、号中填入所编写的若干语句。 文件 test39_2.cpp 的内容如下:#includestdio.h #includeiostream.h void fun(int(*s) 10,int *b, int *n,int mm,int nn) void main( ) int w1010=33,33,33,33,44,44,44,44,55,55,55,55,i,j; int a100=0, n=0; cout“The matrix:/n“ for(i=0; i3; i+) for(j=0; j4; j+ coutwi j; coutendl; fun(w, a, cout“The A arra
5、y:/n“; for(i=0; in; i+) coutai; cout“/n/n“; (分数:40.00)_三、B3综合应用题/B(总题数:1,分数:30.00)3.使用 VC6 打开考生文件夹下的工程 test39_3。此工程包含一个 test39_3.cpp,其中定义了类 ARRAY 和其派生类 STUDENT,其中类 ARRAY 是一个多态数组,该数组可以存放学校中的各类人员,如学生等,但它们的定义并不完整。请按要求完成下列操作,将程序补充完整。 (1)定义类 ARRAY 的三个公有成员纯虚函数 add(int a)、del(int d)和 show(int s),它们的返回值类型都是
6、 void 型的。请在注释“/*1*”之后添加适当的语句。 (2)添加派生类 STUDENT 的保护数据成员 float 型的二维数组 table,使其有 row 行和 s_col 列,请在注释“/*2*”。之后添加适当的语句。 (3)完成派生类 STUDENT 的成员函数del(int s)的定义,实现把二维数组第 s 行删除的功能,即把该行清零,请在注释“/*3*”之后添加适当的语句。 (4)完成派生类 STUDENT 的成员函数 show(int s)的定义,使其以格式“跳格 table行号列号is_换行”依次输出 s 行的各个元素,请在注释“/*4*”之后添加适当的语句。 源程序文件t
7、est39_3.cpp 清单如下: #include iostream.h #include stdlib.h const int row = 100; const int s_col = 3; class ARRAY public: / * 1 * class STUDENT:public ARRAY public: virtual void add(int s) cout“/tInput data of STUDENT.“endl; cout“/tFirst:“; cintables 0; cout“/tSecond:“; cintables 1; cout“/tThird:“; cint
8、ables 2; virtual void del(int s); virtual void show(int s); protected: int i; / * 2 * ; void STUDENT:del(int s) / * 3 * void STUDENT:show(int s) coutendl; for(i=0;i=s_col-1;i+) / * 4 * int main() STUDENT st; ARRAY* ap; ap= ap-add(3); ap-show(3); ap-del(3); ap-show(3); return 0; (分数:30.00)_二级 C+机试 27
9、 答案解析(总分:100.00,做题时间:90 分钟)一、B1改错题/B(总题数:1,分数:30.00)1.使用 VC6 打开考生文件夹下的工程 test39_1,此工程包含一个源程序文件 test39_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为; This is static a: 1 This is non-static b: 1 This is static a: 2 This is non-static b: 2 This is static a: 2 This is non-static b: 1 Press any key to continue 源程
10、序文件 test39_1.cpp 清单如下; #includeiostream.h class shared static int a; int b; public: /* found */ void set(int i=0, int j) a=i; b=j; void show(); ; /* found */ void shared:show() cout “This is static a: “ a; cout “/nThis is non-static b: “ b; /* found */ cout “/n“; void main () shared x, y; x.set (1,
11、1); x.show ( ); y.set (2, 2); y.show ( ); x.show ( ); (分数:30.00)解析:(1) 错误:void set(int i=0,int j)a=i;b=j; 正确:void set(int i,int j)a=i;b=j; (2) 错误:缺少对静态成员的初始化 正确:int shared:a; (3) 错误:cout“/n“; 正确:cout“/n“: 解析 (1)主要考查考生对于类的参数初始化的理解,类的成员函数的参数在函数声明中不能进行初始化,只能在函数体内部进行,这是 C+的规定: (2)主要考查考生是否掌握了静态成员的定义,静态成员
12、是该类共有的数据成员,所有该类的对象共同拥有,它必须在类的内部声明,类的外部进行初始化之后才能使用; (3)主要考查考生对于输出流语句的掌握,“”运算符是 C+的 iostream类重载的输出符号,而“”运算符是 C+的 iostream 类重载的输入符号。二、B2简单应用题/B(总题数:1,分数:40.00)2.请编写函数 fun(),该函数的功能是将 M 行 N 列的二维数组中的数据,按列的顺序依次放到一维数组中。例如:二维数组中的数据为 33333333 44444444 55555555 则一维数组中的内容应是 334455334455334455334455。 注意:部分源程序以存在
13、文件 test_2.cpp 中。 请勿改动主函数 main 和其他函数中的任何内容,仅在函数 fun 的花括号中填入所编写的若干语句。 文件 test39_2.cpp 的内容如下:#includestdio.h #includeiostream.h void fun(int(*s) 10,int *b, int *n,int mm,int nn) void main( ) int w1010=33,33,33,33,44,44,44,44,55,55,55,55,i,j; int a100=0, n=0; cout“The matrix:/n“ for(i=0; i3; i+) for(j=0
14、; j4; j+ coutwi j; coutendl; fun(w, a, cout“The A array:/n“; for(i=0; in; i+) coutai; cout“/n/n“; (分数:40.00)_正确答案:()解析:void fun (int(*s)10,int *b, int *n, int mm, int nn) int i,j; for(j=0;jnn;j+) for(i=0;imm;i+) b*n=*(*(s+i)+j);*n=*n+1; 解析 一个二维数组的存储可以理解为按行进行存储的个维数组,但本题中的二维数组要求按列进行存储。根据在主函数中的调用情况,可以看
15、出,指针数组 s10实质上是用来存放二维数组中各行的首地址,b 是用来存放最终二维数组按列处理完毕后的一个一维数组,第 3 个参数之所以要用“ const int s_col = 3; class ARRAY public: / * 1 * class STUDENT:public ARRAY public: virtual void add(int s) cout“/tInput data of STUDENT.“endl; cout“/tFirst:“; cintables 0; cout“/tSecond:“; cintables 1; cout“/tThird:“; cintables
16、 2; virtual void del(int s); virtual void show(int s); protected: int i; / * 2 * ; void STUDENT:del(int s) / * 3 * void STUDENT:show(int s) coutendl; for(i=0;i=s_col-1;i+) / * 4 * int main() STUDENT st; ARRAY* ap; ap= ap-add(3); ap-show(3); ap-del(3); ap-show(3); return 0; (分数:30.00)_正确答案:()解析:(1) v
17、irtual void add(int a)=0; (2) float tablerow s_co1; (3) for(i=0;i=s_co1-1;i+) tablesi=0; (4) cout“/ttable“s“i“is“tablesiendl; 解析 主要考查考生对于带纯虚函数类的定义及其派生类使用的掌握,其中(1)中 virtual 关键字是定义虚函数必不可少的,而定义纯虚函数应该在陔函数的后面加上“=0”,纯虚函数是在该类中只有声明,没有具体定义的特殊函数,是为了方便其派生类进行扩展而定义的“根”,派生类可以根据不同的需要进行定义,达到了数据封装的目的,(3)中二维数组的一行相当于一个一维数组,对其进行操作使用一个 for 循环即可,(4)中注意连续输出时字符流的使用。