Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Inheritance, C++ and Same Method Signature in Multiple Super Classes

Tags:

I have no experience in C++, and I come from a Java background. Lately, I was asked in an interview on why Java would not allow multiple inheritence and the answer was pretty easy. However, I am still curious on how C++ deals with that since it allows you to inherit from more than one class.

Specifically, say there is a class called MechanicalEngineer and another called ElectricalEngineer. Both have a method called buildRobot().

What happens if we make a third class RoboticsEngineer, that inherets from both and does not override that method, and you just call:

(some instance of RoboticsEngineer).buildRobot() 

Will an exception be thrown, or the method from one of the super-classes will be used? If so, how does the compiler know which class to use?

like image 398
Sam Avatar asked Jul 20 '11 16:07

Sam


People also ask

Can a class inherit from multiple super classes?

So what is multiple inheritance? In multiple inheritance, a class inherits from two or more super classes. It inherits the methods and variables from all super classes. If you create an object, it has all methods and variables from the classes.

How do you handle multiple inheritance?

The only way to implement multiple inheritance is to implement multiple interfaces in a class. In java, one class can implements two or more interfaces. This also does not cause any ambiguity because all methods declared in interfaces are implemented in class.

Does C allow multiple inheritance?

In Multiple inheritance, one class can have more than one superclass and inherit features from all its parent classes. As shown in the below diagram, class C inherits the features of class A and B. But C# does not support multiple class inheritance.

Why is multiple inheritance not allowed?

The reason behind this is to prevent ambiguity. Consider a case where class B extends class A and Class C and both class A and C have the same method display(). Now java compiler cannot decide, which display method it should inherit. To prevent such situation, multiple inheritances is not allowed in java.


2 Answers

The compiler will flag this kind of situation (i.e., attempting to call (some instance of RoboticsEngineer).buildRobot()) as an error.

This happens because the derived object has got a copy of both base objects (a MechanicalEngineer instance and an ElectricalEngineer instance) inside of itself and the method signature alone is not enough to tell which one to use.

If you override buildRobot in your RoboticsEngineer, you will be able to say explicitly which inherited method to use by prefixing the class name, e.g.:

void RoboticsEngineer::buildRobot() {     ElectricalEngineer::buildRobot() } 

By the same coin, you can actually "force" the compiler to use one version or another of buildRobot by prefixing it with the class name:

 (some instance of RoboticsEngineer).ElectricalEngineer::buildRobot(); 

in this case the ElectricalEngineer implementation of the method will be called, no ambiguity.

A special case is given when you have an Engineer base class to both MechanicalEngineer and ElectricalEngineer and you specify the inheritance to be virtual in both cases. When virtual is used, the derived object does not contain two instances of Engineer, but the compiler makes sure that there is only one of it. This would look like this:

 class Engineer {       void buildRobot();  };   class MechanicalEngineer: public virtual Engineer {   };   class ElectricalEngineer: public virtual Engineer {   }; 

In this case,

(some instance of RoboticsEngineer).buildRobot(); 

will be resolved without ambiguities. The same is true if buildRobot is declared virtual and overridden in one of the two derived classes. Anyway, if both derived classes (ElectricalEngineer and MechanicalEngineer) overrides buildRobot, then ambiguity arises once again and the compiler will flag the attempt at calling (some instance of RoboticsEngineer).buildRobot(); as an error.

like image 136
sergio Avatar answered Oct 21 '22 11:10

sergio


It doesn't handle it. It's ambiguous. error C2385: ambiguous access of 'functionName'

The compiler is smart enough to know that it shouldn't guess your meaning.
For the program to compile, you need to tell the compiler that:
A. You know it's a problematic issue.
B. Tell it what exactly you mean.

To do this, you need to explicitly tell the compiler which method you're asking for:

RoboticsEngineer myRobot; myRobot.ElectricalEngineer::buildRobot(); 
like image 44
Yochai Timmer Avatar answered Oct 21 '22 11:10

Yochai Timmer