在下列的程序的横线处填上适当的语句,使该程序的输出为12。 #include<iostream> using namespace std; class TestClass { public: int a,b

admin2010-06-06  25

问题 在下列的程序的横线处填上适当的语句,使该程序的输出为12。
   #include<iostream>
    using namespace std;
    class TestClass
    {
     public:
     int a,b;
     TestClass(int i,int j)
     {
     a=i;
     b=j;
     }
   };
   class TestClass1:public TestClass
   {
     int a;
   public:
      TestClass1(int x):TestClass(x,x+1){}
     void show()
     {
       ______;//输出基类数据成员a的值?
     cout<<b<<endl;
     }
   };
   int main()
   {
      TestClass1 d(1);
   d.show();
   return 0;
   }

选项

答案cout<<TestClass::a

解析 题目中程序TestClass为基类,TestClass1为派生类,在主函数中定义TestClass1对象d(1)。根据题目要求“输出基类数据成员a的值”,基类为TestClass,利用::域运算符取其成员a的值。
转载请注明原文地址:https://kaotiyun.com/show/Zejp777K
0

相关试题推荐
最新回复(0)