下列类的定义中,有( )处语法错误。class Base{public: Base(){} Base(int i) {data=i; }private:

题目

下列类的定义中,有( )处语法错误。 class Base { public: Base(){} Base(int i) { data=i; } private: int data; }; class Derive : public Base { public: Derive() : Base(O) {} Derive (int x) { d=x; } void setvalue(int i) { data=i; } private: int d; };

A.1

B.2

C.3

D.4

参考答案和解析
正确答案:B
解析:本题考核派生类的定义和成员的访问权限。第①处错误:在派生类的构造函数Derive(intx)中没有调用基类的构造函数对基类对象初始化。第②处错误:数据data是基类Base的私有成员,派生类Derive不能访问,所以在函数setvalue中对data的赋值是错误的。
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

下列类的定义中,有( ) 处语法错误。 class Base { public: Base(){} Base(int i) { data=i; } private: int data; }; class Derive: public Base { public: Derive(): Base(O) { } Derive(int x) { d=x; } void setvalue(int i) { data=i; } private: d; };

A.1

B.2

C.3

D.4


正确答案:B
解析:本题考核派生类的定义和成员的访问权限。第一处错误:在派生类的构造函数Derive(intx)中没有调用基类的构造函数对基类对象初始化:第二处错误:数据data是基类Base的私有成员,派生类Derive不能访问,所以在函数setvalue中对data的赋值是错误的。

第2题:

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

include 〈iostream〉

using namespace std;

class Base

{

public:

int a;

Base(int i) { a=i;}

};

class Derived : public Base

{

int a;

public:

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

void show()

{

【 】; //输出基类数据成员a的值

cout〈〈b〈〈end1;

}

};

int main()

{

Derived d(1);

d.show();

return 0;

}


正确答案:cout〈〈Base::a
cout〈〈Base::a 解析:本题考核作用域分辨符“::”的使用。

第3题:

下列程序的输出结果是非曲直【 】。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;

}


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

第4题:

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

第5题:

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

第6题:

已知如下类定义: class Base { public Base (){ //... } public Base ( int m ){ //... } protected void fun( int n ){ //... } } public class Child extends Base{ // member methods } 如下哪句可以正确地加入子类中?()

A.private void fun( int n ){ //...}

B.void fun ( int n ){ //... }

C.protected void fun ( int n ) { //... }

D.public void fun ( int n ) { //... }


正确答案:CD

第7题:

为完成下面的程序,应在划线处填入的语句是includeusingnamespace std;class Base{pri

为完成下面的程序,应在划线处填入的语句是 #include<iostream> using namespace std; class Base { private: int x; public: Base (int i) { x=i; } ~Base(){} }; class Derived:public Base { public: ______________//完成类Derive构造函数的定义 }; int main() { Derived Obj; return 0; }

A.Derived(int i):Base(i){}

B.Derived(){}

C.void Derived (int i):Base(i){}

D.Derived(int i){Base(i);}


正确答案:A
解析:本题考核派生类中的构造函数。程序中,类Derived是基类Base的公有派生。在类Derived的构造函数应该包括调用基类构造函数使基类的数据成员得以初始化。

第8题:

在下面的程序的横线处填上适当的语句,使该程序的输出为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()

{

______;//输出基类数据成员a的值

cout<<b<<endl;

}

};

int main()

{

Derived d(1);

d.show();

return 0;


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

第9题:

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

include<iostream>

using namespace std;

class Base{

public:

int m,n;

Base(int i,int j):m(i),n(j){}

};

class Derived:public Base{

public:

int m,k,;

Derived(int i,int j):Base(i,j),m(i+1),k(j+1){}

};

int main(){

Derived d(1,5);

cout<<d.m<<d.k<<d.n;

return 0;

}


正确答案:265
265 解析:本题主要考查对基类与派生类的重名成员的掌握。如果在派生类中定义了与基类同名数据成员,那么在派生类中对重名成员访问时,屏蔽基类的同名成员。如果要在派生类中使用基类的同名成员,可以显式地使用作用域运算符指定:类名::成员。

第10题:

有如下程序: include using namespace std class Base{ int b; public: Base(int i) {

有如下程序:

include<iostream>

using namespace std

class Base{

int b;

public:

Base(int i) {b=i;}

Void disp ( ) {cout<<"Base:b="<<b<<''; }

};

class Base1:virtual public Base{

public:

Base1(int i):Base(i){}

};

class Base2:virtual public Base{

public:

Base2(int i):Base(i){}

};

class Derived:public Basepublic Base1{

int d;

public:

Derived(int i ,int j):Base1(j),Base2(j),【 】

{ d=i; }

void disp() {cout<<"Derived:d="<<d<<' ';}

};

int main()

Derived objD(1,2);objD. disp()

objD. Base::disp();

objD. Base1::disp()

objD. Base2::disp();

return 0;

}

请将程序补充完整,使程序在运行时输出:

Derivd:d=1 Base:b=2 Base:b=2 Base:b=2


正确答案:Base(j)
Base(j) 解析:因为程序在运行时输出:Derivde:d=1 Base:b=2 Base:b=2 Base:b=2,而前两个Base:b=2 Base:b=2分别来自Base1(j),Base2(j),而在程序类的声明中,Base类也具有输出Base:b=2的功能。所以,程序中应补充的代码为Base(j)。

更多相关问题