Search code examples
c++classobjectinheritanceheader

How can an object access another objects data?


Yet another question about my project for my university! We are creating a cash register program which has an inventory, both held in their own respective header files with their own classes and objects.

Here is our current iteration of our header files, thought it may be messy from debugging.

Inventory.h:


class inventory
{
    protected:
        vector <item> itemList;
        fstream infile;
    public:
        void printList();
        void fillInventory();
        void fileOpen();
};

class item
{
    protected:
        double price;
        string name;
        int amount;

        friend class inventory;
};

Register.h:

class cashRegister : protected inventory
{
    private:
        vector <int> userChoice;
        double total = 0;
    public:
        void input();
        void printReceipt();
        void calcTotal();
        const double getTotal();
};


void cashRegister::input()
{
    int temp;

    try
    {
        do
        {
            cout << "\nPlease select an item using the corresponding item number.\n=> ";
            cin >> temp;
            if (cin.fail())
            {
                throw(256);
            }
            else
            {
                if (temp == -1)
                {
                    break;
                }
                else if (temp >= 0 && temp <= 10)
                {
                    if (inventory.stockChecker(temp) == 1)
                    {
                        userChoice.push_back(temp);
                    }
                }
                else
                {
                    throw(1);
                }
            }
            cout << "\n\n";
            printInventory();

        } while (true);
    }
    catch (int error)
    {
        cout << "Input error: " << error << endl;
        cout << "Please enter a value from 0 to 10 to select an item (-1 to exit).\n";
        cout << "----------------------------------------------------------------\n";
        cin.clear();
        cin.ignore(256, '\n');
        input();
    }
}

The problem here is once we pass ourselves into Register.h, we take user input then call the printInventory inventory method after every entry. But when we go to printInventory we no longer have the itemList data that the inventory object held.

Here is main.cpp for clarification on the objects:

#include"Inventory.h"
#include"Register.h"

int main()
{   
    inventory inv;

    inv.fileOpen();
    inv.fillInventory();
    inv.printInventory();

    cashRegister register1;

    register1.input();
    register1.calcTotal();
    register1.printReceipt();


}

Our object inv holds a vector of item objects, which has the data members price amount name. So when we create the register1 object then try to print the inventory again, we now have an empty vector. How would I achieve being able to print the inv object's itemList or other data in general?

EDIT: Also to note I did try passing the inv object by reference and then calling the methods with that object. It did work but it felt awfully messy.


Solution

  • If you want to access the data of inv from a cashRegister object; instead of passing the inv object as reference every time you use it's data, you may want to hold a pointer in cashRegister class. inventory class will have member functions for manupilating and accessing it's data. Therefore you will be able to access to same cashRegister from multiple inventory objects, easily doing operations on cashRegister. With this approach, you will just need to initialize your cashRegister class with a pointer to inventory class.

    class inventory
    {
    public:
        void printList();
        void changeData();
        ...
    };
    
    class cashRegister
    {
    private:
        inventory* inv;
    public:
        cashRegister(inventory *inv)
            :
            inv(inv)
        {}
    
        void printInventory()
        {
            inv->printList();
        }
        ...
    };
    
    int main()
    {
        inventory inv;
        inv.fileOpen();
        inv.fillInventory();
        inv.printInventory();
    
        cashRegister register1(&inv);
        cashRegister register2(&inv);
    
        register1.input();
        register1.calcTotal();
        register1.printReceipt();
        register1.printInventory();
    
        register2.printInventory();
    }