Hi i'm make an Inventory System for my personal game project with C++ Composite pattern. What I'm going to do is set a limit so player unable add item once hit the specific limit. but here my problem :

  1. Did i set the limit & add item function correctly?
  2. Way to do the limit checking function.

    class Arrow
    {
    public:
    string arrowType;
    double weight;

    Arrow(string type, double w): arrowType(type), weight(w){}
    
    //! pure abstract method
    virtual void getDescription(int level) = 0;
    virtual double getWeight() = 0;

    };

    class FireArrow : public Arrow
    {
    public:
    FireArrow(string type, double w ): Arrow(type,w){}

    void getDescription(int level)
    {
        for(int i=0; i<level; i++)cout<<"\t";
        {
            cout<<"Arrow Type: "<<arrowType<<", Weight: "<<weight<<endl;
        }
    }
    
    double getWeight()
    {
        return weight;
    }

    };

    class StandardArrow : public Arrow
    {
    public:
    StandardArrow(string type, double w ): Arrow(type,w){}

    void getDescription(int level)
    {
        for(int i=0; i<level; i++)cout<<"\t";
        {
            cout<<"Arrow: "<<arrowType<<", Weight: "<<weight<<endl;
        }
    }
    
    double getWeight()
    {
        return weight;
    }

    };

    class ArrowBundle : public Arrow
    {
    private:
    vector<Arrow*> arrowList;
    public:
    ArrowBundle(string type, double w) : Arrow(type, w){}

    void Add(Arrow *a)
    {
        arrowList.push_back(a);
    }
    
    void getDescription(int level)
    {
        cout<<"Arrow: "<<this->arrowType<<", Weight"<<this->weight<<endl;
        if(!arrowList.empty())
        {
            for(int i=0; i<level; i++)cout<<"\t";
            {
                cout<<"Subordinate of: "<<arrowType<<" : "<<endl;
                ++level;
                for(int j=0; j<arrowList.size(); j++)
                {
                    arrowList[j]->getDescription(level);
                }
            }
        }
    }
    
    double getWeight()
    {
        return weight;
    }

    };

    int main()
    {

    ArrowBundle arrow("Arrow", 500);
    ArrowBundle fa("Fire Arrow", 50);
    ArrowBundle sa("Standard Arrow", 30);
    
    reRun:
    cout<<"Add Item "<<endl;
    cout<<"===================="<<endl;
    cout<<"1.Fire Arrow"<<endl;
    cout<<"2.Standard Arrow"<<endl<<endl;
    cout<<"Your Choice: ";
    int choice;
    cin>>choice;
    if(choice == 1)
    {
        for(int i=0; i<5; i++)
        {
             arrow.Add(&fa);
    
        }
    }
    else if (choice == 2)
    {
        for(int i=0; i<5; i++)
        {
             arrow.Add(&sa);
        }
    }
    cout<<"The Hierarchy of the Arrow Inventory"<<endl;
    arrow.getDescription(0);
    cout<<endl;
    goto reRun;
    
    system("pause");
    return 0;

    }

  1. Leave the loop out of getDescription() for Arrow derived classes.
  2. Don't derive ArrowBundle from the Arrow class.
  3. In main() for your choices, the arrow types should not be ArrowBundle types (see #2 above), but should be of the appropriate type (FireArrow or StandardArrow) added to the arrow bundle. Also, you might want to rename the arrow instance to arrows to better describe that it is a set of arrows (Fire and Standard).
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.