Upload
thea
View
47
Download
0
Embed Size (px)
DESCRIPTION
Programmation Orienté Objet en C++. Ricard julien. Organisation de la matiné. Pointeur This Correcton TP Arbre – Pointeur this Entrées - Sorties TP Entrée Sortie : Création d’un image… Les 7 séances prochaines : PROJET CPP. Pointeur This. Pointeur This : - PowerPoint PPT Presentation
Citation preview
http://liris.cnrs.fr/julien.ricard/
Programmation Orienté Objet en C++
Ricard julien
Programmation Orienté Objet en C++ 2
Organisation de la matiné
Pointeur This
Correcton TP Arbre – Pointeur this
Entrées - Sorties
TP Entrée Sortie : Création d’un image…
Les 7 séances prochaines :PROJET CPP
Programmation Orienté Objet en C++ 3
Pointeur This
Pointeur This :
Dans l’écriture des fonctions membres, il est possible d’accéder à l’adresse de la variable classe, par l’intermédiaire du mot clé « this ».
Objet :Attributs :
int a;Méthodes Membres :
Constructeur Descructeurvoid affichage();Autre méthodes {
this->affichage();if(this==NULL)….
}
main()0bjet* A=new Objet();A->a=12;A->affichage();
}
Programmation Orienté Objet en C++ 4
Corretion TP Arbre - Pointeur Thisclass Arbre {
public :
Arbre(int v=0, Arbre* fg=NULL, Arbre* fd=NULL);
~Arbre();
Arbre* ajout(int v);
int maximum();
Arbre* supprimer_max();
Arbre* supression(int v);
void parcours_infixe();
int tri_parcours_infixe(int* tabT, int id=0);
void Aff(int i=0);
private :
Arbre* FG; Arbre* FD; int val;
};
Programmation Orienté Objet en C++ 5
Main
Arbre* A=NULL;
for(i=0;i<N;i++) A=A->ajout(rand()%30);
A->Aff();
A->parcours_infixe();
int* tab= new int[N];
A->tri_parcours_infixe(tab);
for(i=0;i<N;i++){
A=A->supression(tab[i]);
A->Aff();
}
int *tab= new int [N];
for(i=0;i<N;i++)
tab[i]=rand()%30;
int* tabT=tri_tab(tab,N);
Programmation Orienté Objet en C++ 6
Constructeur destructeurArbre::Arbre(int v,Arbre* fg, Arbre* fd){
FD=fd;
FG=fg;
val=v;
}
Arbre::~Arbre(){
if(this!=NULL){
if(FD!=NULL) delete FD;
if(FG!=NULL) delete FG;
}
}
Programmation Orienté Objet en C++ 7
AjoutArbre* Arbre::ajout(int e){
if(this==NULL){
return (new Arbre(e));
} else
if(e <val)
FG=FG->ajout(e);
else
FD=FD->ajout(e);
return this;
}
Programmation Orienté Objet en C++ 8
Affichagevoid Arbre::Aff(int i){
if(this==NULL)
cout << "{}" ;
else {
cout <<"{" << val <<",";
FG->Aff(i+1);
cout << "," ;
FD->Aff(i+1);
cout << "}";
}
if(i==0)
cout<< endl;
}
Programmation Orienté Objet en C++ 9
SuppressionArbre* Arbre::supression(int e){
if( this==NULL) return NULL;
if(e==val)
if((FG==NULL)&&(FD==NULL))
delete this;
return NULL;
if(FG==NULL)
Arbre* tmp=FD;
FG=NULL; FD=NULL; delete this;
return tmp;
if(FD==NULL)
Arbre* tmp=FG;
FG=NULL; FD=NULL; delete this;
return tmp;
val=FD->maximum();
FD=FD->supprimer_max();
return this;
else
if( val<e) FG=FG->supression(e);
if( val>e) FD=FD->supression(e);
return this;
}
Programmation Orienté Objet en C++ 10
Entrée Sortie
Entrée sortie standart Iostream regroupe istream et ostream
Classe ostreamSortie standart : cout et surcharge l’opérateur << Autre fonctions:
cout.put(char c); cout.write(char* tabC, int n); cout.flush();
Programmation Orienté Objet en C++ 11
istream
Classe istreamEntrées strandart : cin et surcharge de >> Autre fonctions :
int cin.get(); ou cin.get(char &c);Int cin.peek();Cin.get(char*ch,int n, cahr delin =‘\n’);Cin.read(char*ch,int n, cahr delin =‘\n’);Int cin.gcount();Cin.flush();
Programmation Orienté Objet en C++ 12
Les fichiers
Classe fstream regroupe ofstream et ifstream ofstream fichierSortie(’’nom_fichier.txt’’, ios::out);
if(!fichierSortie) cout << ’’Problème d’ouverture ’’;
fichierSortie << ’’On écrit ce que l’on veux’’;
char tab[100];
fichierSortie.write(tab,100);
fichierSortie.close();
ifstream fichierEntree(’’nom_fichier.txt’’, ios::out);
if(! fichierEntree) cout << ’’Problème d’ouverture ’’;
char tmp[1024];
fichierEntree >> tmp;
char tab[100];
fichierEntree .read(tab,100);
fichierEntree.close();
Programmation Orienté Objet en C++ 13
Option d’ouverture
ios::out : ouverture en écriture
ios::in : ouverture en lecture
ios::ate : possitionnement en fin de fichier
ios::app : ouverture en allongement
ios::trunc: perte de l’ancien contenu
ios::binary: binaire