下列类的定义中,有( ) 处语法错误。 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.1B.2C.3D.4

题目

下列类的定义中,有( ) 处语法错误。 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的赋值是错误的。
更多“下列类的定义中,有( ) 处语法错误。 class Base { public:Base(){} Base(int i){data=i;}private: ”相关问题
  • 第1题:

    下面程序的打印结果是【】。 include using namespace std; class Base { public:Base(i

    下面程序的打印结果是【 】。

    include <iostream>

    using namespace std;

    class Base

    {

    public:

    Base(int x)

    {

    a=x;

    }

    void show()

    {

    cout<<a;

    }

    private:

    int a;

    };

    class Derived : public Base

    {

    public:

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

    void show()

    {

    cout<<b;

    }

    private:

    int b;

    };

    int main ( )

    {

    Base b(5) , *pb;

    Derived d(1);

    pb=&d;

    pb->show();

    return 0;

    }


    正确答案:2
    2 解析:基类Base派生出派生类Derived,在主函数中,定义了基类对象b,基类指针pb,以及派生类对象d,并让基类指针pb指向派生类对象乙在C++中,当派生类的对象赋值给基类对象时,只能使用派生类对象中从基类继承的成员。所以最后执行语句“pb->show();”是调用基类的成员函数show(),输出a的值2。

  • 第2题:

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

  • 第3题:

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

    A.1

    B.2

    C.3

    D.4


    正确答案:B

  • 第4题:

    为完成下面的程序,应在划线处填入的语句是()。includeusingnamespacestd;classBase{pr

    为完成下面的程序,应在划线处填入的语句是( )。 #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.voidDerived(int i):Base(0){}

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


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

  • 第5题:

    有以下程序:inClUdeusingnamespacestd;ClassBase{public: Base(intx) {a=x; } voidsh

    有以下程序: #inClUde <iostream> using namespace std; Class Base { public: Base(int x) { a=x; } void show() { cout<<a; } private: int a; }; class Derived : public Base { public: Derived(int i) :Base(i+1),b(i){} void Show() { cout<<b; } private: int b; }; int main() { Base b(5),*pb; Derived d(1); pb=&d; pb->show(); return 0; } 运行后的输出结果是( )。

    A.1

    B.5

    C.2

    D.0


    正确答案:C
    解析:基类Base派生出派生类Derived,在主函数中,定义了基类对象b,基类指针pb,以及派生类对象d,并让基类指针pb指向派生类对象d。在C++中,当派生类的对象赋值给基类对象时,只能使用派生类对象中从基类继承的成员。所以最后执行语句“pb->show();”是调用基类的成员函数show(),输出a的值2。