Access attributes of an object vector of another class using C++ pointers

Asked

Viewed 2,388 times

3

I am doing a job for the college in which I have to put together a program to manage a restaurant that only makes deliveries the orders are made by phone and internet using object orientation and pointers in C++.

I’m doing it this way:

Use two files for each class one .cpp and a .hpp, below my implementation Client class.

Client.

#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 two more classes implemented in this way Menu and Driver. But my problem is in the class I have to do now, which is the Requests class, where I need to search for the information contained in the object vectors of the other classes (vectors are pointers). For example I need from within the Orders class to access the client name variable of any of the clients vector to pick up its content.

My class Requests for now this way:

Requests.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

Requests.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 tried several ways, some gave access error and closed the program others the program continued but did not show the result.

Another question is how much my research function is in the class Cliente, how do I return an object of the pointer type?

  • The question is too long, try to attack one problem at a time, don’t make us analyze all your code. Read this: http://answall.com/help/mcve. We need to know where your problem is. We have to figure out the problem to then give the solution gets complicated. Is there a requirement to use pointer or have you decided to use it? I’ve seen some misconceptions as class Cliente have inside of her clientes, which is a collection of Cliente. In thesis could, but doesn’t it sound weird have clients own customers? When the thing is misconceived the implementation will also be.

  • The question is the smallest way I found to show my problem, the first Clients.hpp and Clients.cpp codes have no problems and just to see how this my implementation and hid much of my code there is not even 1/5 of it. Right at the beginning I say I need to use pointers because it’s a job requirement. Well among the codes this my real problem is accessing the information that are in the other classes as to customers within the order class. My Client class has an array called clients where are the data of each of my customers.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

2

I think that this question is far from being answered as you wish, and even if this answer does not answer what has been asked I will try to put you on the right path for later we 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 not knowing your exercise I find it odd that this is necessary, customer is client, customer collection is something totally separate.

By the way, since you are programming in C++ why not use Vector<T> in place of the pointer, it has numerous advantages.

Is it a job requirement? Are you sure? Is this written? Is there any reason why the work has to be done the wrong way?

I don’t know what class does Principal but I doubt I need to have an instance of it within the class Cliente, and probably any other. Maybe it has its usefulness in a static way but not as an instance.

I won’t complain about mixing the use of business rules with user interface in the same class because it is an exercise, but this is conceptually wrong and in real code should never be done this way.

If there were this simplification it would be easier to find errors, or perhaps it would be more difficult to make them. Organization is fundamental in programming.

You still have the error to initialize array of customers only when inserting something in it, but as this is already all wrong nor do I think this is the biggest problem.

It doesn’t make sense to have one either array of Cliente or even a Vector<Cliente> within the order. Because the order would need to access all customers?

There are a few more small problems but this gives an idea of how complicated it is.

Without solving misconceptions it will be difficult to find a solution that really solves anything. Of course we could tell you 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 relationship between them, but they are different things. Do something else cohesive and understand how to make a aggregation.

Even if we were to help above all, there is still a lack of information to give a more objective answer. You don’t have a specific problem yet.

That’s the best I can do right now.

  • It is explicit in the work to use pointers because it is the matter that we are learning. I cannot use Vector because it has not been passed yet. The case of the pointer inside the client was a teacher’s own guidance to solve a problem I had before as well as initialize only when recording the values. The main class and a collection of functions I did so I use it here and in other classes. Basically I need to search for information in the customer class and use it in the order class using "make classes talk" pointers. What information do you need?

  • I regret that you are being taught C++ as if it were C. C++ pointer should be avoided as far as possible without losses, which is the case. P/C++ pointer is an advanced and not basic feature as it is in C. Did you understand the teacher’s guidance about the customer having customers? Maybe you even understand, what you’re talking about makes sense a misconception of how to organize classes. The way it is, I don’t need information because I wouldn’t know how to work with such a wrong class. If you do not fix the problem will only increase it. When misconception, wrong implements, there is no escape.

  • It’s okay that it has to be avoided but I have to use it at some point to learn, in this case this job. If not when I need it like I do. How can I start fixing my classes so I can do what I need?

  • To learn wrong I would dispense. It would save the use for when it was able to evaluate that there the pointer is really useful, what is very rare. Some people can make an app and never use a pointer. The answer is precisely an attempt to show the main problems that need to redesign. The main one is the mixture of Cliente with Clientes. This is so wrong that any solution would be to build a house on top of clay foundation. The class Pedidos is only wrong because it includes the individual request data, it should be a separate class for this.

  • The class Cliente has basically the same error but the name gives a more wrong indication because it seems that it is only one Cliente. It may sound pedantic, but if you give wrong names, it makes it hard to understand what that represents. You may even know, but other people who will read your code will not know. And programming is not doing what you understand but what everyone understands.

  • But how will I know if it is wrong this way that my teacher teaches if I know nothing, I have to do my job using pointers whether or not it is what was asked. Can you give me an example of what would be the right way to do these classes?

  • Researching, studying on their own, looking for alternative sources, developing critical sense. If I have time I’ll do something, but don’t count on it. I’ll be doing the work for you. A good start is the answer to your other question. There it shows how to do it right. Read [tour] and [help], especially [Ask] and [help/on-topic].

  • And what I’m trying to do here by posting my code, trying to learn and research but the vast majority of what I think about c++ and English is a little hard to understand. A single class in the right way you’re not doing my job, you’re just giving me a direction to continue on your own.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.