Latest Web Stories

1

Exploring Smartphone Influence on the Brain: Neurological Perspectives

2

Angular vs. React: Which One Should You Choose?

3

Discover Serenity: Your Guide to Mindfulness Apps

4

Discover how smartphone apps can enhance your mindfulness practice: 5 Senses Exercise with Apps

5

Discover How Smartwatches Measure Blood Pressure: Explained Simply

6

13 Exciting Games Launching in April 2024: From Freedom Planet 2 to TopSpin 2K25!

7

Discover the 5 Amazon Big Spring Deals on Tablets from Samsung, Lenovo

8

Big Savings Alert: Amazfit Smart Watches Now on Sale on Amazon!

9

Amazon's Big Spring Sale: Top 6 Anker Souncore Headphones and Earbuds Deals

10

Affordable VR Adventures: The Best Budget VR Headsets

11

Fly in Peace: Discover the Ultimate Noise-Cancelling Headphones for Flying

12

Bringing AI to Life: NVIDIA's Digital Human Technolgies in Healthcare, Gaming, and More

13

Discover Exciting New Games on NVIDIA GeForce NOW!

14

Steam Spring Sale 2024 is here: Explore the 10 Best FPS Games

15

The Future of iPhones: Apple's Bold Step into AI with DarwinAI

16

Discover the Magic of Sonos Soundbar: Transform Your Home Entertainment Experience!

17

Enhance Your Home Fun: 5 Best Sonos Soundbars to Amp Up Your Entertainment!

18

Pinterest Introduces AI-Powered Body Type Ranges for Inclusive Searches

19

Embrace the Next Wave: 35+ AI Tools for Enhanced Productivity in 2024

20

Xbox Gaming Bonanza: Lots of New Games with Xbox Game Pass!

21

Sony Paves the Way for Gaming Evolution with 'Super-Fungible Tokens' Patent

22

Smart Printing Choices: 10 Key Factors to Consider When Buying an HP Printer or Any Printer

23

Projector Picks: Explore the Best Mini Projectors for Big Screen Fun!

24

JavaScript Essentials: Your Quick Start to Web Coding

25

Gaming Laptop Guide 2024: 10 Crucial Checks Before You Buy + Top 5 Picks for you!

26

Gaming Joy Awaits: Explore the Best PS5 Games of 2024

27

Epic Games Special: Dive into Astro Duel 2 for FREE this week. See What’s Coming Next Week!

28

Fitbit Fitness Tracker Guide 2024: Choose Your Perfect Fit

29

Feel the Beat: Exploring Top Over-Ear Headphones

30

Explore the Web Development Strategies in 2024: A Developers Handbook

31

Explore Must-Play Nintendo Switch Games in 2024!

32

Eclipse Ready: CE and ISO Certified Solar Eclipse Glasses for a Safe Sky Spectacle

33

Disney and Pixar’s Inside Out 2 Introduces New Emotions to Riley's World

34

Discover Waze's cool new features for safer and happier drives!

35

Discover the Top Picks: Best Smartwatches for Your Lifestyle

36

Discover the Best Smartphones Trending Now: Your Easy Guide to the Best Picks!

37

Sound Revolution: Discover the Best Bluetooth Speakers of 2024!

38

Discover the 10 Best Productivity Apps to Supercharge Your Daily Tasks

39

Discover,Install and Enjoy: The Best Chrome Extensions for Developers in 2024

40

Crack the Code: Your Guide to Computer Programming Magic in 2024

41

Boost Your Content Creation: 10 ChatGPT Prompts to Supercharge Content Creation Productivity

42

10 Best Tech Companies in Silicon Valley

43

Top 10 Web Development Interview Questions you can...

44

Learn how to Answer Tell me about Yourself

45

5 Books You Need to Read Right Now

46

25 Practical Ways to Earn Money Online

