1、二级 C+机试-88 及答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:33.00)1.使用 VC6 打开考生文件夹下的工程 test12_1,此工程包含一个源程序文件 test_12.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下:fun (Sample &p) 1 2fun (Sample *p) 3 420 10源程序文件 test12_1 清单如下:#includeiostream .hclass Sampleprivate:int x,y;static int z;public:Sample(int a,int b) x=a
2、;y=b; void fun(Sample &p);void fun(Sample *p);static void print(Sample s);/* found */int z=10;void Sample:fun(Sample &p)x=p.x;y=p.y;cout“fun(Sample &p)“ “x“ “yendl;void Sample:fun(Sample *p)/* found */x=p.x; y=p.y;cout“fun(Sample *p) “ x“ “yendl;void Sample:print (Sample s)/* found */x=20;couts. x“
3、“zendl;void main()Sample p(1,2),q(3,4);p. fun(p);p. fun(&q);p. print(p);(分数:33.00)_二、2简单应用题(总题数:1,分数:33.00)2.请编写一个函数 int Count(double a,int n),统计出具有 n 个元素的一维数组中大于等于所有元素平均值的元素个数并返回这个值。注意:请使用 for 循环实现该函数。注意:部分源程序已存在文件 test12_2.cpp 中。请勿修改主函数 main 和其他函数中的任何内容,仅在函数 Count 的花括号中填写若干语句。文件 test12_2 的内容如下:#in
4、cludeiostream.hint Count(double a, int n)void main()double a5;cout“请输入 5 个 double 型的数字“endl;for(int i=0;i5;i+)cinai;int result=Count(a,5);cout“大于等于所有元素平均值的元素个数:“resultendl;(分数:33.00)_三、3综合应用题(总题数:1,分数:34.00)3.使用 VC6 打开考生文件夹下的工程 test12_3,此工程包含一个 test12_3.cpp,其中定义了类 Base 和类A,类 A 公有继承 Base,但这两个类的定义都并不完
5、整。请按要求完成下列操作,将程序补充完整。(1)定义枚举类型变量 en,它包含两个枚举符 front 和 back,请在注释“/*1*”之后添加适当的语句。(2)在类 Base 中添加常成员虚函数 void E()的定义,该函数输出“In Base E!”,请在注释“/*2*”之后添加适当的语句。(3)在类 A 中添加常成员虚函数 void E()的定义,该函数先调用基类中的虚函数 E()再输出“In AE!”,请在注释“/* 3*”之后添加适当的语句。(4)完成类 A 构造函数的定义,请使用参数列表的形式初始化类 A 的成员,并输出“A constructor.”,请在注释“/* 4*”之后
6、添加适当的语句。输出结果如下:Base constructor.A constructor.In BaseE!In AE!In BaseP!In A!1A destructor.Base destructor.注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。源程序文件 test12_3.cpp 清单如下:#includeiostream .h/ * 1 *class Baseprotected:int b1;int b2;public:Base();Base();int Getb1()const return b1; void Setb1(int x) b1 = x; int Ge
7、tb2()const return b2; void Setb2(int y) b2 = y; void Print()const cout“In Base P!“end1;/ * 2 *;Base:Base():b1(1),b2(5)cout“Base constructor“endl;Base:Base()cout“Base destructor.“endl;class A:public Baseprotected:en enA;public:A();A();en GetColor()const return enA; void SetColor(en color) enA = color
8、; void InA()cout“In A!“endl;/ * 3 *Base:E();cout“In AE!“endl;/ * 4 *cout“A constructor.“endl;A:A()cout“A destructor.“endl;void main()A a1;a1.E();coutendl;a1.Print();a1InA();couta1.Getbl()endl;(分数:34.00)_二级 C+机试-88 答案解析(总分:100.00,做题时间:90 分钟)一、1改错题(总题数:1,分数:33.00)1.使用 VC6 打开考生文件夹下的工程 test12_1,此工程包含一个源
9、程序文件 test_12.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下:fun (Sample &p) 1 2fun (Sample *p) 3 420 10源程序文件 test12_1 清单如下:#includeiostream .hclass Sampleprivate:int x,y;static int z;public:Sample(int a,int b) x=a;y=b; void fun(Sample &p);void fun(Sample *p);static void print(Sample s);/* found */int z=10;void
10、 Sample:fun(Sample &p)x=p.x;y=p.y;cout“fun(Sample &p)“ “x“ “yendl;void Sample:fun(Sample *p)/* found */x=p.x; y=p.y;cout“fun(Sample *p) “ x“ “yendl;void Sample:print (Sample s)/* found */x=20;couts. x“ “zendl;void main()Sample p(1,2),q(3,4);p. fun(p);p. fun(&q);p. print(p);(分数:33.00)_正确答案:(1)错误:int
11、z=10;正确:int Sample:z=10;(2)错误:x=p.x;y=p.y;正确;x=p-x;y=p-y;(3)错误:x=20;正确;s.x=20;)解析:解析(1)主要考查考生对于静态成员初始化定义的理解,静态成员使用关键字 static 修饰,应对其进行类体外初始化,格式为数据类型“类名:静态变量名:初始值”;(2)主要考查考生对于指针与引用区别的掌握,x 和 y 都是指针类型的变量,应使用“-”调用类的成员;(3)主要考查考生对于静态成员函数的掌握,在静态成员函数中使用非静态成员,需要用对象来引用。二、2简单应用题(总题数:1,分数:33.00)2.请编写一个函数 int Cou
12、nt(double a,int n),统计出具有 n 个元素的一维数组中大于等于所有元素平均值的元素个数并返回这个值。注意:请使用 for 循环实现该函数。注意:部分源程序已存在文件 test12_2.cpp 中。请勿修改主函数 main 和其他函数中的任何内容,仅在函数 Count 的花括号中填写若干语句。文件 test12_2 的内容如下:#includeiostream.hint Count(double a, int n)void main()double a5;cout“请输入 5 个 double 型的数字“endl;for(int i=0;i5;i+)cinai;int resu
13、lt=Count(a,5);cout“大于等于所有元素平均值的元素个数:“resultendl;(分数:33.00)_正确答案:(int Count(double a, int n)double m=0int i;for(i=0;in;i+) m+=ai;m=m/n;int c=0;for(i=0;in;i+)if(ai=m) c+;return c;)解析:解析本题考查的是考生对于一维数组作参数的调和 for 循环语句的熟练应用。一维数组作参数时,形参就是数组实参是数组名加上一对中括号,在函数体内部使用与一般数组完全相同,这也是传值调用的一种,注意最后把计算结果是 return 语句返回。三
14、、3综合应用题(总题数:1,分数:34.00)3.使用 VC6 打开考生文件夹下的工程 test12_3,此工程包含一个 test12_3.cpp,其中定义了类 Base 和类A,类 A 公有继承 Base,但这两个类的定义都并不完整。请按要求完成下列操作,将程序补充完整。(1)定义枚举类型变量 en,它包含两个枚举符 front 和 back,请在注释“/*1*”之后添加适当的语句。(2)在类 Base 中添加常成员虚函数 void E()的定义,该函数输出“In Base E!”,请在注释“/*2*”之后添加适当的语句。(3)在类 A 中添加常成员虚函数 void E()的定义,该函数先调
15、用基类中的虚函数 E()再输出“In AE!”,请在注释“/* 3*”之后添加适当的语句。(4)完成类 A 构造函数的定义,请使用参数列表的形式初始化类 A 的成员,并输出“A constructor.”,请在注释“/* 4*”之后添加适当的语句。输出结果如下:Base constructor.A constructor.In BaseE!In AE!In BaseP!In A!1A destructor.Base destructor.注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。源程序文件 test12_3.cpp 清单如下:#includeiostream .h/ * 1
16、*class Baseprotected:int b1;int b2;public:Base();Base();int Getb1()const return b1; void Setb1(int x) b1 = x; int Getb2()const return b2; void Setb2(int y) b2 = y; void Print()const cout“In Base P!“end1;/ * 2 *;Base:Base():b1(1),b2(5)cout“Base constructor“endl;Base:Base()cout“Base destructor.“endl;c
17、lass A:public Baseprotected:en enA;public:A();A();en GetColor()const return enA; void SetColor(en color) enA = color; void InA()cout“In A!“endl;/ * 3 *Base:E();cout“In AE!“endl;/ * 4 *cout“A constructor.“endl;A:A()cout“A destructor.“endl;void main()A a1;a1.E();coutendl;a1.Print();a1InA();couta1.Getb
18、l()endl;(分数:34.00)_正确答案:(1)enum enfront, back;(2)virtual void E()Constcout“In BaseE!“endl;(3)virtual void E()const(4)A:A():enA(front)解析:解析主要考查考生对于枚举和虚函数的定义的掌握,注意(1)中枚举类型使用 enum 关键字定义,它实际上就是一个有名字的常量,定义格式如下:enum 枚举名枚举表,(2)中常成员函数的定义格式中const 的位置不能随便改动,因为 const 关键字是修饰其右侧文字的,(3)中调用基类函数 E 的方法Base:E()中使用了作用域符。