







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Partie 1 Patrons de classe et de fonctions
template
#include "vectgen.h" #include
using namespace std ; main() { vect
int i ; for (i=0 ; i<nelem ; i++) adr[i] = 0 ;
template <class T, int n> class vect { T v [n] ; // vecteur de n elements de type T public : T & operator [] (int) ; // operateur d'acces a un element } ; template <class T, int n> T & vect<T,n>::operator [] (int i) { if ( (i<0) || (i>n) ) i = 0 ; // protection indice hors limites return v [i] ; }
#include "vectgen1.h" #include
{ c.affiche () ; cout << " rayon : " << r ; } } ;
cercle (T abs, T ord, U ray) : c(abs, ord), r(ray) { }
// définition du patron de fonctions template
donnez un entier : 1 catch entier - n = 1 suite et fin du programme
donnez un entier : 2 catch flottant - x = 2
donnez un entier : 3 abnormal program termination /* ou quelque chose de semblable */
exception erreur : 999 suite exception erreur_d : 999 12
class er_compos { } ; class er_constr { public : int num ; er_constr (int n) { num = n ; } } ; class point { int x, y ; public : point (int abs, int ord) { if (abs==ord) { er_compos e;
throw e ; } x=abs ; y=ord ; } point () { er_constr e(0); throw e ; } point (int abs) { er_constr e(1); throw e ; } } ; Voici un exemple de programme qui se contente de signaler les exceptions interceptées en interrompant l’exécution. On notera que les trois constructions proposées provoquent effectivement une exception qui, au bout du compte, interrompt le programme. Autrement dit, pour percevoir l’effet de la seconde ou de la troisième, il faudrait la placer avant les autres. #include
} int & vect::operator [] (int i) { if (i<0 || i>nelem) { vect_limite l(i) ; // anomalie throw l ; } return adr [i] ; // fonctionnement normal } /* test exception */ main () { try { vect v(-3) ; // provoque l'exception vect_creation v[11] = 5 ; // provoquerait l'exception vect_limite } catch (vect_limite l) { cout << "exception indice " << l.hors << " hors limites \n" ; exit (-1) ; } catch (vect_creation c) { cout << "exception creation vect nb elem = " << c.nb << "\n" ; exit (-1) ; } } exception creation vect nb elem = -
exception indice 11 hors limites Remarques
#include
0 1 2 3 4 5 exception out of range
#include
Je suis un point coloré mes coordonnées sont : 8 6 et ma couleur est : 2