Javatpoint Logo
Javatpoint Logo

Diamond Problem in C++

When employing numerous inheritances, a diamond problem can arise in computer languages, particularly in C++. When the code is exceedingly long, many inheritances in C++ are frequently utilized as a technique. So, in order to organize the program and the source code, we utilize classes. However, if they are not handled appropriately, multiple inheritances might be problematic. The DIAMOND issue is mostly present in these puzzles. The primary causes of the diamond problem, how it results from numerous inheritances, and all the remedies needed to overcome it are highlighted in this lesson.

System Requirement for the code

You need to have an Ubuntu system installed and running on the virtual machine in order to run the applications pertaining to "C++ diamond inheritance" under the Linux operating system. It takes two tools. One is any editing tool; therefore we'll utilize Linux's built-in "text editor" for this. Other favorite editing tools are available. The Ubuntu terminal is the second one which you will use to run the program and view the output. Since the "diamond problem" arises when inheritance is present in the source code, we will first talk about many inheritances in this article.

Multiple Inheritances

For instance, in a real-world scenario, if a mother and a father conceive a kid, the child will inherit everything from the parents. As a result, it is recognized that the mother and father of this kid are of a derived class. Returning to the various inheritances, let's go. "Constructors" will be used throughout the course of this conversation. When there are multiple inheritances, the constructors of an inherited class (child class) run in the order of the ancestors. For the destructors, however, the inheritance order is reversed. We will now use a straightforward example to illustrate how inheritance works in C++.

A class may inherit from multiple classes thanks to C++'s Multiple Inheritance feature. Inherited classes' constructors are called in the same order that they were inherited. For instance, in the program below, class second constructor is called before A.

A class can descend from many base classes.

Example:

  • The FATHER and MOTHER classes are the ancestors of the CHILD class.
  • The LIQUID and FUEL classes give rise to the PETROL class.

Let us look at an example to better understand the concept:

OUTPUT:

two's constructor is called here
one's constructor is called here
three's constructor is called here
?????????????????..
Process executed in 0.11 seconds
Press any key continue.

The destructors are called after the constructors, in reverse order.

The Diamond Problem: When two super classes of a class share a base class, the diamond issue arises. For instance, in the diagram below, the TA class receives two copies of every attribute from the Person class, which results in ambiguities. Think about the following program, for instance.

OUTPUT:

Base :: Base ( int ) called
Derived :: Derived ( int ) called
Base :: Base ( int ) called
Derived_2 :: Derived_2 ( int ) called
Result :: Result ( int ) called
?????????????????..
Process executed in 0.11 seconds
Press any key continue.

Explanation

The class "Base" is called twice in the code mentioned above. When object "rs1" is destroyed, the destructor of "Base" will likewise be invoked twice. Due to the fact that object "rs1" contains two of every "Base" element, confusion results. This issue may be solved by using the phrase "virtual." To prevent having two instances of "Base" in the "Result" class, we create the classes "Derived" and "Derived_2" as virtual base classes. Think about the following program, for instance.

OUTPUT:

Base :: Base ( ) is called
Derived :: Derived ( int ) is called
Derived_1 :: Derived_1 ( int ) is called
Result :: Result ( int ) is called	
?????????????????..
Process executed in 0.11 seconds
Press any key continue.

Explanation

A single call to the function Object() { [native code] } for "Person" is made in the program above. The fact that the default function Object() { [native code] } of "Person" is called in the output mentioned above is crucial to notice. Even if the parent classes explicitly call the parameterized function Object() { [native code] }, the default function Object() { [native code] } of the grandparent class is invoked by default when the "virtual" keyword is used. How can I use the 'Person' class's parameterized function Object() { [native code] }? In the "TA" class, the function Object() { [native code] } must be invoked. Take the following software as an illustration.

OUTPUT:

Base :: Base ( int ) is called
Derived :: Derived ( int ) is called
Derived_1 :: Derived_1 ( int ) is called
Result :: Result ( int ) is called
?????????????????..
Process executed in 0.11 seconds
Press any key continue.

Explanation

The function Object() { [native code] } of a grandparent must typically be called through the parent class rather than directly. Only when the keyword "virtual" is used is it permitted.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA