Independent member function in C++

Asked

Viewed 173 times

0

I am learning POO with C++. I made a program here to train object arrays question. My question eh: The program is working, but was it efficient that I put the function display2 as independent? I could not develop the logic to place it as a function-member of the class person leaving the attributes as private, because the function deals with more than one object.

EDIT:

#include"stdafx.h"
#include<iostream>
#include<string>
using namespace std;
#define TAMPESSOAS 4

class pessoa
{
private:
    string nome,signo;
    int idade;
public:
    void mostra_dados();
    void pega_dados();
};

void mostra_dados2(pessoa *pessoas, int cont)
{
    int cont2;

    for (cont2 = 0; cont2 < cont; cont2++)
        pessoas[cont2].mostra_dados();
}

int main()
{
    pessoa pessoas[TAMPESSOAS];//Declaração de um vetor do tipo pessoa
    int cont=0;
    char continuar;
    cout << "\nContinuar a pegar dados? 1 para sim: ";
    cin >> continuar;
    while (continuar == '1')
    {
        pessoas[cont].pega_dados();
        cont++;
        cout << "\nContinuar a pegar dados? 1 para sim: ";
        cin >> continuar;
    }
    mostra_dados2(pessoas, cont);
#if WIN32//Só será executado se o SO for windows
    system("PAUSE");
#endif
return 0;
}
void pessoa::pega_dados()
{
    cout << "\nNome: ";
    cin >> nome;
    cout << "\nSigno: ";
    cin >> signo;
    cout << "\nIdade: ";
    cin >> idade;
}
void pessoa::mostra_dados()
{
    cout << "\nNome: " << nome << "\nIdade: " << idade << "\nSigno: " << 
    signo << endl << endl;
}
  • puts the code in the question for future reference.

2 answers

1

Logically not! You created a function to do something you could have done outside of it (which was to use a loop for)

Better would have been:

int main()
{
    pessoa pessoas[TAMPESSOAS];//Declaração de um vetor do tipo pessoa
    int cont=0;
    char continuar;
    cout << "\nContinuar a pegar dados? 1 para sim: ";
    cin >> continuar;
    while (continuar == '1')
    {
        pessoas[cont].pega_dados();
        cont++;
        cout << "\nContinuar a pegar dados? 1 para sim: ";
        cin >> continuar;
    }
    for(int i=0; i<cont; i++)
    {
        pessoas[i].mostra_dados();
    }
    mostra_dados2(pessoas, cont);
#if WIN32//Só será executado se o SO for windows
    system("PAUSE");
#endif
return 0;
}

With this you not only failed to create two functions with the same name, one of which is global and needs to pass pointers and data by copy, but can also declare your method as const (void mostra_dados() const;) which ensures that no variable in its class will have its modified value during the execution of this method.

1

I believe that a program should be super easy to read by a human, so by dividing its program into functions, whose names show which operation is being performed, it makes it easier to read and understand it. That’s why, yes, the right way is this very.

An array is different than an element. An array handles the collection and the element handles a particular value. Thus, the function mostra_dados() is intended to display, or format, data pertaining to the class pessoa, while mostra_dados2() has the objective of showing the collection data and uses mostra_dados() to format the particular. Maybe the name mostra_dados2() is that it was not very happy (maybe imprimir, may be a better name).

Regarding your program, C++, in fact, STL, has several objects that facilitate and increase the robustness of your program. One of them is the vector. STL also has several algorithms to facilitate the navigation of these objects, one of them is the for_each. Finally, one of the characteristics of C++ is the ability to overload. You can replace the function mostra_dados() by a overload of the operator<<.

Thus, your program can be rewritten as follows:

#include"stdafx.h"
#include<iostream>
#include<string>
#include <vector>
#include <algorithm>

using namespace std;
#define TAMPESSOAS 4

class pessoa
{
private:
    string nome,signo;
    int idade;
public:
    friend std::ostream& operator<<(ostream& o, const pessoa &p);
    void pega_dados();
};

template <typename T>
void imprimir(const std::vector<T> &o)
{
    std::for_each(o.begin(), o.end(), [](const T &p){std::cout << p;});
}

int main()
{
    std::vector<pessoa> pessoas;//Declaração de um vetor do tipo pessoa
    pessoas.reserve(TAMPESSOAS);

    char continuar;
    cout << "\nContinuar a pegar dados? 1 para sim: ";
    cin >> continuar;
    while (continuar == '1')
    {
        pessoa p;
        p.pega_dados();
        pessoas.emplace_back(p);
        cout << "\nContinuar a pegar dados? 1 para sim: ";
        cin >> continuar;
    }
    imprimir(pessoas);
#if WIN32//Só será executado se o SO for windows
    system("PAUSE");
#endif
    return 0;
}
void pessoa::pega_dados()
{
    cout << "\nNome: ";
    cin >> nome;
    cout << "\nSigno: ";
    cin >> signo;
    cout << "\nIdade: ";
    cin >> idade;
}

std::ostream& operator<<(ostream& o, const pessoa &p)
{
    cout << "\nNome: " << p.nome << "\nIdade: " << p.idade << "\nSigno: "     << 
    p.signo << endl << endl;
}

Browser other questions tagged

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