Swaminathan K
asked on
Getting error in Hybrid inheritance program using c++
Hi Team ,
I have written a program for Hybrid inheritance in c++, Iam getting the below error while compiling the code m I have even made AllRounder to class inherit privately, Kindly help me in this regard.
Error:
C++ Code:
I have written a program for Hybrid inheritance in c++, Iam getting the below error while compiling the code m I have even made AllRounder to class inherit privately, Kindly help me in this regard.
Error:
C:\Users\Admin\Desktop\session\inheritance4.cpp [Warning] direct base 'Batsmen' inaccessible in 'Player' due to ambiguity
C:\Users\Admin\Desktop\session\inheritance4.cpp [Warning] direct base 'Bowler' inaccessible in 'Player' due to ambiguity
C:\Users\Admin\Desktop\session\inheritance4.cpp In member function 'void Player::displayPlayerInfo()':
C:\Users\Admin\Desktop\session\inheritance4.cpp [Error] reference to 'typeOfBatsmen' is ambiguous
C:\Users\Admin\Desktop\session\inheritance4.cpp [Note] candidates are: std::string Batsmen::typeOfBatsmen
C:\Users\Admin\Desktop\session\inheritance4.cpp [Note] std::string Batsmen::typeOfBatsmen
C:\Users\Admin\Desktop\session\inheritance4.cpp [Error] reference to 'typeOfBowler' is ambiguous
C:\Users\Admin\Desktop\session\inheritance4.cpp [Note] candidates are: std::string Bowler::typeOfBowler
C:\Users\Admin\Desktop\session\inheritance4.cpp [Note] std::string Bowler::typeOfBowler
C++ Code:
//Hybrid inheritance
#include<iostream>
using namespace std;
class Batsmen
{
protected:
string typeOfBatsmen;
public:
Batsmen()
{
typeOfBatsmen="Unknown";
}
Batsmen(string b)
{
typeOfBatsmen=b;
}
void displayBatsmenInfo()
{
cout<<"Batsmen is "<<typeOfBatsmen<<endl;
}
};
class Bowler
{
protected:
string typeOfBowler;
public:
Bowler()
{
typeOfBowler="Unknown";
}
Bowler(string b)
{
typeOfBowler=b;
}
void displayBowlerInfo()
{
cout<<"Bowler is "<<typeOfBowler<<endl;
}
};
class AllRounders:private Batsmen,private Bowler
{
protected:
string playerType;
public :
AllRounders():Batsmen(),Bowler()
{
playerType="Unknown";
}
AllRounders(string pt, string bts,string bt):Batsmen(bts),Bowler(bt)
{
playerType=pt;
}
void displayAllRounderInfo()
{
cout<<"Player is all rounder"<<endl;
displayBatsmenInfo();
displayBowlerInfo();
}
};
class Player : public Batsmen,public Bowler , protected AllRounders
{
public:
Player():Batsmen(),Bowler(),AllRounders()
{
}
Player(string pt, string bts, string bt): Batsmen(bts),Bowler(bt),AllRounders(pt,bts,bt)
{
}
void displayPlayerInfo()
{
cout<< "Player type "<<endl;
if (playerType=="Batsmen")
cout<< "Batsmen type "<<typeOfBatsmen<<endl;
else if(playerType=="Bowler")
cout<<"Bowler Type :"<<typeOfBowler<<endl;
else
displayAllRounderInfo();
}
};
int main()
{
Player p("Batsmen","Not a Bowler","Right Handed"),p1("All rounder","Right Handed Batsmen","Right hand Medium Fast bowler");
p.displayPlayerInfo();
cout<<""<<endl;
p1.displayPlayerInfo();
return 0;
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Please do a search on "c++ oop tutorial" and pick one that suits you best to understand the basics of OOP. Then advance on to multiple inheritance, which is most often avoided in all the projects I have worked on. It is much harder to maintain than single inheritance designs. You had errors similar to the "death" notes in my previous post.
[Error] reference to 'typeOfBatsmen' is ambiguous
[Note] candidates are: std::string Batsmen::typeOfBatsmen
[Note] std::string Batsmen::typeOfBatsmen
In your inheritance tree (top diagram in previous post), typeOfBatsmen can come from either AllRounders or the base class, Batsmen. That is what the word ambiguous in the error message is referring to.
Now, I have to caution you. The following is code is just to show you one way to get the program to build without warnings or errors, and produces an output. Since I have limited time, I will submit this code to you with the caveat that I only did enough to remove errors and to get an output. I did not look at the output to see whether the output made sense; or try to debug the program if the output is incorrect. If you need further help, you can always ask a question about the semantics of the program; but I would rather see you think about an inheritance tree to use OOD principles in a better manner. Please study the above links that are specific to the errors in the OP, and please study good OOD techniques.
#include<iostream>
using namespace std;
class Batsmen
{
protected:
string typeOfBatsmen;
public:
Batsmen() { typeOfBatsmen = "Unknown"; }
Batsmen(string b) { typeOfBatsmen = b; }
void displayBatsmenInfo() { cout << "Batsmen is " << typeOfBatsmen << endl; }
};
class Bowler
{
protected:
string typeOfBowler;
public:
Bowler() { typeOfBowler = "Unknown"; }
Bowler(string b) { typeOfBowler = b; }
void displayBowlerInfo() { cout << "Bowler is " << typeOfBowler << endl; }
};
class AllRounders :private Batsmen, private Bowler
{
protected:
string playerType;
public:
AllRounders() :Batsmen(), Bowler() { playerType = "Unknown"; }
AllRounders(string pt, string bts, string bt) :Batsmen(bts), Bowler(bt) { playerType = pt; }
void displayBatsmenInfo() { Batsmen::displayBatsmenInfo(); }
void displayBowlerInfo() { Bowler::displayBowlerInfo(); }
void displayAllRounderInfo()
{
cout << "Player is all rounder" << endl;
displayBatsmenInfo();
displayBowlerInfo();
}
};
class Player : /* public Batsmen, public Bowler, */ protected AllRounders
{
public:
Player() : /* Batsmen(), Bowler(), */ AllRounders() { }
Player(string pt, string bts, string bt) : /* Batsmen(bts), Bowler(bt), */ AllRounders(pt, bts, bt) { }
void displayPlayerInfo()
{
cout << "Player type " << endl;
if (playerType == "Batsmen")
displayBatsmenInfo();
// cout << "Batsmen type " << typeOfBatsmen << endl;
else if (playerType == "Bowler")
displayBowlerInfo();
// cout << "Bowler Type :" << typeOfBowler << endl;
else
displayAllRounderInfo();
}
};
int main()
{
Player p("Batsmen", "Not a Bowler", "Right Handed"), p1("All rounder", "Right Handed Batsmen", "Right hand Medium Fast bowler");
p.displayPlayerInfo();
cout << "" << endl;
p1.displayPlayerInfo();
return 0;
}
Output:Player type
Batsmen is Not a Bowler
Player type
Player is all rounder
Batsmen is Right Handed Batsmen
Bowler is Right hand Medium Fast bowler
P.S. - Please use the Code Tag to include blocks of code and error messages - see revised OP.
The string type could be the issue.
Define typeOfBatsmen and typeOfBowler as std::string instead of string.