1. Introduction to C++: A Comprehensive Guide with Code Examples - 2023 2. Mastering Data Types in C++: A Comprehensive Guide with Codes and Examples (2023) 3. Learn About Variables and Types of Variables in C++ | Codzify.com 4. Control Statements in C++: A Comprehensive Guide for 2023 5. C++ Tutorial: Understanding Switch Statements with Codes and Examples in 2023 6. Understanding Memory Allocation and Pointers in C++: A Beginners Guide 7. Functions in C++ 8. Call by value and Call by Reference in C++ in depth 9. Array in C++ 10. 2d arrays in C++ 11. Classes and Objects in C++ 12. Static Functions in C++ 13. Constructors and Destructors in C++ - A Complete Guide with Examples 14. Mastering Copy Constructor in C++ - Shallow vs Deep Copy with Examples | Codzify 15. Understanding Friend Functions in C++ Made Simple! 16. Inline Functions in C++ 17. this Pointer in C++ 18. Mastering Inheritance in C++: Types and Examples Explained 19. Types of Inheritance in C++ 20. Polymorphism in C++ Explained with Codes and Examples in 2023 21. Templates in C++ 22. Getting the Value of a MultiMap in C++: Step-by-Step Guide with Examples 23. Multimap Find and Replace Operator in C++: Step-by-Step Guide - Codzify Topics 24. Exploring the Next_Permutation Algorithm without STL in C++ - Codzify Topics 25. C++ - The Difference Between Map and HashMap in STL 26. Updating Values in a std::multimap in C++ - Codzify Topics 27. Which data structure sorts the elements on insertion in C++ STL? 28. Can we implement Red Black Tree in c++ by STL containers? 29. How to Dynamically Declare an Array of Objects with a Constructor in C++ - A Step-by-Step Guide 30. What is the difference between a pointer and an object in C++? 31. Mastering Red-Black Trees with STLs Internal Implementation: A Step-by-Step Guide

Translate this page in your preferred language:


Mastering Inheritance in C++: Types and Examples Explained

Article by: Manish Methani

Last Updated: October 16, 2021 at 2:04pm IST
9 min 6 sec

Table of Contents:

Introduction

Inheritance is a fundamental concept in C++ that enables a class to inherit properties and behaviors from another class, promoting code reuse and hierarchy within programs.

Syntax of Inheritance

In C++, inheritance syntax involves creating a derived class that inherits attributes and behaviors from a base class. Here's a basic representation:

// Base Class
class Base {
public:
    void display() {
        cout << "This is from the base class." << endl;
    }
};

// Derived Class inheriting from Base
class Derived : public Base {
public:
    void show() {
        cout << "This is from the derived class." << endl;
    }
};

Types of Inheritance

1. Single Inheritance

Single Level Inheritance involves deriving only one class from the base class. It establishes a straightforward parent-child relationship between two classes.

#include <iostream>
using namespace std;

class Base {
public:
    void display() {
        cout <<"This is the Base class" << endl;
    }
};

class Derived : public Base {
public:
    void show() {
        cout << "This is the Derived class" << endl;
    }
};

int main() {
    Derived d;
    d.display(); // Accessing Base class function
    d.show();    // Accessing Derived class function

    return 0;
}

Output:

This is the Base class
This is the Derived class
 

2. Multi-Level Inheritance

Multi-Level Inheritance signifies a chain of inheritance where one derived class becomes the base class for another derived class. It allows for multiple levels of derived classes.

#include <iostream>
using namespace std;

class A {
public:
    void displayA() {
        cout << "This is class A" << endl;
    }
};

class B : public A {
public:
    void displayB() {
        cout << "This is class B" << endl;
    }
};

class C : public B {
public:
    void displayC() {
        cout << "This is class C" << endl;
    }
};

int main() {
    C c;
    c.displayA(); // Accessing Class A method
    c.displayB(); // Accessing Class B method
    c.displayC(); // Accessing Class C method

    return 0;
}

