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

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

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

A.20

B.30

C.10

D.0

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

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

有以下程序 #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。

第2题:

若有以下程序:includeusing namespace Std;Class Base{public:Base(){x=0;}int x;};c

若有以下程序: #include<iostream> using namespace Std; Class Base {public: Base() {x=0;} int x;}; class Derivedl:virtua1 public Base {public: Derived1() {x=10;}}; class Derived2:virtual1 public Base {public: Derived2()

A.20

B.30

C.10

D.0


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

第3题:

若有以下程序:includeusingnamespacestd;classBase{public: Base() {x=0; } intx;};

若有以下程序: #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.10

B.20

C.30

D.0


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

第4题:

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

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

A.10

B.20

C.30

D.0


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

第5题:

有以下程序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。

第6题:

若有以下程序: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。

第7题:

若有以下程序: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。

第8题:

有以下程序: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。

第9题:

有如下程序: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
解析:派生对象在创建时先调用基类的构造函数,然后调用派生类的构造函数;撤销对象时,先调用派生类的构造函数,然后调用基类的构造函数。当类中出现其他类对象时,在初始化时先调用该对象的类的构造函数创建该对象。