1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
//Define a class Player as an ADT that uses separate files for the interface and the implementation.
//The class represents a Player in a video game. This class has the following data members:
//string name; // the name of the player
//string team; // the team for which this player plays
//int level; // the level to which the player has advanced to in the game.
//int points; // the number of points accumulated
//The class should contain a default constructor that initializes name to "Player 0", team to “Team 0”, level to 0 and points to 0.
//It should also contain an overloaded constructor that accepts four parameters to set the name, team, level and points to specified values.
//The destructor should output "Game Over".
//Include accessor functions that return the values stored in each of the member variables of an object of class Player
//(i.e. get functions), as well as mutator functions to update each of the member variables of an object of class Player respectively
//(i.e. set functions with a parameter to set each member variable to a value specified by the parameter).
//The class should also contain a void member function called reset() that resets the member variables of a Player to values specified by parameters.
//Overload the equality operator == as a friend function for class Player.
//This function returns true if the team member variable of Player1 is identical to that of Player2 and false otherwise.
//Use the following prototype:
//bool operator==(const Player & Player1, const Player & Player2)
//Overload the comparison operator> as a friend function for class Player.
//This function returns true if the points member variable of Player1 is bigger than that of Player2 and false otherwise.
//Use the following prototype:
//bool operator>(const Player & Player1, const Player & Player2)
//Define an overloaded prefix operator ++ (implemented as a friend function)
//to return the current instance of the class Player after incrementing the points by 1.
//For every 100 points scored, the level is also incremented by 1. Hint: Use the % (modulus) operator.
//A player can request hints while playing the game.
//For every hint requested, 10 points should be deducted from a player’s points, and the level
// (if necessary) at which (s)he plays adapted accordingly.
// To implement this, define a void member function requestHint()to print a message “Please give me a hint”
// (We will not try to supply the hints themselves at this stage or worry about how this will be done.)
//Member function requestHint()should then adapt the points and level by calling the overloaded prefix operator-- to return
//the current instance of the class Player after decrementing the points by 10 and adapting the level accordingly.
//You should also implement the overloaded prefix operator-- as a friend function.
//Test your class by writing a program to do the following:
//• Overload the stream extraction operator >> and the stream insertion operator << as friend functions for class Player.
// The stream insertion operator << should display the name, team, level and points of a player.
//• Use the default constructor to instantiate two objects player1 and player3.
//• Use the overloaded constructor to instantiate an object player2 with the following values for the member variables:
//Name: “Jane”
//Team: “BlueSwallows”
//Level: 1
//Points: 99
#include <iostream>
#include <string>
using namespace std;
// Header file VPLAYER.H : This is the interface for the class player.
// Values for this type are player1, player2, and player 3. The member variables are name, team , score and level
//
#ifndef VPLAYER_H
#define VPLAYER_H
class Player
{
private :
string name ; // name of the Player
string team ; // Team which player plays for
int level ; // the level to which the player has advanced to in the game
int points; // the number of points accumulated
Player();
Player( string &name, string &team, int &level, int &points)
{
name = " Player 0 ";
team = " Team 0 ";
points = 0;
level = 0;
}
friend void getPlayerName(string &name)
{
cout<<"Enter Player Name : "<<endl;
getline(cin,name);
cout<<endl;
}
friend void getTeamName(string &team)
{
cout<<" Enter Team Name : "<<endl;
getline(cin,team);
cout<<endl;
}
void resetPlayer(string &name, string &team, int &level, int &points )
{
name = " Player 1 ";
team = " Blue Swallow ";
level = 2;
points = 99;
}
friend bool operator == ( const Player &player1, const Player &player2 )
{
if ( player1 == player2 )
{
// cout<<" Player 1 : "<<player1<<" is the same as Player 2:"<<player2<<endl;
// cout<<endl;
}
else
{
// cout<<" Player 1 : "<<player1<<" is not the same as Player 2:"<<player2<<endl;
// cout<<endl;
}
}
friend bool operator > (const Player & player1, const Player & player2)
{
return ( player1.name == player2.name);
}
friend bool operator -( const Player & player1, const Player & player2 )
{
player1.level-1;
player1.points-10;
cout<<" You're on Level "<<player1.level<<" with "<<player1.points<<" points "<<endl;
cout<<endl;
return (( player1.level)&& (player2.level));
}
friend int getPoints (string &name, string &team, int &points )
{
cout<<" Get player points "<<endl;
cin>>points ;
cout<<'\t'<<name<<" has "<<points<<" points "<<endl;
cout<<endl;
return points;
}
friend int getLevel( string &name, string &team, int &points )
{
if (points >= 100)
{
points %= 100;
cout<<" You are on level :"<<points<<endl;
cout<<endl;
}
else
{
cout<<" You are on level : 0 " <<endl;
cout<<endl;
}
return points;
}
friend void getPoints ( int &points, int &level, string &name, string &team)
{
if (points >= 100)
{
points %= 100;
if (points >= 1)
{
level++;
cout<<" You're playing on level : "<<level<<endl;
}
}
}
friend void requestHint ( int &points, int &level, string &name, string &team)
{
points -= 10 ;
if (points <= 100)
{
--level;
cout<<" You're now on level : "<<level<<endl;
cout<<endl;
}
}
friend void displayPlayerDetails( string &name, string &team, int &level, int &points)
{
cout<<" Name : "<<name<<endl;
cout<<" Team : "<<team<<endl;
cout<<" Level : "<<level<<endl;
cout<<" Points : "<<points<<endl;
}
~Player()
{
cout<<" Game Over "<<endl;
}
};
#endif // VPLAYER_H
int main()
{
Player p;
string playerName = "", playerTeam = " ";
char hint;
int playerlevel = 0, playerPoints = 0;
p.Player();
p.getPlayerName(playerName);
p.getTeamName(playerTeam);
p.getPoints( playerName,playerTeam);
p.getLevel( playerName,playerTeam, playerPoints);
cout<<" Do you need a hint ? Press Y or N "<<endl;
if ( hint == 'y'|| hint == 'Y')
{
cout<<" You have requested for a hint "<<endl;
cout<<" You will forfeit 10 points and go one leverl lower "<<endl;
p.requestHint( playerPoints, playerlevel, playerName, playerTeam );
}
if (hint == 'n'|| hint == 'N' )
{
cout<<" Play continues "<<endl;
cout<<endl;
}
else
{
cout<<" Enter a valid option Y or N "<<endl;
}
p.displayPlayerDetails(playerName, playerTeam, playerlevel, playerPoints);
return 0;
}
|