Understanding the Concept of Inheritance in C++

Filed Under: C++
The Concept Of Inheritance In C++

Hi! This time we are here with the topic – Inheritance in C++.

As we know Inheritance is a very important concept or aspect in Object-Oriented Programming.

What is Inheritance?

Theoretically, C++ Inheritance is the capability of one class of things to inherit capabilities or properties from another class.

Inheritance Example
Inheritance Example

Now consider the above Vehicles example. Here, the “Car” inherits some of its properties from the class “Automobiles” which again inherits some properties from the “Vehicles” class. This capability of passing down properties is very useful for supporting the OOP approach.

Hence, C++ Inheritance helps us build closer to the real-world model. The principle behind here is that each subclass shares common characteristics with the class from which it is derived.

Again for the example above, ‘Automobiles’ and ‘Pulled Vehicles’ are sub-classes of ‘Vehicles’. On the other hand, ‘Vehicles’ is the base class for them.

Similarly, ‘Automobiles’ is the base-class of ‘Car’ and ‘Bus’. And ‘Car’ and ‘Bus’ are sub-classes of ‘Automobiles’.

C++ Inheritance

Syntax for C++ Single Inheritance is given below:

class derived_class_name: access_mode base_class_name
{ 
    /*body or properties*/
};

Here, derived_class_name is the name of the sub-class and base_class_name is the name of the base class. The access_mode or the access specifier defines which members of the base class are accessible from the derived classes. You can follow the given table for C++ access specifier definitions.

Access Modepublicprotectedprivate
members of the same classYesYesYes
members of derived classYesYesNo
not membersYesNoNo
Inheritance – Access Modes and Accessibility

Considering the ‘Vehicles’ example again(this time just the ‘Vehicles’, ‘Automobiles’ and ‘Pulled Vehicles’ classes). We can represent this C++ single level inheritance as shown in the code snippet below.

#include<iostream>  
#include<cstring>  
using namespace std;

class Vehicles //base-class
{
	// public members	
	public:
		int No_of_wheels;
		void set_wheels(int w)
		{
			No_of_wheels = w;
		}
};

class Automobiles : public Vehicles // sub-class(inherits from Vehicles)
{
	// members	
	public:
		string fuel;
		void set_fuel_type(string str)
		{
			fuel = str;
		}
};

class pulled_vehicles : public Vehicles // sub-class(inherits from Vehicles)
{
	// members	
	public:
		string puller;
		void set_puller(string str)
		{
			puller = str;
		}
};

int main()
{  
   Automobiles A1; // Automobiles object created
   A1.set_wheels(4); // member function of Vehicles
   A1.set_fuel_type("Petrol"); // member function of Automobiles
   
   pulled_vehicles P1; // pulled_vehicles object created
   P1.set_wheels(3); // member function of Vehicles
   P1.set_puller("Horse"); // member function of pulled_vehicles
   
   // A1 properties
   cout<<"A1:"<<endl;
   cout<<"No. of wheels = "<<A1.No_of_wheels<<endl;
   cout<<"Fule Type = "<<A1.fuel<<endl;
   
   // P1 properties
   cout<<"\nP1:"<<endl;
   cout<<"No. of wheels = "<<P1.No_of_wheels<<endl;
   cout<<"Pulled by = "<<P1.puller<<endl;
   
   return 0;
}

Output:

A1:
No. of wheels = 4
Fule Type = Petrol

P1:
No. of wheels = 3
Pulled by = Horse

C++ Multiple Inheritance

A class can also be derived from more than one base class. In that case, the subclass inherits the properties from all the base-classes. This feature in C++ is termed as Multiple Inheritance.

For using this C++ feature, the base classes along with their access modes should be separated by a comma(','). The syntax for doing so can be given as:

class derived_class_name: access_mode1 base_class_name1, access_mode2 base_class_name2, ... , access_modeN base_class_nameN
{ 
    /*body or properties*/
};

Now let us take an example where a class(C) inherits properties from two other classes(base-classes A and B). We can visualize this simple multiple inheritance model as shown below.

Multiple Inheritance 1
Multiple Inheritance
#include<iostream>  
#include<cstring>  
using namespace std;

class A //base-class for C
{
	public:
		void A_print()
		{
			cout<<"We are in class A"<<endl;
		}
};

class B // Another base-class for C
{
	public:
		void B_print()
		{
			cout<<"We are in class B"<<endl;
		}
}; 

class C : public A, public B // Multiple Inheritance
{
	//body
};
int main()
{  
   C c1; // object of class C
   c1.A_print();
   c1.B_print();
   
   return 0;
}

Output:

We are in class A
We are in class B

As you can see, here we are able to access the public members of the base classes A and B from C . Hence, it is clear that C inherits all the properties from its base classes(A and B).

Importance of Inheritance in C++

  • Real-world Resemblance – The concept of Inheritance in any OOP language gives a user the capability to express inheritance relationship which makes it ensure the closeness with the real-world models(as mentioned earlier),
  • Reusability – It also allows the addition of additional features to an existing class without modifying it,
  • Transitive nature – Inheritance is transitive. For example, if class A inherits properties of another class B, then all the sub-classes of A will automatically inherit the properties of B. This property is called transitive nature of Inheritance.

Summing Up

So, that’s it for today. Hope you had a clear understanding of the concept of Inheritance in C++.

We recommend you to go through our C++ Tutorial for more info.

For any further questions related to this topic, feel free to use the comments below.

References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages