Inheritance: Multiple inheritance and Ambiguity in Member Access

A class can not only be derived from a single class, but it can also be derived from more than one class.   When a class is derived from two or more base class,  then such type of inheritance is called multiple inheritance. In multiple inheritance, the derived class inherits some or all the features of base classes form which it is derived. Multiple inheritance allows the combination of more than one existing class as base for the creation of new derived class. Multiple inheritance has the following syntax:
class derived : [access_to_base]base1,[access_to_base]base2
//member of derived class
The access_to_base is the base class access specifier. It can be private, protected or public. The base class access specifier can be different for different base classes. If access_to base is not mentioned, the default access specifier for base class is private and pubic in struct declaration. Example:

Ambiguity in Member Access in Multiple Inheritance

In multiple inheritance derive classes are created from more than one base classes. If more than one base classes have member functions with the same name then problem of ambiguity occurs if the derived class has not overridden the function. In this case ambiguity arises when accessing those member functions from the derived class or the object of the derived class. Because of multiple inheritance the derived class inherits the members with the same name from multiple base classes. When accessing these members with the same name in multiple base classes then the compiler cannot determine which member function form which base classes the member belongs. So compiler’s error can be eliminated by calling the function with base class name and scope resolution operator before function name.
SHARE Inheritance: Multiple inheritance and Ambiguity in Member Access

You may also like...

1 Response

  1. indianist says:

    An in-depth analysis of family feuds over inheritance of wealth brings to light the mental makeup on this issue across the globe.

Leave a Reply

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