Understanding the code

Hey, C++ community!

I'm learning the basics of classes and came across this one piece of code in the example. I've made my version of it and tried to remove that particular section and came up with an error. What exactly does that particular piece of code do?

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
#include <iostream>

using namespace std;

class player
{
int level, experience, money;
public:
player ();
player (int, int, int);
int rank (void)
{
return (level*experience*money);
}

};

player::player(){
level = 12;
experience = 2;
money = 1;
}

player::player(int a, int b, int c){  //These
level = a;                            //Four
experience = b;                       //Little
money = c;                            //Lines
}

int main(){
player Player1 (5,2,5);
player Player2;
cout << "Player1's rank is " << Player1.rank() << endl;
cout << "Player2's rank is " << Player2.rank() << endl;
return 0;
}


Thanks
It defines that particular constructor.
That you get a linker error is not surprising, as you're using the constructor further down.
So what you're saying is that it connects the constructor with the variables within the class?
Not really, these lines define the constructor declared in the class (player (int, int, int);).
That version of the constructor initializes the member variables with the passed parameters.
Okay, but what if I want to check each and every person's stats like so:

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
#include <iostream>

using namespace std;

class player
{
int speed, bombs, lives;

public:

player();
player (int, int, int);
int check (void)
{
return (speed+bombs+lives);
}

};

player::player(){
speed = 3;
bombs = 1;
lives = 2;
}

player::player(int a, int b, int c){
speed = a;
bombs = b;
lives = c;
}

int main()
{
player Speedy (5,1,2);
player Default();
player Cheapo (20,20,20);

while (player.check() > 10)
{
player.speed--;
}

cout << "Players stats:" << endl;
cout << "Speedy: " << Speedy.check() << endl;
cout << "Default: " << Default.check() << endl;
cout << "Cheapo : " << Cheapo .check() << endl;


return 0;
}


I get a couple of errors
You should post the errors...

player Default(); That defines a function that returns a player an receives nothing
player Default; That creates a player using the default constructor.

1
2
3
4
while (player.check() > 10)
{
player.speed--;
}
player is a class. The methods are executed in objects.
And it seems that you are trying to access a private member. You can't do that outside of methods of the class (or friends).
You don't need the () if you want a default constructor to be used. In fact, if you do use the () with no arguments, then C++ will assume you wanted to define a function. I think. I need coffee. :/

Also... I don't see an object named "player" anywhere, so why are you trying to access its member on lines 38 and 40? >_>

Happy programming!

EDIT: Ye ninjas are getting tiresome.

-Albatross
Last edited on
So how would you be able to access the objects within the class dynamically? Say if the game has hundreds of players to choose from and I want to run a conditional statement that checks each and every player's stats (as well as manipulating them).
Last edited on
You would (should) store all the players in a container (vector, linked list etc.)
Okay. I think there's some sort of misunderstanding as far as terminology goes.

A class is like a blueprint. The compiler has to know what it is when you're compiling. You cannot change this blueprint at run-time (dynamically). Although, with some clever and rather dangerous tricks, you can make it seem like it does this. Please don't do it, though, unless you really know what you're doing.

Also, LBEaston's right. That's one of the safest and best ways to do it, although if you have hundreds and they consume a lot of memory, then I might suggest a deque. Right now, they don't really. :)

-Albatross
Last edited on
Okay, time to continue to learn C++ :V
Last edited on
Topic archived. No new replies allowed.