Inheritance in C++: Types and Syntax Explained with Examples

Introduction

Inheritance is a fundamental concept in object-oriented programming (OOP), and C++ fully supports this feature. It allows a class (called the derived or child class) to inherit properties and behaviors from another class (called the base or parent class). This concept promotes code reusability, maintainability, and scalability, making it easier to build complex applications with less redundancy.

Inheritance in C++

In this blog, we'll explore the types of inheritance in C++, how they work, their syntax, and practical examples. We'll also touch upon where inheritance is useful in real-world scenarios, and how it’s asked in interviews. 

What is Inheritance in C++? 

Inheritance is a mechanism through which one class acquires the properties (data members) and functionalities (member functions) of another class. It helps in building hierarchical relationships between classes. 

Key Concepts: 

  • Base Class: The class whose members are inherited. 
  • Derived Class: The class which inherits members from the base class. 

Benefits of Inheritance 

  • Code Reusability: Avoids duplication by reusing existing code. 
  • Extensibility: Easily extend existing classes. 
  • Maintainability: Cleaner and more organized code. 
  • Polymorphism: Enables function overriding and dynamic method dispatch. 

Syntax of Inheritance 

Copy Code

class BaseClass { 

   // base class members 

}; 

class DerivedClass : accessSpecifier BaseClass { 

   // derived class members 

};

Example: 

Copy Code

#include <iostream> 

using namespace std; 

class Animal { 

public: 

    void speak() { 

        cout << "Animal speaks" << endl; 

    } 

}; 

class Dog : public Animal { 

public: 

    void bark() { 

        cout << "Dog barks" << endl; 

    } 

}; 

int main() { 

    Dog myDog; 

    myDog.speak();  // Inherited function 

    myDog.bark();   // Dog’s own function 

    return 0; 

}

Types of Inheritance in C++ 

C++ supports the following types of inheritance: 

1. Single Inheritance 

A single derived class inherits from a single base class. 

Copy Code

class A { 

public: 

    void display() { 

        cout << "Class A" << endl; 

    } 

}; 

class B : public A { 

};

2. Multiple Inheritance 

A derived class inherits from more than one base class. 

Copy Code

class A { 

public: 

    void showA() { 

        cout << "Class A" << endl; 

    } 

}; 

class B { 

public: 

    void showB() { 

        cout << "Class B" << endl; 

    } 

}; 

class C : public A, public B { 

};

3. Multilevel Inheritance 

A class is derived from a derived class. 

Copy Code

class A { 

public: 

    void display() { 

        cout << "Class A" << endl; 

    } 

}; 

class B : public A { 

}; 

class C : public B { 

};

4. Hierarchical Inheritance 

Multiple derived classes inherit from a single base class. 

Copy Code

class A { 

public: 

    void display() { 

        cout << "Base class A" << endl; 

    } 

}; 

class B : public A { 

}; 

class C : public A { 

};

5. Hybrid Inheritance 

Combination of two or more types of inheritance. 

Copy Code

class A { 

public: 

    void displayA() { 

        cout << "Class A" << endl; 

    } 

}; 

class B : public A { 

}; 

class C { 

public: 

    void displayC() { 

        cout << "Class C" << endl; 

    } 

}; 

class D : public B, public C { 

};

Access Specifiers in Inheritance 

The access level of inherited members depends on the access specifier used: 

Access Specifier Public Members Protected Members Private Members 
Public Public Protected Not Inherited 
Protected Protected Protected Not Inherited 
Private Private Private Not Inherited 

 

Constructor and Destructor in Inheritance 

Constructors of base classes are called before derived class constructors. Destructor order is the reverse. 

Copy Code

class A { 

public: 

    A() { 

        cout << "Constructor of A" << endl; 

    } 

    ~A() { 

        cout << "Destructor of A" << endl; 

    } 

}; 

class B : public A { 

public: 

    B() { 

        cout << "Constructor of B" << endl; 

    } 

    ~B() { 

        cout << "Destructor of B" << endl; 

    } 

};

Function Overriding in Inheritance 

Function overriding allows a derived class to provide a specific implementation of a method defined in the base class. 

Copy Code

class Base { 

public: 

    void show() { 

        cout << "Base class show()" << endl; 

    } 

}; 

class Derived : public Base { 

public: 

    void show() { 

        cout << "Derived class show()" << endl; 

    } 

};

Real-life Example of Inheritance 

Let’s say you’re building a vehicle system. 

Copy Code

class Vehicle { 

public: 

    void start() { 

        cout << "Starting vehicle" << endl; 

    } 

}; 

class Car : public Vehicle { 

public: 

    void drive() { 

        cout << "Driving car" << endl; 

    } 

};

Interview Questions on Inheritance 

1. What is inheritance in C++? 

2. Explain types of inheritance with examples. 

3. What happens when constructors and destructors are involved? 

4. How does access specifier affect inheritance? 

5. What is function overriding? 

6. How is multiple inheritance resolved in C++? 

Use Cases of Inheritance 

  • GUI Libraries (Widget -> Button, TextBox) 
  • Game Development (Character -> Player, Enemy) 
  • Banking Software (Account -> Savings, Current) 
  • E-commerce (User -> Admin, Customer) 

Learn More with Uncodemy 

Want to explore C++ concepts deeper? Join Uncodemy's C++ Programming Course and start your journey with expert-led tutorials: 

 C++ Programming Course on Uncodemy 

Conclusion 

Inheritance in C++ is a cornerstone of object-oriented design. It promotes reusable, maintainable, and scalable software architecture. By understanding the types, syntax, and nuances of inheritance, you can write better and more professional C++ code. 

Prepare well with theory, implement examples, and practice problems to gain mastery whether for projects, interviews, or real-world applications. 

Placed Students

Our Clients

Partners

...

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses