单选题package geometry;  public class Hypotenuse {  public InnerTriangle it = new InnerTriangle();  class InnerTriangle {  public int base;  public int height;  }  }  Which is true about the class of an object that can reference the variable base? ()AIt can 

题目
单选题
package geometry;  public class Hypotenuse {  public InnerTriangle it = new InnerTriangle();  class InnerTriangle {  public int base;  public int height;  }  }  Which is true about the class of an object that can reference the variable base? ()
A

 It can be any class.

B

 No class has access to base.

C

 The class must belong to the geometry package.

D

 The class must be a subclass of the class Hypotenuse.


相似考题
更多“单选题package geometry;  public class Hypotenuse {  public InnerTriangle it = new InnerTriangle();  class InnerTriangle {  public int base;  public int height;  }  }  Which is true about the class of an object that can reference the variable base? ()A  It ca”相关问题
  • 第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题:

    Given:Which statement is true about the class of an object that can reference the variable base? ()

    A.It can be any class.

    B.No class has access to base.

    C.The class must belong to the geometry package.

    D.The class must be a subclass of the class Hypotenuse.


    参考答案:C

  • 第3题:

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

  • 第4题:

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

  • 第5题:

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

  • 第6题:

    下面程序的运行结果是includeclass base{protected:int a;public:base( ){cout<<"0

    下面程序的运行结果是 #include<iostream.h> class base{ protected: int a; public: base( ){cout<<"0";} }; class basel:virtual base { public: base1( ){cout<<"1";} }; class base2:virtual base{ public:

    A.123

    B.3120

    C.312

    D.3012


    正确答案:A
    解析:本题考查的是含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最后调用派生类自己的构造函数。

  • 第7题:

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

  • 第8题:

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

  • 第9题:

    下面程序的运行结果是( )。 #include<iostream.h> class base{ protected: int a; public: base{cout<<”0”;} }; class basel:virtual base{ public: basel{cout<<”1”;} }; class base2:virtual base i public: base2{cout<<”2”;} }; class derived:public basel,public base2{ public: derived{cout<<”3”;} }; void main derived obj; cout<<endl:

    A.0123

    B.3120

    C.0312

    D.3012


    正确答案:A
    本题考查的是含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最后调用派生类自己的构造函数。

  • 第10题:

    下列程序的输出结果是______。 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。

  • 第11题:

    设有类定义如下:

    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

  • 第12题:

    package geometry;  public class Hypotenuse {  public InnerTriangle it = new InnerTriangle();  class InnerTriangle {  public int base;  public int height;  }  }  Which is true about the class of an object that can reference the variable base? ()

    • A、 It can be any class.
    • B、 No class has access to base.
    • C、 The class must belong to the geometry package.
    • D、 The class must be a subclass of the class Hypotenuse.

    正确答案:C

  • 第13题:

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

  • 第14题:

    下面程序的运行结果是()。includeclass base{protected:int a;public:base(){cout<<

    下面程序的运行结果是( )。 #include<iostream.h> class base{ protected: int a; public: base(){cout<<“0”;} }; Class basel:Virtual base{ public: basel(){cout<<“1”;} }; Class base2:virtual base{ public: base2(){cout<<“2”;)

    A.0123

    B.3120

    C.0312

    D.3012


    正确答案:A
    解析: 本题考查的是含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最后调用派生类自己的构造函数。

  • 第15题:

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

  • 第16题:

    下列程序的运行结果是______。include class Base { public: void f(int x){cout<<“B

    下列程序的运行结果是______。

    include<iostream.h>

    class Base

    {

    public:

    void f(int x){cout<<“Base:”<<x<<endl;}

    );

    class Derived:public Base

    {

    public:

    void f(char*str){cout<<“Derived:”<<str<<endl;}

    };

    void main(void)

    {

    Base*pd=ne


    正确答案:Base:97。
    Base:97。 解析: 本题主要考查两个知识点,一是基类指针可以指向派生类对象,并可以访问派生类的所有成员。二是在函数重载中进行隐式类型转换。如pd->f(‘a’);系统到底调用哪个重载函数呢?实参既不是派生类中的形参,也不是基类中f函数的形参类型。此时系统根据就近原则和从高优先级到低优先级的规则尝试隐式转换。单字符更接近整数,故调用的是基类的f函数。

  • 第17题:

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

  • 第18题:

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

  • 第19题:

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

  • 第20题:

    下列类的定义中,有( )处语法错误。 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的赋值是错误的。

  • 第21题:

    下面程序的运行结果是includeclass base{protected: int a;public: base(){cout <<

    下面程序的运行结果是 #include<iostream.h> class base{ protected: int a; public: base(){cout <<"0";} }; class basel:virtual base{ public: basel () {cout <<"1";} }; class base2:virtual base{ public: base2(){cout <<"2";} }; class derived:public basel,public base2{ public: derived(){cout <<"3";} }; void main() { derived obj; cout <<end1; }

    A.123

    B.3120

    C.312

    D.3012


    正确答案:A
    解析:本题考查的是含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最后调用派生类自己的构造函数。

  • 第22题:

    下列程序的运行结果是______。 include class Base { public: virtual void func(int

    下列程序的运行结果是______。

    include<iostream.h>

    class Base

    {

    public:

    virtual void func(int i){cout<<"class Base:"<<i<<end1;)

    };

    class Derived: public Base

    {

    public:

    void func(double d){cout<<"class Derived:"<<d<<endl;}

    };

    void main( )

    {

    Base a,*p=a;

    Derived b;

    p=&b;

    (*p).func(3.3);

    }


    正确答案:class Base:3
    class Base:3 解析:题中基类和派生类中有同名函数,但是参数不同。派生关系中,只有在函数类型、函数名和参数个数、参数类型完全相同时,才表现多态性。本题中参数不同,编译器便认为是两个完全不同的函数。通过基类的指针指向派生类对象时,该指针只能访问到派生类中具有多态性的成员函数,而与基类无关的函数是无法通过基类指针来访问的。故调用的是base类的 func函数,系统将3.3强制转化为整型数3。如果将派生类中的func的形参改为int型,则执行的就会是派生类的func函数,此时表现为多态。

  • 第23题:

    下列程序的输出结果是______。 include class base { int x,y; public: base(int i,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(10,20,30);

    cout<<q->add( )<<endl;

    }


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