Thread: help with class design

  1. #1
    Registered User Mark S.'s Avatar
    Join Date
    May 2005
    Location
    England
    Posts
    16

    help with class design

    Hi,

    I am finally starting to get to grips with OOP and i have been trying to structure real life situations into classes and objects and the relationship between them.

    Can someone please give me an insight into how i may design a class Human in a game that can pick up other classes such as food or a key for example.

    The problem i am having is what to put into
    private:

    Before the game starts i have no idea what the object human will carry.

    What subject do i need to research to do the above?

    Thanks

    Mark S.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    This sounds like a case for inheritance. So, the human can only pick up certain objects. Derive all of these objects from a base class. Then, all you need to do is store objects of this base class type.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    20
    Hi there,

    I had a lot of questions about exactly that, not too long ago.
    That is, until I found out how many more questions I really had, about game structure, and C++ programming in general.

    If you are serious about this, maybe you should check out a book, that someone here (thanks again, Dae) told me about:
    Data Structures For Game Programmers, by Ron Penton.

    Sounds pretty much like what you and I are looking for, eh?

    I am shure going to check it out, after I finish Accelerated C++, by Andrew Koenig, and Barbara E. Moo.

    Hope it helps!

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    22
    Quote Originally Posted by Zach L.
    Derive all of these objects from a base class. Then, all you need to do is store objects of this base class type.
    While this is true, the base class will only contain state and behavior that is common to anything the human can pick up.

    For instance, you could have a class named ObtainableItem (or whatever) that things like Key objects or Food objects could be derived from. The "Human" class could have a private list of ObtainableItem objects. Then, when the human picks up something, it gets placed in the list as an ObtainableItem. The thing is, you would need to have a means of identifying the concrete type of the object when you retrieve it from the list because you don't want your human eating a torch or lighting up his food in a dark passageway. You could use run-time type identification (RTTI) or have some type of cross-reference table in ObtainableItem that tracks each type of item to an id number (maybe use a hash table).

  5. #5
    Registered User Mark S.'s Avatar
    Join Date
    May 2005
    Location
    England
    Posts
    16
    All i ask for is a subject to research. Do i need to look into inheritance or maybe linked lists or maybe something else. Any help is much appreciated.

    Thanks

    Mark S.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yes, inheritance will certainly be an important topic. And, you will need a data structure to store your objects in such as a list (or perhaps even that hash table). With regards to the containers, though: while it can be useful to write your own at some point (a basic one, just to get the feel for it), it is usually more worthwhile to use STL containers, and adapt them if necessary.

    RTTI would be useful, but first, get used to inheritance and virtual methods, etc.

    Cheers
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Thinking in C++ by Bruce Eckel, which is available for free download online, might be a good resource for the type of information you are looking for.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Before the game starts i have no idea what the object human will carry.
    A less complex solution might be designing your Human class to have an array for each type of object a Human can pick up, e.g.

    Food f[10];
    Weapons w[10];
    Keys k[10];

  9. #9
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    You may also want to try enum combined with classes.

    Like

    Code:
    enum Food { Peanut, Jelly, Pizza, etc }
    enum Hats { Blue, White, Black }
    you get the picture?
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    you get the picture?
    nope.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help on class coupling
    By andrea72 in forum C++ Programming
    Replies: 4
    Last Post: 04-17-2011, 10:16 AM
  2. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM