C++ Inheritance

This lesson will teach you about Inheritance in C++ with examples to better understand the topics.


C++ Inheritance

In C++, classes can inherit methods and attributes from one another. The inheritance process occurs when two classes have an “is-a relationship” with one another, and objects of one class take on the traits and characteristics of the other class. The class from which the characteristics are inherited is called the parent class, base class, or superclass. The class doing the inheriting is called the child class, derived class, or subclass.

The “inheritance idea” is divided into two elements:

  • Base class
  • Derived class

Base class

  • The base class (parent) is inherited from.

Derived class

  • Derived class (child) is the class that inherits from another class.
Tip
Use the: sign to indicate that you want to inherit from a class.

Why Choose “Inheritance” And so when?

Because of its advantages, inheritance increases the programming’s efficiency. The following is a discussion of the key applications of inheritance:

Reusability of the code: is one of the primary benefits of using inheritance. Consider the Tiger, Lion, and Panther as three distinct kinds of animals. You can make member functions for these classes, such as predator(), canine(), and claws(), since all three of the animals have large, razor-sharp claws and are all predators. You can add these functions to a basic class called carnivores, from which the tiger, lion, and panther classes can inherit. Given that all three functions for these classes are the same creating distinct functions for each will result in data duplication and raise the possibility of error. So, you can utilize inheritance in this place instead of this.

Transitive nature: Because inheritance is transitive, it is also employed. As an illustration, the derived class mammal inherits the traits of the base class animal. The properties of the class “animal” will now also be inherited by all of the “mammal’s” child classes due to the transitive nature of inheritance which greatly facilitates debugging. You can fix the base class’s flaws, and all inherited classes will automatically debug.

In C++ inheritance, you can derive a child class from the base class. See below the syntax inheritance,

Basic Syntax of Inheritance

class Base {
  //code
};
class Derived: public Base {
  //code
};
class Derived: public Base

Below is an example of inheritance in C++; The Apartment class (child) in the following example inherits the following properties and methods from the building class (parent).

Example

#include <iostream>
#include <string>
using namespace std;
// Base class
class Building {
  public:
    string stories = "Two-Story";
  void highClass() {
    cout << "High-Class Apartment! \n";
  }
};
// Derived class
class Apartment: public Building {
  public: string modelType1 = "Furnished";
};
int main() {
  Apartment myApartment;
  myApartment.highClass();
  cout << myApartment.stories + " " + myApartment.modelType1;
  return 0;
}

Output

High-Class Apartment!
Two-Story Furnished

There are two classes, parent “Building” and child “Apartment,” as may be seen.

  • Parent class inheritance passes to the child class.
  • You first created the main method’s child class instance called Apartment myApartment.
  • Then along with calling the child’s class myApartment.modelType1, the variable is the instance of the class child can inherit the parent class too. You can also get the myApartment.stories  variable of the parent class.

This concludes the C++ Inheritance lesson. In The next lesson, you will learn about Access Specifiers in C++ and their usage.