Home >>C++ Tutorial >C++ Inheritance

C++ Inheritance

Inheritance in C++

The procedure in which all the properties and behaviors of the parent class is acquired by a preceding class automatically is known as the inheritance in C++. The attributes and behaviors can be reused; extended or modified those are basically defined in the other class.

Derived class is basically the class that inherits the members of another class and on the other hand the class whose members are inherited is known as the base class. Please note that for the base class, derived class is the specialized class.

Advantage of C++ Inheritance

Here are the advantages of the inheritance in C++:

Code reusability : The members of the parent class can be used multiple times hence; the need to define the member again and again can be neglected. This is the reason that there is less amount of code is required in the class.

Types Of Inheritance in C++

Here are the types of inheritance that are supported by the C++ programming:

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance

Derived Classes

The class that is derived from the base class is known as derived class.

Here is the syntax of the derived class:

class derived_class_name :: visibility-mode base_class_name  
{  
    // body of the derived class.  
}  

Here is the breakdown of the above syntax:

derived_class_name : It is basically the name of the derived class.

visibility mode : This mode is used is used to specify whether the features presented in the base class are publicly inherited or privately inherited as it can be public or private.

base_class_name : It is basically the name of the base class.

  • Whenever the base class is privately inherited by the derived class, the members that are public members of the base class generally become the private members of the derived class. Hence, the public members of the base class cannot be accessed by the objects of the derived class and can be accessed only by the derived class’s member functions.
  • Whenever the base class is publicly inherited by the derived class, the members that are public members of the base class generally become the public members of the derived class. Hence, the public members of the base class can be accessed by the objects of the derived class and can be accessed only by the derived class’s member functions.

 

Please Note :

  • The default visible mode is private in C++
  • The base class’s private members are never inherited.

C++ Single Inheritance

The inheritance where a derived class is generally inherited from a specific base class, is known as single inheritance in C++.

single inheritance

 

Here are the examples of the single inheritance in C++ that will explain the concept in brief that is better for application:

 

#include <iostream>  
using namespace std;  
 class student 
 {  
   public:  
   string name = "Test";   
 };  
   class course: public student 
   {  
   public:  
   string c_name = "C++";    
   };       
int main(void) {  
     course stu;  
     cout<<"Name  = "<<stu.name<<endl;    
     cout<<"Course name = "<<stu.c_name<<endl;    
    return 0;  
}
Output :
Name = Test
Course name = C++

C++ Multilevel Inheritance

The process in which a class is derived from another derived class is known as multilevel inheritance. Whenever a class is inherited by another class and the process repeats itself then it is known as the multi-level inheritance in C++.

Multilevel inheritance

 

Here is the example of the multi-level inheritance:

 

#include <iostream>  
using namespace std;  
 class student 
 {  
   public:  
   string name = "Test";   
 };  
	class course: public student 
	{  
	public:  
	string c_name = "C++";    
	};
	class profile: public course 
	{  
	public:  
	string p_name = "Web Developer";    
	};
   
int main(void) {  
     profile stu;  
     cout<<"Name  = "<<stu.name<<endl;    
     cout<<"Course name = "<<stu.c_name<<endl;
	 cout<<"Profile = "<<stu.p_name<<endl;	 
    return 0;  
}
Output :
Name = Test
Course name = C++
Profile = Web Developer

C++ Multiple Inheritance

The procedure in which a new class is derived from two or more classes and it inherits the attributes of them is known as the multiple inheritance in C++.

Multiple inheritance
Here is the syntax of the multiple inheritance in C++:

class D : visibility B-1, visibility B-2, ?  
{  
    // Body of the class;  
}   

Here is an example :

#include <iostream>  
using namespace std;  
 class student 
 {  
   public:  
   string name = "Test";   
 };  
	class course 
	{  
	public:  
	string c_name = "C++";    
	};
	class profile: public student,public course 
	{  
	public:  
	string p_name = "Web Developer";    
	};
	
	int main(void) 
	{  
     profile stu;  
     cout<<"Name  = "<<stu.name<<endl;    
     cout<<"Course name = "<<stu.c_name<<endl;
	 cout<<"Profile = "<<stu.p_name<<endl;	 
    return 0;  
}
Output :
Name = Test
Course name = C++
Profile = Web Developer

Ambiguity Resolution in Inheritance

Whenever a function that has the same name occurs in more than one base class then it is known as the Ambiguity in C++.

Here is an Example

#include   
using namespace std;  
class Demo  
{  
    public:  
    void show()  
    {  
        std::cout << "Class Demo" << std::endl;  
    }  
};  
class Demo1  
{  
    public:  
    void show()  
    {  
        std::cout << "Class Demo1" << std::endl;  
    }  
};  
class Demo2 : public Demo, public Demo1  
{  
    void display()  
    {  
        show();  
    }  
};  
int main()  
{  
    Demo2 obj;  
    obj.show();  
    return 0;  
}  
Output :

main.cpp:23:9: error: reference to 'show' is ambiguous

C++ Hybrid Inheritance

The combination of more than one type of inheritance is known as the hybrid inheritance.

Hybrid inheritance

 

Example

 

#include <iostream>  
using namespace std;  
class Demo  
{  
    protected:  
    int x;  
    public:  
    void get_x()  
    {  
       std::cout << "Pls enter the value of 'x' " << std::endl;  
       cin>>x;  
    }  
};  
  
class Demo1 : public Demo   
{  
    protected:  
    int y;  
    public:  
    void get_y()  
    {  
        std::cout << "Pls Enter the value of 'y'  " << std::endl;  
       cin>>y;  
    }  
};  
class Demo2  
{  
    protected:  
    int z;  
    public:  
    void get_z()  
    {  
        std::cout << "Pls Enter the value of 'z' " << std::endl;  
        cin>>z;  
    }  
};  
  
class Demo3 : public Demo1, public Demo2  
{  
    protected:  
    int m;  
    public:  
    void mult()  
    {  
         get_x();  
         get_y();  
         get_z();  
         std::cout << "Multip of x,y and z is  " <<x*y*z<< std::endl;  
    }  
};  
int main()  
{  
    Demo3 obj;  
    obj.mult();  
    return 0;  
}  
Output :
Pls enter the value of 'x' 10
Pls Enter the value of 'y' 10
Pls Enter the value of 'z' 10
Multiplication of a,b,c is : 1000

C++ Hierarchical Inheritance

The process in which more than one class is derived from a base class is known as the Hierarchical Inheritance in C++.

Hierarchical inheritance

 

Here is the Syntax

 

class Demo  
{  
    //Define the  body of class Demo.  
}    
class Demo1 : public Demo   
{  
      //Define the  body of class Demo1. 
}  
class Demo2 : public Demo 
{  
      //Define the  body of class Demo2.
}   
class Demo3 : public Demo  
{  
      //Define the  body of class Demo3.  
}   

Here is the Example

#include <iostream> 
using namespace std;
class A
{
    public:
 	int x, y;
 	void get_data()
 	{
   	    cout << "Please Enter the value of x and y ";
		cin >> x >> y;
 	}
};
class B : public A 
{
    public:
 	void mult()
 	{
 	    cout << "Multiplication of xn and y = " << x * y;
 	}
};
class C : public A
{
    public:
 	void add()
 	{
        cout << "Addition of x and Y= " << x + y;
 	}
};
int main()
{
    B obj;          
    C obj1;          
    obj.get_data();
    obj.mult();
    obj1.get_data();
    obj1.add();
    return 0;
}
Output :
Please Enter the value of x and y 10 20
Multiplication of xn and y = 200
Please Enter the value of x and y 10 20
Addition of x and Y= 30

Popular Tutorials