Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now

Inheritance in C++

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:04:00

What is Inheritance in C++?

In C++, inheritance is a fundamental object-oriented programming (OOP) concept that allows one class to inherit its attributes and functions from another. This implies that in addition to adding new members, the derived class can utilize every member of the base class. 

Why use Inheritance in C++?

  • Inheritance allows code reuse, which reduces duplication and effort.
  • It organizes code hierarchically to improve readability and maintainability.
  • Polymorphism, based on inheritance, increases versatility in C++ OOP.

When to use Inheritance in C++?

  • When there is a class hierarchy.
  • When you need to create polymorphism classes.
  • When you want to organize your code.

Different types of Inheritance Classes

There are two types of inheritance classes in C++:

  1. Base Class
  2. Derived Class

Derived class

A derived class inherits the properties and methods of its base class. The derived class may inherit all or a portion of the original class's characteristics and methods. The derived class may also contain its own set of attributes and methods.

Base Class

A base class is one that other classes derive from. The base class specifies the characteristics and methods that its subclasses inherit. In addition, the base class can define its properties and methods.

Types of Inheritance in C++

C++ supports the 5 types of inheritance listed below:

  1. Single inheritance
  2. Multiple inheritance
  3. Multilevel inheritance
  4. Hierarchical inheritance
  5. Hybrid inheritance

Single inheritance

A derived class inherits only from one base class. It creates a parent-child relationship, allowing the derived class to access members of the base class.

Multiple inheritance

A derived class can inherit from several base classes. This allows the derived class to access the members and behaviors of several parent classes, resulting in complex class hierarchies.


Multilevel inheritance

A chain of inheritance in which one derived class acts as the base class for another. This generates a class hierarchy in which each level inherits characteristics and behaviors from the level above it.


Hierarchical inheritance

Multiple derived classes inherit from a single base class. Each derived class shares similar traits with the original class but may have its distinct features.


Hybrid inheritance

Hybrid inheritance combines many types of inheritance inside a single class hierarchy, including single, multiple, multilevel, and hierarchical inheritance. It enables complex relationships & structures between classes, which may lead to greater code complexity.

Access Modes of Inheritance

In C++, there are 3 Access Modes for Inheritance:

  1. Public Mode
  2. Protected Mode
  3. Private Mode

Advantages of Inheritance in C++

  • In C++, inheritance allows for the creation of new classes based on existing ones, saving time and effort through code reuse.
  • C++ inheritance organizes code logically by creating a hierarchy of related classes, facilitating comprehension.
  • Inheritance allows for polymorphism when derived class objects are treated as their base class counterparts at runtime.
  • C++ inheritance allows for method overriding in derived classes, which changes method behavior without affecting the source code.
  • C++ inheritance enables abstract classes by specifying basic behavior for related classes and delegating implementation details to derived classes.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this