private inheritance

private inheritance

When I want to expose some but not all of a base class’s interface or If inheriting class uses it internally to implement its functionality.

Private Inheritance vs Composition ??

The main use  is for mixins using private multiple inheritance to build up a child object with the proper functionality from the various mixin parents. This can also be done with composition (less  preferred) but the inheritance method DOES allow you to use using to expose some parent methods publicly, and allows for a slightly more convenient notation when using the mixin methods.

Accessibility in Private Inheritance

Accessible from own class?yesyesyes
Accessible from dervied class?noyesyes
Accessible outside dervied class?nonono


Advantage over Composition:

advantage is the parent class can more easily call functions in the derived class by way of virtual functions. This allows for a sort of “two way” communication between classes.

This sort of relationship is a bit harder (but not impossible) to accomplish with the composition approach.

C++ C C# Health concepts Leetcode Programming Data Streucture