Академический Документы
Профессиональный Документы
Культура Документы
Département d’Informatique
Exercice 1 : (5,5pts)
#include <iostream>
using namespace std;
class Machin {
int bidule ;
public :
Machin() { cout <<" ++ Machin defaut " << endl ; bidule = 0; }
Machin(int b) { cout <<" ++ Machin normal " << endl ; bidule = b; }
~Machin() { cout <<" -- Machin normal " << endl ; }
} ;
class Truc {
Machin * machin ; int taille;
public :
Truc(Machin * m, int t) {
cout <<" ++ Truc normal " << endl ;
machin = new Machin[t] ;
for(int i=0; i< t; i++) machin[i].bidule = m[i].bidule;
taille = t;
}
~Truc() { cout << "--Truc normal "<< endl ;
if (machin != NULL) delete machin ;
}
Truc(Truc & truc){
cout <<" ++ Truc clonage " << endl ;
taille = truc.taille; machin = new Machin[taille];
for(int i=0;i<taille;i++) machin[i].bidule = truc.machin[i].bidule;
}} ;
int main() {
Machin *n;
n = new Machin[3];
Truc x(n,3);
Truc y(x);
delete n;
return 0;
}
Exercice 2 : (14,5pts)
Soit le programme C++ suivant:
int main(){
POINT a(7,7), b, c(2), d(c); // creation de deux points a=(7,7) , b=(0,0)
// , c=(2,0) et d=(2,0)
cout<<"a.affichep();cout<<endl; // affichage du point a sous le format : (7,7)
b = a + c; // c = (9,7)
b.affichep(); // (9,7)
cin>>a; // lecture de a
cout<<c; // affichage de c
c += a; // c = c + a;
int k = a > b; // donne 1 si a.abs> b.abs et 0 sinon
if (a>b) cout<<" a est superieur "<<endl;
else cout<<" b est superieur"<<endl;
SEGMENT s1(1,2,6,9); // creation du segment s1 a partir des points (1,2) et (6,9)
s1.affiches(); cout<<endl; // affichage du segment s1
SEGMENT s2(a,b); // creation de s1 a partir des 2 points a et b
SEGMENT S3(s2);
SEGMENT s2(a,b); // creation du segment s2
s2.affiches()<<endl;
// verifier si le point (4,4) appartient au segment s2
if (appartient(POINT(4,4),s2))
cout<<"le point (4,4) appartient au segment s1\n";
else
cout<<"le point (4,4) n’appartient pas au segment s1\n";
cout<<"le nombre de points crees : "<<POINT::nombre_points()<<endl;
cout<<"le nombre de segments crees : "<<SEGMENT::nombre_segments()<<endl;
return 0;
}
Le programme ci-dessus fait appel à deux classes à savoir la classe POINT et la classe SEGMENT.
Donner les fichiers interfaces ".h" et implémentations ".cpp" correspondant aux classes
POINT et SEGMENT pour que le programme tourne correctement. Après exécution le pro-
gramme affichera le résultat ci-dessous.