help w/ classes

This is a discussion on help w/ classes within the C++ Programming forums, part of the General Programming Boards category; I am trying to program a text baced rpg using classes for the info dealing with the equipment. i tried ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    5

    help w/ classes

    I am trying to program a text baced rpg using classes for the info dealing with the equipment. i tried to use a book i have that shows me how to use classes and inheritance.

    i tried something like this

    Code:
    class equipment
    {
    public:
    
    char name[30];
    int cost
    } class weapons : public equipment { public:
    int bat_power;
    } class armor : public equipment { public:
    int armor_bonus;
    } weapon Short_sword; Short_sword.name = [s,h,o,r,t, ,s,w,o,r,d} // I dont think this is right for setting char Short_sword.cost = 50; Short_sword.bat_power = 1; armor leather_armor; leather_armor.name = [l,e,a,t,h,e,r, , a,r,m,o,r]; leather_armor.cost = 100; leather_armor.armor_bonus = 1;
    when i go and compile it, it gives me lots of errors like:

    "missing ';' befor '}'" (it gives this on every line where a class is declared)
    "left to 'leather_armor' must have class/struct/(somthing else) (it gives me this on any line that i try to get it to display the values of say leather_armor.cost)
    it also gives me other errors that make absolutly no idea what they mean

    please help me figure this out

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i tried something like this
    First, your code isn't legal C++. Second, your inheritance hierarchy could be improved. Rather than focusing on equipment, you can go higher up and go with items. Then weapons, armor, potions, poisons and such can derive from the item class. But since an item is abstract, it would be little more than an interface. Likewise with anything that isn't a concrete item. You want your hierarchy to be wide and flat rather than thin and tall. It's easier to manage that way:
    Code:
    // Top of the tree, base interface
    class Item {
      int buy() = 0;
      int sell() = 0;
    };
    
    // All weapons derive from this interface
    class Weapon: public Item {
      int attack() = 0;
    };
    
    // All armor derives from this interface
    class Armor: public Item {
      int defense() = 0;
    };
    
    // All potions derive from this interface
    class Potion: public Item {
      int charges() = 0;
    };
    This gives you a workable tree of behaviors that you can make concrete without any intermingling of unnecessaries:
    Code:
                                                          Item
    
                   Weapon                                 Armor                           Potion
    
     Sword          Axe          Bow         Chest        Head        Legs          Cure         Antidote
    
    Muramasa     OgresBane   SilverWing    IronCuirass  SteelHelm  LeatherPants
    The trick is to imagine the amount of work it would take to add new items or change existing ones. And don't forget, abstract classes are your friends.

    >"missing ';' befor '}'" (it gives this on every line where a class is declared)
    A class definition is terminated with a semicolon. The first error will usually cause other errors that don't really exist. Fix them from the top down and you'll have an easier time of things. Also keep in mind that one problem can cause tons of errors, so even if you have 30 or so errors, they might boil down to a only single bug. So don't be overwhelmed.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    5
    thank you.

    i think i have a better understanding of how classes and inheritance work now. the book i am using was hard to follow and used examples that i couldn't change at all to fit what i was tring to do so it was hard to figure out the right way to do it. after looking at the examples you gave i think i could tweek it enough to fit the game. i do have 1 qustion to ask still. Was i correct in how I created the spacific items?

    ie.

    Code:
    Short_sword.cost = 50;
    Short_sword.bat_power = 1
    Last edited by romerboy55; 07-20-2006 at 07:59 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Was i correct in how I created the spacific items?
    Not for the name, but the other members were okay. However, it's better to make any data members private and then provide a public member function if users need to access them. That way you can restrict access as necessary. Kind of like pants. When you wear them properly, you have complete control over who can play with what's inside them.

    For initialization, a constructor is preferable, especially if you use proper data hiding and make your data members private.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    5
    Quote Originally Posted by Prelude
    However, it's better to make any data members private and then provide a public member function if users need to access them. That way you can restrict access as necessary.
    thanks for the tip i will try and use this info while making this game. you really helped me a lot. i think i have a good enough understanding of c++ and the different aspects i need to make this game work how i want it.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    For the name, and any other strings your program uses, you should use the C++ string class instead of C style character arrays. They are easier to use and harder to mess up. For example, to assign to the name if it was a string, you would use Short_sword.name = "short sword";.

    If your book doesn't go over the C++ string class, consider getting a more modern book, or look at tutorials online to get the basics.

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I suggest setting the name as part of the constructor. And as Daved said, use std::string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21