Thread: setting up and also accessing classes

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    23

    setting up and also accessing classes

    Hi, I have brought a book & also I have read about 2 -3 tutorials, but still don't understand about cllasses. What I need to know is how to use them, most of these books only tell you the basic stuff. If you had a player object and a map object, which one would hold the players position? Also how would the player object and Map object communicate, If the player moves how would the Map update it's self on where the player is.
    Last edited by Stevo; 09-05-2003 at 08:23 PM.

  2. #2
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    Posts
    124
    Well that could be done several ways. First of all, you'd probably store the player's location in the Player object. To communicate movement with a Map object, you'd need a function. I guess you could put that in either class, but I think putting it in Player would be best.

    Brendan
    Draco dormiens nunquam titallandus.
    Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    What data/methods you put into what classes is highly subjective. It depends on the nature of the data, the classes, and what the programmer has in mind as far as design goals for each class. In general, however, avoid circular dependencies between classes (either player should know about map or vice-versa, but if possible, not both).

    A small example of how they might communicate:
    Code:
    // Map contains one (or perhaps a list) of players...
    class map {
      player& p;
    public:
      void update_self()
      {
        draw_player(p.get_position());
      }
    };
    
    // Player knows nothing of map...
    class player {
      int pos;
    public:
      int get_position() { return pos; }
    };
    Brief, and incomplete, but I think it demonstrates the principle at least partially.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    23
    Thanks Brendan, I've only been doing this a couple months. I think instaed of a map object you'd have just a file or list to record where every thing is.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    23
    Thanks Zach, yeah I wanted to know how things can pass information from one class to another, is "player& p;" in Map a referance?

  6. #6
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Hi Stevo,

    It's a reference to an object of type "player", which is another class.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed