(Tutorial) Using Multiple Inheritance in C++
Multiple Inheritance in C++
Using multiple inheritance in C++
Deriving directly from more than one class is usually called multiple
inheritance. Since it's widely believed that this concept complicates the design
and debuggers can have a hard time with it, multiple inheritance can be a
controversial topic. However, multiple inheritance is an important feature in
C++ and C++ programmers think of it as a very good structuring tool.
To get a taste of this, let's consider the following real world example:
class Animal
{
//describes the behavior of the animal
}
class Drawing
{
//contains the drawing properties of the entity like colors, size etc
}
Suppose we need to create a displayable snake object. We can
inherit class Animal in class Drawing or class Drawing in class Animal and then
use the derived one as a base class for snake, but neither of the solutions is
appropriate because they both create a dependency between two independent
concepts. So, instead, we should use multiple inheritance and derive both
Drawing and Animal in the snake class:
class Snake : public Animal, public Drawing
As you may noticed, another solution in the above design could be to use
composition instead of inheritance and have animal and drawing as members of
class snake :
class Snake
{
public:
...
private:
Animal *m_animal;
Drawing *m_drawing;
}
But now the relation between animal and snake is that snake
contains an animal instead of snake is an animal, which is conceptually wrong,
and the lack of categorization makes the design unrealistic. This will make the
code harder to understand and also harder to reuse because we lose the advantage
of dynamic binding and polymorphism. The benefit of dynamic binding and
polymorphism is that they help making the code easier to extend (make it
possible to create operations that work on a class of objects that share the
same interface). Suppose we want later to create another animal, a lion for
example. The old code that operates on objects of type Animal will work without
change, and in the new oneLion class, if it overrides one of the animal methods,
dynamic binding ensures that its new methods are going to be executed
properlyinstead of the Animal versions of those methods depending on the object
type they are called from.
Using inheritance instead of composition, we can completely hide some members of
the parent class, or allow access only for subclasses (classes derived from it)
by specifying them as protected.
As with simple (single) inheritance the derived class has access to all the
non-private members of the base classes. When the Snake's class constructor is
executed it first initializes the base classes by calling their appropriate
constructors in the order they are defined in appear in the list defining the
list of inherited classes when they are declared (first Animal and then
Drawing).
Note that our example has only two base classes, but C++ doesn't impose any
constraint on the number of classes that can be inherited.[..]
Read full article..
Courtesy : Cprogramming.com
- guru's blog
- Login or register to post comments

