My excercise is:
Improve the Lobby class from the Game Lobby program by writing a
friend function of the Player class that allows a Player object to be sent
to cout. Next, update the function that allows a Lobby object to be sent
to cout so that it uses your new function for sending a Player object to
cout.
//Game Lobby
//Simulates a game lobby where players wait
#include <iostream>
#include <string>
usingnamespace std;
class Player
{
public:
Player(const string& name = "");
string GetName() const;
Player* GetNext() const;
void SetNext(Player* next);
private:
string m_Name;
Player* m_pNext; // Pointer to next player in list
};
Player::Player(const string& name): // constructor
m_Name(name),
m_pNext(0)
{}
string Player::GetName() const
{
return m_Name;
}
Player* Player::GetNext() const
{
return m_pNext;
}
void Player::SetNext(Player * next)
{
m_pNext = next;
}
class Lobby
{
friend ostream& operator<<(ostream& os, const Lobby& aLobby); // remember ostream& os then a comma!
public:
Lobby();
~Lobby();
void AddPlayer();
void RemovePlayer();
void Clear();
private:
Player* m_pHead;
};
Lobby::Lobby():
m_pHead(0)
{}
Lobby::~Lobby()
{
Clear();
}
void Lobby::AddPlayer()
{
// creates a new player node
cout << "Please enter the name of your player: ";
string name;
cin >> name;
Player* pNewPlayer = new Player(name);
// if list is empty, make head of list this new player
if(m_pHead == 0)
{
m_pHead = pNewPlayer;
}
// otherwise find the end of the list and add the player there
else
{
Player* pIter = m_pHead;
while(pIter->GetNext() != 0)
{
pIter = pIter->GetNext();
}
pIter->SetNext(pNewPlayer);
}
}
void Lobby::RemovePlayer()
{
if(m_pHead == 0)
{
cout << "The game lobby is empty, No one to remove!\n";
}
else
{
Player* pTemp = m_pHead;
m_pHead = m_pHead->GetNext();
delete pTemp;
}
}
void Lobby::Clear()
{
while(m_pHead != 0)
{
RemovePlayer();
}
}
ostream& operator<<(ostream& os, const Lobby& aLobby) // remember ostream& os then a comma!
{
Player* pIter = aLobby.m_pHead;
os << "\nHere's whos in the game lobby:\n";
if (pIter == 0)
{
os << "The game lobby is empty.\n";
}
else
{
while(pIter != 0)
{
os <<pIter->GetName() << endl;
pIter = pIter->GetNext();
}
}
return os;
}
int main()
{
Lobby myLobby;
int choice;
do
{
cout << myLobby;
cout << "\nGAME LOBBY\n";
cout << "0 - Exit the program.\n";
cout << "1 - Add a player to the lobby.\n";
cout << "2 - Remove a player from the lobby.\n";
cout << "3 - Clear the lobby.\n";
cout << endl << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 0: cout << "Good-bye.\n"; break;
case 1: myLobby.AddPlayer(); break;
case 2: myLobby.RemovePlayer(); break;
case 3: myLobby.Clear(); break;
default: cout << "That was not a valid choice.\n";
}
}
while (choice != 0);
return 0;
}
So far I've gotten to:
1 2 3 4 5 6 7 8 9 10 11 12 13
class Player
{
friend ostream& operator<<(ostream& os, const Player& aPlayer);
public:
Player(const string& name = "");
string GetName() const;
Player* GetNext() const;
void SetNext(Player* next);
private:
string m_Name;
Player* m_pNext; // Pointer to next player in list
};
But have no idea how to update it to allow a lobby object to be sent to cout maybe ostream but that's as far as I get.
You already have a function that allows you to send a Lobby object to cout (line 116-135)
You need to create a similar function for Player objects. You've written the prototype in line 3 of the last block.
What you need to do now is
1. Provide an implementation of friend ostream& operator<<(ostream& os, const Player& aPlayer);
2. change the Lobby function ostream& operator<<(ostream& os, const Lobby& aLobby) so it uses your new output function (in line 129)
ostream& operator<<(ostream& os, const Lobby& aLobby)
{
Player* pIter = aLobby.m_pHead;
os << "\nHere's whos in the game lobby:\n";
if (pIter == 0)
{
os << "The game lobby is empty.\n";
}
else
{
while(pIter != 0)
{
os << (*pIter) << endl;
pIter = pIter->GetNext();
}
}
return os;
}