Computer Science Notes for Class 12 (ASSEB / AHSEC)

Chapter 5 - Inheritance in C++

Introduction

One of the most important features of Object-Oriented Programming (OOP) is Inheritance. It allows a new class to acquire the properties and functions of an existing class. It reduces duplication of code, improves reusability, and makes programs easier to maintain.

Inheritance is widely used in real-world software development because many objects share common characteristics but also have their own unique features.

What is Inheritance?

Definition

The process of creating a new class from an existing class is called Inheritance. The new class acquires the properties (data members) and behaviors (member functions) of the existing class and can add additional features of its own.

The existing class is called Base Class or Super Class and

the new class is called Derived Class or Sub Class.

Example

Consider the case of a father and son:

  • The father has certain properties like house, car, bank balance, etc.
  • A son inherits the properties from his father and can also have his own property like a bike.

Similarly, in programming a new class inherits the properties and functions of an existing class and can also define additional features of its own. Instead of writing the same code again, we simply inherit it. This saves time and effort

Concept of base class and derived class

Base Class

A base class is a class whose properties are inherited by another class. Also called Super Class or Parent Class

Derived Class

A derived class is a class that inherits properties from another class. Also called Sub Class or Child Class)

Need for Inheritance

Inheritance is necessary because :

  • It promotes code reusability
  • Reduces duplication of code
  • Saves programming time
  • Makes programs easier to modify
  • Simplifies maintenance

Syntax of Inheritance

class Base_Class

{

    // data Members and member functions

};

class Derived_Class : visibility_mode Base_Class

{

    // Additional data members and member functions

};


Where visibility_mode can be public, private or protected

Types of Inheritance

C++ supports five types of inheritance

1. Single Inheritance

When a subclass inherits only from one base class, it is called Single Inheritance

single-inheritance

Here A is the base class and B is the derived class

Example

Class A                // Base class

{

// members of class A

};

Class B : public A     // Derived class

{

// new members of class B

};

 

2. Multiple Inheritance

When a subclass inherits from multiple base classes, it is known as Multiple Inheritance

Multiple-inheritance

Here Class A and Class B are base classes and Class C is a derived class. Class C inherits the properties of both class A and class B.

Example

Class A                // Base class

{

// members of class A

};

 

Class B                // Base class

{

// members of class B

};

 

Class B : public A, public B   // Derived class

{

// new members of class C

};

Multiple Inheritance and the diamond problem

Multiple inheritance allows a class to derive from more than one base class. While powerful, it can lead to ambiguity also known as the Diamond Problem.

The Diamond Problem occurs when a child class inherits from two parent classes that both inherit from the same grandparent class. C++ uses the concept of virtual inheritance to deal with this problem.

2. Multilevel Inheritance

When a subclass is derived from another derived class, it is called Multilevel Inheritance

multilevel-inheritance

Here class B inherits from class A and class C inherits from class B

Example

Class A                // Base class

{

// members of class A

};

 

Class B : public A             // Base class

{

// members of class B

};

 

Class C : public B     // Derived class

{

// new members of class C

};

4. Hierarchical Inheritance

When multiple subclasses inherit from a single base class, it is called Hierarchical Inheritance.

hierarchical-inheritance

Here Class A is the base class and Class B and Class C are derived classes. Both Class B and class C inherits properties from Class A.

Example

Class A                        // Base class

{

// members of class A

};

 

Class B : public A             // Derived class

{

// members of class B

};

 

Class C : public A             // Derived class

{

// new members of class C

};

4. Hybrid Inheritance

A combination of two or more types of inheritance, is called Hybrid Inheritance

hybrid inheritance

Here class A acts as the base class as Class B and C inherit from Class A. Class D on the other hand, inherits from both Class B and C

Example

Class A                        // Base class

{

// members of class A

};

 

Class B : public A             // Derived class

{

// members of class B

};

 

Class C : public A             // Derived class

{

// new members of class C

};

Class D : public B, public C          // Derived class

{

// new members of class D

};

Modes of Inheritance or Visibility Modes

When inheriting, the visibility modes controls how the members of the base class are available and visible in the derived class.  

There are three visibility modes

  • Public
  • Private
  • Protected

Public Inheritance

In public inheritance:

  • public members of base class becomes public in derived class
  • protected members of base class becomes protected in derived class
  • private members are not accessible directly

Example

class A

{

public:

    int x;

};

class B : public A

{

};

  • public — public
  • protected — protected

Protected Inheritance

In protected inheritance:

  • public members of base class becomes protected in derived class
  • protected members of base class becomes protected in derived class
  • private members are not accessible directly

Example

class A

{

public:

    int x;

};

class B : protected A

{

};

  • public — protected
  • protected — protected

Private Inheritance

In private inheritance:

  • public members of base class becomes private in derived class
  • protected members of base class becomes private in derived class
  • private members are not accessible directly

Example

class A

{

public:

    int x;

};

class B : private A

{

};

  • public — private
  • protected — private

Advantages of Inheritance

  • Promotes code reusability
  • Eliminates duplicate code
  • Makes maintenance easier
  • Supports software extension
  • Saves development time
  • Represents real-world relationships effectively

Constructor and Inheritance

When an object of the derived class is created:

  1. Base class constructor executes first.
  2. Derived class constructor executes next

Destructor and Inheritance

When an object is destroyed:

  1. Derived class destructor executes first.
  2. Base class destructor executes last.

Quick Revision

Scroll to Top