Ambiguity Error in C++

 


Ambiguity Error When there are two or more base classes having same member functions with same name and signature,ambiguity error occurs when we try to access that member from derived class object as comiler doesn't know which function should be called first. This ambiguity error can be removed  by using objectname.classname::function name();

ie. call that function name which has to be called.

Example showing ambiguity error 


#include <iostream.h>

#include <conio.h>

 class Base1

  {

    protected: int x;

    public: void input();   //same function name

              };

    void Base1::input()

      {

       cout<<"Value of x:";

       cin>>x;

          }

 class Base2

  {

    protected: int y;

    public: void input();  //same function name

              };

    void Base2::input()

      {

       cout<<"Value of y:";

       cin>>y;

          }

      class Base:public Base1,public Base2

       {

        protected: int z;

        public: void inputderived();

           };

        void main()

         {

         Base obj;

         obj.input();

         getch();

           }

Here we have got ambiguity error



 Now to remove this ambiguity error
We use the class object name.class name::function name();

 #include <iostream.h>
#include <conio.h>
 class Base1
  {
    protected: int x;
    public: void input();
              };

    void Base1::input()
      {
       cout<<"Value of x:";
       cin>>x;
          }
 class Base2
  {
    protected: int y;
    public: void input();
              };
    void Base2::input()
      {
       cout<<"Value of y:";
       cin>>y;
          }
      class Base:public Base1,public Base2
       {
        protected: int z;
        public: void inputderived()
        {
         z=x+y;
         cout<<"Z:"<<z;
           }
           };
        void main()
         {
         Base obj;
         obj.Base1::input();
         obj.Base2::input();
         obj.inputderived();
         getch();
           }

In case when we have same data member in both classes

#include <iostream.h>
#include <conio.h>
 class Base1
  {
    protected: int x;
    public: void input();
              };

    void Base1::input()
      {
       cout<<"Value of x:";
       cin>>x;
          }
 class Base2
  {
    protected: int x;
    public: void input();
              };
    void Base2::input()
      {
       cout<<"Value of x:";
       cin>>x;
          }
      class Base:public Base1,public Base2
       {
        protected: int z;
        public: void inputderived()
        {
         z=Base1::x+Base2::x; //additon of same data member
         cout<<"Z:"<<z;
           }
           };
        void main()
         {
         Base obj;
         obj.Base1::input();
         obj.Base2::input();
         obj.inputderived();
         getch();
           }
 

Program Example:

1.   WAP to define a class Student and AcademicMarks which have data

 members to represent Studnet and AcademicMarks respectively. Define

 member function input() and display() on both class  to initialize marks.Derive a class  Result resolved from Student and AcademicMarks  and calculate total marks and display result.

(Multiple Inheritance Example)


#include<iostream.h>

#include<conio.h>

class Student

  {

    protected: char name[20];

               int roll;

    public: void input();

            void display();   //here we have used same function name in Student and AcademicMarks class

                 };

     void Student::input()

       {

        cout<<"Enter name:";

        cin>>name;

        cout<<"Enter roll number:";

        cin>>roll;

          }

       void Student::display()

        {

         cout<<"Name:"<<name<<endl<<"Roll no:"<<roll<<endl;

           }

           //Another class called AcademicMarks

     class AcademicMarks

     {

   protected: int math;

              int physics;

              int Computer;

   public : void input();

            void display();

                   };

    void AcademicMarks::input()

       {

        cout<<"Enter marks obtained in Math:";

        cin>>math;

        cout<<"Enter marks obtained in Physic:";

        cin>>physics;

        cout<<"Enter marks obtained in Computer:";

        cin>>Computer;

          }

    void AcademicMarks::display()

      {

       cout<<"Math:"<<math<<endl<<"Physic:"<<physics<<endl<<"Computer:"<<Computer<<endl;

         }

class Result: public Student,public AcademicMarks

  {

    int total;

  public: void calculate();

            };

    void Result::calculate()

     {

      cout<<endl;

      total=math+physics+Computer;

      cout<<"Total marks :"<<total;

         }

      void main()

      {

         Result r;

         r.Student::input();

         r.AcademicMarks::input();

         cout<<endl;

         r.Student::display();

         r.AcademicMarks::display();

         r.calculate();

         getch();

           }



2.   Write a program to create a class named Person  which has name and age

as data members and member  functions to read and display its data.

 Create another class Student derived from class

  Person which has roll and marks and another different derived

  class Employee having employeeid and salary.Use input() for getting details and

  display() to display details in every class. (Hierarchical Inheritance Example)


#include<iostream.h>

#include<conio.h>

 class Person

  {

   protected: char name[50];

              int age;

   public: void input()

     {

      cout<<"Enter name:";

      cin>>name;

      cout<<"Enter age:";

      cin>>age;

       }

      void display()

         {

         cout<<"Name:"<<name<<endl<<"Age:"<<age<<endl;

         }

          };

     class Student :public Person

           {

             protected: int roll;

                         int marks;

             public:  void input()

                {

                 Person::input();

                   cout<<"Enter roll:";

                   cin>>roll;

                   cout<<"Enter marks:";

                   cin>>marks;

                    }

        void display()

         {

         Person::display();

         cout<<"Roll:"<<roll<<endl<<"Marks:"<<marks<<endl;

          }

           };

 class Employee: public Person

           {

            int employeeid;

            int salary;

             public: void input()

               {

                cout<<endl;

                Person::input();

                cout<<"Enter employeeid:";

                cin>>employeeid;

                cout<<"Enter salary:";

                cin>>salary;

                 }

           void display()

          {

            Person::display();

            cout<<"Employee id:"<<employeeid<<endl<<"Salary:"<<salary<<endl;

           }

            };

          void main()

          {

           Employee E;

           Student S;

           cout<<"Details of Student"<<endl;

           S.Student::input();

           cout<<endl;

           S.Student::display();

           cout<<"Details of Employee"<<endl;

           E.Employee::input();

           cout<<endl;

           E.Employee::display();

           getch();

            }


3. 
Define a class Student which has name,roll number and member function to input and display  name and roll number. Derive a class Examination from class Student
which has marks of two subjects and member function to initialize
and display marks. Agian derive a class Result from class Examination
and calculate total and display result.Use input and display member function in each class(Multilevel Inheritance Example)

#include<iostream.h>
#include<conio.h>
class Student
{
    protected:char name[20];
              int roll;
   public: void input();
           void display();
            };

    void Student::input()
     {
      cout<<"Enter name:";
      cin>>name;
      cout<<"Enter the roll no:";
      cin>>roll;
        }
     void Student::display()
          {
            cout<<"Name:"<<name<<endl<<"RollNo:"<<roll<<endl;
            }
  class Examination: public Student
 {
  protected:int subject1;
            int subject2;
   public : void input();
            void display();
              };
     void Examination::input()
              {
               Student::input();
               cout<<"Enter the marks of Subject1:";
               cin>>subject1;
               cout<<"enter the marks of subject2:";
               cin>>subject2;
                }
    void Examination::display()
        {
            Student::display();
            cout<<"The marks of subject1 & subject2 :" <<subject1<<"&"<<subject2<<endl;
                   }
  class Result: public Examination
  {
    int total;
  public: void display();
     };
   void Result::display()
      {
      total=subject1 + subject2;
      cout<<"The total marks is:"<<total<<endl;
          }
   void main()
    {
    Result r;
    r.Result::input();
    cout<<endl;
    r.Result::display();
    getch();
     }


Comments