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
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
Comments
Post a Comment