Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have initialized all the properities but i want to

assign username and password to properities
know how to call function (fire) in main
know how to print directly from the class


I appreciate your help

this is my code

What I have tried:

#include<iostream>
#include<string>
using namespace std;

class COD_player{
	
	string name,id,username,password;
	int level,shots,online_players,died_players;
	bool gender;
	public:
COD_player()
{
	cout<<"new player has been created"<<endl;
	this->name="";
	this->id="";
   this-> username="";
	this->password="";
	this->level=0;
	this->shots=0;
	this->gender=false;
	online_players++;
	

}


void fire()
{
	shots++;
	cout<<"kill him boy!FINISH HIM!!"<<endl;
	online_players--;
	died_players++;
}
~COD_player()
{
	cout<<"this player is died and removed from the server"<<endl;
	online_players--;
	died_players++;
}
};


int main()
{

	COD_player p1=COD_player();
	




	return 0;


}
Posted
Updated 17-Nov-21 20:22pm

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.
C++
int main()
{
    COD_player pl = COD_player(); // creates an object of type COD_player
    pl.fire();    //calls the fire() method for object pl
} 
To access the private class variables, you can of course create getters and setters
C++
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
C++
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:
C++
class COD_player {
private:
    static int online_players;  // class static variable, shared among all instances
};

int COD_player::online_players = 0; // initialize class static variable

int main()
{
   /* your code here */
} 
But really, you should probably have a "game server" object that manages the players, and a separate "player" object to manage an individual players.
 
Share this answer
 
Comments
Member 15420232 17-Nov-21 22:14pm    
how can I print online_players from the class directly
i tried cout COD_Player::online_player
it did not work
Member 15420232 17-Nov-21 22:16pm    
I also want to print died_players from p1
I tried cout p1.died_players
but it gives error too
do I need to static any variable?
Shao Voon Wong 17-Nov-21 22:52pm    
Put "public:" at the top of the class to make your members publicly accessible.
class COD_player{
public:
string name,id,username,password;
...
Member 15420232 18-Nov-21 0:00am    
the question wants the properties to be private
Shao Voon Wong 18-Nov-21 0:08am    
If you want the member to be private, then add getter and setter as suggested by k5054 above and call those to access member.
The question is correctly anwsered, but take the advise from a seasoned developer: you better visit and read into some Learn C++ tutorial to learn better about the language and other tools, like IDE and Debugger.
Because you need the skills the time is very well invested.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900