Thread: player class

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    player class

    im just starting to learn classes i understand some of them now but im still confused by alot..

    for example im trying to create a player class but would i put the players stat variables in private public or protected?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It depends. If you intend to create derived classes of the player class, then you might want to make the variables protected; otherwise, you'd probably make them private. (Making them public wouldn't be a good idea because it bypasses encapsulation.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >would i put the players stat variables in private public or protected?
    Private. Unless you have a very good reason and can defend that reason against people who are way smarter than you, all data members should be made private and all access controlled through public or protected member functions.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    thanks ive been using zeuscmd and it put them in public so i wasnt sure thanks yall

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    It really depends on you how to create ur player class. The general trend among programmers is that they declare datatypes in the private section of the class and provide functions in the public section of the class to access these datatypes but this is not the rule to do it.
    Look if u want that your datatypes must only be accessed only by the functions belonging to the player class then declare them as private. If you r going to derive some new class from the player class and u intend to use the variables defined in the player class in ur newly created class then declare them as protected and if u want that everybody should access those variabels then declare them as public.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Making variables public is like using a structure. It defeats the purpose of a class. Admittedly, sometimes you need to use public variables, but "players stat variables" should almost definitely be private.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If you r going to derive some new class from the player class and u intend to use the variables defined in the player class in ur newly created class then declare them as protected and if u want that everybody should access those variabels then declare them as public.

    You can do those things if you'd like, but it is generally bad design.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    now im confused about constructors


    in that stupid zeus tutorial it shows
    Code:
    player()
    player(int h)
    player(int h,int m)
    
    int health;
    int mana;
    then it goes
    Code:
    player()
    {
    health = 100;
    mana = 50;
    }
    player(int h)
    {
    health = h;
    mana = 50
    }
    player(int h,int m)
    {
    health(h);
    mana(m);
    }
    theres no possible way you could do this it doesnt make any sense

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    player()
    player(int h)
    player(int h,int m)


    what u see above in the first three lines is called function overloading i think u must have read about it before if not i would suggest u to read about it from a good book. It is one of the features of C++ and is of great help. Try putting urself in a situation where u have to initialize the variable/variables of player class differently ( as shown in ur code) and u will appreciate function overloading.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I have trouble believing that's what's in the tutorials because of the random missing semicolons. I'm sure they don't miss semicolons in the tutorial, which means that's not an exact copy of the tutorials, which leads me to believe you've made other mistakes, specifically the lack of the scope resolution on your constructor definitions. Now other than those syntax errors there isn't much wrong with those constructors that I can see. They could be simplified with some initialization lists or you could use default values in your arguments rather than the void argument constructor (though, I prefer a void argument constructor), but that's about it. What do you see wrong with them?
    Sent from my iPadŽ

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    But what's this? It's not an initializer list . . .
    Code:
    health(h);
    And why the missing semicolons? And why the lack of class qualifiers (such as theclass::theclass) for the constructor definitions?

    You might want to try a different tutorial.

    [edit] Sly beat me to it. [/edit]

    [edit=2] Sorry, my apologies to whoever wrote the tutorial; I thought the code above was exactly what it contained. [/edit]
    Last edited by dwks; 08-29-2006 at 08:21 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    ive heard of operator over loading but thought it was too advanced to get into so fast but ill take a look at it


    sly

    no its not an exact copy this is

    http://www.zeuscmd.com/tutorials/cpl...nstructors.php

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by http://www.zeuscmd.com/tutorials/cplusplus/47-Constructors.php
    Code:
    Player::Player(int h, int m) :
    	health(h),
    	mana(m)
    {}
    This is why you copy the code exactly. This is an initialization list... what you had was just an obscure use of parentheses to initialize when the tutorial would appear to be using the equal sign the whole time. You seem to think code you don't understand must automatically be incorrect and you try to fix it. What you seem to not realize is that you don't yet understand all that much. Don't ignore what appears new to you, try to understand it... that's how you learn.
    Last edited by SlyMaelstrom; 08-29-2006 at 08:25 PM.
    Sent from my iPadŽ

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If I was you I would use inherientence and make a base class that declares all the player and enemy data members, make them all "protected" and then create a new class called Player that can acsess the variables of the base class

    eg:
    Code:
    class Base
    {
    public:
        Base() {};
        ~Base{};
    
    protected:
        int m_hp;
        long m_score;
        short m_mp;
    
    private:
        string m_pNm;
    };
    // player class

    Code:
    class Player : public Base // player class can use Base class members
    {
    public:
        Player() {};
        ~Player() {};
    
    // player class functions here
    };
    Just an idea. If you have trouble understanding inherientence, read up on it or google it
    good luck in your game! - pete

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If I was you I would use inherientence and make a base class that declares all
    >the player and enemy data members, make them all "protected" and then
    >create a new class called Player that can acsess the variables of the base class
    Why? What reason do you have for a single base class and a single child? This strikes me as an inappropriate use of inheritance, or as I like to call it, inheritance for the sake of inheritance. If you really want to inherit something, a much better solution would be to abstract away the behavior of players in general to an abstract interface which concrete player classes derive from and override. That way you can define a class (haha! I made a funny) of players with common behavior that may or may not have the same data and may or may not extend the base functionality. Consider using the behavior for both user controlled players and AI controlled players. They both will likely have the same base behavior though each class will extend that behavior beyond the base for AI or user-specific needs. Yet with the abstract base design, they can both be used as players in a generic fashion.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  3. Issue with pointers to an abstract class and inheritance
    By bainevii in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2009, 07:51 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM