Inheritance in C++

Inheritance in C++

  • In C++, inheritance is a fundamental OOP concept that enables you to create new classes based on existing classes.
  • Inheritance promotes code reuse and supports the "is-a" relationship between objects.
  • Inheritance involves two types of classes:
  • Base Class (Parent Class): This is the class that provides a blueprint or template for creating derived classes.
  • Derived Class (Child Class): This is the class that inherits properties and behaviors from the base class. It can also add its own unique attributes and methods to its behavior.

Why and When to Use Inheritance in C++?

  • Inheritance helps in organizing code into manageable and understandable modules.
  • Inheritance supports polymorphism, which allows objects of derived classes to be treated as objects of their base class.
You should consider using inheritance when:
  • For example, if you have multiple classes that share common attributes and behaviors, inheritance can be a suitable choice.
  • You want to create a hierarchy of classes, where base classes define common characteristics, and derived classes provide specialization.

Modes of Inheritance in C++

  • In C++, there are three modes of inheritance: public, private, and protected.
  • Public Inheritance: With public inheritance, the public members of the base class become public members within the derived class
  • and protected members become protected members.
  • Members in the base class marked as "private" cannot be accessed in the derived class. let's understand with an example
Loading…
  • Private Inheritance: With private inheritance, both the public and protected members of the base class transform into private members within the derived class.
Loading…
  • Protected Inheritance: In protected inheritance, public members of the base class become protected members in the derived class,
  • and protected members of the base class also become protected members in the derived class.
Loading…

Types of Inheritance in C++

  • Single inheritance
  • Multilevel inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Hybrid inheritance

C++ Single Inheritance

  • Single inheritance is a form of inheritance in which a derived class inherits from just one base class.
  • This type of inheritance is straightforward and commonly used to model "is-a" relationships. for example
  • C++ Single Inheritance Syntax as follows:
Loading…

C++ Single Inheritance Example

Let's grasp the concept of Single Inheritance with an example:
Loading…
  • In this example, we have a base class Animal with a method eat, and a derived class Dog that inherits from Animal.
  • Dog can access and use the eat method from its base class.

C++ Multilevel Inheritance

Multilevel inheritance occurs when a derived class inherits from another derived class, creating a chain of inheritance.
C++ Multilevel Inheritance Syntax :
Loading…

C++ Multilevel Inheritance Example

let's understand with an example :
Loading…
  • In this example, we have a base class Animal, an intermediate class Dog that inherits from Animal, and a derived class GoldenRetriever that inherits from Dog.
  • This creates a chain of inheritance where each class can access the methods of its parent class.

C++ Multiple Inheritance

  • Multiple inheritance happens when a derived class inherits from two or more base classes where ambiguity may arise in the case of shared features.
  • Example: A FlyingCar class inheriting from both Car and Aircraft classes, combining features of both types of vehicles.
  • C++ multiple inheritance Syntax is as follows:
Loading…

C++ Multiple Inheritance Example

Loading…
  • We have two base classes, Animal and Machine, each with their respective methods.
  • The Robot class is derived from both Animal and Machine using multiple inheritance.

Ambiguity resolution in C++ Inheritance

  • In C++, ambiguity resolution refers to the process of resolving conflicts that arise when there is ambiguity in the usage of functions or variables due to multiple inheritance or overloaded functions.
  • Ambiguity can occur when the compiler encounters a situation where it's unsure which function or variable to use, and it needs to determine the correct choice.
C++ Ambiguity resolution Syntax:
This is typically done using the scope resolution operator::
Loading…

Resolving Ambiguity in Multiple Inheritance

Loading…
In this example, we resolve the ambiguity between Base1 and Base2's display functions by using the scope resolution operator to specify which one to call.

C++ Hierarchical Inheritance

  • Hierarchical inheritance is a type of inheritance where several derived classes inherit from a common base class.
  • Each derived class forms its own unique branch of the inheritance tree, sharing common characteristics from the base class.
  • Example: A Bird class, Fish class, and Mammal class all inherit from an Animal class.
  • Syntax of Hierarchical Inheritance as follows:
Loading…

C++ Hierarchical Inheritance Example

Loading…
  • In this example, we have a base class Shape, and two derived classes, Circle and Rectangle, both inheriting from Shape.
  • This represents hierarchical inheritance, where multiple classes inherit from a single base class.

C++ Hybrid Inheritance

  • Hybrid inheritance is a combination of two or more types of inheritance within a program.
  • It often includes a mix of single, multiple, multilevel, or hierarchical inheritance.
  • syntax of hybrid Inheritance as follows:
Loading…

C++ Hybrid Inheritance Example

Loading…
In this example, we have a combination of multiple inheritance types. RoboCop inherits from both RobotDog and Machine, demonstrating hybrid inheritance.