CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C++ – Multiple Inheritance – Virtual inheritance to overcome inheritance ambiguity

Another interesting feature of Object Oriented Programming is its inheritance. Inheritance inherits the behavior or attributes from its parent or base classes to the derived classes. C++ uses virtual inheritance to overcome the inheritance ambiguity in multiple inheritance. Multiple inheritance means, derived classes inherit from multiple base classes.

Inheritance ambiguity will occur when a derived class object is converted to its parents’ parent class; where its immediate parents’ have a common base class.

Let’s take a simple example:

// Sample.cpp
//
#include <iostream>

using namespace std;

class Shape
{
public:
	virtual void Display()
	{
		cout << "Shape" << endl;
	}
};

class Circle : public Shape
{
public:
	void Display()
	{
		cout << "Circle" << endl;
	}
};

class Rectangle : public Shape
{
public:
	void Display()
	{
		cout << "Rectangle" << endl;
	}
};

class CircleInsideRectangle : public Circle, public Rectangle
{
public:
	void Display()
	{
		cout << "CircleInsideRectangle" << endl; 	
	} 
}; 
//
//  
int main()
{ 	
	Shape *pObj = new CircleInsideRectangle();
 	if ( pObj != NULL )
 		pObj->Display();

	return 0;
}

Observe that the Shape is the base class. Circle and Rectangle classes are derived from the Shape class. Another class CircleInsideRectangle is created and it is derived from both Circle and Rectangle classes.

We can create an instance of CircleInsideRectangle class and assign it to its super class Shape. Theoretically, it’s correct.

Let’s compile the code:

cl Sample.cpp /EHsc

The compiler throws the below error message:

sample.cpp(45) : error C2594: 'initializing' : ambiguous conversions from 'CircleInsideRectangle *' to 'Shape *'

Because CircleInsideRectangle class is derived from Circle and Rectangle classes, and these classes have a common parent class Shape. When we cast CircleInsideRectangle to Shape; it didn’t understand whether to cast to Circle’s Shape or Rectangle’s Shape.

One solution to this is, explicitly cast the objects. So, it will be like this:

Shape *pObj = (Circle *)new CircleInsideRectangle();

But, if we have large lines of code and if we need to do this type of casting in multiple places; this is not the recommended way. Another alternative is, to use Virtual Inheritance.

If multiple classes have the same base class, derive the classes using virtual inheritance instead of normal inheritance. So, at the time of assigning a derived class object to its super class object; it will ensure to take the single path to its super class.

So, our code will become like this:

class Circle : public virtual Shape

and

class Rectangle : public virtual Shape

Once you made these changes and compile the program; it will compile with NO errors.

These are the benefits to use virtual keywords in C++ programming language.

..

C++ – Multiple Inheritance – Virtual inheritance to overcome inheritance ambiguity

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top