2
I have a simple college exercise to mess with the vector. Basically these are the actions that we should do:
cout << "\tMexendo com Vetor\n";
cout << "\n\nEscolha uma das opcoes para fazer com o Vetor:";
cout << "\n01 - Imprimir ";
cout << "\n02 - Adicionar";
cout << "\n03 - Limpar ";
cout << "\n04 - Localizar um numero";
cout << "\n05 - Excluir o numero de uma posicao";
cout << "\n06 - Trocar o numero de posicao";
cout << "\n\n00 - Encerrar o programa";
In option "03 - Clear" you have to clear the whole vector.
For this I would like to call the Constructor within my method, after all it starts my object with the clean vector, doing the same as the chosen option!
My method Clear:
void Vetor::clear_vet () {
while (contador != 0) { del_pos(0); }
}
Currently it keeps calling another method (to delete the position) and keeps deleting the first position until there is no more element (when the counter reaches 0)
The idea I had would be like this:
void Vetor::clear_vet () {
Vetor();
}
my file .hpp
in case you have any doubts:
#ifndef VETOR_HPP
#define VETOR_HPP
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
using namespace std;
class Vetor {
private:
int contador, i;
int vet[10];
public:
Vetor () {
contador = 0;
for (i = 0; i < 10; i++) { vet[i] = 0; }
};
void print ();
void add_vet (float num);
bool del_pos (int pos1);
void clear_vet ();
int loc_num (float num);
bool change_pos (int pos1, int pos2);
bool valid(int pos);
};
#endif // VETOR_HPP
Did not understand, call another method within a method eh wrong? The part that should not be called the Builder again I understood. In the case of the local variable, I see no other way to verify how many elements have already been placed, besides that this variable tb is used in other methods to do the other operations..
– Gui Reis
No, I didn’t say that. You don’t need
i
to find out how many elements have already been used.– Maniero
What would be another way to do it?
– Gui Reis
The way I did.
– Maniero
I get it, obr !!
– Gui Reis