Tutorial Study Image

C++ Inheritance


May 31, 2023, Learn eTutorial
346

The concept of inheritance in C++ is discussed in this article. Additionally, it also discusses the various C++ inheritance types. It is called inheritance when a class has the capacity to take on properties from another class. The class whose properties are inherited is known as the base class, while the class that inherits the properties is known as the derived class.

Introduction

Imagine a class called "Car," under which there are subclasses like "BMW," "Mercedes," and so on. Under the name "Car," such as "Mercedes," there are other subclasses titled "models of the vehicles." This is a typical example in order to understand the concept of inheritance.

Now, what exactly is inheritance?

As in the example above, the Mercedes class inherits the property of the Car class, this ability of a class to take properties from another class is known as inheritance.

One of the key fundamentals of Object Oriented Programming is inheritance, which is also super beneficial. It facilitates the reuse of code, which saves a great deal of time, thereby making it an essential concept in order to understand in the world of programming.

A class is a template that outlines the variables as well as the methods that are shared by all objects of a particular kind. Car is a class, just like in the aforementioned example

In inheritance, there are two categories of classes:

Parent Class or Base Class: A class from which a subclass inherits its properties. Car is a parent class, just like in the example mentioned before.

Child Class or Derived Class: A class that inherits a property from another class is known as a "child class" or "derived class." BMW and Mercedes belong to the same child class as in the aforementioned example.

The ":" symbol is used to inherit from a class.

The Car class (child) in the following example inherits the following methods as well as the properties just from the Vehicle class (parent):

Program for showing inheritance


#include <iostream>
#include <string>
using namespace std;

// Base class
class Vehicle {
  public: 
    string brand = "BMW";
    void honk() {
      cout << "hoo, huut! \n" ;
    }
};

// Derived class
class Car: public Vehicle {
  public: 
    string model = "BMW X5";
};

int main() {
  Car myCar;
  myCar.honk();
  cout << myCar.brand + " " + myCar.model;
  return 0;
}

 

Output:


hoo, huut! 
BMW BMW X5

Why and when should C++ programmers use inheritance?

In C++, inheritance is used to allow us to reuse code from pre-existing classes. Strongly promoting the idea of reuse is C++. Reusability is another crucial aspect of OOP (Object Oriented Programming).

In order to save time and improve reliability, it is always preferable to reuse something that already exists rather than trying to construct something similar that does not exist.

In C++, we utilize inheritance when we want a class to use both the properties of its superclass and its own classes in addition to their own properties, and when the two classes in the program share the same logical domain.

For example, there is a base class or parent class named "Animal," and a child class named "Dog." Because the dog is an animal, all of the common attributes of the "Animal" class should be present in the "Dog class," as well as the property of the dog animal.

Modes of Inheritance in C++

mainly there are 3 modes of inheritance:

  • Public mode
  • Protected mode
  • Private mode
  1. Public mode

    When a child class is derived just from the base or parent class in the public mode of inheritance, the public member that is of the base or parent class will become public in the child class, and the protected member of the base class becomes protected in the child class. The private members of the base class are not all accessible in the derived class.
  2. Protected mode

    When a child class derives from a base class or parent class in protected mode, then both the public as well as protected members of the base class will become protected in the derived class. Private members of the base class are also inaccessible in the derived class, but protected members can be easily accessed.
  3. Private mode

    When a child class is derived from a base class in private mode, both the public as well as protected members of the base class will definitely become private in the derived class, and the private members of the base class are no longer accessible in the derived class.

The following table lists the three kinds of inheritance mentioned above and displays the Base class member access specifier in C++ when all three modes of inheritance are used:

C++ Inheritance

What is Multilevel Inheritance in C++?

There are at least two and perhaps more classes involved in multi-level inheritance. A subclass that has just been formed becomes the base class for a new class, and one class inherits the features from its parent class.

A class might also be derived just from another class that has already been derived from another class.

MyGrandChild is derived from MyChild in the following example (which is derived from MyClass).

The program shows Multilevel Inheritance


#include <iostream>
using namespace std;

// Parent class
class MyClass {
  public: 
    void myFunction() {
      cout << "a few items in the parent class." ;
    }
};

// Child class
class MyChild: public MyClass {
};

// Grandchild class 
class MyGrandChild: public MyChild {
};

int main() {
  MyGrandChild myObj;
  myObj.myFunction();
  return 0;
}

 

Output:


a few items in the parent class.

What do you mean by multiple inheritance in C++?

A class could be derived from multiple base classes by using a comma-separated list:

A class may inherit from many classes due to C++'s Multiple Inheritance features. Inherited classes' constructors are called in the same order as they were inherited.

For example, in the code below, constructor B is invoked before constructor A.

Program for multiple inheritances


#include <iostream>
using namespace std;

// Base class
class MyClass {
  public:
    void myFunction() {
      cout << " content in the parent class.\n" ;
    }
};

// Another base class
class MyOtherClass {
  public:
    void myOtherFunction() {
      cout <<" content with in the another class.\n" ;
    }
};

// Derived class
class MyChildClass: public MyClass, public MyOtherClass {
};

int main() {
  MyChildClass myObj;
  myObj.myFunction();
  myObj.myOtherFunction();
  return 0;
} 

 

Output:


content in the parent class.
content with in the another class.

The benefits of inheritance include:

  • In C++, inheritance encourages code reuse. Whenever a derived class inherits the base class, the derived class gains access to all of the base class's capabilities, and the base class's code can be reused in the derived class.
  • As a result of not having to repeatedly write the same code, it makes the code easier to read and looks cleaner.>
  • Because the code has already been created and is inherited, it saves time to not have to write it from scratch.
  • Because new classes can be simply added to existing ones, inheritance supports extensibility.