Once you've created an instance of a given class, you call the class methods the same way you would to say get the length of a std::string. e.g.
int main()
{
COD_player pl = COD_player(); pl.fire(); }
To access the private class variables, you can of course create getters and setters
class COD_player() {
string name;
void set_name(string new_name) {
name = new_name;
}
string get_name() {
return name;
}
};
If you're going to do that though, then it gains you very little from just making the data members public, and just get/set them directly. It does, however "future proof" the class to some extent, in that you can change the representation of a data member, but not have to change the interface to the class. More normal, though would be to provide intializers as part of the constructor
class COD_player {
string player_name;
string player_id;
COD_player(string name, string id)
: player_name(name), player_id(id)
{
cout << "created player " << player_name << '\n';
}
string name() { return player_name; }
};
int main()
{
COD_player player("Steve", "Blue Leader");
cout << player.name() << '\n';
}
I also note that it looks like you are trying to keep track of the total number of players in the games using the variable
online_players
. Note that, as currently defined, that class member is unique for each object that you have created. To share that variable across all instances of the class you need to declare the member as static. You also need to then initialize the variable separately:
class COD_player {
private:
static int online_players; };
int COD_player::online_players = 0;
int main()
{
}
But really, you should probably have a "game server" object that manages the players, and a separate "player" object to manage an individual players.