Inheritance:

Inheritance is an important feature of C++. Inheritance uses a concept Generalization which means extraction of class from generic class. The generic class is the main class or the parent class of the program. Inheritance means to make objects out of other objects. The data members are protected in the parent class or the protected access specifier should be used in the parent class. Let us take a simple example:

inheritance

The class person is the generic class. To link the child class to parent class following method is used:

Suppose circle is a child class and shape is parent class we will link them as:

class circle : public shape

{

…..

}

There are two types of inheritance; multiple inheritance and multi level inheritance:

 

Multiple inheritance

In multiple inheritance there are more than one parent classes. this concept is illustrated as follows:

multiple-inheritance

Here A and B are parent classes. The child of both these classes that is C is defined as follows:

class C : public A, public B;

Note that A and B are not linked together.

 

Multilevel Inheritance

In multilevel inheritance there is a mid class who is a child and a parent class at the same time that is:

multilevel-inheritance

A is the parent of B and B is the parent of C. In other words B is the child of A. C is only child. C is directly linked to B and indirectly to A.

Child classes are derived classes and parent classes are base classes.

 

Function overriding

Which function (having same name) is called is decided by the compiler using scope resolution is called function overriding. For example void print() is used in the parent class and in a child class as well, through the name of the class and the scope resolution the compiler will decide which function is to be called.