Access attributes of a vector of objects of another class using C ++ pointers

3

I'm doing a job for college where I have to set up a program to manage a restaurant that only delivers the orders are made by phone and internet using object orientation and pointers in C ++.

I'm doing the following:

I use two files for each class a .cpp and a .hpp , below my Client class implementation.

Client.hpp

#ifndef CLIENTE_HPP
#define CLIENTE_HPP

#include "Principal.hpp"
#include "Bibliotecas.hpp"

class Cliente {

    public:
        Cliente();

        int idCliente;
        string enderecoCliente;
        string nomeCliente;
        string cpfCliente;

        void inserirCliente();
        void alterarCliente();
        void listarCliente();
        void incrementaVetor();
        int excluirCliente();
        int pesquisar(int id);        

        virtual ~Cliente();
    protected:
    private:
        Cliente *clientes;
        Principal *principal;
        int TAM;
        int cont;
        int indice;
};

#endif // CLIENTE_HPP

Client.cpp

include "Cliente.hpp"

Cliente::Cliente() {

    idCliente = 0;
    enderecoCliente = "";
    nomeCliente = "";
    cpfCliente = "";

    TAM = 2;
    cont = 1;
    indice = 0;

    clientes =  NULL;
    principal = new Principal();
}

void Cliente::inserirCliente() {

    if(clientes == NULL) {
        clientes = new Cliente[TAM];
    }

    cout << "\n";
    principal->centralizaTexto("Cadastro de Clientes");
    cout << "\n";

    cout << endl << endl;
    principal->alinhaTexto("ID Cliente: ", 5, 14);
    cout << cont << endl;
    clientes[indice].idCliente = cont;
    cout << endl;
    cin.ignore();

    do {
        principal->alinhaTexto("Informe o nome: ", 6, 14);
        getline(cin, clientes[indice].nomeCliente);
        cout << endl;
    } while(clientes[indice].nomeCliente == "");

    do {
        principal->alinhaTexto("Informe o endereço: ", 6, 14);
        getline(cin, clientes[indice].enderecoCliente);
        cout << endl;
    } while(clientes[indice].enderecoCliente == "");

    do {
        principal->alinhaTexto("Informe o número do CPF: ", 6, 14);
        getline(cin, clientes[indice].cpfCliente);
        cout << endl;
    } while(clientes[indice].cpfCliente == "");

    cont++;
    indice++;

    if(indice == TAM) {
        incrementaVetor();
    }

    cout << endl;
    principal->centralizaTexto("Cliente cadastrado com sucesso!", 10);
    principal->pause();
}

int Cliente::excluirCliente()  { ... }

void Cliente::alterarCliente() { ... }

void Cliente::listarCliente() { ... }

void Cliente::incrementaVetor()  { ... }

int Cliente::pesquisar(int id) {
    for(int i = 0; i < indice; i++) {
        if (clientes[i].idCliente == id)
            return i;
    }
    return -1;
}

I have 2 more classes implemented in this way, Menu and Driver. But my problem is in the class I have to do now that is the class Orders, in it I need to fetch the information contained in the vectors of objects of the other classes (vectors are pointers). For example I need to within the Orders class access the variable CustomerName from any index of the client vector to get its contents.

My class Orders for now:

Orders.hpp

#ifndef PEDIDOS_HPP
#define PEDIDOS_HPP

#include "Principal.hpp"
#include "Bibliotecas.hpp"
#include "Cliente.hpp"

class Pedidos {
    public:
        Pedidos();

        int idPedido;
        string clientePedido;
        string motoristaPedido;
        string itemsPedido;
        string placaVeiculoPedido;

        void inserirPedido();
        void alterarPedido();
        void listarPedido();
        void incrementaVetor();
        int excluirPedido();
        int pesquisar(int id);

        virtual ~Pedidos();
    protected:
    private:
        Cliente *clientes;
};

#endif // PEDIDOS_HPP

Orders.cpp

#include "Pedidos.hpp"
#include "Cliente.hpp"

Pedidos::Pedidos() {
    idPedido = 0;
    clientePedido = "";
    motoristaPedido = "";
    itemsPedido = "";
    placaVeiculoPedido = "";
}

void Pedidos::inserirPedido() {

}

void Pedidos::alterarPedido() {}
void Pedidos::listarPedido() {}
void Pedidos::incrementaVetor() {}
int Pedidos::excluirPedido() {}
int Pedidos::pesquisar(int id) {}

Pedidos::~Pedidos() {
    //dtor
}

I have tried in many ways, some gave access error and closed the program other the program continued but did not display the result.

Another question is what is my search function that is in class Cliente , how do I return an object of type pointer?

    
asked by anonymous 19.09.2015 / 19:40

1 answer

2

I think this question is far from being answered as you wish that even if this answer does not answer what was asked I will try to put you on the right path and then see specific problems.

I see a serious error in having a pointer to Cliente within the class itself Cliente . Although technically this is possible, in a few cases it makes sense to do this. And even though I do not know your exercise, I find it strange that this is necessary. Customer is customer. Collection of clients is something totally separate.

By the way, since you're programming in C ++ why not Vector<T> instead of the pointer. It has numerous advantages.

Is work requirement? Are you sure? Is this written? Is there any reason for work to have to be done the wrong way?

I do not know what makes the Principal class, but I doubt I need to have an instance of it within the Cliente class, and probably any other. It may have its utility statically but not as an instance.

I will not complain about the mix of using UI business rules in the same class because it is an exercise, but this is wrong conceptually and in real code it should never be done this way.

If there were this simplification it would be easier to find mistakes, or maybe it would be harder to make mistakes. Organization is key in programming.

It still has the error of initializing the array of clients only when it is going to insert something in it, but since this is already all wrong nor do I think this is the biggest problem.

It also does not make sense to have array of Cliente or even Vector<Cliente> within the request. Why would the request need access to all clients?

There are some small problems but this already gives an idea of how complicated it is.

If you solve wrong conceptualizations it will be difficult to give a solution that really solves something. Of course we could say what to do on top of this, but the problem would only increase.

Think about how things really are. Understand that Pedido is something very different from Pedidos . The second is composed of several objects of the first, there is a relation between them, but they are different things. Do something more cohesion and understand how to do a aggregation .

Even if we were to help with the above, we still lack information to give a more objective answer. You do not have a specific problem yet.

It's the best I can do to help right now.

    
19.09.2015 / 20:12