Inheritance is a core pillar of object-oriented programming (OOP). A class inherits variables and member functions of another class and allows code reusability.
Inheritance allows a new class to build upon an existing class. The newly created class is called a derived class, and the existing class is called a base class. The derived class can access the variables and functions of the base class after inheritance.
The syntax of inheritance in C++ is shown below.
// Base classclass Person{public:string name;int age;};//Derived class// Derived_class_name: access_modifier Base_class_nameclass Student: public Person{public:int studentID;};
The basic syntax, as shown above, is to add a colon in front of the derived class name, then an access modifier, and then the base class name.
The modes of inheritance depend on the access modifier used during inheritance. There are three modes of inheritance:
Public
Protected
Private
These modes control the visibility of the base class elements to the derived class.
Public inheritance keeps the visibility of variables and member functions in the derived class. This means that access to the variables in the derived type depends upon the base class’s access modifier, as seen in the table below:
Protected inheritance reduces the visibility of public variables from public to protected in the derived class. The visibility of protected and private variables remains the same, as seen in the table below:
Private inheritance changes the visibility of all variables to private. This means that the variables in the base class cannot be accessed from the derived class irrespective of their access modifiers, as seen in the table below:
There are multiple types of inheritance in C++, as listed below:
Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical inheritance
Hybrid inheritance
Single inheritance contains two classes, one base class and one derived class, as seen in the example above. The structure of single inheritance is as follows:
The syntax of single inheritance is as follows:
class A{ /* Variables and functions */ };class B: public A{ /* Variables and functions */ };
Note: The access modifier can be changed from public to protected or private, depending on the mode of inheritance required.
Multiple inheritance contains three classes, two base classes, and one derived class. The structure of multiple inheritance is as follows:
The syntax of multiple inheritance is as follows:
class A{ /* Variables and functions */ };class B{ /* Variables and functions */ };class C: public A, public B{ /* Variables and functions */ };
Multilevel inheritance contains three classes, where the derived classes inherit from another derived class. The structure of multilevel inheritance is as follows:
The syntax of multilevel inheritance is as follows:
class A{ /* Variables and functions */ };class B: public A{ /* Variables and functions */ };class C: public B{ /* Variables and functions */ };
Hierarchical inheritance contains three classes, where two derived classes are inherited from the same base class. The structure of hierarchical inheritance is as follows:
The syntax of hierarchical inheritance is as follows:
class A{ /* Variables and functions */ };class B: public A{ /* Variables and functions */ };class C: public A{ /* Variables and functions */ };
Hybrid inheritance contains multiple classes which follow more than one type of inheritance. The structure of hybrid inheritance is as follows:
The syntax of hybrid inheritance is as follows:
class A{ /* Variables and functions */ };class B: public A{ /* Variables and functions */ };class C: public A{ /* Variables and functions */ };class D{ /* Variables and functions */ };class E: public C, public D{ /* Variables and functions */ };
Let’s consider some types of vehicles we can create classes for, such as cars, buses, and trucks.
Now, let’s think of some very basic functions of these vehicles that can be implemented in the classes:
Start_Engine()
Accelerate()
Apply_Brakes()
If we create the classes for the three mentioned vehicles without inheritance, we need to write these functions in all three classes. Data duplication occurs if they are not written, since the methods have to be written for each class separately. This then increases the chance of error and data redundancy. To avoid this, we use inheritance.
With inheritance, there will be a base class, Vehicle
, and three derived classes: Car
, Bus
, and Truck
.
The syntax below creates a derived class that inherits a base class:
class name_of_derived_class : access_mode name_of_base_class
{
// The body of a derived class
};
The code snippet below illustrates the usage of inheritance in OOP in C++:
#include <bits/stdc++.h>using namespace std;// The base classclass Vehicle{public:int reg_number;void Start_engine(){cout << "Engine is starting" << endl;}void Accelerate(){cout << "Vehicle is accelerating" << endl;}void Apply_Brakes(){cout << "Brakes are being applied" << endl;}};// The derived class inheriting from the base class (Vehicle)class Car : public Vehicle{public:int year_made;};// The derived class inheriting from the base class (Vehicle)class Bus : public Vehicle{public:int passenger_capacity;};// The derived class inheriting from the base class (Vehicle)class Truck : public Vehicle{public:int model_number;};int main(){Car corolla;// An object of the Car class inherits the// property of the Vehicle classcorolla.reg_number = 3110;corolla.year_made = 2011;cout << "1) \t";cout << "Registration number of vehicle is " << corolla.reg_number << endl;cout << "2) \t";cout << "Vehicle was manufactured in " << corolla.year_made << endl;cout << "3) \t";corolla.Start_engine();cout << "4) \t";corolla.Accelerate();return 0;}
Lines 1–2: The necessary header files are included, and the std
namespace is declared.
Lines 5–22: The base class Vehicle
is defined, with member variables and member functions representing common vehicle actions.
Lines 25–29: The derived class Car
is defined. It inherits publicly from the base class Vehicle
and adding a specific member variable.
Lines 32–36: The derived class Bus
is defined, also inheriting publicly from Vehicle
and adding a specific member variable.
Lines 39–43: The derived class Truck
is defined, also inheriting publicly from Vehicle
and adding a specific member variable.
Lines 45–66: In the main()
function, an object of the Car
class is created. The object inherits the properties of the base class and can access and modify its member variables. The values are assigned and printed, and the inherited member functions are called to simulate vehicle actions.
Free Resources