使用VC6打开考生文件夹下的工程test39_1,此工程包含一个源程序文件test39_1.cpp,但该程序运行有问题,请改正函数中的错误,使该程序的输出结果为; This is static a: 1 This is non-static b

admin2010-02-08  26

问题 使用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
   源程序文件test39_1.cpp清单如下;
       #include<iostream.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, 1);
             x.show ( );
             y.set (2, 2);
             y.show ( );
             x.show ( );
       }

选项

答案(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)主要考查考生是否掌握了静态成员的定义,静态成员是该类共有的数据成员,所有该类的对象共同拥有,它必须在类的内部声明,类的外部进行初始化之后才能使用;
(3)主要考查考生对于输出流语句的掌握,“>>”运算符是C++的iostream类重载的输出符号,而“<<”运算符是C++的iostream类重载的输入符号。
转载请注明原文地址:https://kaotiyun.com/show/7KID777K
0

最新回复(0)