To understand private inheritance, you must first understand public inheritance.
If you have the following:
class Base
{
public:
void SomeFunc();
};
class Pub : public class Base {};
This means that the following is allowed:
Pub aPub;
Base &basePart = aPub;
You can take the derived class and get a reference (or pointer) to a base class. You can also do this:
Pub aPub;
aPub.SomeFunc();
That which was public in Base
is public in Pub
.
Public inheritance means that everyone, everywhere can access the base class components of the drived class.
Private inheritance is a bit different:
class Priv : private class Base {};
Priv aPriv;
Base &basePart = aPriv;
That last line does not work in general. Private inheritance means that the only functions and classes that can perform this operation are either direct members of Priv
or friends of Priv
. If you write this code outside of a Priv
member or friend, it will fail to compile.
Similarly:
Priv aPriv;
aPriv.SomeFunc();
Only works if this is within a member or friend of Priv
.
Private inheritance means that only members or friends of the derived class can access the base class components. So member functions can call the base class part of the derived class, but nobody in the outside world can.
When you publicly derive a class from another class, you are saying that the derived class "is a" base class. It is exactly one of those types. When you privately derive from a base class, you are hiding the derivation from the outside world. This means that you are using the implementation in the base class to implement your derived class, but nobody else needs to know it. The derived class is "implemented in terms of" the base class.