Output:

This is class A
This is class B
This is class C

3. Hierarchical Inheritance

Hierarchical Inheritance occurs when multiple classes are derived from a single base class. It forms a hierarchical structure, branching out from a common base.

#include <iostream>
using namespace std;

class Base {
public:
    void display() {
        cout << "This is the Base class" << endl;
    }
};

class Derived1 : public Base {
public:
    void show1() {
        cout << "This is the first derived class" << endl;
    }
};

class Derived2 : public Base {
public:
    void show2() {
        cout << "This is the second derived class" << endl;
    }
};

int main() {
    Derived1 d1;
    Derived2 d2;
    d1.display(); // Accessing Base class function via first derived class
    d1.show1();   // Accessing first derived class function
    d2.display(); // Accessing Base class function via second derived class
    d2.show2();   // Accessing second derived class function

    return 0;
}

Output:

This is the Base class
This is the first derived class
This is the Base class
This is the second derived class

Multiple Inheritance

Multiple Inheritance occurs when a single class inherits properties and methods from multiple base classes. It provides a way to combine functionalities from different sources.

#include <iostream>
using namespace std;

class A {
public:
    void displayA() {
        cout << "This is class A" << endl;
    }
};

class B {
public:
    void displayB() {
        cout << "This is class B" << endl;
    }
};

class C : public A, public B {
public:
    void displayC() {
        cout << "This is class C" << endl;
    }
};

int main() {
    C c;
    c.displayA(); // Accessing Class A method
    c.displayB(); // Accessing Class B method
    c.displayC(); // Accessing Class C method

    return 0;
} 

Output:

This is class A
This is class B
This is class C

Hybrid Inheritance

Hybrid Inheritance is a combination of multiple inheritance types, encompassing multilevel, multiple, and hierarchical inheritance. It brings together various inheritance forms.

#include <iostream>
using namespace std;

class A {
public:
    void displayA() {
        cout << "This is class A" << endl;
    }
};

class B : public A {
public:
    void displayB() {
        cout << "This is class B" << endl;
    }
};

class C : public A {
public:
    void displayC() {
        cout << "This is class C" << endl;
    }
};

class D : public B, public C {
public:
    void displayD() {
        cout << "This is class D" << endl;
    }
};

int main() {
    D d;
    d.B::displayA(); // Accessing Class A method from class B
    d.C::displayA(); // Accessing Class A method from class C
    d.displayB();    // Accessing Class B method
    d.displayC();    // Accessing Class C method
    d.displayD();    // Accessing Class D method

    return 0;
}

Output:

This is class A
This is class A
This is class B
This is class C
This is class D

Conclusion

Inheritance in C++ allows for the creation of complex class hierarchies, promoting code organization, and reuse. Understanding its types and syntax is crucial for building scalable and maintainable applications.

FAQ

1. What is Inheritance in C++?

In C++, Inheritance is a mechanism that allows a new class to inherit properties and behaviors (methods and attributes) from an existing class. It enables code reusability and establishes a hierarchical relationship between classes.

2. How does Single Level Inheritance work?

Single Level Inheritance involves only one base class and one derived class. The derived class inherits properties and behaviors from a single base class, forming a parent-child relationship.

3. What is Multi-Level Inheritance?

Multi-Level Inheritance refers to a chain of inheritance where a derived class inherits from a base class, and another class derives from this derived class. It creates a hierarchical order of classes.

4. Can a class be derived from multiple base classes in C++?

Yes, C++ supports Multiple Inheritance, allowing a derived class to inherit from more than one base class. It is essential to manage potential ambiguities and conflicts that can arise due to multiple inheritance.

5. What is Hybrid Inheritance?

Hybrid Inheritance is a combination of different types of inheritance (e.g., multiple, multilevel, or hierarchical). It incorporates multiple inheritance mechanisms within a single program, offering flexibility in class relationships.

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App