C++ Inheritance

Inheritance is an important concept of Object-Oriented Programming. It allows one class to use the properties and functions of another class.

For example, suppose you have a Vehicle class with a start() function. If you create a Car class from Vehicle, the Car class can use that function without writing it again.

The existing class is called the base class, and the new class is called the derived class.

Basic Inheritance

Inheritance is created using a colon (:).

class Vehicle
{
public:
    void start()
    {
        cout << "Vehicle started";
    }
};

class Car : public Vehicle
{
};

Now you can create a Car object:

Car c;
c.start();

The Car class can use the start() function because it inherited it from Vehicle.

This is the basic idea behind Inheritance in C++.


Types of Inheritance in C++

C++ supports different types of inheritance:

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

Let’s look at each one.

Single Inheritance in C++

In single inheritance, one derived class inherits from one base class.

class Animal
{
public:
    void eat()
    {
        cout << "Eating";
    }
};

class Dog : public Animal
{
};

Here, Dog inherits from Animal.

Dog d;
d.eat();

The Dog object can use the eat() function inherited from Animal.


Multiple Inheritance in C++

In multiple inheritance, one class inherits from more than one base class.

class Father
{
public:
    void fatherWork()
    {
        cout << "Father's work";
    }
};

class Mother
{
public:
    void motherWork()
    {
        cout << "Mother's work";
    }
};

class Child : public Father, public Mother
{
};

Now the Child class can use functions from both Father and Mother.

Child c;

c.fatherWork();
c.motherWork();

This is called Multiple Inheritance in C++.


Multilevel Inheritance in C++

In multilevel inheritance, a class is derived from another derived class.

For example:

class Animal
{
public:
    void eat()
    {
        cout << "Eating";
    }
};

class Dog : public Animal
{
public:
    void bark()
    {
        cout << "Barking";
    }
};

class Puppy : public Dog
{
};

Here, Dog inherits from Animal, and Puppy inherits from Dog.

Therefore, a Puppy object can access functions from both levels:

Puppy p;

p.eat();
p.bark();

This is Multilevel Inheritance in C++.


Hierarchical Inheritance

In hierarchical inheritance, multiple classes inherit from the same base class.

class Animal
{
public:
    void eat()
    {
        cout << "Eating";
    }
};

class Dog : public Animal
{
};

class Cat : public Animal
{
};

Both Dog and Cat inherit from Animal.

Dog d;
Cat c;

d.eat();
c.eat();

The same base class can therefore provide common functionality to different derived classes.


Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance.

For example, a program can combine multiple and multilevel inheritance. The structure can become more complicated because a class may receive features through different paths.

One common issue with hybrid inheritance is the diamond problem, where the same base class can be inherited through more than one path. C++ provides virtual inheritance to handle such situations.

You don’t need to memorize every possible structure. The important thing is to understand that hybrid inheritance combines different inheritance types.


Access Modes in Inheritance

C++ provides three access modes for inheritance:

  • public
  • protected
  • private

For example:

class Dog : public Animal
{
};

The access mode affects how the inherited members can be accessed in the derived class and from outside the class.

With public inheritance, public members of the base class remain public in the derived class, while protected members remain protected.

class Animal
{
public:
    void eat()
    {
        cout << "Eating";
    }
};

class Dog : public Animal
{
};

Here, eat() remains public and can be called through a Dog object.

The choice of access mode depends on how you want the base class features to be exposed through the derived class.


Frequently Asked Questions (FAQs)

Q1. Why is inheritance used in C++?

Inheritance allows a new class to reuse properties and functions of an existing class. It helps reduce duplicate code and supports the reuse of common functionality.

Q2. What are the different types of inheritance in C++?

The main Types of Inheritance in C++ are single, multiple, multilevel, hierarchical, and hybrid inheritance. Each type defines a different relationship between base and derived classes.

Q3. When should I use multiple inheritance in C++?

Multiple Inheritance in C++ is useful when a derived class needs to inherit features from more than one base class. It should be used carefully to avoid complexity and ambiguity.

Q4. How does multilevel inheritance work in C++?

Multilevel Inheritance in C++ creates a chain of inheritance where one derived class becomes the base class for another class.

Q5. How do access modes affect inheritance in C++?

Access modes (public, protected, and private) determine how inherited members can be accessed from the derived class and outside the class.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top