Basically as far as I know, when you create a base class with a public, protected, and private section and variables/functions in each the public and protected sections will get inherited into the appropriate section of the sub-class (defined by class subclass : private base, which will take all public and private members of base and put them into public, changing the word private to public puts them all in public and changing it to protected puts them all into protected).
There's a bit of confusion in this statement.
Recall that inheritance is defined for classes and structs in C++. Individual objects (ie. instances) do not inherit from other objects. Constructing an object using other objects is called composition.
When a class inherits from another class, it gets everything from that class, but the access level of the inherited fields may inhibit their use within the inheritor.
Furthermore, there are 3 kinds of inheritance for classes: private
(which is the default), protected
, and public
. Each of them changes the access level of a class properties and methods when inherited by a subclass.
If we order the access levels in this manner: public
, protected
, private
, from the least protected to the most protected, then we can define the inheritance modifiers as raising the access levels of the inherited class fields to at least the level they designate, in the derived class (ie. the class inheriting).
For instance, if class B
inherits from class A
with the protected
inheritance modifier:
class B : protected A { /* ... */ };
then all the fields from A
will have at least the protected
level in B
:
public
fields becomeprotected
(public
level is raised toprotected
),protected
fields stayprotected
(same access level, so no modification here),private
fields stayprivate
(the access level is already above the modifier)