Here is an example to help you better understand
class Base{ public: int foo; }; class Derived: private Base{ public: int bar; };
Now in this program you can see what an object of a derived class can do. A derived class object may
- Access the panel with integer variables publicly.
- Access to the integer variable foo is confidential (only inside the Derived class), because the inheritance relation is private.
Now let's see what happens if the base pointer can do, if it is made to point to such an object. For a base class pointer
- The base pointer cannot access the variable bar, because it only points to the base section of the derived class object.
- And according to the base class pointer, the variable foo is public. As defined in the base class, it is publicly available.
So, now you can see the ambiguity that a base pointer would encounter if it points to a class object inherited in a particular respect.
Ambiguity: according to the derived class, the foo object is private, but the base pointer considers it public.
Thus, the morale of private or protected inheritance is lost if such things are permitted. Hope this clears your doubts.
source share