Game Development Reference
In-Depth Information
string name;
cin >> name;
player.SetName("Bruce");
Player player1, player2, player3, player4;
player1 = player2 = player3 = player4 = player;
cout << endl << "Hello " << player.GetName() << endl;
}
Operator chaining has allowed us to assign the value from player to all of the temporary Player
objects we have created. You can think of these statements being operated in the following order.
player4 = player;
player3 = player4;
player2 = player3;
player1 = player2;
Chaining makes sense for the assignment operator; however it might not make a lot of sense in
other areas. You will see examples of practical uses for operator overloading and chaining as
we move through the remaining chapters of the topic.
Updating Text Adventure to Use Classes
Now that you have a grasp of what classes are and how they can be used, we will update the Text
Adventure game to be based around the principles of OOP. The first change you will make will be to
remove the GameLoop namespace and create a Game class. Listing 8-15 shows this new Game class.
Listing 8-15. The Game Class
class Game
{
private:
Player m_player;
void WelcomePlayer();
void GivePlayerOptions();
void GetPlayerInput(std::string& playerInput);
PlayerOptions EvaluateInput(std::string& playerInput);
public:
void RunGame();
};
Our Game class contains a private member variable representing our player and private methods to
update the game state. These methods are listed in the corresponding source file shown in
Listing 8-16.
 
Search WWH ::




Custom Search