Understanding Inheritance in C++ with Code Examples

C++ is an object-oriented programming language that allows developers to create complex programs through the use of classes and objects. One of the key features of C++ is inheritance, which allows developers to create new classes that inherit properties and methods from existing classes.

In this article, we’ll explore the concept of inheritance in C++ and how it works. We’ll also provide code examples to illustrate how inheritance can be used in practice.

Introduction

Inheritance is a concept in object-oriented programming that allows developers to create new classes based on existing classes. The new class, known as the derived class or child class, inherits properties and methods from the existing class, known as the base class or parent class.

The purpose of inheritance is to reuse code and avoid redundancy. Instead of writing new code for each new class, developers can simply inherit the properties and methods they need from existing classes.

What is Inheritance?

Inheritance is the process of creating a new class that is a modified version of an existing class. The new class, called the derived class, inherits properties and methods from the existing class, called the base class.

The derived class can add new properties and methods, or modify existing ones, but it also has access to all the properties and methods of the base class.

Types of Inheritance

There are several types of inheritance in C++, including:

Single Inheritance

Single inheritance is the most common type of inheritance in C++. In single inheritance, a derived class inherits from a single base class.

Multiple Inheritance

Multiple inheritance is a type of inheritance in which a derived class inherits from multiple base classes. This allows developers to create more complex class hierarchies.

Multi-Level Inheritance

Multi-level inheritance is a type of inheritance in which a derived class inherits from a base class, and that base class itself inherits from another base class.

Hierarchical Inheritance

Hierarchical inheritance is a type of inheritance in which multiple derived classes inherit from a single base class.

Hybrid Inheritance

Hybrid inheritance is a combination of multiple inheritance and hierarchical inheritance.

Syntax of Inheritance

The syntax of inheritance in C++ is as follows:

class DerivedClass : accessSpecifier BaseClass {
    // Body of derived class
};

In this syntax, DerivedClass is the name of the derived class, accessSpecifier specifies the access level of the base class (public, protected, or private), and BaseClass is the name of the base class.

Access Control in Inheritance

Access control in inheritance determines how the properties and methods of the base class are accessed by the derived class. There are three access specifiers in C++: public, protected, and private.

  • Public: public members of the base class are accessible by the derived class.
  • Protected: protected members of the base class are accessible by the derived class, but not by code outside the class hierarchy.
  • Private: private members of the base class are not accessible by the derived class.

Constructors and Destructors in Inheritance

Constructors and destructors are special member functions in C++ that are used to create and destroy objects.

  • Constructors in Inheritance: Constructors are special member functions that are used to initialize objects when they are created. Constructors are called automatically when an object is created, and they are used to set the initial values of member variables. When a derived class is created, its constructor automatically calls the constructor of the base class to initialize the inherited properties.
  • Destructors in Inheritance: Destructors are special member functions that are used to destroy objects when they are no longer needed. Destructors are called automatically when an object goes out of scope or is explicitly deleted. When a derived class is destroyed, its destructor automatically calls the destructor of the base class to destroy the inherited properties.

Constructors examples

Virtual Functions in Inheritance

Virtual functions are a feature of C++ that allows functions to be overridden in derived classes. Virtual functions are declared in the base class and are implemented in the derived class. When a virtual function is called on a derived class object, the implementation in the derived class is called instead of the implementation in the base class. Virtual functions enable polymorphism, which we will discuss in the next section.

Polymorphism in Inheritance

Polymorphism is a key feature of object-oriented programming that allows objects of different classes to be treated as if they are the same type. Polymorphism is achieved through the use of virtual functions, which enable derived classes to provide their own implementation of a function. Polymorphism enables code to be written that is more generic and can be applied to a wider range of objects.

How can you use inheritance and polymorphism in C++?

Code Example: Understanding Inheritance in C++

Let’s take a look at an example to understand how inheritance works in C++. Suppose we have a base class called “Shape” that has two properties, “width” and “height”. We can create a derived class called “Rectangle” that inherits from “Shape” and adds a new property called “color”.

#include <iostream>
using namespace std;

class Shape {
   protected:
      int width, height;

   public:
      Shape( int a=0, int b=0) {
         width = a;
         height = b;
      }

      int getArea() {
         cout << "Parent class area :" <<endl;
         return 0;
      }
};

class Rectangle: public

2 thoughts on “Understanding Inheritance in C++ with Code Examples”

Leave a Comment