Objectifs
Synthse
Synthse
Fondamentaux
Fondamentaux
Mthode de
rvision
Mthode de
rvision
Informatique II :
Exemple concret
Exemple concret
Rvisions finales
1. Introduction lInformatique
2. Bases de programmation (procdurale)
Jamila Sam Haroud
c EPFL 20022012
Jamila Sam
Jamila Sam
Informatique II Rvisions 1 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Synthse
Fondamentaux
Mthode de
rvision
Exemple concret
Objectifs
Synthse
traitements
Mthode de
rvision
Algorithme
Traitements
AB
TIO
Porte
Chanes de caractres
Tableaux statiques
Tableaux dynamiques
Structures
Pointeurs
Entres/Sorties
OBJET
attributs
c EPFL 20022012
Jamila Sam
mthodes
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
(vide)
Dtails d
Implmentation
S.D.A.
Donnes
Variables
Interface
(partie visible)
donnes
influencent
AC
S TR
Fondamentaux
Exemple concret
oprent sur
Informatique II Rvisions 2 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
c EPFL 20022012
Jamila Sam
(partie interne/cache)
Informatique II Rvisions 3 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Informatique II Rvisions 3 / 34
Objectifs
Synthse
Fondamentaux
Mthode de
rvision
Exemple concret
c EPFL 20022012
Jamila Sam
Synthse
Fondamentaux
Mthode de
rvision
Exemple concret
Synthse
fondamentaux de la POO
Mthode de
rvision
Exemple concret
2.1
2.2
2.3
2.4
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
c EPFL 20022012
Jamila Sam
Informatique II Rvisions 4 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Synthse
Fondamentaux
Mthode de
rvision
class Rectangle {
public:
double surface() { ... };
...
private:
double hauteur;
double largeur;
}
Exemple concret
de quoi a parle ?
ce que a veut dire ?
lutiliser ?
Jamila Sam
variables
int i;
vector<double> v;
fonctions prototype
double sin(double x);
bool cherche_valeur(Listechainee l, Valeur v);
classes Attributs et prototypes des mthodes
Objectifs
c EPFL 20022012
FONDAMENTAUX
Fondamentaux
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Objectifs
c EPFL 20022012
Jamila Sam
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Informatique II Rvisions 6 / 34
Objectifs
Synthse
Rvisions
Objectifs
Synthse
Fondamentaux
Fondamentaux
Mthode de
rvision
Mthode de
rvision
Exemple concret
Exemple concret
Un exemple concret
Reprenons lun de nos exercices de sries. Il sagit de :
dfinir une collection de figures gomtriques
classes virtuelles
c EPFL 20022012
c EPFL 20022012
Jamila Sam
Jamila Sam
Informatique II Rvisions 7 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Synthse
Fondamentaux
Mthode de
rvision
Exemple concret
Pensons Objet
Objectifs
Synthse
On nous demande :
dimplmenter une collection, Dessin, de figures gomtriques
Objet Figure
attributs : ? et mthodes : affiche()
afficher la description dune Figure
Objet Dessin
attribut: une collection de Figure? et
mthodes : affiche()
afficher les figures de la collection
do dj :
Informatique II Rvisions 8 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Fondamentaux
Mthode de
rvision
Exemple concret
class Figure {
...
void affiche () const;
...
};
c EPFL 20022012
Jamila Sam
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
class Dessin {
// une collection de Figures
...
void affiche () const;
};
c EPFL 20022012
Jamila Sam
Informatique II Rvisions 9 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Synthse
Objectifs
Synthse
Fondamentaux
Fondamentaux
Mthode de
rvision
Mthode de
rvision
Exemple concret
Exemple concret
La classe Cercle
Spcifions un peu plus compltement cette classe :
Tout dabord respectons les principes dune bonne encapsulation :
class Cercle {
void affiche() const {
cout << "Un cercle de rayon " << rayon << endl;
}
double rayon;
};
class Cercle {
public: //DROIT DACCES
void affiche() const {
cout << "Un cercle de rayon " << rayon << endl;
}
private: //DROIT DACCES
double rayon;
};
c EPFL 20022012
c EPFL 20022012
Jamila Sam
Jamila Sam
Informatique II Rvisions 11 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Synthse
Objectifs
Synthse
Fondamentaux
Mthode de
rvision
Exemple concret
Fondamentaux
Mthode de
rvision
class Cercle {
public:
Cercle(double x = 0.0) // CONSTRUCTEUR PAR DEFAUT
: rayon(x) {
cout << "Et hop, un cercle de plus !" << endl;
}
Exemple concret
c EPFL 20022012
Jamila Sam
Jamila Sam
Informatique II Rvisions 13 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Informatique II Rvisions 12 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Synthse
Fondamentaux
Mthode de
rvision
Exemple concret
La classe Dessin
Objectifs
Synthse
Fondamentaux
Mthode de
rvision
Exemple concret
Synthse
Jamila Sam
Informatique II Rvisions 15 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
c EPFL 20022012
COMMENT FAIRE ?
Pointeurs et polymorphisme
Mthode de
rvision
Synthse
Exemple concret
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Mthodes virtuelles
Mthode de
rvision
Jamila Sam
Informatique II Rvisions 16 / 34
Fondamentaux
Exemple concret
c EPFL 20022012
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Fondamentaux
Jamila Sam
c EPFL 20022012
c EPFL 20022012
Jamila Sam
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Informatique II Rvisions 18 / 34
Objectifs
Synthse
Objectifs
Synthse
Fondamentaux
Fondamentaux
Mthode de
rvision
Mthode de
rvision
Exemple concret
Exemple concret
copie doit aussi tre virtuelle pour les mmes raisons que
affiche
};
c EPFL 20022012
c EPFL 20022012
Jamila Sam
Jamila Sam
Informatique II Rvisions 19 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Synthse
La classe Figure
Informatique II Rvisions 20 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Synthse
Fondamentaux
Fondamentaux
Mthode de
rvision
Mthode de
rvision
Exemple concret
Exemple concret
La classe Figure
Au lieu de donner des dfinitions arbitraires affiche et copie,
il faut les dclarer comme virtuelles pures.
Elles nont alors pas de dfinition associe et la classe Figure
devient une classe abstraite
class Figure {
public:
// METHODES VIRTUELLES PURES :
virtual void affiche () const = 0;
virtual Figure* copie() const = 0;
...
};
class Figure {
public:
virtual void affiche () const { ??? }
virtual Figure* copie() const { ??? }
..
};
c EPFL 20022012
Jamila Sam
Jamila Sam
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Informatique II Rvisions 21 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Informatique II Rvisions 22 / 34
Objectifs
Synthse
Objectifs
Synthse
Fondamentaux
Fondamentaux
Mthode de
rvision
Mthode de
rvision
Exemple concret
Exemple concret
c EPFL 20022012
c EPFL 20022012
Jamila Sam
Jamila Sam
Informatique II Rvisions 23 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Synthse
Fondamentaux
Mthode de
rvision
Exemple concret
Mthodes const
Synthse
Mthode de
rvision
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Informatique II Rvisions 25 / 34
Informatique II Rvisions 24 / 34
Classes virtuelles
Passons un autre sujet dlicat, li cette fois lhritage multiple.
Supposons que nous ayons coder une hirarchie de classes
en losange se prsentant comme suit :
A
Jamila Sam
Exemple concret
class Figure {
public:
virtual void affiche () const= 0; // METHODE CONST
virtual Figure* copie() const = 0;
virtual ~Figure() { cout << "Une figure de moins." << endl; }
};
c EPFL 20022012
Fondamentaux
Exemple :
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Jamila Sam
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Informatique II Rvisions 26 / 34
Objectifs
Synthse
La classe A
Objectifs
Synthse
Fondamentaux
Fondamentaux
Mthode de
rvision
Mthode de
rvision
Exemple concret
Exemple concret
class A {
public:
int a;
A(int i) : a(i)
{ cout<< "Creation de A" << endl; }
virtual ~A() {cout << "Destruction de A"<<endl; }
};
c EPFL 20022012
c EPFL 20022012
Jamila Sam
Jamila Sam
Informatique II Rvisions 27 / 34
Objectifs
Synthse
La classe D
Informatique II Rvisions 28 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Objectifs
Synthse
Fondamentaux
Fondamentaux
Mthode de
rvision
Mthode de
rvision
Exemple concret
Exemple concret
Excution
Si lon excute le petit main suivant :
int main ()
{
D x(1,2,3,4);
x.affiche();
return 0;
}
c EPFL 20022012
c EPFL 20022012
Jamila Sam
Jamila Sam
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
class B: public A {
public:
int b;
B(int i, int j)
:A(i), b(j)
{cout<<"Creation de B" <<endl;}
virtual ~B(){cout << "Destruction de B" <<endl;}
void affiche() const{
A::affiche();
cout << "B: " << b <<endl;
}
};
class C: public A {
public:
int c;
C(int i, int j)
:A(i), c(j)
{cout << "Creation de C" << endl;}
virtual ~C(){cout << "Destruction de C"<< endl;}
void affiche() const{
A::affiche();
cout << "C: " << c <<endl;
}
};
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Les classes B et C
Informatique II Rvisions 29 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Cration de
Cration de
Cration de
Cration de
Cration de
A: 0
C: 3
A: 1
B: 2
D: 4
Destruction
Destruction
Destruction
Destruction
Destruction
A
B
A
C
D
de
de
de
de
de
D
C
A
B
A
Informatique II Rvisions 30 / 34
Objectifs
Synthse
Classe A virtuelle
Objectifs
Synthse
Fondamentaux
Mthode de
rvision
Exemple concret
Fondamentaux
Mthode de
rvision
Exemple concret
c EPFL 20022012
c EPFL 20022012
Jamila Sam
Jamila Sam
Informatique II Rvisions 31 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Synthse
Objectifs
Synthse
Fondamentaux
Mthode de
rvision
Mthode de
rvision
Exemple concret
Exemple concret
Fin
A
B
C
D
Bonnes rvisions !
de
de
de
de
D
C
B
A
c EPFL 20022012
c EPFL 20022012
Jamila Sam
Jamila Sam
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Informatique II Rvisions 32 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Fondamentaux
Cration de
Cration de
Cration de
Cration de
A: 1
C: 3
A: 1
B: 2
D: 4
Destruction
Destruction
Destruction
Destruction
Objectifs
Informatique II Rvisions 33 / 34
C O L E P O L Y T E C H N I Q U E
F DR A L E D E L A U S A N N E
Informatique II Rvisions 34 / 34