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

Recommended Answers

All 2 Replies

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.

Thank you.. that went over my head. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.