Search code examples
c++linked-listbinaryfiles

Saving in binary files


I've been working on a project for the past few days that involves three linked lists.

Here is an example of the header with the nodes and the lists themselves:

class nodeA
{
   int data1;
   double data2;


    nodeA* next;

}
class listA
{
    nodeA* headA;
    ...
}
class nodeB
{
   int data3;
   double data4;


    nodeB* nextB;

}
class listB
{
    nodeB* headB;
    ...
}
class nodeC
{
   int data5;
   double data6;


    nodeC* nextC;

}
class listC
{
    nodeC* headC;
    ...
}

What i'd like to know is how can i save the lists that i declare in my main so that if i close the program and then open it again i can recover the lists data


Solution

  • So lista_vendas::novaVenda needs to call lista_produto::escolheProduto.

    To call lista_produto::escolheProduto you need a lista_produto object. Where are you going to get that lista_produto object from?

    There's really only three ways this can be done. I don't know which way is the correct way for you, but I'll list them and you can decide.

    1) Have a lista_produto global variable, a global list of products. Then lista_vendas::novaVenda can use the global lista_produto to call lista_produto::escolheProduto. This is the simple solution, but global variables are rightly considered bad style. It also means that you program can only have one list of products, is that a problem? Think carefully before trying this solution.

    2) Have a lista_produto as a member variable of lista_vendas or no_vendas. I guessing here but perhaps something like this

    class no_vendas
    {
    public:
        unsigned int codigoVenda, dia, mes, ano, numeroItens;
        double precoTotal;
        lista_produto productList; // list of products
        no_vendas* proxi; //referencia para o proximo no
    };
    

    Now each vendor has a list of products, which makes sense. So lista_vendas::novaVenda now has access to a lista_produto in each no_vendas and it can use that to call lista_produto::escolheProduto. If this makes sense then this is problably the best solution.

    3) Pass a lista_produto as a parameter to lista_vendas::novaVenda. Like this

    void novaVenda(unsigned int codigoVenda, lista_produto* productList);
    

    So whatever code calls lista_vendas::novaVenda must also supply the lista_produto that it needs.

    As I said I don't know which of these possibilities is correct, because I don't know what you are trying to do (and I don't speak Spanish). But this is a problem in the relationships between your different objects. It's up to you to design your classes so that they can access the different objects that they need to work.

    You mentioned inheritance in your title, but this doesn't feel like the right thing to do in this case.