1

Does C++ support multiple inheritance?

 2 years ago
source link: https://www.programmerinterview.com/c-cplusplus/does-c-support-multiple-inheritance/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Does C++ support multiple inheritance?

Yes, C++ does support the use of multiple inheritance – where a class is derived from more than one direct base class. Let’s show an actual example of multiple inheritance. Suppose we have a class D, that derives from 2 classes – B and C. This is what the code would look like in order to implement multiple inheritance in C++ (assuming we have classes B and C):

class B { /* ... */ };
class C { /* ... */ };

// this is multiple inheritance:
class D : public B, public C 
{ 
/* ... */ 

};

What problems could be caused by multiple inheritance?

There is a problem known as the diamond problem that occurs when multiple inheritance is used in a certain way. You can read more about it here: Diamond Problem Explained

Does Java have multiple inheritance?

No, Java does not have multiple inheritance, but it has interfaces which can be used instead of multiple inheritance. For more information on this read here: Multiple Inheritance in Java.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK