Thread: Health...

  1. #16
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    I refer you to your reference material - you want to look at
    constructors - they initialise the members of a class whenever
    you create an instance of one.

    There are two methods of initialising members - one is covered
    here, and the other here. Ignore the stuff about inheritance
    for the moment, just look at the syntax for the member initialisation
    list in that second one.

    Don't want to sound rude, but I'm not on call as a reference,
    so only ask questions that you cant answer or about concepts
    you cant grasp properly - I won't have the time to teach you
    everything you'll need to do this task.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Actually all the data members are private in Richie's class declaration, which meand that the variables are only accessable by the class itself, and not any other functions, like main() or other classes. That's why Richie declared getter functions. This is good because it protects your data from unauthorized change.

    These getter functions make it so that the data is made available to other parts of the program in a safe way. They look something like this:
    Code:
    // This function returns whatever Health is declared as. Say, an int.
    int Fighter::GetHealth(void) const {
        return Health;
    }
    Last edited by whiteflags; 05-18-2006 at 05:26 PM.

  3. #18
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Somehow missed that last post!

    Exactly as citizen said, the variables are private. The reason?
    Data encapsulation is one of the principles of good object
    oriented design, the idea that the object is a fully self sufficient
    entity - the only way to manipulate it being through methods
    coded by the programmer. Thats why OOP is so popular in
    industry - a class should contain carefully designed methods
    so that the code can be reused without need to tinker with the
    class's inards. Thats the main difference between classes and
    structs - in a struct, all variables are accessible (if an instance is
    in scope). In a class, anything under private can only be seen
    and changed by the class itself (or friend objects/functions, but
    thats another matter). Public means that they are visible to
    everyone, so thats why we normally put methods under public.
    Protected then has to do with inheritance - where we want
    the variables to be restricted like private, but classes
    that inherit from this class will inherit variables under protected
    as well.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little Game
    By cjwenigma in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2007, 12:50 PM
  2. health...
    By psycho88 in forum Game Programming
    Replies: 6
    Last Post: 12-04-2005, 09:37 AM
  3. Help With Health in Games
    By Mustang5670 in forum Game Programming
    Replies: 8
    Last Post: 01-04-2004, 04:43 PM
  4. AOL And your health
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-09-2003, 09:51 AM
  5. Health Bar demo
    By jdinger in forum Game Programming
    Replies: 7
    Last Post: 04-12-2002, 12:27 PM