下列程序的输出结果是______。 include using namespace std; class base { public: in下列程序的输出结果是______。include<iostream>using namespace std;class base{public:int n;base(int x){n=x;}virtual void set(int m){n=m;cout<<n<<'';}};class deriveA:public base{public:deriveA(int x)

题目
下列程序的输出结果是______。 include using namespace std; class base { public: in

下列程序的输出结果是______。

include<iostream>

using namespace std;

class base

{

public:

int n;

base(int x){n=x;}

virtual void set(int m){n=m;cout<<n<<'';}

};

class deriveA:public base

{

public:

deriveA(int x):base(x){}

void set(int m){n+=m;cout<<n<<'';}

};

class deriveB:public base

{

public:

deriveB(int x):base(x){}

void set(int m){n+=m;cout<<n<<'';}

};

int main( )

{

deriveA d1(1);

deriveB.d2(3);

base*pbase;

pbase=&d1;

pbase->set(1);

pbase=&d2;

pbase->set(2);

return 0;

}


相似考题
更多“下列程序的输出结果是______。 include<iostream> using namespace std; class base { public: in ”相关问题
  • 第1题:

    程序的输出结果是【 】。 include using namespace std; class A{ int x; public: A(int

    程序的输出结果是【 】。

    include <iostream>

    using namespace std;

    class A{

    int x;

    public:

    A(int x=1):x(x){cout<<x;}

    };

    void main(){

    A a,b(2),c(3);

    }


    正确答案:123
    123 解析:a对象使用和默认的构造函数,b对象使用2来初始化对象c对象使用3来初始化对象,输出相应的值后,结果变为123。

  • 第2题:

    有如下程序:include using namespace std;class Base{public:Base(){cout<<"BB"; f()

    有如下程序:#include <iostream>using namespace std;class Base{ public: Base(){cout<<"BB"; f(); } Void f(){cout<<"Bf"; }};class Derived: public Base{ public: Derived() { cout<<"DD"; } void f() { cout<<"Df"; }};int main() { Derived d; return 0; }执行上面的程序将输出( )。

    A.BBBfDD

    B.BBDfDDDf

    C.DD

    D.DDBBBf


    正确答案:A

  • 第3题:

    有如下程序: include using namespace std; class BASE { public

    有如下程序: #include<iostream> using namespace std; class BASE { public: ~BASE(){cout<<"BASE";} }; class DERIVED:public BASE { public: ~DERIVED(){cout<<"DERIVED";} }; int main(){DERIVEDx;retum 0;} 执行后的输出结果是

    A.BASE

    B.DERIVED

    C.BASEDERIVED

    D.DERIVEDBASE


    正确答案:D
    解析:本题考查基类析构函数和派生类析构函数的调用次序。

  • 第4题:

    有如下程序:includeusing namespace std;class Base{public:Base(int x=0){cout<

    有如下程序: #include<iostream> using namespace std; class Base{ public: Base(int x=0){cout<<x;} }; class Derived:public Base{ public: Derived(int x=0){cout<<x;} private: Base val; }; int main( ){ Derived d(1); return 0; } 程序的输出结果是

    A.0

    B.1

    C.01

    D.001


    正确答案:D
    解析:派生对象在创建时先调用基类的构造函数,然后调用派生类的构造函数;撤销对象时,先调用派生类的构造函数,然后调用基类的构造函数。当类中出现其他类对象时,在初始化时先调用该对象的类的构造函数创建该对象。

  • 第5题:

    有以下程序:include using namespace std; class Base { public: Base() { K=0; } int

    有以下程序:

    include<iostream>

    using namespace std;

    class Base

    {

    public:

    Base()

    {

    K=0;

    }

    int x;

    };

    class Derivedl:virtual public Base

    {

    public:

    Derivedl()

    {

    x=10;

    }

    };

    class Derived2:virtua1 public Base


    正确答案:20。
    20。 解析: 本题中,虽然Derived1和Derived2由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derived1中修改,还是在类Derived2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derived obi;”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derived1的构造函数使得x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。

  • 第6题:

    有如下程序:include using namespace std;class BASE{public:~BASE(){cout<<"BASE";}

    有如下程序: #include <iostream> using namespace std; class BASE{ public: ~BASE(){cout<<"BASE";} }; class DERIVED:public BASE{ public: ~DERIVED(){cout<<"DERIVED";} }; int main(){DERIVED x;return 0;} 执行后的输出结果是

    A.BASE

    B.DERIVED

    C.BASEDERIVED

    D.DERIVEDBASE


    正确答案:D
    解析:本题考核派生类的定义和使用。当对象被删除时,派生类的析构函数就被执行。由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反。所以当main函数结束时,派生类DERIVED的对象x将被删除,那么派生类DERIVED的析构函数先被调用,输出DERIVED,然后调用基类的析构函数输出BASE。

  • 第7题:

    下面程序的输出结果是【】。include using namespace std; class base { protected: int

    下面程序的输出结果是【 】。

    include <iostream>

    using namespace std;

    class base

    {

    protected:

    int a;

    public:

    base(){cout<<"0":}

    };

    class basel: virtual public base

    {

    public:

    base1(){ cout<<"1";}

    };

    class base2 : virtual public base

    {

    public:

    base2(){cout<<"2";}

    };

    class derived : public base1,public base2

    {

    public:

    derived () {cout<<"3"; }

    }

    int main ()

    {

    derived obj;

    cout<<end1;

    return 0;

    }


    正确答案:0123
    0123 解析:本题考核含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最有调用派生类自己的构造函数.题中先调用base的构造函数,然后调用base1、base2的构造函数,最后调用derived的构造函数。

  • 第8题:

    下列程序的输出结果为2,请将程序补充完整。 include using namespace std; class Base

    下列程序的输出结果为2,请将程序补充完整。

    include<iostream>

    using namespace std;

    class Base

    {

    public:

    ______void fun(){cout<<1;}

    };

    class Derived:public Base

    {

    public:

    void fun(){cout<<2;}

    };

    int main()

    {

    Base*p=new Derived;

    p->fur();

    delete p;

    return 0;

    }


    正确答案:virtual
    virtual 解析:本题考核虚函数的概念。在C++中,一个基类指针(或引用)可以用于指向它的派生类对象,而且通过这样的指针(或引用)调用虚函数时,被调用的是该指针(或引用)实际指向的对象类的那个重定义版本,这样的调用称为多态调用。基类Base和派生类Derived中都定义了成员函数fun,但是有不同的实现。程序最后输出的结果为2,表明通过对象指针p调用的函数版本为派生类中定义的,只有把基类的fun函数定义为虚函数,才能满足要求。

  • 第9题:

    下面程序的打印结果是【】。 include using namespace std; class Base { public:Base(i

    下面程序的打印结果是【 】。

    include <iostream>

    using namespace std;

    class Base

    {

    public:

    Base(int x)

    {

    a=x;

    }

    void show()

    {

    cout<<a;

    }

    private:

    int a;

    };

    class Derived : public Base

    {

    public:

    Derived(int i) :Base(i+1) ,b(i) { }

    void show()

    {

    cout<<b;

    }

    private:

    int b;

    };

    int main ( )

    {

    Base b(5) , *pb;

    Derived d(1);

    pb=&d;

    pb->show();

    return 0;

    }


    正确答案:2
    2 解析:基类Base派生出派生类Derived,在主函数中,定义了基类对象b,基类指针pb,以及派生类对象d,并让基类指针pb指向派生类对象乙在C++中,当派生类的对象赋值给基类对象时,只能使用派生类对象中从基类继承的成员。所以最后执行语句“pb->show();”是调用基类的成员函数show(),输出a的值2。

  • 第10题:

    若有以下程序:include using namespace std; class Base public: Base() { x=0;} in

    若有以下程序: #include <iostream> using namespace std; class Base public: Base() { x=0; } int x; }; class Derivedl: virtual public Base public: Derived1() { x=10; } }; class Derived2: virtual public Base publici Derived2() x=20; }; class Derived :public Derived1,protected Derived2 {}; int main() Derived obj; cout<<obj.x<<end1; return 0; } 该程序运行后的输出结果是

    A.20

    B.30

    C.10

    D.0


    正确答案:A
    解析:本题考核虚基类的应用。本题中,虽然Derived1和Derived2都是由共同的基类x派生而来的,但山于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derived1修改,还是在类Derived2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derivedobj”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derived1的构造函数使得x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。

  • 第11题:

    下面程序的输出结果是()。include using namespace std;class A {public:A( ) {cout<<

    下面程序的输出结果是( )。 #include <iostream> using namespace std; class A { public: A( ) {cout<<"A";} } class B { public: B() {coat<<"B" ;} } class C: public A { public: B b; C() {cout<<"C";} } void mian(){ C c; }

    A.CBA

    B.ABC

    C.ACB

    D.BCA


    正确答案:B
    解析:先执行基类A构造函数输出A,调用类B的构造函数输出B,调用本身构造函数输出C。

  • 第12题:

    请在下列程序的横线处填写正确的语句。include using namespace std; class Base{ publ

    请在下列程序的横线处填写正确的语句。

    include<iostream>

    using namespace std;

    class Base{

    public:

    void fun(){cout<<"Base fun"<<endl;}

    };

    class Derivde:public Base{

    public:

    void fun(){

    ______∥ 调用基类的函数


    正确答案:Base∷fun()
    Base∷fun() 解析: 此题考查的是派生类对基类成员的访问。本题中派生类Derived覆盖了基类Base中的fun(),如果需要调用基类中的fun(),则需要使用域运算符“∷”。故应填写Base∷fun()。

  • 第13题:

    以下程序的执行结果为______。include using namespace std; class base { public: vir

    以下程序的执行结果为______。

    include<iostream>

    using namespace std;

    class base

    public:

    virtual void who()

    cout<<"base class"<<endl;

    };

    class derivel:public base

    public:

    void who()

    cout<<"d


    正确答案:base class derivel class derive2 class
    base class derivel class derive2 class

  • 第14题:

    有如下程序: include using namespace std; class A { public:

    有如下程序: #include<iostream> using namespace std; class A { public: A(){cout<<"A";} }; class B<public:B(){cout<<"B";)); class C:public A { B b; public: C(){cout<<"C";} }; int main(){Cobj;retum 0;} 执行后的输出结果是

    A.CBA

    B.BAC

    C.ACB

    D.ABC


    正确答案:D
    解析:本题考查基类构造函数,数据成员构造函数的调用次序。系统首先要通过派生类的构造函数调用基类的构造函数,对基类成员初始化:然后对派生类中新增的成员初始化。

  • 第15题:

    如下程序的输出是includeusing namespace std;class Base{public:Base( ){cout<<"BB"

    如下程序的输出是 #include<iostream> using namespace std; class Base{ public: Base( ){cout<<"BB";f( );} void f( ){cout<<"Bf";} }; class Derived:public Base{ public: Derived( ){eout<<"DD";} void f( ){cout<<"Df";} }; int main( ){Derived d;return 0;}

    A.BBBff)D

    B.BBDfDDDf

    C.DD

    D.DDBBBf


    正确答案:A
    解析:当创建派生类对象时,首先调用基类的构造函数,最后调用派生类的构造函数。

  • 第16题:

    下列程序的输出结果为2,请将程序补充完整。 include using namespace std; class Basc

    下列程序的输出结果为2,请将程序补充完整。

    include<iostream>

    using namespace std;

    class Basc

    {

    public:

    【 】void fun(){cout<<1;}

    };

    class Dcrived:public Base

    {

    public:

    void fun(){cout<<2;}

    };

    int main()

    {

    Base*p=new Derived;

    p->fun();

    delete p;

    return 0;

    }


    正确答案:virtual
    virtual 解析:利用虚函数实现多态。

  • 第17题:

    若有以下程序:include using namespace std;class Base{public: Base ( ) {x=0; } in

    若有以下程序: #include <iostream> using namespace std; class Base { public: Base ( ) { x=0; } int x; }; class Derivedl : virtual public Base { public: Derivedl () { x=10; } }; class Derived2 : virtual public Base { public: Derived2 () { x=20; } }; class Derived : public Derivedl,protected Derived2{ }; int main ( ) { Derived obj; cout<<obj .x<<end1; return 0; } 该程序运行后的输出结果是 ( )。

    A.20

    B.30

    C.10

    D.0


    正确答案:A
    解析:本题中,虽然Derivedl和Derived2都是由共同的基类x派生而来的,但由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derived1修改,还是在类Derived2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derivedobj”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derived1的构造函数,使得x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。

  • 第18题:

    有如下程序:includeusing namespace std;class BASE{public:~BASE( ){cout<<"BASE";}

    有如下程序: #include<iostream> using namespace std; class BASE{ public: ~BASE( ){cout<<"BASE";} }; class DERIVED:public BASE{ public: ~DERIVED( ){cout<<"DERIVED";} }; int main( ){DERIVED x;return 0;} 程序的输出结果是

    A.BASE

    B.DERIVED

    C.BASEDERIVED

    D.DERIVEDBASE


    正确答案:D
    解析:基类和派生类的析构函数的执行顺序是先执行派生类的析构函数,最后执行基类的析构函数;故先执行DIVERED的析构函数,后执行BASE的析构函数。

  • 第19题:

    执行如下程序将输出():includeusing namespace std;class Base{public:Base(){cout<<

    执行如下程序将输出( ): #include<iostream> using namespace std; class Base { public: Base(){cout<<"BB";fun();} void fun (){tout<<"Brim";} }; class Derived:public Base { public: Derived(){cout<<"DD";} void fun (){cout<<"Dfun";} }; int main(){Derived d;return 0;}

    A.DD

    B.BBDfunDDDfun

    C.BBBfunDD

    D.DDBBBfun


    正确答案:C
    解析:C++中创建一个类的实例时,如果该类存在基类,将首先执行基类的构造函数,然后执行该类本身的构造函数。本题中首先执行类Base的构造函数,所以将先输出“BB”,然后调用基类成员函数fun(),输出“Bfun”,最后调用子类Derived的构造函数输出“DD”。

  • 第20题:

    下面程序的输出结果是【】。 include using namespace std; class A {int a,b; public:A

    下面程序的输出结果是【 】。

    include <iostream>

    using namespace std;

    class A

    {

    int a, b;

    public:

    A()

    {

    a = b = 0;

    }

    A(int aa, int bb ) : a(aA) , b(bB)

    {

    cout <<"a="<<a<<","<<"b="<<b<<",";

    }

    ~A()

    {

    cout<<"D";

    };

    int main ( )

    {

    A x, y(2, 3);

    return 0;

    }


    正确答案:a=2b=3DD
    a=2,b=3DD 解析:本题主要考核构造函数与析构函数的应用。主函数中定义 A类对象x时无输出,定义对象y时调用构造函数输出a=2,b=3。在主函数结束前,对象x,y都调用各自的析构函数输出DD。所以最后答案为a=2,b=3DD。

  • 第21题:

    有以下程序:include include using namespace std;class base{private: cha

    有以下程序: #include <iostream> #include <string> using namespace std; class base { private: char baseName[10]; public: base ( ) { strcpy (baseName, "Base"); } virtual char *myName() {

    A.DerivedBase

    B.BaseBase

    C.DerivedDerived

    D.BaseDerived


    正确答案:A
    解析:本题考核虚函数的应用。类Derived是从基类Base公有派生而来的。因此,Derived是基类Base的子类型。主函数中定义了一个基类对象bb和一个派生类对象dd。从程序中可看出,派生类Derived的对象dd交给了处理基类Base的对象的函数showPtr进行处理。由于在基类中函数myName被定义成虚函数,所以在函数showPtr中调用的myName函数为派生类的成员函数mySame,从而输出Derived。然后输出className,即基类名称Base。

  • 第22题:

    有以下程序include using namespace std:class Base{private:char c;public:Base(cha

    有以下程序#include <iostream>using namespace std:class Base{private: char c;public: Base(char n) :c(n) {} ~Base ( ) { cout<<c; }}; class Derived : public Base{private: char c;public: Derived(char n):Base (n+1),c(n) {} ~Derived() { cout<<c; }};int main(){ Derived obj('x'); return 0;} 执行后的输出结果是

    A.xy

    B.yx

    C.x

    D.y


    正确答案:A
    解析:本题考核继承与派生中继承基类的数据成员与成员函数。在C++中,由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反.在此题的程序中,在主函数main结束时,派生类Derived对象obj将被删除,所以就会调用对象的析构函数。先调用派生类的析构函数,输出x,然后调用基类的析构函数,输出y。

  • 第23题:

    有如下程序:includeusing namespace std;class BASE{public:~BASE(){cout<<"BASE";}}

    有如下程序: #include<iostream> using namespace std; class BASE{ public: ~BASE(){cout<<"BASE";} }; class DERIVED:public BASE{ public: ~DERIVED(){cout<<"DERIVED";} }; int main(){DERIVED x;return 0;} 执行后的输出结果是( )。

    A.BASE

    B.DERIVED

    C.BASEDERIVED

    D.DERIVEDBASE


    正确答案:D
    解析:此题考查的是派生类的定义和使用。当对象被删除时,派生类的析构函数就被执行。由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反。

  • 第24题:

    在下面横线上填上适当的语句,完成程序。include using namespace std; class Base { in

    在下面横线上填上适当的语句,完成程序。

    include<iostream>

    using namespace std;

    class Base

    {

    int x;

    public:

    Base(int i){x=i;}

    ~Base(){}

    );

    class Derived:public Base

    {

    public:

    ______//完成类Derive构造函数的定义

    };

    iht main()

    {

    Derived obj


    正确答案:Derived(int i;Base(i){}。
    Derived(int i;Base(i){}。 解析: 程序中,类Derived是基类Base的公有派生。在类Derived的构造函数应该包括调用基类构造函数使基类的数据成员得以初始化,