Wednesday, 1 April 2026

Ambiguity in Single Inheritance in C++

 

Ambiguity in Single Inheritance in C++
Whenever a data member and member function are defined with the same name in both the base and the derived classes, these names must be without ambiguity. The scope resolution operator (::) may be used to refer to any base member explicitly. This allows access to a name that has been redefined in the derived class.
For example, the following program segment illustrates how ambiguity occurs when the getdata () member function is accessed from the main () program.

class baseA {
public :
int i;
getdata();
};
class baseB {
public :
int i;
getdata();
};
class derivedC : public baseA, public baseB {
public :
int i;
getdata();
}
int main()
{
derivedC obj;
obj.getdata();
return 0;
}
The members are ambiguous without scope operators. When the member function getdata () is accessed by the class object, naturally, the compiler cannot distinguish between the member function of the class baseA and the class baseB. Therefore it is essential to declare the scope operator explicitly to call a base class member as illustrated below:
obj.baseA::getdata();
obj.baseB::getdata();

Example 4.
A program to demonstrate how ambiguity is avoided in single inheritance using scope resolution operator.

// ambiguty in the single inheritance
#include <iostream>
using namespace std;
class baseA {
private :
int i;
public :
void getdata(int x);
void display();
};
class baseB {
private :
int j;
public :
void getdata(int y);
void display();
};
class derivedC : public baseA,public baseB
{
};
void baseA :: getdata(int x)
{
i = x;
}
void baseA :: display()
{
cout << “ value of i = “ << i << endl;
}
void baseB :: getdata(int y)
{
j = y;
}
void baseB :: display()
{
cout << “ value of j = “ << j << endl;
}
int main()
{
derivedC objc;
int x,y;
cout << “ enter a value for i ?\n”;
cin >> x;
objc.baseA::getdata(x); // member is ambiguous without scope
cout << “ enter a value for j ?\n”;
cin >> y;
objc.baseB::getdata(y);
objc.baseA::display();
objc.baseB::display();
return 0;
}
Output of the above program
enter a value for i?
10
enter a value for j?
20
value of i = 10
value of j = 20

ARRAY OF CLASS OBJECTS AND SINGLE INHERITANCE
This section emphasizes on how an array of class objects can be inherited from a base class. Once a derived class has been defined, the way of accessing a class member of the array of class objects are same as the ordinary class types. The general syntax of the array of class objects in a singly derived class is:
class baseA {
private :
-------
-------
public :
-------
-------
};
class derivedB : public baseA {
private :
-------
-------
public :
-------
-------
};
int main()
{
derivedB obj[100];//array of class objects of the derived class
-------
-------
return 0;
}

Example 5.
A program consists of a base class and a derived class. The base class data members are name, roll number and sex. The derived class data members are height and weight. The derived class has been declared as an array of class objects. The member functions are used to get information on the derived class from the keyboard and display the contents of the array of class objects on the screen.

//single inheritance using array of objects
#include <iostream>
#include <iomanip>
using namespace std;
const int MAX = 100;
class basic_info {
private :
char name[20];
long int rollno;
char sex;
public :
void getdata();
void display();
}; // end of class de nition
class physical_ t :public basic_info
{
private :
oat height;
oat weight;
public:
void getdata();
void display();
}; //end of class de nition
void basic_info :: getdata()
{
cout << “ enter a name ? \n”;
cin >> name;
cout << “ roll no ? \n”;
cin >> rollno;
cout << “ sex ? \n”;
cin >> sex;
}
void basic_info :: display()
{
cout << name << “ ”;
cout << rollno << “ ”;
cout << sex << “ ”;
}
void physical_ t :: getdata()
{
basic_info::getdata();
cout << “ height ? \n”;
cin >> height;
cout << “ weight ?\n”;
cin >> weight;
}
void physical_ t :: display()
{
basic_info::display();
cout << height << “ ”;
cout << weight << “ ”;
}
int main()
{
physical_ t a[MAX];
int i,n;
cout << “ How many students ? \n”;
cin >> n;
cout << “enter the following information \n”;
for (i = 0; i <= n-1; ++i) {
cout << “record : ” << i+1 << endl;
a[i].getdata();
}
cout << endl;
cout << “ ------------------------------------- \n”;
cout << “ Name Rollno Sex Height Weight \n”;
cout << “ ------------------------------------ \n”;
for (i = 0; i <= n-1; ++i) {
a[i].display();
cout << endl;
}
cout << endl;
cout << “ ------------------------------------- \n”;
return 0;
}
Output of the above program
How many students?
3
enter the following information
record: 1
enter a name?
Antony
roll no?
20071
sex?
M
height?
167
weight?
65
record: 2
enter a name?
Sulaiman
roll no?
20073
sex?
M
height?
179
weight?
90
record: 3
enter a name?
Sandeep
roll no?
20076
sex?
M
height?
187
weight?
78
-------------------------------------------------------------------------
Name Rollno Sex Height Weight
-------------------------------------------------------------------------
Antony 20071 M 167 65
Sulaiman 20073 M 179 90
Sandeep 20076 M 187 78
-------------------------------------------------------------------------

0 comments

Post a Comment