Hey there i am looking for some help with the exercises in my book for c++. The first ch9 exercise states: "Improve the lobby class from the Game Lobby program by writing a copy constructor and an overloaded assignment operator for it." I am confused on what i am doing here i have re read the chapter three times and can not make sense of it . i added these two lines to the lobby class but i dont know if this is correct or how to make it correct if it is not
class Lobby
{
friend ostream& operator<<(ostream& os, const Lobby& aLobby)
Lobby& operator=(const Lobby& c) //OVERLOADED ASSIGNMENT OPERATOR I ADDED DO NOT KNOW IF THIS WORKS
public:
Lobby(): m_pHead(0) {}
~Lobby() { Clear(); }
Lobby(const lobby& c) // COPY CONSTRUCTOR I ADDED DONT KNOW IT THIS IS CORRECT
{
cout << "Copy constructor called";
}
void AddPlayer();
void RemovePlayer();
void Clear();
private:
Player* m_pHead;
};
//Game lobby
//Simulates a game lobby where players wait
#include <iostream>
#include <string>
using namespace std;
class Player
{
public:
Player(const string& name = ""): m_Name(name), m_pNext(0) {}
string GetName() const { return m_Name; }
Player* GetNext() const { return m_pNext = next; }
void SetNext(Player* next) { m_pNext = next; }
private:
string m_Name;
Player* m_pNext; //Pointer to next player in list
};
class Lobby
{
friend ostream& operator<<(ostream& os, const Lobby& aLobby)
Lobby& operator=(const Lobby& c) //OVERLOADED ASSIGNMENT OPERATOR I ADDED DO NOT KNOW IF THIS WORKS
public:
Lobby(): m_pHead(0) {}
~Lobby() { Clear(); }
Lobby(const lobby& c) // COPY CONSTRUCTOR I ADDED DONT KNOW IT THIS IS CORRECT
{
cout << "Copy constructor called";
}
void AddPlayer();
void RemovePlayer();
void Clear();
private:
Player* m_pHead;
};
void Lobby::AddPlayer()
{
//create a new player node
cout << "Please enter the name of the 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 = pNewPlayerl
}
//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_pHeadl
m_pHead = m_pHead->GetNext();
delete pTemp;
}
}
void Lobby::Clear()
{
while (m_pHead != 0)
{
RemovePlayer();
}
}
ostream& operator<<(ostream& os, const Lobby& aLobby)
{
Player* pIter = aLobby.m_pHead;
os << "\nHere's who's in the game lobby:\n";
if (pIter == 0)
{
os << "The 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 << "\nGameLobby\n";
cout << "0 - Exit the programm.\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;
}
The second exercise states: " The lobby::AddPlayer() function is inefficient because it iterates through all of the player nodes to add a new player to the end of the line. Add an m_pTail pointer data member to the Lobby class that always points to the last node in the line and use it to more efficiently add a player." I have no idea how to add this so if anyone could please help that would be amazing thank you :).