Introduction to Inheritance in C++
Let me introduce you to the concept of Inheritance in C++.
Reusability is yet another important feature of C++. It is always nice if we could reuse something that already exists rather than trying to create the same all over again. It would not only save the time and money but also reduce frustration and increase readability. For instance, the reuse of a class that has already been tested, debugged and used many time can save us the effort of developing and testing the same again.
Fortunately, 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 adapted by other programmers to suit their requirements. This is basically done by creating new classes which reuse the properties of the existing ones. The mechanism of deriving a new class from an old one is called inheritance in C++ (or derivation). The old class is referred to as the base class and the new one is called the derived class.
The derived class inherits some or all of the traits from the base class. A class can also inherit properties from more than once class or from more than one level.
A derived class with only one base class is called single inheritance and one with several base classes is called multiple inheritance. On the other hand, the traits of one class may be inherited by more than one class. This process is known as hierarchical inheritance. The mechanism of deriving a class from another ‘derived class’ is known as multilevel inheritance. The above figure shows various forms of inheritance in C++ that could be used for writing extensible programs.