Inheriting private members in C++

They are included, but not inherited. What this means is:

  • Any inheriting type (: public SomeClass, : protected SomeClass or even : SomeClass, equivalent to : private SomeClass) will not make them accessible from child class methods, or outside (this->a and someobject.a respectively);
  • They will still be there - take space in memory allocated for child class instance;
  • Inherited methods will be able to access that private field.

So, basically protected is not visible outside while visible inside and from derived classes (if : private Parent wasn't used), while private is not visible from neither derived classes nor outside of the parent class; it's only visible for parent class' methods, even if they are inherited (but not overrided).


Because the getters and setters are public -- they're callable by anyone, not just derived classes.


A derived class doesn't inherit access to private data members. However, it does inherit a full parent object, which contains any private members which that class declares.


It depends on the inheritance type. If you inherit privately, then the derived class does NOT have access to the Base's private members.

Access                      public     protected    private
-----------------------------------------------------------
members of the same class      yes           yes        yes
members of derived classes     yes           yes         no
not members                    yes            no         no