使用VC6打开考生文件夹下的工程test20_1,此工程包含一个源程序文件test21_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
(1,2)
5,6
(6,9)
源程序文件test20_1.cpp清单如下:
#include<iostream.h>
class A
public:
A(int i,int j) a=i; b=j;
/**************** found *******************/
void Move( int x, iht y) a+=x;b+=y
void Show() cout <<"("<<a<<","<<b<<")"<<end1;
private :
int a,b;
;
class B:private A
public:
/**************** found *******************/
B(int i,int j,int k, int 1): (i,j) x=k;y=1;
void Show () cout<<x<<", "<<y<<end1;
void fun() Move(3,5);
/**************** found *******************/
void f1() Show();
private:
int x,y;
;
void main ()
A e(1,2);
e. Show ( );
B d(3,4,5,6);
d. fun();
d. Show ( );
d.f1();
参考答案:
(A)错误:void Move(int x,int y){a+=x;b+=y}
正确:void Move(int x,int y){a+=x;b+=y;}
(B)错误:B(int i,int j,int k,intA):(i,j)(x=k;y=A;}
正确:B(in i,int j,int k,int A):A(i,j){x=k;y=A;}
(C)错误:void fA(){Show();}
正确:viid fA(){A::Show();}
解析:
(1)主要考查考生对于表达式定义规则的掌握,此处缺少一个“;”,任何表达式都应该以“;”作为结束标志;
(2)主要考查考生对于派生类构造函数定义的掌握,参数列表中基类的成员应使用基类构造函数初始化,所以必须向基类传递参数,传递时直接使用基类名;
(3)主要考查考生对于基类函数调用方法的掌握,为了调用基类的函数应该使用作用域符“::”以限定访问的位置。