Creating Classes


Hey guys...

Wondering if anyone will assist me in creating a class..
In this class i need to create two Players...

Thnx

Would be a pleasure if u really did help me
Uhh, need a tad bit more info then that. Anyways, this should get you started.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class players
{public:                       ///makes it all accessable from the outside
   int score;                  ///decalring a variable attached to class
   void addpoints (int); ///declaring a function attached to class
};

void players::addpoints (int a) ///setting up the function
   {score = score + a;}            ///used to change class variable

players player1, player2;       ///declaring the players

player1.score = 0;player2.score = 0; ///setting scores

player1.addpoints(50)     ///awarding player 1 with 50 points
cout<<player1.score;      ///show the score 


There ya go, all the basics of a class, neatly packaged and labled, relavent to your project. Build off of that, if you need any other variables or functions to do things to the class, just follow the models. It tends to be better to just declare the functions inside the class and set them up outside. Don't know why, I'm still a newbie but I get alot of errors about while statements not being able to be expanded to optimize them if they are set up inside.

Remember the variables and functions are in the class so you have to use a class member and a dot to access them, ei. player1.score, because you declared him with players player1, which is the same this as saying int score, except its a player instead of an int.
Last edited on
Topic archived. No new replies allowed.