One of the key concepts in an object-oriented language like c++ is Inheritance. Just like a child inherit the characteristics of his parents and also have a certain new character of his own. Similarly, in C++ a new class can inherit the data members and member functions of an existing class and can add members of its own. This process of creating a new class from an existing class is called Inheritance.
Now you know what is Inheritance, Its time to know its advantages.
- The main advantage of using inheritance in c++ is the reusability of code.
- This allows the code to be reused and enable changes in the base class. Allowing the changes to take effect in all the relevant places.
- Testing and debugging is much easier at one need to debug the code in the base class rather than doing it everywhere if inheritance is not used.
- Due to reusability, it reduces the amount of the same code to be written again and again.
- It saves time and increases reliability.
The advantages of inheritance can be seen in complex projects where many developers are working as a team. Each developer can use inheritance instead of writing the same code again and again.
They can always extend the existing class to meet the new requirement without having to modify or affect the base class.
The above diagram shows that instead of writing to separate classes with the same features included, we can rather inherit and derive the required features from the base class.
Note 1: Inheritance only works in one direction meaning the derived calls inherit members of the base class but the base class is unaware of anything about the derived class.
Note 2: Also, One should know that there must exist a kind of relationship between delivered the Base class and Derived class. For example, if Car (derived class) from Vehicle (base class) makes sense. But if Car class is derived from Rocket Class then applying inheritance does not make any sense.
You can declare a derived class in c++
//syntax for declaring derived class
class derived_class_name : access_specifier base_class_name
{
//additional code
};
Note 3: The colon operator (:) indicates the derived_class_name is derieved from the base_class_name the access specifier determines the scope of the member of base class. It can be public, private or protected.
Types of inheritance in c++
There are many different forms of inheritance in c++.
- Single Inheritance
- Multi-level Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
Each form of inheritance in c++ differs slightly from one another.
Single Inheritance in C++
This is the simplest form of inheritance. In single inheritance, a class is derived from only one base class. In this form of inheritance, we define one base class and one derived class.
#include <iostream>
using namespace std;
class person // base class
{
private:
char name[20];
long int phno;
public:
void read()
{
cout<<"Enter name and Phno= ";
cin>>name>>phno;
}
void show()
{
cout<<"\nName= "<<name;
cout<<"\nPhone number= "<<phno;
}
};
class student : public person //publicly derived class
{
private:
int rollno;
char course[20];
public:
void read()
{
person::read();
cout<<"\nEnter rollno and course= ";
cin>>rollno>>course;
}
void show()
{
person::show();
cout<<"\nRollno = "<<rollno;
cout<<"\nCourse = "<<course;
}
};
int main()
{
//clrscr();
student s1;
s1.read();
cout<<"\nDisplaying student Information";
s1.show();
//getch();
return 0;
}
Multilevel Inheritance in C++
In multilevel inheritance, we define at least three classes. Further, each derived class must have a kind of relationship with its immediate base class.
Hierarchical Inheritance in C++
In hierarchical inheritance a single base class can be used to derive multiple derived class. This form of inheritance in which more than one class is derived from a single base class is called hierarchical inheritance.
Multiple Inheritance in C++
When we derive a class from two or more base classes then it is known as multiple inheritance. The newly derived class inherits the functionality of two or more base classes, allowing using their features. This form of inheritance is useful in the case where we need to have combine representation/use of features from two or more base classes.
Hybrid Inheritance in C++
There might be a situation where we need to design a class using two or more forms of inheritance. The method of combining two or more forms of inheritance to build a program is known as hybrid inheritance.
Superb explanation
Mary, Hope you got the concept of inheritance in c++.