1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| #include <iostream>
/*----------------------------------------------------------*/
/* DECLARATION de la classe */
/* ****CONTENEUR**** */
/*----------------------------------------------------------*/
template <class T>
class conteneur
{
private:
int _taille;
int _taille_max;
public:
conteneur(int, int);
~conteneur();
int taille_max() const;
int taille() const;
virtual void affiche(std::ostream &)const=0;
bool est_vide() const;
bool est_plein() const;
};
/*----------------------------------------------------------*/
/* DEFINITION des fonctions membres de la classe */
/* ****CONTENEUR**** */
/*----------------------------------------------------------*/
/*----constructeur---*/
template <class T> conteneur<T>::conteneur(int taille, int max):_taille(taille),_taille_max(max)
{
std::cout<<"Construc. conteneur: "<<this<<std::endl;
};
/*---destructeur---*/
template <class T>conteneur<T>::~conteneur()
{
std::cout<<"Destruc. conteneur: "<<this<<std::endl;
};
template <class T> int conteneur<T>::taille_max() const
{
return _taille_max;
};
template <class T> int conteneur<T>::taille() const
{
return _taille;
};
template <class T> bool conteneur<T>::est_vide() const
{
return (_taille==0);
};
template <class T> bool conteneur<T>::est_plein() const
{
return (_taille==_taille_max);
};
/*----------------------------------------------------------*/
/* DECLARATION de la classe */
/* ****TABLEAU**** */
/*----------------------------------------------------------*/
template <class T>
class tableau:public conteneur<T>
{
private:
T * _tab;
public:
tableau(int taille=10, int max=10, int init=0);
~tableau();
void affiche(std::ostream &)const;
T & operator[](int);
};
/*----------------------------------------------------------*/
/* DEFINITION des fonctions membres de la classe */
/* ****TABLEAU**** */
/*----------------------------------------------------------*/
/*---constructeur---*/
template <class T> tableau<T>::tableau(int taille, int max, int init):conteneur<T>(taille,max), _tab(new T[max])
{
std::cout<<"Construc. tableau: "<<this<<std::endl;
for (int i=0; i<taille;i++)
{
_tab[i]=init;
}
};
/*---destructeur---*/
template <class T> tableau<T>::~tableau()
{
delete[] _tab;
std::cout<<"Destruct. tableau: "<<this<<std::endl;
};
/*---surcharge de l'operateur []---*/
template <class T> T & tableau<T>::operator[](int i)
{
return _tab[i];
};
template <class T> void tableau<T>::affiche(std::ostream & f)const
{
for (int i=0; i<_taille;i++)
f<<_tab[i]<<" | ";
f<<std::endl;
}; |
Partager