What is inheritance in C++?

Inheritance in C++ is a key feature of Object-Oriented Programming (OOP) that allows a class (called the derived class or child class) to inherit properties and behaviors (methods and data members) from another class (called the base class or parent class). This concept enables code reuse, extensibility, and hierarchical relationships among classes.

Key Concepts of Inheritance:

  1. Base Class (Parent Class): The class from which properties and methods are inherited.
  2. Derived Class (Child Class): The class that inherits the properties and methods from the base class.

Syntax:

class Base { // Members and methods of the base class }; class Derived : public Base { // Inherits members and methods from Base class };

Types of Inheritance in C++:

  1. Single Inheritance: A derived class inherits from only one base class.

    class A { public: void display() { cout << "Class A"; } }; class B : public A { // Class B inherits from class A };
  2. Multiple Inheritance: A derived class inherits from more than one base class.

    class A { // Class A definition }; class B { // Class B definition }; class C : public A, public B { // Class C inherits from both A and B };
  3. Multilevel Inheritance: A derived class inherits from another derived class, creating a chain of inheritance.

    class A { // Class A definition }; class B : public A { // Class B inherits from A }; class C : public B { // Class C inherits from B };
  4. Hierarchical Inheritance: Multiple derived classes inherit from the same base class.

    class A { // Class A definition }; class B : public A { // Class B inherits from A }; class C : public A { // Class C also inherits from A };
  5. Hybrid Inheritance: A combination of two or more types of inheritance, typically using multiple inheritance along with other forms.

Example of Inheritance:

#include <iostream> using namespace std; class Animal { public: void eat() { cout << "Eating..." << endl; } }; class Dog : public Animal { // Inheritance public: void bark() { cout << "Barking..." << endl; } }; int main() { Dog myDog; myDog.eat(); // Inherited from Animal class myDog.bark(); // Defined in Dog class return 0; }

Access Specifiers in Inheritance:

The relationship between the base class and the derived class depends on the access specifier (public, protected, or private) used when inheriting:

  • Public Inheritance: Members of the base class retain their access control in the derived class (public members remain public, protected members remain protected).
  • Protected Inheritance: Public and protected members of the base class become protected in the derived class.
  • Private Inheritance: Public and protected members of the base class become private in the derived class.

Benefits of Inheritance:

  • Code Reusability: Inherited classes can reuse the code from the base class, reducing redundancy.
  • Extensibility: New functionality can be added in derived classes without modifying the existing base class.
  • Modularity: Allows the program to be organized in a hierarchical manner, making it easier to maintain.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What coding language is for AI?
Is Cloudflare a good company?
How to prepare for Mongodb coding interview for experienced individuals?
What are the tips for coding interviews at educational tech companies?
Employing caching strategies thoughtfully in system design
What is the best color to wear for a Zoom presentation?
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.