A diamond problem is an issue that occurs in programming languages, especially in C , when you are using multiple inheritances. Multiple inheritances in C are commonly used as a tool when the code is very lengthy. So to handle the source code, we use classes to manage the program. However, the multiple inheritances cause a problem if it is not used properly. These problems mainly contain the DIAMOND problem. This tutorial aims to highlight the main factors of the diamond problem, how it occurs from the multiple inheritances, and all the solutions required to resolve it.

To execute the programs regarding “C diamond inheritance” in the Linux operating system, you need to have an Ubuntu system installed and running on the Virtual machine. Two tools are used. One is any editing tool, and as such, we will use the default “text editor” of Linux. You may use other preferred editing tools. The second one is the Ubuntu terminal. On which, you will run the program and can see the output displayed.

First, we will discuss multiple inheritances in the article, as the “diamond problem” occurs in the case of having inheritance in the source code.

Multiple Inheritances in C

When we talk about the use of classes hierarchically in the program, we always know OOP (Object-Oriented Programming). Because this inheritance is an important feature of object-oriented, where a subclass is capable of inheriting from one or more superclasses. In this way, a child class has two or more parents.

For instance, if a mother and father have a child in a real-life scenario, the child will inherit everything from the parents. So this child is known to be a derived class with mother and father as parents. Let us go back toward the multiple inheritances. We will use “constructors” as a part of our current discussion. The constructors of an inherited class (child class) in multiple inheritances execute by following their inherited order. Whereas for the destructors, the order is the reverse of the inheritance. Now, we will quote a simple example to demonstrate the functionality of inheritance in C .

Example of Multiple Inheritances

Consider an example in which there are two classes, class A and class B, as a parent class, and these two classes have a child class named class C. This class is a derived class of both of its parents. We will use a constructor in the public part of each class.

The first step in the code is to use a library to allow the input-output streaming:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/C-Diamond-Problem-1.png" data-lazy- height="402" src="data:image/svg xml,” width=”659″>

Then we have to declare class A, having the constructors with the name of the class. As you know that constructors are declared with the name of that class, and these are called whenever the object is created. In the constructor, a simple message has been displayed that shows which class constructor is executed. Now, we define class B with the same approach. After both parent classes, the child class is mentioned.

One thing that should be noted here is the order of parent classes the child inherits because this order will matter at the time of constructor execution and a message displaying.

Now, in the main class, we will create an object of the child class. As it has multiple inheritances, there is no need to create the object for the parent class. They are automatically executed because of the child class object:

Int main ()

{ C c;


Return 0;   }

After writing the previous code in the text editor, save this file with the extension of ‘.C’. We will execute the file in the Ubuntu terminal. For the execution purpose, a compiler is required. In C , we use a G compiler. Otherwise, you need first to install it:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/C-Diamond-Problem-2.png" data-lazy- height="98" src="data:image/svg xml,” width=”407″>

Use g with the name of the file having the source code and the one in which you want to show the output. Note, –o is used to save the output. As Class B is inherited above the class A, so its constructor is executed first, you can see the output from the previous image.

As the concept of inheritance is clear now, we will discuss the “Diamond problem” here.

Diamond Problem

A diamond problem is a case only in multiple inheritances that occurs when a child class has the values inherited from the two parents. Wherein these parent classes are inherited from a common grandparent class.

For example, consider an example in which we have a Child class inherited from the classes of Mother and Father. These classes inherit a third-class named “person”:

Child > Mother> Person


         > Father > Person

So, according to the given scenario, the child class inherits the “person” class two times in the program. Once, it is from the mother, and again, the second time is from the father. This creates confusion for the compiler to execute which constructor first. This situation causes a diamond-shaped inheritance graph. Hence, it is known as “The Diamond Problem”.

The code approach is almost the same. Declare the base class and then two inherited child (mother, father) classes of the base class. Each class is followed by the constructor with a variable to store a value in it:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/C-Diamond-Problem-3.png" data-lazy- height="409" src="data:image/svg xml,” width=”589″>

Now, introducing the child class:

# Class Child: public father, public mother

The child class will inherit both parent classes. The main function will use the object of the child and a value in the parameter of the constructor call:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/C-Diamond-Problem-4.png" data-lazy- height="205" src="data:image/svg xml,” width=”444″>

After saving the code, it is time to use the compiler for the execution and see the result:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/C-Diamond-Problem-5.png" data-lazy- height="129" src="data:image/svg xml,” width=”365″>

Now, you can observe that the base class is called two times. This is a diamond problem. After describing the problem, we will now find a possible solution.

Solution of Diamond Problem

The solution depends on the use of the keyword “virtual”. Two-parent classes with a common base class will now inherit the base class virtually to minimize the occurrence of copies of the base class in the child class. Now, we will modify the code by using that keyword:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/C-Diamond-Problem-6.png" data-lazy- height="489" src="data:image/svg xml,” width=”526″>

Whenever the parent class inherits from the grandparent class, “virtual” is used but only with the parents, not in the case of a child. This is “the virtual inheritance”. It limits the passing of more than a single instance from the base class to be passed.

# Class father: virtual public person

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/C-Diamond-Problem-7.png" data-lazy- height="206" src="data:image/svg xml,” width=”426″>

Now, we will execute the code. The resultant values show that ambiguity is removed using this concept:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/C-Diamond-Problem-8.png" data-lazy- height="118" src="data:image/svg xml,” width=”353″>

To avoid the repetition of the base constructor to be called, the constructor for a virtual base class is not reached through the class that has inherited it. However, this constructor is called from the concrete class constructor. In the current example, the child class calls the “person” class constructor directly.

Conclusion

“C diamond problem” is an article written to remove the ambiguity of the base class repetition in multiple inheritances. The concept of inheritance is explained briefly with examples. Similarly, the cause and the solution for the diamond problem are also elaborated in detail. We hope this tutorial can provide you guidance in the field of C programs.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/Author-Image.jpg6172d01c0542d.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.