C++C++11C++14C++17Introduction to C++Learn C++

Learn C++ Inheritance :: Ambiguity in Multiple Inheritance

Let’s remember that, Object Oriented Programming (OOP) is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methodsfunctions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members. In C++, classes have members (attributesmethods) and we should protect each member inside this class.

The Inheritance is one of the most important concepts in object-oriented C++ programming as in other features of Classes. Inheritance allows us to define a class in terms of another class, and it makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. Inheritance implements the relationship between classes. 

Multiple Inheritance is another feature of C++ that a class can inherit from more than one class. For example, a derived class can be inherited from more than one base class or derived classes.

The most obvious error with Multiple Inheritance is Ambiguity errors, this occurs during function overriding. Do you want to learn how I can solve the Ambiguity problem in Multiple Inheritance? This question is also the same as how I can solve the Ambiguity problem in Class methods?

To explain this, first, let’s give an example. Suppose that we have two base classes ( Animal and Wing ), and they have the same function name ( info() ) which is not overridden in the derived class. If we try to call the function using the object of the derived class, the compiler will show us an Ambiguity error, because there is a conflict with two same names and the compiler doesn’t know which function to call. For example,

If we run this code above in C++, we will face with Ambiguity Error as in here,

C++ allows to use same function names in different classes, to avoid this error in Multiple Inheritance we should use their class names with :: operator. So first we should write object then dot with base class name and :: operator with the method name in that base class. See example below,

So we can use both info() functions (methods) of two different bases in Multiple Inheritance. Full example will be as follows,

Get started building powerful apps with C++Builder!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?

C++C++11Introduction to C++Learn C++Syntax

Full List Of Features In C++ 11

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

How To Use Basic Methods Of Strings In C++?

Worth reading...
Learn C++ Inheritance :: Hybrid Inheritance