×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Inheritance in C++ – Complete Tutorial



Last Updated on: 16th Dec 2025 18:27:09 PM

Introduction

Inheritance is one of the most important concepts of Object Oriented Programming in C++.
It allows a class to acquire the properties and behavior of another class.
Using inheritance, programmers can reuse existing code, reduce duplication, and create a clear relationship between classes.

 

Definition of Inheritance

Inheritance is a mechanism in which one class (called the derived class or child class) inherits data members and member functions of another class (called the base class or parent class).

The class whose properties are inherited is known as the base class, and the class that inherits those properties is known as the derived class.

 

Why Inheritance is Used

  • Code reusability

  • Reduced code duplication

  • Easy maintenance

  • Logical class hierarchy

  • Improved scalability

 

Real-life example

In an organization:

  • Employee is a general category

  • Manager and Developer are specific types of employees

A Manager and a Developer both share common properties such as name, ID, and salary, but they also have their own specific responsibilities.
This relationship can be represented using inheritance.

 

Basic Syntax of Inheritance in C++

class BaseClass {
    // data members and functions
};

class DerivedClass : access-specifier BaseClass {
    // additional data members and functions
};

 

Access Specifiers

  •  public 

  •  protected 

  •  private 

 

Simple Example of Inheritance 

#include <iostream>
using namespace std;

class Employee {
public:
    int salary;

    void showSalary() {
        cout << "Salary: " << salary << endl;
    }
};

class Manager : public Employee {
public:
    int bonus;

    void showBonus() {
        cout << "Bonus: " << bonus << endl;
    }
};

int main() {
    Manager m;
    m.salary = 50000;
    m.bonus = 10000;
    m.showSalary();
    m.showBonus();
    return 0;
}

 

Types of Inheritance in C++

C++ supports the following types of inheritance:

  1. Single Inheritance

  2. Multilevel Inheritance

  3. Multiple Inheritance

  4. Hierarchical Inheritance

  5. Hybrid Inheritance

 

1. Single Inheritance

Single inheritance occurs when one derived class inherits from one base class.

 

Real-life example

A Car is a type of Vehicle.

 

Example

#include <iostream>
using namespace std;

class Vehicle {
public:
    int maxSpeed;

    void showSpeed() {
        cout << "Max Speed: " << maxSpeed << " km/h" << endl;
    }
};

class Car : public Vehicle {
public:
    int doors;

    void showDoors() {
        cout << "Number of doors: " << doors << endl;
    }
};

int main() {
    Car c;
    c.maxSpeed = 180;
    c.doors = 4;

    c.showSpeed();
    c.showDoors();

    return 0;
}

 

Output

Max Speed: 180 km/h
Number of doors: 4

 

2. Multilevel Inheritance

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

 

Real-life example

  • A Person has basic identity

  • An Employee is a person with a job

  • A Manager is an employee with authority

 

Example

#include <iostream>
using namespace std;

class Person {
public:
    string name;

    void showName() {
        cout << "Name: " << name << endl;
    }
};

class Employee : public Person {
public:
    int empId;

    void showId() {
        cout << "Employee ID: " << empId << endl;
    }
};

class Manager : public Employee {
public:
    int teamSize;

    void showTeam() {
        cout << "Team Size: " << teamSize << endl;
    }
};

int main() {
    Manager m;
    m.name = "Amit";
    m.empId = 101;
    m.teamSize = 8;

    m.showName();
    m.showId();
    m.showTeam();

    return 0;
}

 

Output

Name: Amit
Employee ID: 101
Team Size: 8

 

 

3. Multiple Inheritance

Multiple inheritance occurs when a class inherits from more than one base class.

 

Real-life example

An All-in-One Machine works as:

  • A printer

  • A scanner

 

Example

#include <iostream>
using namespace std;

class Printer {
public:
    void print() {
        cout << "Printing document..." << endl;
    }
};

