Biologically, Inheritance is a term which means acquiring characters of parents by their offspring. Children often seems to have traits that their parents have like similar eye color, style of speaking, walking, etc. Additionally, they have their own traits too.
Similarly in Object Oriented Programming, Inheritance is the process of inheriting properties of objects of one class by objects of another class. The class which inherits the properties of another class is called Derived or Child or Sub class and the class whose properties are inherited is called Base or Parent or Super class.
Syntax of Inheritance in C++
class base_classname { properties... methods... }; class derived_classname : visibility_mode base_classname { properties... methods... };
For example: Car, Bicycle, Motorcycle are all vehicles and have many similar properties like tire, brakes, seat, etc. So they can be derived from class Vehicle. Therefore, vehicle is base class and car, bus, motorcycle are derived classes.
class Vehicle { //Properties public: int numOfTires; bool BrakesWorking; int numOfSeats; //Methods public: void GoLeft(); void GoRight(); void Stop(); }; class Bicycle : public Vehicle { //All Vehicle properties are inherited //Additional Properties public: int numOfPedals; //Additional Methods public: void Jump(); };
Implementing inheritance helps in code reuse. We don’t need to rewrite same methods and properties for derived class once it has already been declared or defined in the parent class. Hence, inheritance is a useful concept that saves time for writing same code again and again.
Inheriting a class to create a new class doesn’t necessarily mean all the properties of parent class are inherited. The visibility mode or access specifier determines which properties are inheritable. There are 3 access specifier in C++: private, public and protected. Private members (properties/features and methods) are not inherited to the child class while protected members are only inherited to the immediate child class. Likewise, Public members are inherited to all derived classes.
According to the visibility mode specified in the derived class declaration statement, the members of base class are inherited in the derived class. It can be illustrated from the figure below.
From this figure, we can conclude the following things:
- If the derived class is inherited publicly, the protected members of base class becomes protected member of derived class and the public members of base class becomes public member of derived class.
- If the derived class is inherited protectedly, the protected and public members of base class becomes protected member of derived class.
- If the derived class is inherited privately, the protected and public members of base class becomes private member of derived class.
Example of C++ Inheritance
C++ Program to Inherit a Student class from Person Class printing the properties of the Student
#include <iostream> #include <conio.h> using namespace std; class person /*Parent class*/ { private: char fname[100],lname[100],gender[10];
protected: int age; public: void input_person(); void display_person(); }; class student: public person /*Child class*/ { private: char college_name[100]; char level[20]; public: void input_student(); void display_student(); }; void person::input_person() { cout<<"First Name: "; cin>>fname; cout<<"Last Name: "; cin>>lname; cout<<"Gender: "; cin>>gender; cout<<"Age: "; cin>>age; } void person::display_person() { cout<<"First Name : "<<fname<<endl; cout<<"Last Name : "<<lname<<endl; cout<<"Gender : "<<gender<<endl; cout<<"Age : "<<age<<endl; } void student::input_student() { person::input_person(); cout<<"College: "; fflush(stdin); gets(college_name); cout<<"Level: "; cin>>level; } void student::display_student() { person::display_person(); cout<<"College : "<<college_name<<endl; cout<<"Level : "<<level<<endl; } int main() { student s; cout<<"Input data"<<endl; s.input_student(); cout<<endl<<"Display data"<<endl; s.display_student(); getch(); return 0; }
Output
Input data First Name: Harry Last Name: Potter Gender: Male Age: 23 College: Abc International College Level: Bachelors Display data First Name : Harry Last Name : Potter Gender : Male Age : 23 College : Abc International College Level : Bachelors
In the above example, we have a class person with attributes fname (first name), lname (last name), gender and age and methods input_person() to input data and display_person() to display data. Another class student is derived from person which has college_name and level as attributes and input_student() and display_student() as methods to input and display data respectively.
Here, person is base class and student is derived class. Since, person has publicly inherited student members, the private members fname, lname and gender are not inherited. The protected member age is inherited as a protected member in student, and public member functions input_person() and display_person() are inherited as public members. This is the simplest example of inheritance.
Note: The private members are not inherited to the derived class but can be accessed using their public setter-getter methods.
Types of Inheritance
Inheritance are of following types.
- Single Inheritance: If a single child is derived from single parent class, it is called single inheritance. It is the simplest type of inheritance. For e.g. Typist is derived from staff.
- Multiple Inheritance: If a single child is derived from more than one parents, it is called multiple inheritance. For e.g. Petrol is derived from liquid and fuel.
- Hierarchical Inheritance: If more than one classes are derived from a single parent class, it is called hierarchical inheritance. For e.g. Employee, student, teacher are derived from class person.
- Multilevel Inheritance: If a class is derived from another derived class, it is called multilevel inheritance i.e. at least one class must have a parent and a child. For e.g. Lion is derived from class animal and animal is derived from class living things.
- Hybrid Inheritance: The combination of more than one type of inheritance is called hybrid inheritance. For e.g. Employee, student, teacher are derived from class person(hierarchical) and person is derived from class LivingThing(single).