Inheritance: Types & Implementation in C++

0
379
Inheritance

In C++, one of the most powerful and essential features of Object-Oriented Programming is Inheritance. It enables code reusability and hierarchical class structuring, making your programs more modular, organized, and easier to maintain.

In this detailed guide by Kamlesh Singad, from the Code With Kamlesh C++ course series, we’ll walk through the theoretical and practical aspects of Inheritance in C++, including its various types, syntax, and real-life use cases to help beginners and intermediates master it confidently.

What is Inheritance in C++?

It is the process by which one class (called the derived class) acquires the properties and behaviors (data and functions) of another class (called the base class). It allows you to build new classes on top of existing ones, promoting code reusability and logical hierarchy.

Why Use Inheritance in Object-Oriented Programming?

  • Code Reusability: Avoid rewriting the same code by reusing base class members.
  • Hierarchical Modeling: Reflect real-world relationships (e.g., AnimalDog, Cat).
  • Extensibility: Add new features to existing functionality.
  • Maintainability: Manage large codebases more efficiently.

Also Read: C vs. C++: Key Differences and When to Use Each

Inheritance

Basic Syntax of Inheritance

class Base {
public:
    int baseValue;
    void displayBase() {
        cout << "Base class function" << endl;
    }
};

class Derived : public Base {
public:
    void displayDerived() {
        cout << "Derived class function" << endl;
    }
};

Here, Derived inherits from Base and gains access to baseValue and displayBase().

Types of Inheritance in C++

C++ supports five primary types of inheritance:

Single Inheritance

A single derived class inherits from one base class.

class Animal {
public:
    void eat() { cout << "Eating...\n"; }
};

class Dog : public Animal {
public:
    void bark() { cout << "Barking...\n"; }
};

Multiple Inheritance

A derived class inherits from more than one base class.

Also Read: C/C++ Environment Setup Guide (Windows, Mac, Linux)

Inheritance
class A {
public:
    void showA() { cout << "Class A\n"; }
};

class B {
public:
    void showB() { cout << "Class B\n"; }
};

class C : public A, public B {
};

Multilevel Inheritance

A class is derived from another derived class, forming a chain.

class A {
public:
    void showA() { cout << "Class A\n"; }
};

class B : public A {
public:
    void showB() { cout << "Class B\n"; }
};

class C : public B {
public:
    void showC() { cout << "Class C\n"; }
};

Hierarchical Inheritance

Multiple classes are derived from the same base class.

class Vehicle {
public:
    void run() { cout << "Running...\n"; }
};

class Car : public Vehicle {
};

class Bike : public Vehicle {
};

Hybrid Inheritance

A combination of more than one type of inheritance.

Also Read: Understanding C++: An Introduction and Learning Guide

Inheritance
class A {
public:
    void showA() { cout << "A\n"; }
};

class B : public A {
};

class C {
public:
    void showC() { cout << "C\n"; }
};

class D : public B, public C {
};

Access Specifiers in Inheritance

The type of inheritance affects the access level of base class members in the derived class.

Inheritance Typepublic membersprotected membersprivate members
publicpublicprotectednot inherited
protectedprotectedprotectednot inherited
privateprivateprivatenot inherited

Constructor and Destructor in Inheritance

Constructors of the base class are executed first, followed by the derived class.

class Base {
public:
    Base() { cout << "Base constructor\n"; }
};

class Derived : public Base {
public:
    Derived() { cout << "Derived constructor\n"; }
};

Destructors work in the reverse order: derived class destructor runs first, then base class destructor.

Calling Base Class Methods

Use the derived object directly or with scope resolution:

Derived d;
d.displayBase();

Function Overriding

A derived class can override a base class function with the same signature.

Inheritance
class Base {
public:
    void display() { cout << "Base Display\n"; }
};

class Derived : public Base {
public:
    void display() { cout << "Derived Display\n"; }
};

Virtual Functions and Runtime Polymorphism

When you declare a function virtual, C++ determines the correct function to call at runtime.

class Base {
public:
    virtual void show() { cout << "Base\n"; }
};

class Derived : public Base {
public:
    void show() override { cout << "Derived\n"; }
};

Base* ptr = new Derived();
ptr->show(); // Outputs: Derived

Using protected for Controlled Inheritance

If you don’t want public access to all base members, use protected inheritance.

class Parent {
protected:
    int age;
};

class Child : protected Parent {
public:
    void setAge(int a) { age = a; }
};

Ambiguity in Multiple Inheritance

When multiple base classes have functions with the same name:

class A {
public:
    void greet() { cout << "Hello from A\n"; }
};

class B {
public:
    void greet() { cout << "Hello from B\n"; }
};

class C : public A, public B {
public:
    void greet() {
        A::greet(); // Specify explicitly
    }
};

Real-Life Example: Employee and Manager

class Employee {
public:
    string name;
    double salary;
    void setDetails(string n, double s) {
        name = n;
        salary = s;
    }
};

class Manager : public Employee {
public:
    string department;
    void setDepartment(string dept) {
        department = dept;
    }
    void showDetails() {
        cout << name << " earns " << salary << " in " << department << endl;
    }
};

Common Mistakes in Inheritance

  • Forgetting to declare destructors as virtual when using base pointers.
  • Accessing private base class members directly.
  • Overriding without virtual, losing polymorphic behavior.
  • Creating ambiguity in multiple inheritance without scoping.

Best Practices for Inheritance in C++

  • Use inheritance only when there’s a genuine “is-a” relationship.
  • Prefer composition over multiple inheritance if design allows.
  • Always make base class destructors virtual when needed.
  • Keep base classes as abstract as possible.

FAQs

What is inheritance in simple terms?
Inheritance allows one class to acquire properties and methods of another.

Can a derived class access private members of a base class?
No, private members are not accessible directly. Use protected/public accessors.

What is function overriding in inheritance?
When a derived class redefines a base class function with the same signature.

What are virtual functions used for?
To achieve runtime polymorphism and dynamic method dispatch.

Can you inherit constructors in C++?
C++11 and above allows constructor inheritance using using Base::Base;.

Conclusion

It is the backbone of Object-Oriented Programming, and understanding it well sets the stage for mastering C++ software design. Through this tutorial by Kamlesh Singad in the Code With Kamlesh series, you now know the types of inheritance, how they work, and how to apply them effectively.

Use this knowledge to build real-world projects, organize code better, and take your C++ skills to an advanced level.

LEAVE A REPLY

Please enter your comment!
Please enter your name here