Inheritance in C++

Inheritance is a mechanism that allows an object to acquire all the properties and behaviors of its parent class. In C++, the class whose features are inherited is called base class and the class which inherits the properties and behaviors of base class is called derived class.

Example

#include <iostream>
using namespace std;

class Vehicle
{
public:
    string color = "Red";
    void fuelCapacity()
    {
        cout << "Fuel" << endl;
    }
    void brake()
    {
        cout << "Brake" << endl;
    }
};

class Car : public Vehicle
{
public:
    void modelName()
    {
        cout << "Model";
    }
};

int main() {
    Car car;
    cout << "Inherited Color : "<< car.color <<endl;

    car.color = "Blue";
    cout << "Color : "<< car.color <<endl;
    
    car.fuelCapacity();
    car.brake();
    car.modelName();
}
Output
Inherited Color : Red
Color : Blue
Fuel
Brake
Model

Here, the Car class is derived from the Vehicle class. Since Car is derived from Vehicle, fields (properties) and functions (behaviors) of Vehicle class are inherited by Car class.

Types of Inheritance

There are five types of inheritance in C++:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hybrid Inheritance
  5. Hierarchical Inheritance

Single Inheritance

In Single Inheritance, the derived class (child class) inherits only one base class (parent class).

Example

#include<iostream>
using namespace std;

//base class
 class Animal {
  public:
    Animal()
    {
      cout << "Single Inheritance\n";
    }
};
 
// subclass derived from a single base class
class Dog : public Animal {
 
};
 
// main function
int main()
{  
    // creating object of the derived class
    Dog dog; //constructor of base class and derived class will be called
    return 0;
}
Output
Single Inheritance

Multiple Inheritance

In Multiple Inheritance, a derived class (child class) inherits more than one base class (parent class).

Example

#include <iostream>
using namespace std;

class Doctor
{
protected:
    int a;

public:
    void get_a(int n)
    {
        a = n;
    }
};

class Driver
{
protected:
    int b;

public:
    void get_b(int n)
    {
        b = n;
    }
};

class Person : public Doctor, public Driver
{
public:
    void display()
    {
        std::cout << "The value of a is : " << a << std::endl;
        std::cout << "The value of b is : " << b << std::endl;
        cout << "Addition of a and b is : " << a + b;
    }
};

int main()
{
    Person obj;
    obj.get_a(10);
    obj.get_b(20);
    obj.display();

    return 0;
}
Output
The value of a is : 10
The value of b is : 20
Addition of a and b is : 30

Multilevel Inheritance

In Multilevel Inheritance, a derived (child class) class is created from another derived class (child class).

Example

#include <iostream>
using namespace std;

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

// subclass FourWheeler derived from Base class vehicle
class FourWheeler : public Vehicle
{
public:
    FourWheeler()
    {
        cout << "Vehicles with four wheel\n";
    }
};

// Car subclass derived from another derived class FourWheeler
class Car : public FourWheeler
{
public:
    Car() { cout << "Car has 4 Wheels\n"; }
};

int main()
{
    // creating object of the subclass Car
    Car obj; // constructor of base classes will be called

    return 0;
}
Output
This is a Vehicle
Vehicles with four wheel
Car has 4 Wheels

Hybrid Inheritance

Hybrid Inheritance is a combination of one or more types of inheritance.

Example

#include <iostream>
using namespace std;

// base class
class Product
{
public:
    Product() { cout << "This is Product\n"; }
};

// base class
class Price
{
public:
    Price() { cout << "Product has Price\n"; }
};

// first subclass
class Computer : public Product
{
};

// second subclass
class Laptop : public Product, public Price
{
};

// main function
int main()
{
    // creating object of the subclass Laptop
    Laptop laptopObj; // constructor of base class will be called
    return 0;
}
Output
This is Product
Product has Price

Hierarchical Inheritance

In Hierarchical Inheritance, more than one class is inherited from a single base class.

Example

#include <iostream>
using namespace std;

class Animal
{
public:
    Animal() { cout << "This is an Animal\n"; }
};

// first subclass
class Dog : public Animal
{
};

// second subclass
class Cat : public Animal
{
};

// main function
int main()
{
    // creating object of the subclasses
    Dog obj1; // constructor of base class will be called
    Cat obj2; // constructor of base class will be called
    return 0;
}
Output
This is an Animal
This is an Animal