Public inheritance is easy.
A : public B means every A is a B. In most programming language, like vb.net and objective-c, this is the only type of inheritance.
Private inheritance is also easy but pointless
A : private B means A is implemented by B. However, that's pointless because that means A should contain B instead. Ownership means less coupling with no disadvantage.
Then we have protected inheritance.
Can anyone explain to me what the hell is that for? Some says it's an "as a relationship". I am still not very clear on that.
Does anyone has some sample cases where people uses protected inheritance in good pattern (and conscience) for actual productive use?
Protected Inheritance − When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. Private Inheritance − When deriving from a private base class, public and protected members of the base class become private members of the derived class.
With protected inheritance, the public and protected members become protected, and private members stay inaccessible. With private inheritance, all members from the base class are inherited as private. This means private members are inaccessible, and protected and public members become private.
If one wishes to access a protected modifier outside a package, then inheritance is needed to be applied.
The private -inheritance variant allows access to the protected members of the base class. The private -inheritance variant allows Car to override Engine 's virtual functions.
Private inheritance is also easy but pointless
A : private B means A is implemented by B. However, that's pointless because that means A should contain B instead. Ownership means less coupling with no disadvantage.
That you might not see reasons for private inheritance does not mean it's pointless. There are several cases where private inheritance has it's reasons. You are right in that at a first glance, private inheritance means has-a relationships just like aggregation, and that private inheritance has a (slightly) tighter coupling.
Reasons for favouring private inheritance over aggretations could be some of the following:
These reasons are obviously technical reasons, some might even say "hacks". However, such reasons exist, so private inheritance is not completely pointless. It is just not "pure OO style" - but C++ isn't a pure OO language either.
The reason for protected inheritance are quite simple once you understood the ones for private inheritance:
If you have reasons to inherit something privately and want to make those benefits (i.e. that would-be member of your class or the typedefs) accessible to derived classes, use protected inheritance. Obviously, private inheritance should be used scarcely, and protected inheritance even more so.
The main motivation for protected inheritance is orthogonality. in all other contexts, you have three different access controls: private, protected and public. Why should inheritance be different? In practice, one could argue that there is no need or use for protected access in general. That may be overstating the case, but it is certain that a lot less is protected than private or public.
Also, private inheritance is not at all pointless, and in fact, corresponds to the original use of inheritance. As soon as the base class doing the implementing uses virtual functions which the derived class has to overload, containment cannot be used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With