CPP Language / Inheritance and its Types

It means acquiring the properties of Super / parent / Base /Root class by Derived /sub / child / leaf class.

Types of Inheritance
1. Single inheritance.
2. Multiple inheritance.
3. Hierarchical inheritance.
4. Multilevel inheritance.
5. Hybrid inheritance.

Inheritance Access specifiers
Member Private Protected Public
Private Yes Yes No
Protected Yes Yes No
Public Yes No No
Accessibility of base class members in derived class.

Single Inheritance (A<--- B)
Program Output

#include
using namespace std;
class A
{
public:
A(){ cout<<"A class Constructor"< void AM(){cout<<"A class Method"< };

class B: public A
{
public: B(){ cout<<"B class Constructor"< void BM(){ cout<<"B class Method"< };

int main()
{
B bobj;
bobj.AM();
bobj.BM();
return 0;
}


A class Constructor
B class Constructor
A class Method
A class Method

Explanation
B bobj;

Output
A class Constructor
B class Constructor
Method Output
bobj.AM();-->A class Method
bobj.BM();-->B class Method


Multilevel Inheritance (A<--- B<---C)
Program Output

#include
using namespace std;
class A
{
public:
A(){ cout<<"A class Constructor"< void AM(){ cout<<"A class Method"< };

class B: public A
{
public:
B(){ cout<<"B class Constructor"< void BM(){ cout<<"B class Method"< };

class C: public B
{
public:
C(){ cout<<"C class Constructor"< void CM(){ cout<<"C class Method"< };

int main()
{
C Cobj;
Cobj.AM();
Cobj.BM();
Cobj.CM();
return 0;
}


A class Constructor
B class Constructor
C class Constructor
A class Method
B class Method
C class Method

Explanation
B Cobj;

Output
A class Constructor
B class Constructor
C class Constructor

Method Output
Cobj.AM();-->A class Method
Cobj.BM();-->B class Method
Cobj.CM();-->C class Method


Multiple Inheritance (A<---C, B<---C)
Program Output

#include
using namespace std;
class A
{
public:
A(){ cout<<"A class Constructor"< void AM(){ cout<<"A class Method"< };

class B
{
public:
B(){ cout<<"B class Constructor"< void BM(){ cout<<"B class Method"< };

class C: public A, public B
{
public:
C(){ cout<<"C class Constructor"< void CM(){ cout<<"C class Method"< };

int main()
{
//Creating object of class C
C Cobj;
Cobj.AM();
Cobj.BM();
Cobj.CM();
return 0;
}


A class Constructor
B class Constructor
C class Constructor
A class Method
B class Method
C class Method

Explanation
B Cobj;
Output
A class Constructor
B class Constructor
C class Constructor

Method Output
Cobj.AM();-->A class Method
Cobj.BM();-->B class Method
Cobj.CM();-->C class Method

Hierarchical Inherit Inheritance (A<---B, A<---C)
Program Output

#include
using namespace std;
class A
{
public:
A(){ cout<<"A class Constructor"< void AM(){ cout<<"A class Method"< };

class B: public A
{
public:
B(){ cout<<"B class Constructor"< void BM(){ cout<<"B class Method"< };

class C: public A{
public:
C(){ cout<<"C class Constructor"< void CM(){ cout<<"C class Method"< };

int main()
{
//Creating object of class C
C Cobj;
B Bobj;
Cobj.AM();
Bobj.BM();
Cobj.CM();
return 0;
}


A class Constructor
C class Constructor
A class Constructor
B class Constructor
A class Method
B class Method
C class Method

Explanation
C Cobj;
B Bobj;
A class Constructor
C class Constructor
A class Constructor
B class Constructor

Method Output
Cobj.AM();-->A class Method
Bobj.BM();-->B class Method
Cobj.CM();-->C class Method

Hybrid Inheritance
It is a combination of more than one type of inheritance. Example, A child and parent class relationship that follows multiple and hierarchical inheritance .



Home     Back