0
Good morning guys, I have the following problem: I have a class that represents a List, and from it I created three objects (I don’t know which one to give) list1(5), Lista2(5), list3(10).
template<class T>class Lista{
private:
T *itens;
int ultimo, maxTam;
public:
Lista(int maxTam);
void insere(T item);
};
template <class T> Lista<T>::Lista(int maxTam){
this->maxTam = maxTam;
this->itens = new T[maxTam];
this->ultimo = 0;
}
template <class T> void Lista<T>::insere(T item){
if(ultimo == maxTam){
cout<< "Lista Cheia!"<<endl;
}else{
this->itens[this->ultimo] = item;
this->ultimo++;
}
}
int main(){
Lista<int> lista1(5); //esta lista ja está cheia
Lista<int> lista2(5); //esta lista já está cheia
Lista<int> lista3(10); //esta lista deve receber o conteudo das duas listas anteriores intercalados.
return 0;
}
Suppose I’ve filled the first two lists, like copying their items to row 3, passing them as a function parameter? I did the function down, but it doesn’t work.
//Aqui o protótipo do método.
template<class T> T Lista<T>::MisturaListas(Lista<T> *l1, Lista<T> *l2, Lista<T> *l3){
for(int i = 0; i < 5; i++){
l3->insere(l1[i]);
l3->insere(l2[i]);
}
}
//Aqui a parte que fica na classe
T MisturaListas(Lista<T> *l1, Lista<T> *l2, Lista<T> *l3);
//Aqui o main
int main(){
Lista<int> lista1(5); //esta lista ja está cheia
Lista<int> lista2(5); //esta lista já está cheia
Lista<int> lista3(10); //esta lista deve receber o conteudo das duas listas anteriores intercalados.
lista3.MisturaListas<int>(lista1, lista2, lista3);
return 0;
}
I hope you understand, if you can help me,.
Fabio just didn’t understand how this get() method works, because I did a print() method to print the three lists, he printed the first two and returned that the list3 is empty :S.
– CA_93
The method
get()
aims to obtain a particular item from the list and with it you expose the ability to manipulate the list by whoever is using it. It is so because who uses an instance ofLista
does not know that within this class there is an array. This way you promote two important items in POO: low coupling and high cohesion.– Fábio