class Scanner {
public:
    void scan() {
        cout << "Scanning document..." << endl;
    }
};

class AllInOneMachine : public Printer, public Scanner {
};

int main() {
    AllInOneMachine machine;
    machine.print();
    machine.scan();

    return 0;
}

 

Output

Printing document...
Scanning document...

 

4. Hierarchical Inheritance

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

 

Real-life example

In a bank:

  • Account is the base

  • Savings Account and Current Account are different types

 

Example

#include <iostream>
using namespace std;

class Account {
public:
    int accountNo;

    void showAccount() {
        cout << "Account Number: " << accountNo << endl;
    }
};

class SavingsAccount : public Account {
public:
    float interestRate;

    void showInterest() {
        cout << "Interest Rate: " << interestRate << "%" << endl;
    }
};

class CurrentAccount : public Account {
public:
    float overdraftLimit;

    void showLimit() {
        cout << "Overdraft Limit: " << overdraftLimit << endl;
    }
};

int main() {
    SavingsAccount sa;
    sa.accountNo = 1111;
    sa.interestRate = 4.5;

    CurrentAccount ca;
    ca.accountNo = 2222;
    ca.overdraftLimit = 50000;

    sa.showAccount();
    sa.showInterest();

    ca.showAccount();
    ca.showLimit();

    return 0;
}

 

Output

Account Number: 1111
Interest Rate: 4.5%

Account Number: 2222
Overdraft Limit: 50000

 

 

5. Hybrid Inheritance

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

 

Real-life example

In an education system:

  • Person → Student

  • Person → Teacher

  • TeachingAssistant behaves as both student and teacher

(Implemented conceptually; often handled using interfaces/virtual inheritance.)

 

Access Control in Inheritance

Effect of Access Specifiers

Base Class Member Public Inheritance Protected Inheritance Private Inheritance
public public protected private
protected protected protected private
private Not accessible Not accessible Not accessible

 

Constructor Order in Inheritance

Rule

When an object of a derived class is created:

  1. Base class constructor executes first

  2. Derived class constructor executes next

 

Example (Real-life: Device Startup)

#include <iostream>
using namespace std;

class Device {
public:
    Device() {
        cout << "Power supply started" << endl;
    }
};

class Laptop : public Device {
public:
    Laptop() {
        cout << "Laptop system booted" << endl;
    }
};

int main() {
    Laptop l;
    return 0;
}

 

 

Output

Power supply started
Laptop system booted

 

Destructor Order in Inheritance

Rule

When an object is destroyed:

  1. Derived class destructor executes first

  2. Base class destructor executes last

 

Function Overriding in Inheritance

When a derived class provides its own version of a base class function.

 

Real-life example

  • Generic notification

  • Specific notification for mobile app

 

Example

#include <iostream>
using namespace std;

class Notification {
public:
    void send() {
        cout << "Sending general notification" << endl;
    }
};

class MobileNotification : public Notification {
public:
    void send() {
        cout << "Sending mobile app notification" << endl;
    }
};

int main() {
    MobileNotification n;
    n.send();
    return 0;
}

 

Output

Sending mobile app notification

 

Using  virtual  Keyword

The  virtual  keyword ensures the correct function is called at runtime.

class Animal {
public:
    virtual void sound() {
        cout << "Animal sound" << endl;
    }
};

 

Advantages of Inheritance

  • Saves development time

  • Improves code reuse

  • Makes system extensible

  • Represents real-world hierarchy

 

Disadvantages of Inheritance

  • Tight coupling between classes

  • Increased complexity in deep hierarchies

  • Difficult debugging if poorly designed

 

Summary

  • Inheritance allows one class to reuse another class

  • It models real-world relationships naturally

  • C++ supports single, multilevel, multiple, hierarchical, and hybrid inheritance

  • Constructors and destructors follow a fixed order

  • Function overriding customizes inherited behavior

 

Keep practicing — you're doing amazing!

Happy Coding!    yes

 


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo