C++ ambiguity resolution in inheritance

C++ ambiguity resolution in inheritance

occasionally , we may face a problem in using the multiple inheritance, when a function with the same inheritance appears in more than one base class. consider the following two classes

class M
{
public:
void dipslay()
{
std::cout << "class M\n";
}
};

class N
{
public:
void display()
{
std::cout << "class N\n";
}
};

so which display() function is used by the derived class when we inherit these two classes? we can solve this problem by defining a named instance within the derived class , using the class resolution operator with the function as shown

class P: public M, public N
{
public:
void display()
// overrides display() of N and M
{
M::display();

}
};
we can now use the derived class as follows
int main()
{
P p;
p.display();
}

ambiguity may also arise in single inheritance application, for instance, consider the follow situation:

class A
{
public:
void display()
{
std::cout<<"A\n";
};
class B: public A
{
public:
void display()
{
std::cout<<"B\n";
}
};

in this case the function in the derived class overrides the inheritance the inherited function and therefore, a simple call to display() by B type will invoke function defined in B only. however we may invoke the function defined in A by using the scope resolution operator to specify the class Example

void main()
{
B b; // derived class object
b.display(); //invokes display() in B
b.A::display(); // invokes display() in A
b.B::display(); // invokes display() in B
}

This will produce the following output:
B
A
B

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise