CMS专题

多选题1. class Super {  2. private int a;  3. protected Super(int a) { this.a = a; }  4. }  .....  11. class Sub extends Super {  12. public Sub(int a) { super(a); }  13. public Sub() { this.a= 5; }  14. }  Which two, independently, will allow Sub to compile

题目
多选题
1. class Super {  2. private int a;  3. protected Super(int a) { this.a = a; }  4. }  .....  11. class Sub extends Super {  12. public Sub(int a) { super(a); }  13. public Sub() { this.a= 5; }  14. }  Which two, independently, will allow Sub to compile?()
A

Change line 2 to: public int a;

B

Change line 2 to: protected int a;

C

Change line 13 to: public Sub() { this(5); }

D

Change line 13 to: public Sub() { super(5); }

E

Change line 13 to: public Sub() { super(a); }

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

第1题:

给出下列代码,如何使成员变量m被方法fun( )直接访问?Class Test{private int m;public static void fun( ){} }

A.将private int m改为protected int m

B.将private int m改为public int m

C.将private int m改为static int m

D.将private int m改为int m


正确答案:C

第2题:

已知:Manager extends Employee观察:public Manager(String n,double s,int year,int month,int day) { super(n,s,year,month,day); bonus=0; }其中super是 ( )

A.Object类

B.Manager类

C.Employee类

D.Class类


正确答案:C
解析:本题中有extends这说明是继承,子类Manager继承父类Employee,使用变量super能够实现对父类成员的访问,所以本题中super指的是父类Employee。

第3题:

有如下类声明:

class MyClass

{

int i;

private: int j;

protected:int k;

public:int m, n;

};

其中,私有成员的数量为 【 9 】 。


正确答案:

第4题:

若有以下程序:include using namespace std;class Base{private: int x;protected: i

若有以下程序: #include <iostream> using namespace std; class Base { private: int x; protected: int y; public: int z; void setx(int i) { x=i; int getx () { return x; } }

A.1,2,3,4

B.产生语法错误

C.4,3,2,1

D.2,3,4,5


正确答案:A
解析:本题考核私有继承中类成员的访问权限。当类的继承方式为私有继承时,基类公有成员和保护成员都以私有成员属性出现在派生类中。私有派生类的成员对其基类成员的访问权和公共派生的方式相同,但是,由私有派生的类声明的对象,不能访问任何基类的成员。本题中,基类Base中的保护成员y和公有成员setx和getx,经过私有继承以后,称为派生类Inherit的私有成员,所以可以在派生类Inherit的函数成员中对它们进行访问。类Inherit中的函数成员setvalue和display都是公有成员,所以可以通过Inherit的对象对它们进行访问。本程序的功能是对类中各数据成员进行赋值,然后查看赋值是否正确。

第5题:

下面程序中错误之处是 ______。 include classA{private:intxl;protected:intx2;publ

下面程序中错误之处是 ______。

include<iostream.h>

class A{

private:

int xl;

protected:

int x2;

public:

int x3;

};

class B: public A{

private:

int b1;

protected:

int b2;

public:

int b3;

void disp(){cout<<x1<<b2<<end1;} //A

void set(int i){x3=i;} //B

};

void main()

B bb;

bb. a3=10 //C

bb. b3=10 //D

}


正确答案:√
1

第6题:

给出下列代码,如何使成员变量m被方法fun( )直接访问? class Test{ private int m; public static void fun( ){ … } }

A.将private int m改为protected int m

B.将private int m改为public int m

C.将private int m改为static i

D.将private int m改为int m


正确答案:C
解析:在静态方法中不能直接访问非静态的成员,如果要在fun()中直接访问变量m,应将变量m用static修饰。

第7题:

若有以下程序:include using namespace std;class A{private: int x;protected: int

若有以下程序: #include <iostream> using namespace std; class A { private: int x; protected: int y; public: int z; void setx(int i) { x=i; } int getx () { return x; }; class B : protected A { public: void setvalue(int a, int b, int c) { setx (a); y=b; z=c; } void display() { cout<<getx ( ) <<", "<<y<<", "<<z<<", "<<end1; } }; int main () { B obj; obj.setvalue(5, 6, 7); obj.display ( ); return 0; } 程序运行后的输出结果是( )。

A.产生语法错误

B.7,6,5

C.5,6,7

D.7,5,6


正确答案:C
解析:本题考核保护继承中对类成员的访问权限。①在保护继承中,基类公有成员和保护成员都以保护成员身份出现在派生类中,而基类私有成员不可访问。②基类的公有成员和保护成员被继承以后作为派生类的保护成员,这样,派生类的其他成员可以直接访问它们。③由保护派.生的类声明的对象,不能访问任何基类的成员。在本题中,基类A中的数据成员y和函数setx,经过保护继承以后,在派生类B中成为保护成员,派生类B的对象不能访问它们。而派生类B中的函数setvalue和display都是公有成员,可以通过对象对它们进行访问。所以程序中对各成员的访问是正确的。本程序的功能是对类中各数据成员进行赋值,然后查看赋值是否正确。

第8题:

已知如下类定义: 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

第9题:

请找出下列程序中错误之处 ______。 include classA{private: intx1;protected: int

请找出下列程序中错误之处 ______。

#include<iostream.h>

class A{

private:

int x1;

protected:

int x2;

public:

int x3;

};

class B:public A{

private:

int y1;

protected:

int y2;

public:

int y3;

void disp(){cout<<x1<<y1<<end1:} //A

void set(int i) {x2=i;} //B

};

void main() {

B bb;

bb.x3=10; //C

bb.y3=10; //D

}

A.A

B.B

C.C

D.D


正确答案:A

第10题:

设有类定义如下:

class Base{

public Base(int i){}

}

public class MyOver extends Base{

public static void main(String arg[]){

MyOver m = new MyOver(10);

}

MyOver(int i){

super(i);

}

MyOver(String s, int i){

this(i);

//Here

}

}

以下哪条语句可以安排在//Here处 ?

A.MyOver m = new MyOver();

B.super();

C.this("Hello",10);

D.Base b = new Base(10);


正确答案:D

更多相关问题