Independent member function in C ++

0

I'm learning OOP with C ++. I made a program here to train the question of object arrays. My question eh: The program is working, but was it efficient for me to have put the show_dados2 function as independent? I could not develop the logic to put it as a member function of the person class leaving the attributes as private because the function handles 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;
}
    
asked by anonymous 27.08.2017 / 22:15

2 answers

1

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

It would have been better:

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 fail to create two functions with the same name, one of them is global and you need to pass pointers and data per copy, but you can also declare your method as const ( void mostra_dados() const; ). ensures that no variable of its class will have its value modified during the execution of this method.

    
28.08.2017 / 01:04
1

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

An array is different than an element. An array handles the collection and the element handles a particular value. Therefore, the mostra_dados() function is intended to show, or format, the data pertaining to the pessoa class, while mostra_dados2() is intended to show collection data and uses mostra_dados() to format the particular. Maybe the name mostra_dados2() is that you were not very happy (maybe imprimir , might 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 vector . The STL also has several algorithms to facilitate the navigation of these objects, one of them is for_each . Finally, one of the characteristics of C ++ is the ability of overload . You can replace the mostra_dados() function with a overload of the

28.08.2017 / 17:56