下列程序的输出结果是非曲直【 】。includeclass base{ int x, y;public: base(int i,下列程序的输出结果是非曲直【 】。include<iostream, h>class base{int x, y;public:base(int i, int j){x=i; y=j;}virtual int add(){return x+ y;}};class three: public base{int z;public:three(int i, int j, int k)

题目
下列程序的输出结果是非曲直【 】。includeclass base{ int x, y;public: base(int i,

下列程序的输出结果是非曲直【 】。

include<iostream, h>

class base

{

int x, y;

public:

base(int i, int j){x=i; y=j;}

virtual int add(){return x+ y;}

};

class three: public base

{

int z;

public:

three(int i, int j, int k) :base(i, j){z=k; }

int add() { return (base:: add()+z); }

};

void main()

{

three * q=new three(lO,20,30);

cout<<q->add()<<end1;

}


相似考题
更多“下列程序的输出结果是非曲直【 】。include<iostream, h>class base{ int x, y;public: base(int i, ”相关问题
  • 第1题:

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

    若有以下程序: #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和Derivec[2都是由共同的基类x派生而来的,但由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derivedl中修改,还是在类Derivect2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derivedobj;”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derivedl的构造函数使得x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。

  • 第2题:

    下列程序段的输出结果是 include void fun(int * X,int * y) {cout < < * X < <

    下列程序段的输出结果是 #include<iostream.h> void fun(int * X,int * y) { cout < < * X < < * y; * X=3; * y=4; } void main( ) { int x=1,y=2; fun(&y,&x); cout < < X < < y < < end1; }

    A.2143

    B.1212

    C.1234

    D.2112


    正确答案:A
    解析:在fun函数中,x接收的是main函数中y的地址,所以*x值为2,同样,*y值为1,所以第1次输出的是21,第2次改变*x的值等同于改变y的值,改变*y的值也即改变x的值,所以第2次输出的是43。注意:C++语言中函数的传参方式中关于指针的应用。

  • 第3题:

    下列程序的输出结果是______。 include class base { int x,y; public: base(int i,i

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

    include<iostream.h>

    class base

    {

    int x,y;

    public:

    base(int i,int j){x=i;y=j;}

    virtual int add( ){return x+y;}

    };

    class three:public base

    {

    int z;

    public:

    three(int i,int j,int k):base(i,j){z=k;)

    int add( ){return(base::add( )+z);}

    };

    void main( )

    {

    three*q=new three(10,20,30);

    cout<<q->add( )<<endl;

    }


    正确答案:60
    60 解析:本题考察继承中子类对父类的继承方式,注意子类的add成员函数,它直接使用了父类的成员函数进行运算。

  • 第4题:

    下列程序段的输出结果是includevoid fun(int*x,int*y){cout<<*x<<*y;*X=3;*y=4;}vo

    下列程序段的输出结果是 #include<iostream.h> void fun(int*x,int*y) {cout<<*x<<*y; *X=3; *y=4; } void main() {int x=1,y=2; fun(&y,&x); cout<<X<<y<<endl; }

    A.2143

    B.1212

    C.1234

    D.2112


    正确答案:A

  • 第5题:

    在下列的程序的横线处填上适当的语句,使该程序的输出为12。include using namespace

    在下列的程序的横线处填上适当的语句,使该程序的输出为12。

    include<iostream.h>

    using namespace std;

    class Base

    {

    public:

    int a,b;

    Base(int i){a=i;}

    };

    class Derived:public Base

    {

    int a;

    public:

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

    void show()

    {


    正确答案:eoutBase::a。
    eoutBase::a。 解析: 本题考查的是基类和派生类的构造函数。派生类构造函数的执行顺序:首先调用基类的构造函数,调用顺序按它们被继承时说明的顺序;然后调用子对象的构造函数,调用顺序按它们在类中说明的顺序;最后是派生类构造函数中的内容。本题要求结果是输出12,分析题目,首先调用基类的构造函数,然后是调用子对象的构造函数,横线处要求输出基类成员a的值,填入toutBase::a即可。