Open In App

Inheritance in C++

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the information about how it affects different properties of the class.

Syntax

C++
class DerivedClass : mode_of_inheritance BaseClass {
    // Body of the Derived Class
};

where mode of inheritance controls the access level of the inherited members of the base class in the derived class. In C++, there are 3 modes of inheritance:

Mode

Description

Public Inheritance Mode

Public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class.

Protected Inheritance Mode

Both public and protected members of the base class will become protected in the derived class.

Private Inheritance Mode

Both public members and protected members of the base class will become private in the derived class. Private mode is the default mode that is applied when we don’t specify any mode.

Access Base Class Members

Members of the base class can be accessed in the derived class by simply using their name.

C++
class Base {
public:
    int n;
    void printN() {
        cout << n << endl;
    }
};

// Inheriting Base class publicly
class Derived : public Base {
    public:
    void func () {

        // Accessing Base class members
        n = 22;
    }
};

The public members of the Base class can be accessed through the objects of the Derived class if the Base class is inherited publicly as in the above example.

C++
//Driver Code Starts{
#include <bits/stdc++.h>
using namespace std;

class Base {
public:
    int n;
    void printN() {
        cout << n << endl;
    }
};

// Inheriting Base class publicly
class Derived : public Base {
    public:
    void func () {

        // Accessing Base class members
        n = 22;
    }
};

int main() {
    
//Driver Code Ends }

    // Creating objects of derived
    Derived d;
    
    // Accessing Derived class member
    d.func();
    
    // Accessing Base class member
    d.printN();

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
22

The private members in the base class cannot be directly accessed in the derived class, while protected and public members can be directly accessed. To access or update the private members of the base class in derived class, we have to use the corresponding getter and setter functions of the base class or declare the derived class as friend class.

Example of Inheritance

CPP
//Driver Code Starts{
#include <bits/stdc++.h>
using namespace std;

// Base class that is to be inherited
//Driver Code Ends }

class Parent {
public:
    int id_p;
    Parent(int x = 22) : id_p(x) {}
    void printID_p() {
        cout << "Base ID: " << id_p << endl;
    }
};

// Derived publicly inheriting from Base
// Class
class Child : public Parent {
public:
    int id_c;
    Child(int x = 22) : id_c(x) {}
    void printID_c() {
        cout << "Child ID: " << id_c << endl;
    }
};

int main() {
    Child obj1;

    // An object of class child has all data members
    // and member functions of class parent
    // so we try accessing the parents method and data from
    // the child class object.
    obj1.id_p = 7;
    obj1.printID_p();

    // finally accessing the child class methods and data
    // too
    obj1.id_c = 91;
    obj1.printID_c();

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
Base ID: 7
Child ID: 91

Explanation: In the above program, the ‘Child‘ class is publicly inherited from the ‘Parent‘ class so the public data members of the class ‘Parent’ will also be inherited by the class ‘Child’.

Types Of Inheritance in C++

The inheritance can be classified on the basis of the relationship between the derived class and the base class. In C++, we have 5 types of inheritances:

  • Single inheritance
  • Multilevel inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Hybrid inheritance

1. Single Inheritance

In single inheritance, a class is allowed to inherit from only one class. i.e. one base class is inherited by one derived class only.

inheritence

Single Inheritance

Example:

CPP
//Driver Code Starts{
#include <bits/stdc++.h>
using namespace std;

//Driver Code Ends }

class Vehicle {
public:
    Vehicle() {
        cout << "This is a Vehicle"<< endl;
    }
};

// Sub class derived from a single base classes
class Car : public Vehicle {
public:
    Car() {
        cout << "This Vehicle is Car"<< endl;
    }
};

//Driver Code Starts{

int main() {
    
    // Creating object of sub class will
    // invoke the constructor of base classes
    Car obj;
    return 0;
}
//Driver Code Ends }

Output
This is a Vehicle
This Vehicle is Car

2. Multiple Inheritance

Multiple Inheritance is a feature of C++ where a class can inherit from more than one class. i.e one subclass is inherited from more than one base class.

inheritence3

Multiple Inheritance


Example:

CPP
//Driver Code Starts{
#include <bits/stdc++.h>
using namespace std;

//Driver Code Ends }

class LandVehicle {
public:
    LandVehicle() {
        cout << "This is a LandVehicle"<< endl;
    }
};

class WaterVehicle {
public:
    WaterVehicle() {
        cout << "This is a WaterVehicle"<< endl;
    }
};

// sub class derived from two base classes
class AmphibiousVehicle : public WaterVehicle, public LandVehicle {
  public:
    AmphibiousVehicle() {
        cout << "This is an AmphibiousVehicle"<< endl;
    }

//Driver Code Starts{
};

int main() {
    
    // Creating object of sub class will
    // invoke the constructor of base classes.
    AmphibiousVehicle obj;
    return 0;
}
//Driver Code Ends }

Output
This is a WaterVehicle
This is a LandVehicle
This is an AmphibiousVehicle

3. Multilevel Inheritance

In multilevel inheritance, a derived class is created from another derived class and that derived class can be derived from a base class or any other derived class. There can be any number of levels. For example, a vehicle can be a four-wheeler, and a four-wheeler vehicle can be a car.

Multilevel-inheritence2

Multilevel Inheritance

Example:

CPP
//Driver Code Starts{
#include <bits/stdc++.h>
using namespace std;

//Driver Code Ends }

class Vehicle {
public:
    Vehicle() {
        cout << "This is a Vehicle"<< endl;
    }
};

class fourWheeler : public Vehicle {
public:
    fourWheeler() {
        cout << "4 Wheeler Vehicles"<< endl;
    }
};

class Car : public fourWheeler {
public:
    Car() {
        cout << "This 4 Wheeler Vehical is a Car";
    }
};


//Driver Code Starts{
int main() {
    
    // Creating object of sub class will
    // invoke the constructor of base classes.
    Car obj;
    return 0;
}

//Driver Code Ends }

Output
This is a Vehicle
4 Wheeler Vehicles
This 4 Wheeler Vehical is a Car

4. Hierarchical Inheritance

In hierarchical inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class. For example, cars and buses both are vehicle.

Hierarchical-inheritance

Hierarchical Inheritance

Example:

CPP
//Driver Code Starts{
#include <bits/stdc++.h>
using namespace std;

//Driver Code Ends }

class Vehicle {
public:
    Vehicle() {
        cout << "This is a Vehicle"<< endl;
    }
};

class Car : public Vehicle {
public:
    Car() {
        cout << "This Vehicle is Car"<< endl;
    }
};

class Bus : public Vehicle {
public:
    Bus() {
        cout << "This Vehicle is Bus"<< endl;
    }
};

//Driver Code Starts{

int main() {
    
    // Creating object of sub class will
    // invoke the constructor of base class.
    Car obj1;
    Bus obj2;
    return 0;
}
//Driver Code Ends }

Output
This is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is Bus

5. Hybrid Inheritance

Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance will create hybrid inheritance in C++.

There is no particular syntax of hybrid inheritance. We can just combine two of the above inheritance types. Below image shows one of the combinations of hierarchical and multiple inheritances:

hybrid-inheritance

Hybrid Inheritance

Example:

CPP
//Driver Code Starts{
#include <bits/stdc++.h>
using namespace std;

//Driver Code Ends }

class Vehicle {
public:
    Vehicle() {
        cout << "This is a Vehicle"<< endl;
    }
};

class Fare {
public:
    Fare() {
        cout << "Fare of Vehicle"<< endl;
    }
};

class Car : public Vehicle {
  public:
  Car() {
      cout << "This Vehical is a Car"<< endl;
  }
};

class Bus : public Vehicle, public Fare {
  public:
  Bus() {
      cout << "This Vehicle is a Bus with Fare";
  }
};

//Driver Code Starts{

int main() {
    
    // Creating object of sub class will
    // invoke the constructor of base class.
    Bus obj2;
    return 0;
}
//Driver Code Ends }

Output
This is a Vehicle
Fare of Vehicle
This Vehicle is a Bus with Fare

Multipath Inheritance

This is the special case of special case of hybrid inheritance. In multipath inheritance, a class is derived from two base classes and these two base classes in turn are derived from one common base class. An ambiguity can arise in this type of inheritance in the most derived class. This problem is also called diamond problem due to the diamond shape formed in the UML inheritance diagram.

Effects of Inheritance

Let’s see how different components of class are affected in inheritance:

Static Members and Inheritance

In C++, static members belong to the class itself, not to any object. This means static variables and methods are shared across all instances of the class. When it comes to inheritance, static members from the base class are not inherited by the derived class in the traditional way. However, they can still be accessed using the class name like className::staticMember.

Friend Function and Class in Inheritance

Friend functions and classes in inheritance provides functions or classes to access private and protected members of a class, providing flexibility and better control over class interactions.

  • Friend Function: It is a special function that can access private and protected members of a class, even though they are not part of that class.
  • Friend Class: It is a class that is allowed to access the private and protected members of another class.

Constructors and Destructors in Inheritance

Constructors and Destructors are generally defined by the programmer and if not, the compiler automatically creates them, so they are present in every class in C++. Now, the question arises what happens to the constructor and destructor when a class is inherited by another class.

In C++ inheritance, the constructors and destructors are not inherited by the derived class, but we can call the constructor of the base class in derived class.

  • The constructors will be called by the complier in the order in which they are inherited. It means that base class constructors will be called first, then derived class constructors will be called.
  • The destructors will be called in reverse order in which the compiler is declared.
  • We can also call the constructors and destructors manually in the derived class.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

//Driver Code Ends }

class Parent {
public:

    // base class constructor
    Parent() { cout << "Inside base class" << endl; }
};

// sub class
class Child : public Parent {
public:

    // sub class constructor
    Child() { cout << "Inside sub class" << endl; }
};

//Driver Code Starts{

int main() {

    // creating object of sub class
    Child obj;
    return 0;
}
//Driver Code Ends }

Output
Inside base class
Inside sub class

Polymorphism in Inheritance

In Inheritance, we can redefine the base class member function in the derived class. This type of inheritance is called Function Overriding. Generally, in other programming languages, function overriding is runtime polymorphism but in C++, we can do it at both runtime and compile time. For runtime polymorphism, we have to use the virtual functions.

Example:

C++
//Driver Code Starts{
#include <bits/stdc++.h>
using namespace std;

//Driver Code Ends }

class Parent {
public:
    void GeeksforGeeks_Print() {
        cout << "Base Function" << endl;
    }
};

class Child : public Parent {
public:
    void GeeksforGeeks_Print() {
        cout << "Derived Function";
    }
};

//Driver Code Starts{

int main() {
    Child Child_Derived;
    Child_Derived.GeeksforGeeks_Print();
    return 0;
}
//Driver Code Ends }

Output
Derived Function

Inheritance vs Polymorphism

Inheritance and Polymorphism both works differently. Inheritance allows a new class to inherit properties from an existing class, promoting code reuse, while polymorphism enables a class to perform tasks in different ways, depending on the method used. Inheritance focuses on class relationships, and polymorphism focuses on method behaviour.



Next Article
Article Tags :
Practice Tags :

Similar Reads