Monday, April 5, 2021

Ambiguity in multiple inheritance (OOP) (27)

 /*Ambiguity in multiple inheritance

                                ***********************************/

/*in multiple inheritance single child class is derived from two parent classes.so there may be

a possiblity that the two parent classes have the same named member function. if the child class 

need to access one of the same named member function then it result in ambiguity.and the compiler 

confussed that which class function should be call.*/

#include<iostream>

using namespace std;

class Base1

{

    public:

        void greet()

{

            cout<<"i am the function of base one class:"<<endl;

        }

};


class Base2

{

    public:

        void greet()

        {

            cout << "i am the function of base two class:" << endl;

        }

};



class Derived : public Base1, public Base2

{

   public:

    /*  one way

    void greet()

    {

   

        Base1::greet();

        Base2 :: greet();

    }

    */

};

int main()

{

     Derived d;

     //this is the second way . so there are two way to resolve Ambiguity.

     d.Base1::greet();

     d.Base2::greet();


    return 0;

}

No comments:

Post a Comment