One of the best feature that OOP provides is inheritance, where a new class can be constructed from an existing class. The class which is newly constructed is known as derived class and the class from which new class is constructed is known as Base class.

Derived classes
We can derive as many new classes from an existing class and there is no limitation of it.

Inheritance
Inheritance is the property of OOP language which provides code reusability and hence
reduces lines of code in the program.

Types of Inheritance
Single Inheritance:
When a single class is derived from on single base class is known as single inheritance.

Multiple Inheritance:
When one single class is derived from more than one base class is known as multiple
inheritance.

Multilevel Inheritance:
When a class is derived from a class which is derived from some other class for which this
class is a derived class is known as multilevel Inheritance.

Hierarchical Inheritance:
When more than one class is derived from an existing class is known as Hierarchical
inheritance.

Hybrid Inheritance:
An inheritance which is mix of more than one type of inheritance is known as Hybrid
Inheritance.

Multipath Inheritance:
When multiple path exists for class members for multiple or hybrid inheritance for which
duplicity arises for member functions and data members is known as multipath inheritance.

Inheriting in different access Modes
A class can be inheritated in three different modes i.e. Public, Private and Protected mode.
Note: Only Public and Protected members can be inheritated to derived class. In any case
private members can’t be inherited

Private inheritance
When a class is derived in private mode then all the public and protected members will be
private for the derived class.

Public inheritance
When a class is derived in public mode then the protected members will be protected and
public members will be public for the derived class.

Protected inheritance
When a class is derived in protected mode then all the protected members as well as the
public members will be protected for the derived class.
The relation between mode of inheritance and its parent class scope can be classified as
follows

access-specifiers

Multipath Inheritance
This is one type of inheritance where a class can be derived from more than one base class
and that base class is a derived class for some more parent class. In this chain of class
hierarchy, a base class is inherited more than once which result in duplication of members
present in the subsequent classes.

Virtual Base Class
The ambiguity which aries in multipath inheritance is overcome by virtual base class.
Virtual class declaration

Syntax:
Class A{
//class body
}
Class B: virtual public A
{
}

Note1: When classes has been declared virtual in inheritance then the compiler will take
necessary precaution to avoid the duplication of members. Generally we make a class virtual
when it is used more than once in the program.

Note2: virtual keyword can appear after public but it shoud not come after the class name.

Structure and Union work similar to class, the following example will illustrate the
concept of struct, class and union. Inheritance using structure

#include <iostream>
using namespace std;
struct Base
{
int A;
};
struct Derived:public Base
{
int B;
 //Derived()
 //{B=90;}
void display();
};
void Derived::display()
{
cout <<"A = ";
cout <<"B =";
}
int main()
{
 Derived D;
 D.A = 111;
 D.display();
 return 0;
}
Share with : Share on Linkedin Share on Twitter Share on WhatsApp Share on Facebook