Classes and Instances

Posts 1–5 of 5 · Page 1 of 1
Classes and Instances
Hey, I'm sorta of a long time member here at MPGH, but aside from that I'm really trying to get into CPP. I've found a good book somewhat to help me understand C++, it uses players in a game, etc. I'm a beginner in C++ and trying to understand the basics really well before I go onto anything else.

In the code there's this:
Code:
Player blueHat;
Player redHat;
Player greenHat;
Player yellowHat;
later on it has this piece of code in it, which I'm confused about:
Code:
void Player::getTreasure() {
  health++;  // increments the value of the player's health by one
}
Why not just add this:
Code:
void Player.greenHat::getTreasure() {
  health++;  // increments the value of the player's health by one
}
Because with function "Player::getTreasure" would just mean that all players get treasure. Could anyone explain this to me?

Also, the website I found that all on and link below is the chapters:
online C++ tutorial: Using a Class (Instantiation)ⓘ
Yea I see what you mean. When you are using
Code:
void Player::getTreasure() {
  health++;  // increments the value of the player's health by one
}
It will give all players Treasure
But when you use
Code:
void Player.greenHat::getTreasure() {
  health++;  // increments the value of the player's health by one
}
You are giving just the greenHat players treasure.
The book should have wrote this

Code:
void Player.greenHat::getTreasure() {
  health++;  // increments the value of all player's health by one
}
Code:
void Player.greenHat::getTreasure() {
  health++;  // increments the value of the greenHat player's health by one
}
That's just telling the function what it's supposed to do, declaration type thing.

[php]
void Player::SaySomething()
{
cout <<"Something";
}

int main()
{
Player Void; //instance
Void.SaySomething();
}
[/php]

When you tell the function what it's supposed to do, you're generalizing it for all instances of that class, not for that specific instance.

Not sure if that's what you asked.
Yeah, that's what I asked Void. Sorry, I'm new to cpp and trying to understand all these new terms. I've been spending a few hours of my time reading up on it, I think it's interesting, but then trying to explain it to someone else is different. Thanks for helping.
Quote Originally Posted by tactics View Post
Yeah, that's what I asked Void. Sorry, I'm new to cpp and trying to understand all these new terms. I've been spending a few hours of my time reading up on it, I think it's interesting, but then trying to explain it to someone else is different. Thanks for helping.
Lol ok I just got outsmarted by void. Again...
Posts 1–5 of 5 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?