I have the below program. When I try to execute it, I found that Allrounder
didn't get the valid name. Any idea how can I solve it?
#include <iostream>
#include <string>
using namespace std;
class Player
{
std::string playerName;
public:
Player(std::string &playerName) :playerName(playerName) { cout << "Player Constructed\n"; }
void printPlayerName() const
{
std::cout<<playerName<<endl;
}
Player() = default;
virtual ~Player() { cout << "Player Destructed\n"; }
};
class Batsman : virtual public Player
{
public:
Batsman(std::string playerName) : Player(playerName) { cout << "Batsman info added\n"; }
~Batsman() { cout << "Batsman Destructed\n"; }
};
class Bowler : virtual public Player
{
public:
Bowler(std::string playerName) : Player(playerName) { cout << "Bowler info added\n"; }
~Bowler() { cout << "Bowler Destructed\n"; }
};
class Allrounder : public Batsman, public Bowler
{
public:
Allrounder(std::string playerName) :Batsman(playerName), Bowler(playerName) { cout << "Allrounder info added"; }
~Allrounder() { cout << "Allrounder Destructed\n"; }
};
int main()
{
Player *ptr = new Batsman("Sachin Tendulkar");
ptr->printPlayerName();
delete ptr;
cout << endl;
Player *ptr1 = new Bowler("Anil Kumble");
ptr1->printPlayerName();
delete ptr1;
cout << endl;
Player * ptr2 = new Allrounder("Ravindra Jadeja");
ptr2->printPlayerName();
delete ptr2;
cout << endl;
}
I ensured calling super class constructor, but in case of multiple inheritance, somehow this doesn't seem to work.
Currently ptr2->printPlayerName()
failed to print the name of the Allrounder
.