使用VC6打开考生文件夹下的工程test26_1,此工程包含一个源程序文件test26_1.cpp,但该程序运行有问题,请改正 函数中的错误,使该程序的输出结果为: Values are: 1,2 and 3 源程序文件test26_1.cp

admin2010-02-08  13

问题 使用VC6打开考生文件夹下的工程test26_1,此工程包含一个源程序文件test26_1.cpp,但该程序运行有问题,请改正
函数中的错误,使该程序的输出结果为:
   Values are: 1,2 and 3
   源程序文件test26_1.cpp清单如下;
       #include <iostream.h>
       class CommonBase
       {
       public:
         int x;
       };
       /*****************found*****************/
       class DeriveCommonA::public CommonBase
       {
       public:
         int y;
       };
       class DeriveCommonB:public CommonBase
       {
       public:
         int z;
       };
       /*****************found*****************/
       class Overlapping:public DeriveCommonA; public DeriveCommonB
       {
       public:
         void Display()
         {
            cout<<"Values are: "<<DeriveCommonA::x<<", "<<y<<" and "<<z<<end1;
         }
       };
       int main ( )
       {
          Overlapping ov;
       /*****************found*****************/
          ov.x=1;
          ov.y=2;
          ov.z=3;
          ov.Display();
          return 0;
       }

选项

答案(1)错误:class DeriveCommonA::public CommonBase 正确:class DeriveCommonA:public CommonBase (2)错误:class Overlapping:public DeriveCommonA;public DeriveCommonB 正确:class Overlapping:public DeriveCommonA,public DeriveCommonB (3)错误:ov.x=1; 正确:ov.DeriveCommonA::x=1;

解析 (1)主要考查考生对于派生类定义的理解,C++规定的继承格式是在类名的后面加冒号,之后是继承方式和继承类的名称,题目中错误的使用了作用域运算符;
(2)主要考查考生是否掌握了多继承的定义,多继承的格式基本上和单继承相同,不过在多个基类之间应该使用逗号分开,题目中错误的使用了分号,分号在C++中是结束标志;
(3)主要考查考生对于派生类的对象访问的掌握,x是类CommonBase的成员,如果不加限制的访问就会产生二义性,编译程序不知道这个x是A类的,还是B类的,所以必须使用作用域限制符“::”,为了解决这个问题可以使用虚基类。
转载请注明原文地址:https://kaotiyun.com/show/U7ID777K
0

最新回复(0)