Okay so I have MainShop(base class) then SwordShop and BowShop(derived classes) and another derived class called Inventory. I have a vector under protected in my base class, and when I access it from my derived classes it's okay, but each one has its own value. How can I set them all to have the same value?

//MainShop.h
    #pragma once
    class MainShop
    {

    private:
    //some variables
    protected:
        vector <string> WeaponInventory;
    public:
    //Some functions not related to the vector

    };

    //MainShop.cpp
    /*Implementation of functions*/

    //SwordShop.h
    #pragma once
    #include "MainShop.h"

    class SwordShop: public MainShop
    {
        private:
        int choice;
        public:
            void getSwordShop();
            void setWeaponSoldier(int i, string s);
            void soldierShop();
    };

    //SwordShop.cpp
    #include "SwordShop.h"
    void SwordShop::soldierShop()
    {
        this->setWeaponSoldier(1, "1) Meito Ichimonji\n   +4 Damage\n   150Gold");
        this->setWeaponSoldier(2, "2) Shusui\n   +10 Damage\n   230Gold");
        this->setWeaponSoldier(3, "3) Elixir\n   +16 Damage\n   300Gold");
        this->setWeaponSoldier(4, "4) Blade of scars\n   +24 Damage\n   550Gold");
        this->setWeaponSoldier(5, "5) Ragnarok\n   +32 Damage\n   610Gold");
        this->setWeaponSoldier(6, "6) Eternal Darkness\n   +40 Damage\n   690Gold");
        this->setWeaponSoldier(7, "7) Masamune\n   +52 Damage\n   750Gold");
        this->setWeaponSoldier(8, "8) Soul Calibur\n   +60 Damage\n   900Gold");

        this->getSwordShop();

        cout << "What would you like to buy?";
        cin >> choice;
        switch (choice)
        {
            case 1:
              WeaponInventory.push_back("Meito Ichimonji");
              cout << "You have Successfully Bought Meito Ichimonji\nIt has been added
 to your inventory\n";
                break;

            case 2:
              WeaponInventory.push_back("Shusui");
              cout << "You have Successfully Bought Shusui\nIt has been 
added to your inventory\n";
                break;
                //ETC

            default:
                cout << "Error! You have entered an invalid answer\nPlease try again";
                this->soldierShop();
        }
        cout << "your total items are: "<< WeaponInventory.size();

Okay So lets say I bought two items. Here it would display that I have 2 items. But if I do cout << "your total items are: "<< WeaponInventory.size(); in my Inventory.cpp it would say I have 0! That's my problem.

void SwordShop::getSwordShop()
    {
        //Display Choices
        for (map<int, string>::iterator iter = WeaponSoldier.begin();
 iter != WeaponSoldier.end(); iter++)
        {
            cout << iter->second << endl;
            cout << endl;
        }
    }

    void SwordShop::setWeaponSoldier(int i, string s)
    {
        WeaponSoldier[i] = s;
    }


    //BowShop.h
    #pragma once
    #include "MainShop.h"

    class BowShop: public MainShop
    {
        private:
        int choice2;
        public:
            void getBowShop();
            void setWeaponArcher(int i, string s);
            void ArcherShop();
    };

    //BowShop.cpp
    #include "BowShop.h"
    void BowShop::ArcherShop()
    {
        BowShop::setWeaponArcher(1,"1) Arondight\n   +4 Damage\n   150Gold");
        BowShop::setWeaponArcher(2,"2) Gugnir\n   +10 Damage\n   230Gold");
        BowShop::setWeaponArcher(3,"3) Susano'\n   +16 Damage\n   300Gold");
        BowShop::setWeaponArcher(4,"4) Longinus\n   +24 Damage\n   550Gold");
        BowShop::setWeaponArcher(5,"5) Hrunting\n   +32 Damage\n   610Gold");
        BowShop::setWeaponArcher(6,"6) Clarent\n   +40 Damage\n   690Gold");
        BowShop::setWeaponArcher(7,"7) Shinigami\n   +52 Damage\n   750Gold");
        BowShop::setWeaponArcher(8,"8) Caliburn\n   +60 Damage\n   900Gold");

        this->getBowShop();//Display options

        cout << "What would you like to buy?";
        cin >> choice2;
        switch (choice2)
        {
            case 1:
                    WeaponInventory.push_back("Arondight");
                    cout << "You have Successfully Bought Arondight\nIt has been added
 to your inventory\n";
                break;

            case 2:
                    WeaponInventory.push_back(" Gugnir");
                    cout << "You have Successfully Bought  Gugnir\nIt has been added
 to your inventory\n";
                break;

            //ETC

            default:
                cout << "Error! You have entered an invalid answer\nPlease try again";
                this->ArcherShop();
        }
    }

    void BowShop::getBowShop()
    {
        //Display Choices
        for (map<int, string>::iterator iter = WeaponArcher.begin();
 iter != WeaponArcher.end(); iter++)
        {
            cout << iter->second << endl;
            cout << endl;
        }
    }

    void BowShop::setWeaponArcher(int i, string s)
    {
        WeaponArcher[i] = s;
    }


    //Inventory.h
    #Pragma once
    #include "MainShop.h"

    class Inventory: public MainShop//access base class data like protected members
    {
        private:
        int choice;
        public:
            void DisplayInventory();
            void DisplayStats();

    };

    //Inventory.cpp
    #include "Inventory.h"

    void Inventory::DisplayInventory()
    {
        cout << "\nWhat do you want to do?\n1) Check Status\n2) Equip Weapons";
//Equip what is in your inventory
        cin >> choice;
        switch (choice)
        {
            case 1: this->DisplayStats();
                break;
            case 2:cout << WeaponInventory.size() << endl;//debug
                if (!WeaponInventory.empty())//Make sure inventory is not empty
                {
                    cout << "Your current Weapons are: \n";
                    for (unsigned int i = 0; i < WeaponInventory.size(); ++i)
                        cout << i << ") " << WeaponInventory[i] << endl;
                }
                else cout << "You do not currently have any items!";
            default: cout << "Error on switch!";
        }
    }

Here every time I run it, it goes straight to the else and it displays that I do not have any items. I want to just have one vector for both my bow and Sword shops because vectors take up a lot of memory and my teacher said keep them to a minimum. So I just want one vector that takes in items from my bow class as well as my sword class, but for some reason it's acting as if I have multiple vectors, each with its own set of items. Please help!

Recommended Answers

All 4 Replies

Why would every different shop have the same inventory? ;) But to answer your question, look into making the vector a static member of the base class. Then it will be shared across the board.

I know! I thought the same thing, but my teacher said vectors allocate way too much memory and its bad to use so many of them in one program :( and if i make the variable static, will i have to change anything else for it to work?

but my teacher said vectors allocate way too much memory and its bad to use so many of them in one program :(

Sounds like premature optimization to me. Vectors are quite efficient, they only hold just enough memory to contain and manage what you put in them. If you choose what you put in carefully, it takes effort to waste memory.

if i make the variable static, will i have to change anything else for it to work?

Not really. But you'll find that if you change the vector in one object, all others will change as well. That can be surprising and undesirable in a case like this, depending on your needs for this particular inventory.

okay thank you!

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.