有如下类声明: class Base { protected: int amount; public: Base(int n=0):amount(n) { } int getAmount( )const {r

admin2019-05-22  18

问题 有如下类声明:
    class Base {
    protected:
    int amount;
    public:
    Base(int n=0):amount(n) { }
    int getAmount( )const {return amount;}
    };
    class Derived:public Base {
    protected:
    int value;
    public:
    Derived(int m,int n):value(m),Base(n) { }
    int getData()const{return value+amount;}
    };
    已知x是一个Derived对象,则下列表达式中正确的是(    )。

选项 A、x.value+x.getAmount( )
B、x.getData( )-x.getAmount( )
C、x.getData( )-x.amount
D、x.value+x.amount

答案B

解析 本题考查公有继承中派生类对象对基类的访问属性。在公有继承中,派生类对象只能私有成员。题中λ足派生类的对象,只能访问基类中公有的Base( )和getAmount( )成员,而不能访问保护类型的amount成员,故选项C、D错误。而类对象对类成员的访问也存在类似的情况,即类对象只能访问类的公有成员,而value是Derived的保护成员,所以A选项也错误。故答案为B。
转载请注明原文地址:https://kaotiyun.com/show/JI8p777K
0

相关试题推荐
最新回复(0)