niusouti.com

下列程序输出结果为: include using namespace std; class TestClass1 { public: Test下列程序输出结果为:include<iostream>using namespace std;class TestClass1{public:TestClass1(){}TestClass1(int i){x1=i;}void dispa(){cout<<"x1="<<x1<<",";}private:int x1;};class TestClass2:public

题目
下列程序输出结果为: include using namespace std; class TestClass1 { public: Test

下列程序输出结果为:

include<iostream>

using namespace std;

class TestClass1

{

public:

TestClass1(){}

TestClass1(int i)

{

x1=i;

}

void dispa()

{

cout<<"x1="<<x1<<",";

}

private:

int x1;

};

class TestClass2:public TestClass1

{

public:

TestClass2(){}

TestClass2(int i):TestClass1(i+10)

{

x2=i;

}

void dispb()

{

dispa();

cout<<"x2="<<x2<<endl;

}

private:

int x2;

};

int main()

{

TestClass2 b(2);

b.dispb();

return 0;

}


相似考题
更多“下列程序输出结果为: include<iostream> using namespace std; class TestClass1 { public: Test ”相关问题
  • 第1题:

    如下程序的输出结果是includeusing namespace std;class Test{public:Test( ){n+=2;}

    如下程序的输出结果是 #include<iostream> using namespace std; class Test{ public: Test( ){n+=2;} ~Test( ){n-=3;} static int getNum( ){return n;} private: static int n; }; int Test::n=1; int main( ){ Test*P=new Test: delete P; cout<<"n="<<Test::getNum( )<<endl; return 0; }

    A.n=0

    B.n=1

    C.n=2

    D. n=3


    正确答案:A
    解析:静态数据成员的初始值n=1,执行Test*p=new Test;,调用构造函数后,n= 3,deletep;调用析构函数,n-=3,所以最终n=0。

  • 第2题:

    请将如下程序补充完整,使得输出结果为:bbaa。 include using naluespace std; class

    请将如下程序补充完整,使得输出结果为:bbaa。

    include<iostream>

    using naluespace std;

    class A{

    public:

    ______{eout<<"aa";}

    };

    class B:public A{

    public:

    ~B( ){eont<<"bb";}

    };

    int ulain( ){

    B*P=new B;

    delete P;

    return 0;

    }


    正确答案:~A( )
    ~A( ) 解析:派生类和基类的析构函数调用顺序是先调用派生类的析构函数,然后调用基类的析构函数,打印“bb”说明已经在调用派生类的析构函数,则要继续打印“aa”,显然就只有定义在基类的析构函数中去打印,故答案为~A( )。

  • 第3题:

    下列程序的输出结果是______。 include using namespace std; class Test( public: Te

    下列程序的输出结果是______。 #include<iostream> using namespace std; class Test( public: Test() {cnt++;} ~Test() {cnt--;} static int Count(){return cnt;} private: static int cnt; }; int Test::cnt=0; int main() { cout<<Test::Count()<<""; Test t1,t2; Test*pT3=new Test; Test*pT4=new Test; cout<<Test::Count()<<""; delete pT4; delete pT3; cout<<Test::Count()<<end1; return 0; }

    A.024

    B.042

    C.420

    D.240


    正确答案:B
    解析:此题考查的是类的构造函数与析构函数的调用。语句 coutTcst::Count()"";;输出“0”,因为static型变量cnt的默认值是0;“T Test t1,t2;Test*pT3=new Test;Test*pT4=new Test;”调用4次类的构造函数,使得cnt的值增加到4,并输出4;然后delete pT4;delete pT3;调用两次析构函数,cnt的值变为2,并输出2。

  • 第4题:

    请将下列程序的横线处补充完整,使得输出结果为bbaaincludeusing namespace std;class

    请将下列程序的横线处补充完整,使得输出结果为bbaa

    include<iostream>

    using namespace std;

    class A{

    public:

    ______{cout<<"aa";}

    };

    class B:public A{

    public:

    ~B(){cout<<"bb";}

    };

    int main(){

    B*p=new


    正确答案:~A()
    ~A() 解析: 此题考查的是类的构造与析构。本题要求输出的结果中包含aa,所以基类A中横线处的函数一定要被执行。横线处应是定义基类的构造或者析构函数,如果定义的是构造,那么aa肯定在new操作时就输出,而下面的bb是在析构函数中,会在delete时被输出。故可以肯定应填入基类A的析构函数定义:~A()。

  • 第5题:

    下列程序的输出结果为2,横线处应添加语句()。includeusing namespace std;{public:___

    下列程序的输出结果为2,横线处应添加语句( )。 #include<iostream> using namespace std; { public: ______void fun(){cout<<1;} }; class TestClass2:public TestClass1 { public:void fun(){cout<<2;) }; int main() { TestClass1 *p=new TestClass2; p->fun(); delete p; Teturn 0; }

    A.public

    B.private

    C.virtual

    D.protected


    正确答案:C
    解析:由主函数main入手,定义TestClass1类的指针对象p指向派生类Testclass2。因为基类和派生类中都有fun函数,题目要求输出为2,就是基类对象访问派生类中fun函数。通过虚函数与指向基类对象的指针变量的配合使用,就能方便调用同名函数。所以这里将基类中的fun函数声明为virtual。并且当一个成员函数被声明为虚函数后,其派生类中的同名函数自动成为虚函数。