sur 29 points ****Exercice Magma R ******* (sur 6 points) #include class MagmaR { private: int val; public: MagmaR(int pv){ if(pv<0 || pv>2) throw std::invalid_argument("l'argument doit être compris" " entre 0 et 2"); int val=pv; MagmaR operator*(Magmar o) const { if(val<2 && o.val<2) return (o.val+1)%2 if(val<2) return 2; if(o.val<1) return 2; return 1; } int operator==(Magmar o)const {return val==o.val;} int operator!=(Magmar o)const {return val!=o.val;} }; ****Exercice trouver l'erreur static ******* (sur 3 points) Une erreur est produite à la ligne 5 << g(); } >>. On ne peut pas appeler une méthode d'objet (donc non statique) dans une méthode de classe (donc statique). Raison : le compilateur ne sait pas sur quel objet appeler g(). ***** Exercice Constructeur ****** (sur 3 points) Affichage : << Ma ob1{3}; >> affiche "construction MA(sdl)" (sdl :saut de ligne) << Ma ob2{4}; >> affiche "construction MA(sdl)" << ob1.f(); >> affiche "3(sdl)" << ob2.f(); >> affiche "4(sdl)" **** Exercice reference, pointeur et virtuel ***** (sur 4 points) pa->f(); //affiche "fA" pa->g(); //affiche "gB" ra.f(); //affiche "fA" ra.g(); //affiche "gB" ob.f(); //affiche "fB" ob.g(); //affiche "gB" **** Exercice construction en cascade ***** (sur 5 points) Question 1 L1 : "A1 " L2 : rien L3 : "A5 " L4 : rien L5 : rien L6 : rien L7 : "A6 C8 A16 C12 B4 " L8 : rien L9 : "C1 " L10: "~B4 ~C12 ~A16 ~C8 ~A6 " L11: "fin " L12: "~A1 ~C5 ~A1 " Question 2 Si la ligne L8 était decommentée la compilation produirait une erreur car le compilateur ne connait pas de constructeur d'objet A sans paramètre. ****Exercice tableau 2D****** (sur 8 points) #include #include template class Tab{ protected: T* t; unsigned l; // nombre de lignes unsigned c; // nombre de colonnes int ind(unsigned li,unsigned ci) const { return li*c+ci;} public: void check(unsigned li,unsigned ci) const{ if(ci>=c || li>=l) throw -1; } Tab(unsigned lp, unsigned cp, const T& v) { // constructeur l=lp; c=cp; unsigned i; t=static_cast(malloc(sizeof(T)*l*c)); for(i=0;i(){free(t);} // destructeur Tab(const Tab& o){ //constructeur de copie l=o.l; c=o.c; t=static_cast(malloc(sizeof(T)*l*c)); unsigned i; for(i=0;i& operator=(const Tab& o){ // opérateur affectation unsigned i; if(this==&c) return *this; l=o.l; c=o.c; free(t); t=static_cast(malloc(sizeof(T)*l*c)); for(i=0;i(Tab&& o){ // constructeur par déplacement de ressources l=o.l; c=o.c; t=o.t; o.t=nullptr; // Ok même si free est appelé sur o.t o.l=0; o.c=0; } const Tab& operator=(Tab&& o){ // opérateur de déplacement de ressource if(this==&o) return *this; free(t); l=o.l; c=o.c; t=o.t; o.t=nullptr; // Ok même si free est appelé ensuite o.l=0; o.c=0; return *this; } void set(unsigned li,unsigned ci, const T& v){ check(li,ci); t[ind(li,ci)]=v; } const T& get(unsigned li, unsigned ci)const { check(li,ci); return t[ind(li,ci)]; } int operator==(const Tab& o) const { // opérateur de comparaison unsigned i; if(o.l!=l || o.c!=c) return 0; for(i=0;i& o) const { return !this->operator==(o); } }; typedef Tab Tableau2dInt; typedef Tab Tableau2dDouble; class MatriceInt : public Tab { public: MatriceInt(unsigned lp, unsigned cp, int v) : Tab(lp,cp,v){} MatriceInt operator+(const MatriceInt& o) const { if(l!=o.l || c!=o.c) throw -1; MatriceInt r{*this}; unsigned i; for(i=0;i