Inheritance and types of Inheritance in C++

Inheritance is one of the most important features of object-oriented programming.

Inheritance allows an object of one class to acquire the properties of another object of another class.

It supports reusability and supports hierarchical classification. 

The class that inherits the other class is called the derived class and the class that inherited is called the base class.

 Syntax of creating a derived class:

class derived_class_name: access_specifier base_class_name {}

Example:

        
 class B : public A	// public derivation								{										}	
 

In the above syntax, class A is a base class, class B is a derived class. Here class B is derived publicly.

Example:

 
class B : private A	// private derivation								{										}
 

In the above syntax, class A is a base class, class B is a derived class.

Here class B is derived privately.

Example:

class B :  A	// by default private derivation								{										}
 

If no access specifier is given, by default derivation is private.

Public & Private Inheritance: In C++ when a class derived and public access specifier is used.

All the public members of base class can be accessed directly in the derived class but when private access specifier is used, then even public members of base class can not access directly in the derived class

Public Inheritance: When a public access specifier is used, the public member of the base class are the public member of the derived class.

Example:

#include 
using namespace std;
class A { //   Base class							
    public: int x;
};
class B: public A { //   Derived class 
    public: int y;void show() {
        x = 10;
        y = 20; // x is also public member of derived class B	
        cout << x << "  " << y;
    }
};
int main() {
    B obj;
    obj.show();
}

Output

 
10 20
 

Description: In the above program, class B derives class A  means class A is a base class, and class B is a derived class. 

During inheriting a class public access specifier is used therefore public member of base class A can directly access in derived class B. 

In the above program “x” is a public member of base class A but it can directly access in derived class B.

Private Inheritance: When a class-derived and private access specifier is used, then an object of the derived class has no permission to access even a public member of the base class directly in the main() function.

In such a case, the public member of a base class can be accessed using public member function of the derived class.

Example: When a class-derived and private access specifier is used, then an object of the derived class has no permission to access even a public member of the base class directly in the main() function.

#include 
using namespace std;
class A { //   Base class
    public: int x;
};
class B: private A { //   Derived class
    public: int y;
};
int main() {
    B obj;
    obj.x = 10; // error, x is not accessible here because x is a private member of derived class
    obj.y = 20;
    cout << obj.x; // error, x is not accessible here because x is a private member of derived class
    cout << obj.y;
}

Output

 
 obj.x = 10; // error, x is not accessible here because x is a private member of derived class
 cout << obj.x; // error, x is not accessible here because x is a private member of derived class
 

The solution of such a problem is that the public member of a base class can be accessed using public member function of the derived class.

#include 
using namespace std;
class A { //   Base class
    public: int x;
};
class B: private A { //   Derived class  
    public: int y;
    void show() {
        x = 10;
        y = 20; // x is private member of derived class & accessible here		
        cout << x << "  " << y;
    }
};
int main() {
    B obj;
    obj.show();
}

Output

 
10  20
 

Derived class can not access private data of base class

In C++  programming,derived class can’t access the private data of the base class.

If we will try to do so we will get an error message.

Example: In the below program we are trying to access the private data of base class in a derived class.

#include 
using namespace std;
class A { //   Base class
    private: int x;
};
class B: public A { //   Derived class  
    public: int y;
    void show() {
        x = 10;
        y = 20; //error, x is private member of derived class		
        cout << x << "  " << y; //error, x is private member of derived class
    }
};
int main() {
    B obj;
    obj.show();
}

Output

 error: ‘int A::x’ is private within this context
 cout << x << "  " << y; //error, x is private member of derived class
 

Protected access specifier with public  derivation: when a protected access specifier is used, the protected member of the base class are the protected member of the derived class.  

Protected access specifier: A protected access specifier is similar to a private only difference is that it has access to their derived classes. 

Example 1

#include 
using namespace std;
class A { //   Base class
    protected: int x;
};
class B: public A { //   Derived class
    public: void show() {
        x = 10; //x is accessible here because x is protected member of derived class	
        cout << x;
    }
};
int main() {
    B obj;
    obj.show();
}

Output

 
10
 

Example 2

#include 
using namespace std;
class A { //   Base class
    protected: int x;
};
class B: public A { //   Derived class  
    protected: int y;
};
int main() {
    B obj;
    obj.x = 10; // error, x is not accessible here because x is a protected member of derived class                       
    obj.y = 20; //error	
    cout << obj.x << " " << obj.y; //error
    return 0;
}

Output

 
error: ‘int A::x’ is protected within this context
    obj.x = 10; // error, x is not accessible here because x is a protected member of derived class
 

Types of Inheritance in C++

