Which line contains a constructor in this class definition?()   public class Counter { // (1)   int current, step;   public Counter(int startValue, int stepValue) { // (2)   set(startValue);   setStepValue(stepValue);  }   public int get() { return curren

题目

Which line contains a constructor in this class definition?()   public class Counter { // (1)   int current, step;   public Counter(int startValue, int stepValue) { // (2)   set(startValue);   setStepValue(stepValue);  }   public int get() { return current; } // (3)   public void set(int value) { current = value; } // (4)   public void setStepValue(int stepValue) { step = stepValue; } // (5)  }  

  • A、Code marked with (1) is a constructor
  • B、Code marked with (2) is a constructor
  • C、Code marked with (3) is a constructor
  • D、Code marked with (4) is a constructor
  • E、Code marked with (5) is a Constructor
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

定义属性错误的是()。

A、public int A{ get {return "100"}}

B、public int A( get;set;)

C、public int A{ get;set;}

D、public int A{ get;}


正确答案:B

第2题:

有如下类声明: class MyBASE{ int k; public: void set(iht n){k=n;} int get()coast{return k;} }; class MyDERIVED:pnoted My BASE { protected: int j; public: void set(int m,int n){MyBASE::set(m);j=n;} int get()const{return MyBASE::get()+j;} }; 则类MyDERIVED中保护的数据成员和成员函数的个数是

A.4

B.3

C.2

D.1


正确答案:B
解析:protected派生:pnvate->不可见;protected->private;public->protected。

第3题:

有如下类声明: class MyBASE { int k: public; void set(int n){k=n;} int get() const{return k;} }; class MyDERIVED: protected MyBASE { protected: int j; public: void set(int m, int n){MyBASE::set(m);j=n;} int get() const{return MyBASE::get()+j;} }; 则类MyDERIVED中保护的数据成员和成员函数的个数是( )。

A.4

B.3

C.2

D.1


正确答案:B

第4题:

阅读以下说明和Java程序,将应填入(n)处的字句写在对应栏内。

[说明]

下面程序输出一个矩形面积,以及矩形区域上的假想的作物产量。

[Java程序]

public class MainJava {

public static void main(String[] args){

Lot_size small=new Lot_size();

Lot_size medium=new Lot_size();

small.set(5,5,5,25);

medium.set(10,10,10,50);

System.out.println("For a small lot of area"

+small.get_area()+"\n");

System.out.println("the actual crops are $"

+small.get_data2()+"\n");

System.out.println("and ideal crops are $"

+small.get data()+"\n");

System.out.println("For a medium lot of area"

+medium.get_area()+“\n”);

System.out.println("the actual crops are $"

+medium.get_data2()+"\n");

System.out.println ("and ideal crops are $"

+medium.get_data()+"\n");

}

}

class Crop_assessment{

private int actual_crop;

private int ideal_crop;

public void set(int in_actual,int in ideal){

actual_crop=in_actual;

ideal_crop=in_ideal;

}

public int get_actual_crop(){return (1) ;}

public int get_ideal_crop()(return (2) ;}

}

class Lot_size{

private int length;

private int width;

private Crop_assessment crop= (3) ;

public void set(int 1,int W,int a,int i){

length=1;

width=W;

crop.set(a,i);

}

public int get_area(){return length*width;}

public int get_data()freturn (4) ;}

public int get_data2()(return (5) ;}

}


正确答案:(1) return actual_crop (2) return ideal_crop (3) new Crop_assessment() (4) crop.get_ideal_crop (5) crop.get_actual_crop()
(1) return actual_crop (2) return ideal_crop (3) new Crop_assessment() (4) crop.get_ideal_crop (5) crop.get_actual_crop() 解析:本题以Java语言为载体,考查面向对象程序设计中的几个重要概念—类的嵌套及函数的使用。
本题的功能是通过已定义的粮食收成类定义了一个计算特定矩形域上粮食收成的类,在主函数中定义了两个对象,并调用了相应的函数,来输出理想和实际的粮食产量。
首先,由于Crop assessment定义的成员数据缺省为私有的,所以想要获得实际和理想的粮食产量,要通过两个公有成员函数,所以(1)处应填入“return actual_crop”,(2)处应填入“return ideal_crop”。
其次,在类Lot size中,由于我们要反映矩形域上的粮食产量,所以我们在类中嵌套定义了一个类,Java中对象需要实例化,故(3)应填入“new Crop_assessment()”。
最后,由于我们想通过Lot_size类中的get_data和get_data2函数得到粮食产量,但由于这两个成员数据是私有函数,所以我们必须通过定义的对象调用它,所以(4)应填入“crop.get_ideal_crop()”,(5)应填入“crop.get_actual_crop()”。

第5题:

有以下程序:include using namespace std;class A{private: int x,y;public: void se

有以下程序: #include <iostream> using namespace std; class A { private: int x,y; public: void set (int i,int j) { x=i; y=j; } int get_y() { return y; } }; class box { private: int length,width; A label; public: void set(int 1,int w, int s,int p) { length=1; width=w; label.set(s,p); } int get_area() { return length*width; } }; int main() { box small; small.set(2,4,1,35); cout<<small.get_area()<<end1; return 0; } 运行后的输出结果是( )。

A.8

B.4

C.35

D.70


正确答案:A
解析:本题考核成员对象的应用。类box的成员函数set()为设置对象的坐标值和对象的长、宽值。成员函数setarea返回该对象的面积。程序最后输出为8。

第6题:

Given:Which code, inserted at line 15, allows the class Sprite to compile?()

A.Foo { public int bar() { return 1; }

B.new Foo { public int bar() { return 1; }

C.new Foo() { public int bar() { return 1; }

D.new class Foo { public int bar() { return 1; }


参考答案:C

第7题:

有如下类声明: class MyBASE{ int k; public: void set(int n){k=n;} int get( )const{return k;} }; class MyDERIVED:protected MyBASE{ protected: intj; public: void set(int m,int n){MyBASE::set(m);j=n;} int get( )const{return MyBASE::get( )+j;} }; 则类MyDERIVED中保护的数据成员和成员函数的个数是

A.4

B.3

C.2

D.1


正确答案:B
解析:在保护派生的单继承中,基类的公有数据成员和成员函数在派生类中成为保护成员。

第8题:

下面这个程序的结果是includeclass A{private:int a;public:void seta();int geta(

下面这个程序的结果是 #include<iostream.h> class A { private: int a; public: void seta();int geta();}; void A∷seta() {a=1;} int A∷geta() {return a;} class B { pnvate: int a; publiC: void sera();int geta();}; void B∷seta() {a = 2;} int B∷geta() {return a;} class C:public A,public B { private: int b; public: void display();}; void C∷display() { int b=geta(); cout<<b;} void main() { C c; c.seta(); c.display();}

A.1

B.2

C.随机输出1或2

D.程序有错


正确答案:D
解析:在类A中有geta()函数,在类B中也有geta()函数,类C继承了类A和类B,这样就产生了二义性,所以程序会出错。

第9题:

下列程序的输出结果是______。 include using namespace std; class base { public: in

下列程序的输出结果是______。

include<iostream>

using namespace std;

class base

{

public:

int n;

base(int x){n=x;}

virtual void set(int m){n=m;cout<<n<<'';}

};

class deriveA:public base

{

public:

deriveA(int x):base(x){}

void set(int m){n+=m;cout<<n<<'';}

};

class deriveB:public base

{

public:

deriveB(int x):base(x){}

void set(int m){n+=m;cout<<n<<'';}

};

int main( )

{

deriveA d1(1);

deriveB.d2(3);

base*pbase;

pbase=&d1;

pbase->set(1);

pbase=&d2;

pbase->set(2);

return 0;

}


正确答案:25
25 解析:本题考核虚函数的定义和调用。在C++中,一个基类指针(或引用)可以用来指向它的派生类对象,而且通过这样的指针(或引用)调用虚函数时,被调用的是该指针(或引用)实际所指向的对象类的那个重定义版本。题中基类成员函数set( )被定义成虚函数,而且其派生类 deriveA和deriveB都有函数set( )的重定义版本,所以通过指针pbase的两次调用分别调用的是派生类deriveA和deriveB中定义的版本,即输出25。

第10题:

class A {  protected int method1(int a, int b) { return 0; }  }  Which two are valid in a class that extends class A?() 

  • A、 public int method1(int a, int b) { return 0; }
  • B、 private int method1(int a, int b) { return 0; }
  • C、 private int method1(int a, long b) { return 0; }
  • D、 public short method1(int a, int b) { return 0: }
  • E、 static protected int method1(int a, int b) { return 0; }

正确答案:A,C

更多相关问题