Appelez un intervenant dès que quelquechose ne semble pas clair dans le sujet ou si, après avoir réfléchi, vous ne trouvez pas la solution.
Utilisez si possible les noms de variables et de fonctions donnés dans les sujets pour rester compatible avec les solutions partielles qui vous seront données.
Créez deux fichiers pour chaque classe, et "déclarez" dans le .H et "définissez" dans le .CPP.
Empêchez systématiquement les éventuelles inclusions multiples, déclarez les fonctions 'public', et déclarez les données 'private' et les fonctions 'const' à chaque fois que c'est possible.
Utilisez les fonctions qui accèdent aux données à chaque fois que c'est possible, plutôt que d'accéder aux données directement.
Créez sur C: un répertoire TP4 et des sous-répertoires pour chaque exercice. N'oubliez pas de recopier tous les fichiers sources sur votre compte à la fin du TP.
// TESTCARR.CPP
#include <iostream.h>
#include <math.h>
#include "carre.cpp" // exercice 1.1
//#include "carre.h" // exercice 1.2
int main()
{
int i=5;
float f=1.2f , prf=1E-6f;
double d=3.1415926535, prd=1E-13;
if (carre(i) != i*i) cout << "ERR";
else cout << "OK";
cout << " : int" << endl;
if ( fabs( carre(f) - f*f ) > prf ) cout << "ERR";
else cout << "OK";
cout << " : float" << endl;
if ( fabs( carre(d) - d*d ) > prd ) cout << "ERR";
else cout << "OK";
cout << " : double" << endl;
return 0;
}
// TESTPOIN.CPP
//
#include <iostream.h>
#include "point.h"
int main()
{
Point<int> p1( -1, -3 ); Point<double> p2( 2.1, 4.3 );
p1.affiche(); p2.affiche();
p1.deplace( -5, 5 ); p2.deplace( 1.5, -1.5 );
p1.affiche(); p2.affiche();
return 0;
}
/*
DEVRAIT AFFICHER : (-1,-3) (2.1,4.3) (-6,2) (3.6,2.8)
*/
// TESTPCOL.CPP
//
#include <iostream.h>
#include "pointcol.h"
int main()
{
Pointcol<int,char> pc1(1,1,'R');
Pointcol<double,int> pc2(2.1,4.3,9);
pc1.affiche(); pc2.affiche();
pc1.colore('G'); pc2.colore(7);
pc1.affiche(); pc2.affiche();
pc1.deplace(-5,5); pc2.deplace(1.5,-1.5);
pc1.affiche(); pc2.affiche();
return 0;
}
/*
DEVRAIT AFFICHER :
R:(1,1) 9:(2.1,4.3) G:(1,1) 7:(2.1,4.3) G:(-4,6) 7:(3.6,2.8)
*/
// TESTPCDI.CPP
//
#include <iostream.h>
#include "pointcol.h"
#include "pcoldiag.h"
int main()
{
Pointcol<double,int> pc1(6.2,8.1,9);
Pcoldiag<int> pcd2( -5 );
pc1.affiche(); pcd2.affiche();
pc1.colore( 7 ); pcd2.colore( 'G' );
pc1.affiche(); pcd2.affiche();
pc1.deplace(1.1,2.2); pcd2.deplace( -5, 5 );
pc1.affiche(); pcd2.affiche();
return 0;
}
/*
DEVRAIT AFFICHER :
9:(6.2,8.1) D:(-5,-5) 7:(6.2,8.1) D:(-5,-5) 7:(7.3,10.3) D:(-10,0)
*/
// TABDYN.H
// De'claration de la classe Tabdyn
#ifndef _TABDYN_H
#define _TABDYN_H
class Tabdyn
{
public:
// Constructeur / Destructeur
Tabdyn( int taille=2 );
~Tabdyn();
// Accesseurs
int taille() const;
int & operator[]( int ) const;
// Autres me'thodes
void agrandit( int );
private:
// Donne'es membres
int * debut;
int nb_elem;
};
#endif
// TABDYN.CPP
// De'finition de la classe Tabdyn
#include <iostream.h>
#include "tabdyn.h"
const int BIDON = 1; // 1e`re case pour indices hors limites
// Constructeur / Destructeur
Tabdyn::Tabdyn( int taille )
{
nb_elem = taille;
if (nb_elem < 1)
nb_elem = 1;
debut = new int[BIDON+nb_elem];
cout << "Tabdyn[" << nb_elem;
if (debut)
cout << "] alloue'." << endl;
else
cout << "] non alloue' !" << endl;
}
Tabdyn::~Tabdyn()
{
delete [] debut;
}
// Accesseurs
int Tabdyn::taille() const
{
return nb_elem;
}
int & Tabdyn::operator[]( int i ) const
{
if ( 0<=i && i<taille() )
return debut[BIDON+i];
cout << " ERR indice=" << i << " : hors limites !" << endl;
return debut[BIDON-BIDON];
}
// Autres me'thodes
void Tabdyn::agrandit( int n=2 )
{
int * tmp = new int[BIDON+taille()+n];
cout << "Tabdyn[" << (taille()+n);
if (tmp)
cout << "] re'alloue'." << endl;
else
cout << "] non re'alloue' !" << endl;
for ( int i=BIDON; i<BIDON+taille(); i++ )
tmp[i] = debut[i];
nb_elem += n;
delete [] debut;
debut = tmp;
}
// TESTDYN.CPP
//
#include <iostream.h>
#include "tabdyn.h"
int main()
{
Tabdyn t(5); Tabdyn u(32000);
int i,j;
if (t.taille()!=5 || u.taille()!=32000)
cout << "ERREUR sur les tailles !" << endl;
else
cout << "Tailles OK." << endl;
for (i=-1; i<=6; i++)
{ t[i] = 100+i; u[i] = 200+i; }
for (j=-1; j<=6; j++)
cout << t[j] << ',' << u[j] << endl;
cout << "----------" << endl;
t.agrandit( 3 );
for (i=-1; i<=6; i++)
t[i] = 100+i;
for (j=-1; j<=6; j++)
cout << t[j] << ", ";
cout << endl;
return 0;
}