开发者

Changing levels of inherited class

Is it fine to change the acess levels of inherited class like the way we want them?

class Base {

public:
   size_t x;

protected:
   size_开发者_开发问答t y;
};


class Derived : private Base {

protected:
   size_t x;

public:
   size_t y;
};

Does the Base class inherits only the private members or all the members of Base will be private by default? I tried to interchange the access levels but i don't understand what am i doing here?


No, Derived can't suddenly decide that it's going to steal the ability to read a member that's private in Base.

What you're actually doing is creating entirely different variables in Derived, which happen to have the same name.


What happens when you inherit from Base with private modifier is that only the Derived class has access to Base's members. If you decide to create a AnotherDerived that inherits from Derived, you won't be able to access any of Base's members.

Check this link for more information on inheritance and access specifiers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