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

Accessibility private protected public
Accessible from own class? yes yes yes
Accessible from dervied class? no yes yes
Accessible outside dervied class? no no no

 

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.

[[code]]czozNzU6XCI8dmFyPmNsYXNzPC92YXI+IFBhcmVudA0Kew0KPHZhcj5wdWJsaWM8L3Zhcj46DQogIDx2YXI+dmlydHVhbDwvdmFyPiB7WyYqJl19PHZhcj52b2lkPC92YXI+IENhbGxDaGlsZCgpID0gMDsNCg0KICA8dmFyPnZvaWQ8L3Zhcj4gRG9TdHVmZigpDQogIHsNCiAgICBDYXtbJiomXX1sbENoaWxkKCk7ICA8Y2l0ZT4vLyBjYWxscyBhIGZ1bmN0aW9uIGluIHRoZSBjaGlsZCBjbGFzczwvY2l0ZT4NCiAgfQ0KfTsNCg0Ke1smKiZdfTx2YXI+Y2xhc3M8L3Zhcj4gQ2hpbGQgOiA8dmFyPnByaXZhdGU8L3Zhcj4gUGFyZW50DQp7DQo8dmFyPnB1YmxpYzwvdmFyPjoNCiB7WyYqJl19IDx2YXI+dmlydHVhbDwvdmFyPiA8dmFyPnZvaWQ8L3Zhcj4gQ2FsbENoaWxkKCkgeyA8Y2l0ZT4vKi4uLiovPC9jaXRlPiB9DQp9O3tbJiomXX0NClwiO3tbJiomXX0=[[/code]]
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