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

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

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

第1题:

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

第2题:

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

第3题:

若有以下程序:include using namespace std;class Base{public: void who(){cout<<" B

若有以下程序:

include <iostream>

using namespace std;

class Base

{

public:

void who(){cout<<" Base"<<end1:}

};

class Derived1: public Base

{

public:

void who(){ cout<<"Derived"<<end1;}

};

int main()

{

Base *p;

Derived1 obj1;

p=&obj1;

p->who();

return 0;

}

则该程序运行后的输出结果是【 】。


正确答案:Derived
Derived 解析:本题考核对象指针的使用。题中基类Base对象指针p用派生类 Derived对象obj1来初始化,那么通过对象指针p调用的who函数版本为基类Base中定义的版本,所以程序输出Derived。

第4题:

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

第5题:

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


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

第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;}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。

第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题:

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

第10题:

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