Inheritance – The property of one class to derive the properties of another class is known as Inheritance.
SubClass (Base Class) – A subclass is a base class whose properties are inherited.
Superclass(Derived Class) – A superclass is a derived class that inherits the properties of another Class.
Uses of Inheritance –
- We use inheritance for the reusability of code from an existing class. C++ strongly supports the concept of reusability.
- If we want to add or modify the properties of the base class, we can do it without changing them, we can just create a class that inherits the properties of the base class and add some new properties to the derived class.
For Example, a vehicle that has some properties like mileage, capacity, etc is common in all. But some of them have other characteristics like in scooters, there is no need to kick which differs them from other vehicles. So we can say that some common properties can be derived and other properties which are different can be added to the respective classes of vehicle.
There are various modes of inheritance –
- Private mode ( By default ) – If the derived class inherits the properties of the base class in private mode, then both public and protected members will become private in the derived class.
- Public mode – If the derived class inherits the properties of the base class in public mode, then public members of the base class will become public in the derived class while protected members of the base class will become protected in the derived class.
- Protected mode – If the derived class inherits the properties of the base class in protected mode, then both public and protected members will become protected in the derived class.
Private members cannot be accessed directly while protected members can be directly accessed.
Code:
C++ Code
/* Base Class */
class Base
{
public:
int a;
protected:
int b;
private:
int c;
};
/* Some of the Derived classes */
/* First Derived Class - privately inherit the base class 'Base' */
class Derived1:private Base
{
a is accessible - private
b is accessible - private
c is not accessible
};
/* Second Derived Class - Publicly inherit the base class 'Base' */
class Derived2: public Base
{
a is accessible - public
b is accessible - protected
c is not accessible
};
/* Third Derived Class - protectedly inherit the base class 'Base' */
class Derived3: protected Base
{
a is accessible - protected
b is accessible - protected
c is not accessible
}
Types of Inheritance : –
- Single Level Inheritance – In Single Level Inheritance, one subclass inherits the properties of one superclass.
Code:
C++ Code
#include<iostream>
using namespace std;
/* Base Class */
class Teacher
{
public:
string Name;
};
/* Derived Class */
class BioTeacher: public Teacher
{
public:
cout<<Name<<endl;
};
int main()
{
BioTeacher B;
return 0;
}
- Multiple Inheritance – In this type of inheritance, one sub-class inherits the properties of more than one base class.
Code:
C++ Code
#include<iostream>
using namespace std;
/* Base Class 1 */
class Teacher
{
public:
string Name;
};
/* Base Class 2 */
class TeacherDetails
{
public:
int age;
string othersubjecttaught;
}
/* Derived Class */
class BioTeacher: public Teacher,public TeacherDetails
{
public:
cout<<Name<<endl;
cout<<age<<endl;
cout<<othersubjecttaught<<endl;
};
int main()
{
BioTeacher B;
return 0;
}
- Multilevel Inheritance – In this type of inheritance, the subclass inherits from the base class which is also inheriting the properties of other base classes.
Code:
C++ Code
#include<iostream>
using namespace std;
/* Base Class 1 */
class Teacher
{
public:
string Name;
};
/* Derived Class - Deriving the properties of base class 'Teacher'*/
/* Base class for 'BioTeacher' class */
class TeacherDetails: public Teacher
{
public:
int age;
string othersubjecttaught;
}
/* Derived Class for Base class 'TeacherDetails' */
class BioTeacher: public TeacherDetails
{
public:
cout<<Name<<endl;
cout<<age<<endl;
cout<<othersubjecttaught<<endl;
};
int main()
{
BioTeacher B;
return 0;
}
- Hierarchical Inheritance – In this type of inheritance, more than one sub-class is inheriting the base class .
Code:
C++ Code
#include<iostream>
using namespace std;
/* Base Class */
class Teacher
{
public:
string Name;
};
/* Derived Class 1*/
class TeacherDetails:public Teacher
{
public:
int age;
string othersubjecttaught;
cout<<Name<<endl;
}
/* Derived Class 2 */
class BioTeacher: public Teacher
{
public:
cout<<Name<<endl;
};
int main()
{
BioTeacher B;
TeacherDetails T;
return 0;
}
- Hybrid Inheritance – This type of inheritance combines more than one type of inheritance. It is basically the combination of hierarchical and multiple inheritances or hierarchical and multilevel inheritance.
Given diagram shows the combination of hybrid and multiple inheritances.
Code:
C++ Code
#include<iostream>
using namespace std;
/* Base Class */
class Teacher
{
public:
string Name;
};
/* Derived Class 1*/
class TeacherDetails:public Teacher
{
public:
int age;
string othersubjecttaught;
cout<<Name<<endl;
}
/* Derived Class 2 */
class BioTeacher: public Teacher
{
public:
cout<<Name<<endl;
};
/* Derived Class which is further inheriting the two derived classes */
class Teacherproperties:public TeacherDetails,public BioTeacher
{
public:
cout<<age<<endl;
cout<<Name<<endl;
cout<<othersubjecttaught<<endl;
}
int main()
{
Teacherproperties T;
return 0;
}
Special thanks to Gurmeet Singh for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article