How can I access a specific protected inherited member depending on the class?

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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    //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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    
    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!
Declare WeaponInventory as a static member of the base class.
http://msdn.microsoft.com/en-us/library/b1b5y48f.aspx

1
2
3
4
5
6
7
8
9
class MainShop
{
    private:
        //some variables
    protected:
        static std::vector <std::string> WeaponInventory;

        // ...
};


And define it in MainShop.cpp

std::vector <std::string> MainShop::WeaponInventory;
closed account (3qX21hU5)
Though be aware that this will not be a permanent solution to the problem if you plan on having more then one instance of the class to represent other entities and you will most likely have to rework your design.

The reason why is that you will have to share that inventory with every instance of that class. This won't be a problem if you only plan on having one player (Or one shop I really can't tell if the inventory is for a shop where you buy stuff at or for a players inventory) but if you plan on using the class for another instance that will be another player it won't work because they would have to share the same inventory.

Last edited on
@JLBorges, thank you! That fixed my problem. and @Zereo, my inventory is for the player, where you equip stuff and check stats. Im thinking of making a virtual function for my mainShop though. Any tips or suggestions are gladly welcome! :p
Topic archived. No new replies allowed.