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:
#include<iostream>

using namespace std;

class student{

    private:

        char name[25];

        int studID;

    public:

        void getdata(){

            cout<<"\nEnter Name: ";

            cin>>name;

            cout<<" Enter student ID: ";

            cin>>studID;

        }

        void showdata(){

            cout<<"\n Name: "<<name;

            cout<<"\n Student ID: "<<studID;

        }

};


class employee{

    private:

        char org_name[25];

        int empID;

    public:

        void getdata(){

            cout<<"Enter Name of associated Organization: ";

            cin>>org_name;

            cout<<"\n Enter employeeID: ";

            cin>>empID;

        }

        void showdata(){

            cout<<"\n Name so associated student union: "<<org_name;

            cout<<"\n Employee ID: "<<empID;

        }

};

class marketing_officer:public student, public employee{

    private:

        int working_hour;

    public:

        void getdata(){

            student::getdata();

            employee::getdata();

            cout<<" Enter working hours: ";

            cin>>working_hour;

        }

        void showdata(){

            student::showdata();

            employee::showdata();

            cout<<"\n Working hour: "<<working_hour;

        }

};


int main(){

    marketing_officer moff;

    cout<<"Enter data of marketing officer: "<<endl;

    moff.getdata();

    cout<<"\n Data of marketing offier"<<endl;

    moff.showdata();

    return 0;

}

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.

Share