Help understand a class question

I had a question like this

You are writing a computer game and you are going to allow the player to possess various items in the game, such as weapons, armour and jewellery. Some items can be worn, others can be equipped. All items can be bought or sold. Equipped items can be used. Describe a class hierarchy for these items and decide which methods should go in which classes. Explain why you made these choices?


Answer: The base class is ‘item’, its methods are ‘buy’ and ‘sell’. The subclasses are ‘wearable’, the method is ‘wear’, and ‘equipable’, the methods are ‘equip’ and ‘use’.

But if I was going to put this in C++ wouldit by like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Class item{
public:
buy();
sell();
}

class wearable{
wear(){
equipe

};



equipeable(){

use

}
line 1: class must be lower case
line 3: needs type
line 4: needs type
line 5: needs ;

Lines 7,15: Your derived classes need to inherit from item.
Line 8: Your functions need type declarations.
Line 9: What is this? A variable? A function?
Line 17: I assume this is a function. It needs a type declaration and ().
Line 19: Need ;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class item	  //  class is lower case
{
public:
    void buy();    //  needs type
    void sell();    //  needs type
};  // needs ;

class wearable : public item // needs to inherit from item
{  
public:	            // Functions should be public
    void wear();    // needs type
//  equipe		What is this?  Function?  Variable?
};

class equipable : public item //  needs to inherit from item
{
public:
    void use ();  // needs type
};	//	needs ; 
But if I was going to put this in C++ wouldit by like this


Not exactly, you need to inherit from your base class which you are forgetting to do.

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Base Item Class
class Item
{
    public:
        void buy();
        void sell();
}

// Publicly Inherits from the base Item class
class Wearable : public Item
{
    public:
        void wear();
        void equip();
}


Here is a good tutorial on this site to help walk your through the basics of inheritance http://www.cplusplus.com/doc/tutorial/inheritance/ .

Answer: The base class is ‘item’, its methods are ‘buy’ and ‘sell’. The subclasses are ‘wearable’, the method is ‘wear’, and ‘equipable’, the methods are ‘equip’ and ‘use’.


Though this probably past the scope of your assignment question but if you are curious about game design and want to read into this problem more a good topic to look into would be a component based design for your entities in the game.

Here is some links on the subject.

A nice little explanation of the subject.
http://www.gamedev.net/page/resources/_/technical/game-programming/understanding-component-entity-systems-r3013

From the same guy as above he walks you through implementing a simple
component system in C
http://www.gamedev.net/page/resources/_/technical/game-programming/implementing-component-entity-systems-r3382

Nice articles on the subject
http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/

http://www.gamedev.net/page/resources/_/technical/game-programming/case-study-bomberman-mechanics-in-an-entity-component-system-r3159

Personally I think a design like that would be a much cleaner/better route to go for the problem that was stated. You could then just create different components for the items that would allow them to be "equipped", "wearable", etc. and add them to your items.

Anyways like I stated before this is probably way past the scope of your assignment it might be some fun reading if you are interested in the subject.

Anyways hope this helps a bit if you have any questions or anything else you don't really understand feel free to just ask and I am sure someone else or myself would be glad to help.
Last edited on
Topic archived. No new replies allowed.