troubleshooting Question

Getting error in Hybrid inheritance program using c++

Avatar of sam_2012
sam_2012 asked on
C++
4 Comments1 Solution10 ViewsLast Modified:
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:\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
phoffricSoftware Engineering and Matlab Analyst

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros