计算机二级

有以下程序 include using namespace std; class Base { int a; public: Base(int x)有以下程序include <iostream>using namespace std;class Base{int a;public:Base(int x){ a=x; }void show(){ cout<<a; }class Derived : public Base{int b;public:Derived(int i) :Base(i+1),b(i)

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

有以下程序

include <iostream>

using namespace std;

class Base

{

int a;

public:

Base(int x){ a=x; }

void show(){ cout<<a; }

class Derived : public Base

{

int b;

public:

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

void show() { cout<<b;

};

int main ()

{

Base b(5),*pb;

Derived d(1);

pb=&d;

pb->show ();

return 0;

}

运行后的打印结果是______。

参考答案和解析
正确答案:2
2 解析:本题考核基类指针与派生类指针的使用。本例程序中类Derived是从基类Base公有继承来的。main()中定义了基类对象b和一个基类指针pb,又定义了派生类Derived的对象d。由于Derived是Base的子类型,因此可以将派生类Derived的对象d的地址赋值给指向基类Base的指针pb,但这时指针pb只能使用从基类Base继承的成员。所以通过对象指针Pb调用的show函数是基类的成员函数show(),从而输出基类私有数据成员a的值2。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

阅读下列说明和C++代码,填写程序中的空(1)~(6),将解答写入答题纸的对应栏内。
【说明】
以下C++代码实现一个简单绘图工具,绘制不同形状以及不同颜色的图形。部分类及其关系如图6-1所示。



【C++代码】
#include?#include?using?namespace?std;class?DrawCircle?{??????//绘制圆形,抽象类? ? ? public: (1);//定义参数为?int?radius,?int?x,?inty? ?virtual~DrawCircle()?{?}};class?RedCircle:public?DrawCircle?{????//绘制红色圆形? ? ? ? public: void?drawCircle(intradius,?int?x,?int?y)?{cout?<?drawCircle?=?drawCircle;? }? ?virtual~shape()?{?}? public:? ?virtual?void?draw()?=?0;};class?Circle:public?Shape?{????//圆形? ? private:? ? ?int?x,y,radius;? ? public:? Circle(int?x,inty,int?radius,DrawCircle?*drawCircle)? (3)? {? this->x?=?x;? ?this->y?=?y;? ? this->radius?=?radius; }? ? ? public:? void?draw(){? drawCircle?-> (4); }};int?main(){Shape?*redCirclenew?Circle(100,100,10,????(5)????);//绘制红色圆形? Shape?*greenCircle=new?Circle(100,100,10, (6)??);//绘制绿色圆形redCircle >draw();? ?greenCircle?->draw();? ?return?0;}


答案:
解析:
(6)(1)void drawCircle (int radius,int x,int y)
(2)DrawCircle*drawCircle
(3)drawcircle
(4)drawCircle(radius,x,y)
(5)new RedCircle()
(6)new GreenCircle()【解析】
第一空是填接口里面的方法,在接口的实现里面找,可以发现应该填void drawCircle (int radius,int x,int y)。
第二空可以根据后面this drawCircle=drawCircle判断,这里应该有一个drawCircle属性,因此应该填)DrawCircle drawCircle。
第三空这里填drawcircle,用-> drawcircle来引用父类的成员。
第四空调用drawCircle(radius,x,y)方法。
第五、六空分别创建一个红色圆形对象和一个绿色圆形对象作为Circle里面的实参。

第2题:

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

第3题:

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

第4题:

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

第5题:

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

有以下程序: #include <iostream> using namespace std; class BASE { private: char c; public: BASE(char n):c(n);{} virtual~BASE() { cout<<c; } }; class DERIVED:public BASE { char c; p

A.XY

B.YX

C.X

D.Y


正确答案:A
解析:在C++中,由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反。在此题的程序中,在主函数结束时,派生类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 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。

第8题:

下面程序的输出结果是【】。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的构造函数。

第9题:

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