Inheritance :
C++ strongly supports the concept of reusability. The c++ classes can be reused in several ways. Once a class has been written and tested, it can be adopted by other programmers to suit their requirements. This is basically done by creating new classes, reusing the properties of the existing ones. The mechanism of deriving a new class from old one is called inheritance (orderivation). The old class is referred to as the base class and the new one is called the derived class.
Various Forms of inheritance :
1. | Single inheritance : A derived class with only one base class is called single inheritance. |
2. | A derived class with several base classes is called multiple inheritance. A class can inherit the attributes of two or more classes as shown in Fig. This know as multiple inheritance. Multiple inheritance allows us to combine the features of several existing classes as a starting point for defining new classes. It is like a child inheriting the physical features of one parent and the intelligence of another. |
3. | Hierarchical inheritance : One class inherited by more than one base class is called Hierarchical inheritance. |
4. | The mechanism of deriving a class from another 'derived class' is known as multilevel inheritance. It is not uncommon that a class is derived from another derived class as show fig. The class A serves as a base class for the derived class B which is turn serves as a base class for the derived class C.The class B is known as intermediate base class since it provides a link for the inheritance between A and C.The chain ABC is known as inheritance path. A derived class with multilevel inheritance is declared as follows : class A { ...... }; class B : public A { ...... }; class C: public B { ...... }; This process can be extended to any number of levels. |
5. | There could be situation where we need to apply two or more types of inheritance to design a program. We just discussed a situation which would require the use of both the multiple and multilevel inheritance. Consider a situation where all the three kinds of inheritance, namely, multilevel, multiple and hierarchical inheritance, are involved. This illustrated in fig. The 'child' has two direct base classes 'parent1' and 'parent2' which themselves have a common base class 'grandparent'. The 'child' inherits the traits of 'grandparent' via two separate paths. It can also inherit directly as shown by the broken line. The 'grandparent' is sometimes referred to as indirect base class. Inheritance by the 'child' as shown in fig. might pose some problems. All the public and protected members of 'grandparent' are inherited into 'child' twice, first via 'parent1' and again via 'parent2'. This means 'child' would have duplicate sets of the members inherited from 'grandparent'. This introduces ambiguity and should be avoided. The duplication of inherited members due to these multiple paths can be avoided by making the common base class (ancestor class) as virtual base class while declaring the direct or intermediate base classes . |