0

Hi, I have an Item class, how can I make my Item object be composed of more Item objects? Say I have a gun and its composed of more items like bullet and magazine, how can i make it do that?

/*Item.h handles the item in game*/

#ifndef ITEM_H
#define ITEM_H
#include <string>

class Item
{
public:
    Item(std::string IDesc, std::string IName) : name(IName), desc(IDesc), used(false){};
    ~Item(){};
    bool useable(){ return !used; }
    void useItem(){ used = true; }
    std::string getName() const { return name; }
    std::string getDesc() const { return desc; }
private:
    std::string name;
    std::string desc;
    bool used;
};
#endif

Edited by HuePig

2
Contributors
2
Replies
17
Views
3 Years
Discussion Span
Last Post by HuePig
1

A vector springs to mind:

/*Item.h handles the item in game*/
#ifndef ITEM_H
#define ITEM_H
#include <string>
#include <vector>
class Item
{
public:
    Item(std::string IDesc, std::string IName) : name(IName), desc(IDesc), used(false){};
    ~Item(){};
    bool useable(){ return !used; }
    void useItem(){ used = true; }
    std::string getName() const { return name; }
    std::string getDesc() const { return desc; }
private:
    std::string name;
    std::string desc;
    bool used;
    std::vector<Item> containedItems;

};
#endif

With the contained items being added and used through appropriate class functions.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.