There are the following types of inheritance supported by C++:

  1.  Single Inheritance                                                        
  2.  Multilevel Inheritance
  3.  Multiple Inheritance
  4.  Hierarchical Inheritance
  5. Hybrid Inheritance
  6. Multipath Inheritance

Single inheritance in C++

When only one class is derived a class and derived class is not used as a base class Such types of inheritance are called Single inheritance.

Here, X is a base class, and Y is a derived class. In a single inheritance, there is only one base class and one derived class.

Single Inheritance
Single Inheritance

Multilevel Inheritance in C++

When a class derived from another derived class such type of inheritance is called multilevel inheritance.

Multileve Inheritance
Multileve Inheritance

Here X is s base class for Y, Y is a derived class from X and Y is also base class for Z. Z is a derived class from Y.

Multiple Inheritance in C++

When a single class is derived from two or more than two classes then such type of inheritance is called multiple inheritance.

X & Y are the base class for Z, and Z is the derived class. Class Z  is derived from Class X & Y.

Hierarchical Inheritance:

When two or more classes are derived from a single base class is known as hierarchical inheritance.

W is the only base class and X, Y & Z are derived classes. X, Y & Z are derived from W.

Hybrid Inheritance in C++

The combination of two or more inheritance are called hybrid inheritance.

In this type two types of inheritance are use single inheritance and multilevel inheritance.          

Single Inheritance:  W is a base class, Z is a derived class. Single inheritance has only one base class and one derived class.                   

Multilevel Inheritance:X is s base class for Y, Y is a derived class from X and base class for Z. Z is a derived class from Y.

Multipath Inheritance : When a class derived from two or more classes that are derived from same base class such type of inheritance is known as multipath inheritance.

W is a base class and X & Y are derived class. The class X & Y are the derived  from W. 

Further Z is derived from X & Y. means X & Y  is a base class for Z and Z is a derived class.

Here, ambiguity is generated. Hence, virtual keyword is used to avoid ambiguity.

Single Inheritance

Single inheritance: When only one class derived a class and derived class is not used as a base class such type of inheritance are called Single inheritance.

Single Inheritance
Single Inheritance

 Here, X is a base class, Y is a derived class. In a single inheritance there is only one base class and one derived class.

Example:   Write a program to create a base class student and derived class record to read student record such as name, roll number, age  & height and display by using single inheritance.

#include 
using namespace std;
class student { // base class
    protected:
        char name[20];
    int rollno;
};
class record: public student { // derived class
    public: float height;
    int age;
    void getdata() {
        cout << " Enter Name, Roll number, Height & Age";
        cin >> name >> rollno >> height >> age;
    }
    void showdata() {
        cout << "Name= " << name << endl;
        cout << "Roll number =" << rollno << endl;
        cout << "Height=" << height << endl;
        cout << "Age =" << age << endl;
    }
};
int main() {
    record obj;
    obj.getdata();
    obj.showdata();
}

Output

 
 Enter Name, Roll number, Height & Age 
Ram
2 
5.6
22

Name= Ram
Roll number =2
Height=5.6
Age =22
 

Note: In the above program, we have created base class student and derived class record.

Student class holds the protected data member name & rollno(A protected data member  is similar to the private only difference is that it has a access to their derived classes).

Class record is derived from student means that it inherits all properties of student class.

Multilevel Inheritance :When a class derived from another derived class such type of inheritance is called multilevel inheritance.

Multileve Inheritance
Multileve Inheritance

Here X is s base class for Y, Y is a derived class from X and Y is also base class for Z. Z is a derived class from Y.

Example:   Write a program to read student record such as name, roll number & age and display by using the concept of multilevel  inheritance, create classes A1, A2 & A3.

#include 
using namespace std;
class A1 { // base class		
  protected: char name[20];
};
class A2: public A1 { // base class for A3 & derives class from A1	
  protected: int rollno;
};
class A3: public A2 { // derived class from A2
  protected: int age;
  public:
  void getdata() {
    cout << "Enter name, roll number & age";
    cin >> name >> rollno >> age;
  }
  void showdata() {
    cout << "name = " << name << endl;
    cout << "roll number = " << rollno << endl;
    cout << "age = " << age << endl;
  }
};
int main() {
  A3 obj;
  obj.getdata();
  obj.showdata();
  return 0;
}

Output

Enter name, roll number & age
name = Ram
roll number = 2
age = 23

 

Multiple Inheritance :  When a single class is derived from two or more then two class then such type of inheritance is called multiple inheritance

X & Y are the base class for Z , Z is deriver class. Class Z  is derived from Class X & Y.

Example:   Write a program to derive a class from multiple base classes.

