Inheritence in C++

From design point of view why and when we do inheritence in c++.Is there any substitute of doing Inheritence.I mean composition or aggregation.
we do inheritance when we want to extend a class (i.e - a monkey inherits all traits of an animal in addition to it's own, and a spider monkey inherits all traits of a monkey and adds some others,, etc.)
I assume aggregation is including a class inside another. I guess this is a substitute for it, but inheritance is way neater.
According to Herb Sutter, who is on the C++ standards committee, use inheritance when modeling an IS-A and WORKS-LIKE-A relationship. Prefer containment (composition) when modeling IMPLEMENTED-IN-TERMS-OF.

To rephrase this, use inheritance when you want to write a class that will be used polymorphically(used with a base-class pointer) or can be used in place of the base class. If the new class is implemented as another class, but should not be used as that base class, prefer containment.
To add to nkendra's post, here's an example form one of my own C++ books:
Imagine you have a Plane class with components like wings, weight, wheels, capacity, etc.
Now let's say you want different types of planes like a Boeing or a fighter jet. This relation is IS-A because a Boeing IS-A plane and the same with a fighter jet. Here you use inheritance because you are extending the Plane class and are specifying the type, i.e. fighter jets also need weapon components while a Boeing needs extra compartments.
Now let's consider some of the components I listed from the Plane class, say wings and wheels. Wings and wheels could be classes of their own with attributes like individual weight, length in terms of the wing, and pressure in terms of the wheel. The relationship between the Plane class and its component classes is a HAS-A, or IMPLEMENTED-IN-TERMS-OF as nkendra stated. A plane HAS two wings and at least three wheels, so instead of deriving Plane from Wheel and Wing, you would use containment or aggregation.
Last edited on
From design point of view why and when we do inheritence in c++.Is there any substitute of doing Inheritence.I mean composition or aggregation.

You should use inheritance as little as possible.

Beginners apparently (over) use inheritance where composition or aggregation would be more appropriate. Rather than expounding on the matter here, if you Google for "overuse of inheritance" or the like, you will find a number of articles and forum posts about it.

But one of the key principles of object-oriented design is:

Program to an interface, not an implementation.

(p18, Design Patterns, Gamma et al)

Inheritance breaks this rule, as the implentation of a derived class is coupled to that of it's base class. It should be used sparingly, when the penalty of the increased coupling is outweighed by the benefits of a (markedly) simpler implementation.

A special case of inheritance relates to vtable-only ("interface") base classes. Interface inheritance can be used to implement polymorphic behaviour without sharing any of the implementation. (Strictly, speaking we've been talking about inheritance of implementation before.)

In addition to the matter of inheritance (of implementation) versus composition/aggregation, as C++ is a multi-paradigm language, you also have object-oriented verus generic programming (as well as plain old proceedural and even functional programming.)

Returning to the subject of polymprhism, you therefore have a choice between object-oriented, virtual method "subtype" polymorphism and generic template-based "parametric" polymorphism.

And when discussing interfaces, you can also think about binary reuse versus code reuse (and Component Based Development, or CBD.)


Why should I prefer composition over inheritance?

Functional-Style Programming in C++

Component-based software engineering

A Realistic Look at Object-Oriented Reuse

Last edited on
Topic archived. No new replies allowed.