Correction TD6POO

Texte de la première page
Institut Supérieur d’Informatique
2SIL - 2010/2011
Correction TD6: Généricité
#include<iostream.h>
template<class T=int>
class pile
{
private:
int taille ;
T *tab;
int sommet;
public:
pile(int);
~pile();
pile(const pile<T>&);
bool est_vide();
bool est_pleine();
void empiler(T);
T depiler();
pile<T>& operator=(const pile<T>&);
bool operator==(const pile<T> &);
friend ostream& operator<<(ostream& out , const pile<T>&
Pil)
{
for(int i=0; i<=Pil.sommet;i++)
cout<<Pil.tab[i]<<"|";
return(out);
}
};
template<class T>
pile<T>::pile(int n)
{taille=n;
sommet=-1;
tab=new T[taille];
}
template<class T>
pile<T>::pile(const pile<T>& p)
{
taille=p.taille;
sommet=p.sommet;
for(int i=0;i<=sommet;i++)
tab[i]=p.tab[i];
}
template<class T>
pile<T>::~pile()
Page 1 sur 3
Institut Supérieur d’Informatique
2SIL - 2010/2011
{delete[]tab;}
template<class T>
T pile<T>::depiler()
{
if (!this->est_vide())
return(tab[sommet--]);
}
template<class T>
void pile<T>::empiler(T elm)
{
if(!this->est_pleine())
{
tab[++sommet]=elm;
}
}
template<class T>
bool pile<T>::est_pleine()
{return(sommet==taille-1);}
template<class T>
bool pile<T>::est_vide()
{return(sommet==-1);}
template<class T>
pile<T>& pile<T>::operator=(const pile<T>& Pil)
{
if(this!=&Pil)
{
taille=Pil.taille;
sommet=Pil.sommet;
for(int i=0;i<=sommet;i++)
tab[i]=Pil.tab[i];
}
return(*this);
}
template<class T>
bool pile<T>::operator==(const pile<T>&Pil)
{ if(sommet!=pil.sommet)
return false;
else
{
for(int i=0;i<=sommet;i++)
if (tab[i]!=Pil.tab[i])
return false;
}
return true;
}
Page 2 sur 3
Institut Supérieur d’Informatique
2SIL - 2010/2011
/***********Programme principal
void main()
{
pile<> p(6);
for(int i=0;i<6;i++)
p.empiler(i);
cout<<p;
pile<char> p2(4);
p2.empiler('a');
p2.empiler('b');
p2.empiler('c');
p2.empiler('d');
cout<<p2;
}
Page 3 sur 3
2SIL - 2010/2011
Correction TD6: Généricité
#include<iostream.h>
template<class T=int>
class pile
{
private:
int taille ;
T *tab;
int sommet;
public:
pile(int);
~pile();
pile(const pile<T>&);
bool est_vide();
bool est_pleine();
void empiler(T);
T depiler();
pile<T>& operator=(const pile<T>&);
bool operator==(const pile<T> &);
friend ostream& operator<<(ostream& out , const pile<T>&
Pil)
{
for(int i=0; i<=Pil.sommet;i++)
cout<<Pil.tab[i]<<"|";
return(out);
}
};
template<class T>
pile<T>::pile(int n)
{taille=n;
sommet=-1;
tab=new T[taille];
}
template<class T>
pile<T>::pile(const pile<T>& p)
{
taille=p.taille;
sommet=p.sommet;
for(int i=0;i<=sommet;i++)
tab[i]=p.tab[i];
}
template<class T>
pile<T>::~pile()
Page 1 sur 3
Institut Supérieur d’Informatique
2SIL - 2010/2011
{delete[]tab;}
template<class T>
T pile<T>::depiler()
{
if (!this->est_vide())
return(tab[sommet--]);
}
template<class T>
void pile<T>::empiler(T elm)
{
if(!this->est_pleine())
{
tab[++sommet]=elm;
}
}
template<class T>
bool pile<T>::est_pleine()
{return(sommet==taille-1);}
template<class T>
bool pile<T>::est_vide()
{return(sommet==-1);}
template<class T>
pile<T>& pile<T>::operator=(const pile<T>& Pil)
{
if(this!=&Pil)
{
taille=Pil.taille;
sommet=Pil.sommet;
for(int i=0;i<=sommet;i++)
tab[i]=Pil.tab[i];
}
return(*this);
}
template<class T>
bool pile<T>::operator==(const pile<T>&Pil)
{ if(sommet!=pil.sommet)
return false;
else
{
for(int i=0;i<=sommet;i++)
if (tab[i]!=Pil.tab[i])
return false;
}
return true;
}
Page 2 sur 3
Institut Supérieur d’Informatique
2SIL - 2010/2011
/***********Programme principal
void main()
{
pile<> p(6);
for(int i=0;i<6;i++)
p.empiler(i);
cout<<p;
pile<char> p2(4);
p2.empiler('a');
p2.empiler('b');
p2.empiler('c');
p2.empiler('d');
cout<<p2;
}
Page 3 sur 3
Partager ce document
Télécharger le fichier
Faire un lien vers ce document
Lien permanentpour un message, un forum, un réseau social
Lien courtpour un SMS ou un profil
Code HTMLpour un site ou un blog
Proposés d'après le contenu
Documents similaires












Sur le même sujet
Les mots qui reviennent le plus dans ce document. Chacun lance une recherche dans les fichiers publics.



