Results 1 to 5 of 5
  1. #1
    tactics's Avatar
    Join Date
    Nov 2007
    Gender
    male
    Posts
    2
    Reputation
    10
    Thanks
    0

    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)

  2. #2
    mwb1234's Avatar
    Join Date
    May 2009
    Gender
    male
    Posts
    460
    Reputation
    7
    Thanks
    65
    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
    }

  3. #3
    Void's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Location
    Inline.
    Posts
    3,198
    Reputation
    205
    Thanks
    1,445
    My Mood
    Mellow
    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.

  4. The Following User Says Thank You to Void For This Useful Post:

    danng4280 (05-04-2010)

  5. #4
    tactics's Avatar
    Join Date
    Nov 2007
    Gender
    male
    Posts
    2
    Reputation
    10
    Thanks
    0
    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.

  6. #5
    mwb1234's Avatar
    Join Date
    May 2009
    Gender
    male
    Posts
    460
    Reputation
    7
    Thanks
    65
    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...

Similar Threads

  1. [Release] Roll The Dice with Custom Classes and Killstreaks
    By littlegandhi1199 in forum Call of Duty Black Ops Server & GSC Modding
    Replies: 9
    Last Post: 02-17-2011, 10:59 AM
  2. [Discussion] New AVA coding class and other games
    By sam22 in forum Alliance of Valiant Arms (AVA) Discussions
    Replies: 5
    Last Post: 12-21-2010, 06:33 AM
  3. SELLING: 1ST LT 4 CLASS AND CAPTAIN
    By Jabe1228 in forum Selling Accounts/Keys/Items
    Replies: 5
    Last Post: 10-28-2010, 09:43 PM
  4. SOLD: 1STLT 4 CLASS AND CAPTAIN
    By Jabe1228 in forum Selling Accounts/Keys/Items
    Replies: 0
    Last Post: 10-28-2010, 06:03 PM
  5. Replies: 14
    Last Post: 09-06-2010, 02:59 AM