#include 
using namespace std;
class A { // base class									
  public: int a;
};
class B { // base class	
  public: int b;
};
class C { // base class						
  public: int c;
};
class D: public A, public B, public C { // derived class D
  public: int d;void getdata() {
    cout << " Enter values of a, b, c, d ";
    cin >> a >> b >> c >> d;
  }
  void showdata() {
    cout << "Values of a, b, c, d = " << a <<" " << b<<" " << c<<"  "<< d;
  }
};
int main() {
  D obj;
  obj.getdata();
  obj.showdata();
  return 0;
}

Output

 
  Enter values of a, b, c, d
  10	// enter by user	
  20	// enter by user
  30	// enter by user
  40	// enter by user	 
  Values of a, b, c, d= 10  20  30  40
 

Note: In this program, A, B & C are the base class and class D is derived class from A ,B & C .

Class D inherits the properties of class A, B & C.

Hierarchical Inheritance: When a two or more classes are derived from a single base class is  known as hierarchical inheritance

W is a only base class and X,Y & Z are derived class. X,Y & Z are the derived from W.

Example:   Write a program to show the hierarchical inheritance.

#include 
using namespace std;
class A { // base class A					
  public: int a;
};
class B: public A { // derived class	B from class A
  public: int b;
  void showB() {
    a = 10;
    b = 20;
    cout << "a + b = " << (a + b)<Output

 
a + b = 30
a + c = 40
a + d = 50
 

Note: A is a only base class. B, C & D are the derived class from A. Class B,C & D all inherits the properties of class A.

Hybrid Inheritance :  The combination of two or more inheritance are called hybrid inheritance.

Hybrid Inheritance
Hybrid Inheritance

In this type two types of inheritance are use single inheritance and multilevel inheritance. 

  Single Inheritance:  W is a base class, Z is a derived class. Single inheritance has only one base class and one derived class. 

Multilevel Inheritance:                X is s base class for Y, Y is a derived class from X and base class for Z. Z is a derived class from Y.   

Example:   Write a program to show the hybrid inheritance. 

or

Write a program to create a derived class from multiple base class.

 or

 Write a program to create hybrid inheritance and take data members such as name of the player, age, city and game name.

Hybrid Inheritance Example
Hybrid Inheritance Example
#include 
using namespace std;
class location { // base class  
  public: char city[20];
};
class player { // base class	
  public: char name[20];
};
class year: public player { //   derived class from player & base class for game  
  public: int age;
};
class game: public year, public location { //  derived class from player & location.
  public: char game_name[20];
  void getdata() {
    cout << "Enter name, age, city & game name";
    cin >> name >> age >> city >> game_name;
  }
  void showdata() {
    cout << "name = " << name << endl;
    cout << "age = " << age << endl;
    cout << "city = " << city << endl;
    cout << "game name = " << game_name << endl;
  }
};
int main() {
  game ob;
  ob.getdata();
  ob.showdata();
}

Output

 
Enter name, age, city & game name
name = Ram
age = 23
city = Bhopal
game name = Chess
 

Note: In this type two types of inheritance are used single inheritance and multilevel inheritance.

Single Inheritance: Location is a base class, and game is a derived class.

Single-level inheritance has only one base class and one derived class.

The class game inherits the properties of the class location.

Multilevel Inheritance: the player is a base class for Year, Year is a derived class from player .

year is a  base class for game. game is a derived class from Year. Class game inherits the properties of both  class player & year.

Multipath Inheritance : When a class derived from two or more classes that are derived from same base class such type of inheritance is known as multipath inheritance. 

  W is a base class and X & Y are derived class. The class X & Y are the derived  from W.  Further Z is derived from X & Y.

Means X & Y  is a base class for Z and Z is a derived class. Here, ambiguity is generated.

Hence, virtual keyword is used to avoid ambiguity.

Multipath Inheritance Example
Multipath Inheritance Example

Example:

class A1 {
  protected: int a1;
};
class A2: public A1 {
  protected: int a2;
};
class A3: public A1 {
  protected: int a3;
};
class A4: public A2, public A3 {
  protected: int a4;
};

In the above example, A1 is a base class. The class A2 & A3 are the derived  from class A1. Both A2 and A3 can access the variables/data of class A1 .

Further A4 is derived from A2 & A3.

If we try to access variable a1 of class A1 from class A4, Here, ambiguity is generated &  compiler shows error message.  

Member is ambiguous : “A1:a1” and “A1:a1”

The derived class A4 has two sets of data member of class A1 through the middle base classes A2 and A3.

The A1 is inherited twice.

Hence, virtual keyword is used to avoid ambiguity.

Read More

Function Overriding in C++ with Example

Abstract Class in C++

Categories C++