ap4 - programmation orientée objet chapitre 2 : introduction au langage c++ c c++ principales...
TRANSCRIPT
![Page 1: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/1.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
C C++
Principales Améliorations :
Ajout du type booléen
![Page 2: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/2.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Programme simple
![Page 3: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/3.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Programme simple
Fichier Interface
![Page 4: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/4.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Programme simple
Fichier Interface
Fichier d’implémentation
![Page 5: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/5.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Programme simple
Fichier Interface
Fichier d’implémentation
Extension :
.h
(Header)
.c
Ou
.cpp
![Page 6: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/6.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Une fonction principale main()
![Page 7: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/7.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Une fonction principale main()
int main(){
;
}
Exemple n°1 :
![Page 8: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/8.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Une fonction principale main()
int main(){
;
}
en tête
Exemple n°1 :
![Page 9: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/9.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Une fonction principale main()
int main(){
;
}
en tête
Équivalent du begin et end en ADA
Exemple n°1 :
![Page 10: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/10.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Une fonction principale main()
int main(){
;
}
en tête
Équivalent du begin et end en ADA } Bloc d’instructions
Exemple n°1 :
![Page 11: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/11.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Une fonction principale main()
int main(){
;
}
en tête
Équivalent du begin et end en ADA
Instruction vide } Bloc d’instructions
Exemple n°1 :
![Page 12: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/12.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Une fonction principale main()
int main(){
;
}
en tête
Équivalent du begin et end en ADA
Instruction vide
} Bloc d’instructions
Quelques règles
Exemple n°1 :
![Page 13: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/13.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
2 méthodes :
![Page 14: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/14.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
2 méthodes :
/* Voila comment ajouter des commentaires à vos programmes */
Cette méthode permet de faire un commentaires sur plusieurs lignes
Exemple n°2 :
![Page 15: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/15.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
2 méthodes :
/* Voila comment ajouter des commentaires à vos programmes */
Cette méthode permet de faire un commentaires sur plusieurs lignes
Cette méthode permet de faire un commentaires sur une seule ligne
int main() // Voila comment
//ajouter des commentaires à
vos //programmes
Exemple n°2 : Exemple n°3 :
![Page 16: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/16.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
char Caractère
int Entier
float Réel
double Réel très précis
void Type rien utilisé pour faire des procédures
bool Booléen (seulement en C++)
![Page 17: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/17.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main()
{
int A, B;
double C;
B=A;
}
En C, pas de déclarations possible après la première instruction
Exemple n°4 :
![Page 18: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/18.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main()
{
int A, B;
double C;
B=A;
}
En C, pas de déclarations possible après la première instruction
En C++, tout est possible !!
int main()
{
int A, B;
B=A;
double C;
}
Exemple n°4 :
Exemple n°5 :
![Page 19: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/19.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Const int A=2;
Exemple n°6 :
![Page 20: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/20.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
A+B Addition
A–B Soustraction
A*B Produit
A/B Division
A%B Modulo
-A Opérateur Unaire
![Page 21: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/21.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Exemple n°7 :
int main()
{
int A, B;
double C;
C = A + B;
A = C + 1;
char D = 'b';
D = D + 1;
}
Possible :
A + B int
C double
Et double > int
![Page 22: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/22.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main()
{
int A, B;
double C;
C = A + B;
A = C + 1;
char D = 'b';
D = D + 1;
}
Possible :
C sera tronqué
Mieux vaut écrire ça :
A = (int)C+ 1;
Exemple n°7 :
![Page 23: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/23.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main()
{
int A, B;
double C;
C = A + B;
A = C + 1;
char D = 'b';
D = D + 1;
}
Possible :
Code ASCII de 'b' = 98
Donc 98 + 1 = 99
99 soit 'c'
Donc D = 99
Exemple n°7 :
![Page 24: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/24.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
== Égal
!= différent
if ( A == B )
{
A++;
C = A;
}
A=(B==2)*4
À évité :
Si B est égale à 2 alors (B==2) vaudra 1 et donc A vaudra 1*4=4
Exemple n°8 :
![Page 25: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/25.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
&& ET logique
|| OU logique
! NON logique
if (A < 1 && B > 3)
{
instructions;
}
Exemple n°9 :
![Page 26: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/26.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
A = B + 3;
B + C = A; INTERDIT!!!
Exemple n°10 :
![Page 27: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/27.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
i = i + 1; i++; ou ++i;
i = i – 1; i--; ou --i;
int i = 3;
int n;
n = ++i – 3; // n vaut 1, i vaut 4
n = i++ - 3; // n vaut 0, i vaut 4
Incrémentation avant affectation
Incrémentation après affectation
Exemple n°11 :
![Page 28: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/28.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
i = i + 2; i += 2;
A = 3 * A; A *= 3;
B = B / 3; B /= 3;
C = C - 2; C -= 2;
Exemple n°12 :
![Page 29: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/29.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
A = A << 1; A *= 2;
A = A >> 1; A /= 2;
~ Complément à 1
& ET logique
| OU logique
^ OU Exclusif logique
![Page 30: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/30.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Catégorie d'opérateurs Opérateurs Assoc
fonction, tableau, membre de structure, pointeur sur un membre de structure
() [] . -> G=>D
opérateurs unaires - ++ -- ! ~ * & sizeof (type) D=>G
multiplication, division, modulo * / % G=>D
addition, soustraction + - G=>D
opérateurs binaires de décalage
<< >> G=>D
opérateurs relationnels < <= > >= G=>D
opérateurs de comparaison == != G=>D
et binaire & G=>D
ou exclusif binaire ^ G=>D
ou binaire | G=>D
et logique && G=>D
ou logique || G=>D
opérateur conditionnel ?: D=>G
opérateurs d'affectation = += -= *= /= %= &= ^= |= <<= >>= D=>G
opérateur virgule , G=>D
![Page 31: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/31.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Un bloc est une suite d'instructions qui est délimité par { et }
{
int X=1;
{
X *= 2;
}
cout << X;
}
Exemple n°13 :
![Page 32: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/32.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Un bloc est une suite d'instructions qui est délimitée par { et }
{
int X=1;
{
X *= 2;
}
cout << X;
}
Bloc "Père"
Exemple n°14 :
![Page 33: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/33.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Un bloc est une suite d'instructions qui est délimité par { et }
{
int X=1;
{
X *= 2;
}
cout << X;
}
Bloc "Fils"
Exemple n°14 :
![Page 34: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/34.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Structure générale : Exemple n°13 :
if (Condition)
{
instructions;
}
else
{
instructions;
}
if (C==0){
cout << "perdu" << endl;C = 1;
}else
cout << "gagné << endl;
Les {} ne sont pas obligatoire si il n'y a qu'une seule instructions dans le bloc
![Page 35: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/35.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Structure générale : Exemple n°13 :
if (Condition)
{
instructions;
}
else
{
instructions;
}
if (C==0){
cout << "perdu" << endl;C = 1;
}else
cout << "gagné << endl;
if (C==0)
i++;
Exemple n°14 :
Le bloc else n'est pas obligatoire si il ne contient pas d'instructions.
![Page 36: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/36.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°15 :
if (i==0)
if (A>10)
{
instructions;
}
else
instructions;
else
instructions;
![Page 37: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/37.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°16 :
int i = 2;
switch (i)
{
case 0 : instructions;
break;
case 1 : instructions;
break;
case 2 : instructions;
break;
default : instructions;
break;
}
Toujour en char ou int
![Page 38: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/38.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°16 :
int i = 2;
switch (i)
{
case 0 : instructions;
break;
case 1 : instructions;
break;
case 2 : instructions;
break;
default : instructions;
break;
}
On termine toujours par break si on ne souhaite pas que les instructions des autres case soit executer .
![Page 39: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/39.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°16 :
int i = 2;
switch (i)
{
case 0 : instructions;
break;
case 1 : instructions;
break;
case 2 : instructions;
break;
default : instructions;
break;
}
Le default n'est pas obligatoire
La valeur de i n'est évaluer qu'une seule fois
![Page 40: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/40.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°17 :
p = i = 1;
do {
p = p * i;
i++;
} while (i != 10);
La répétitive est exécutée au moins une fois
![Page 41: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/41.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°18 :
p = i = 1;
while (i != 10)
{
p *= i;
i++;
}
Pas d'obligation d'exécution
![Page 42: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/42.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°18 :
{
int i, Somme = 0;
for ( i = 1; i <= 10 ; i++ )
{
Somme += i;
}
A = 0;
}
Partie d'initialisation
![Page 43: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/43.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°18 :
{
int i, Somme = 0;
for ( i = 1; i <= 10 ; i++ )
{
Somme += i;
}
A = 0;
}
Partie d'initialisation
Condition de continuité
![Page 44: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/44.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°18 :
{
int i, Somme = 0;
for ( i = 1; i <= 10 ; i++ )
{
Somme += i;
}
A = 0;
}
Partie d'initialisation
Condition de continuité
Partie Incrémentation
![Page 45: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/45.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°18 :
{
int i, Somme = 0;
for ( i = 1; i <= 10 ; i++ )
{
Somme += i;
}
A = 0;
}
1 2 3
4
1 2 34
2 234
![Page 46: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/46.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°19 :
float F1 (int, char, int, float);
main ()
{
A = F1(3,'c',2,3.14);
void F2();
}
Peut s'écrire :
void F2(void);
Mais pas
void F2;
![Page 47: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/47.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°20 :
float F1 (int, char, int, float);
{
instructions;
return A;
}
int Max (int A, int B, int C)
{
int M = (A > B)? A : B;
return (M > C)? M : C;
}
Équivalent à :
if (A > B)
M = A;
else
M = B;
![Page 48: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/48.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Une Fonction peut être appelée que si sa déclaration est accessible
![Page 49: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/49.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°21 :
void F();int A = 0;main(){
int B = 0;float A;F();{
char C;instructions;
}A++;
}void F();{
int C;instructions;
}
Variable Globale
![Page 50: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/50.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°21 :
void F();int A = 0;main(){
int B = 0;float A;F();{
char C;instructions;
}A++;
}void F();{
int C;instructions;
}
Variable Globale
Variables locales à main()
![Page 51: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/51.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°21 :
void F();int A = 0;main(){
int B = 0;float A;F();{
char C;instructions;
}A++;
}void F();{
int C;instructions;
}
Variable Globale
Variables locales à main()
Variables locales à ce bloc
![Page 52: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/52.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°21 :
void F();int A = 0;main(){
int B = 0;float A;F();{
char C;instructions;
}A++;
}void F();{
int C;instructions;
}
Variable Globale
Variables locales à main()
Variables locales à ce bloc
Variables locales à F()
![Page 53: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/53.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°22 :
int A;main (){
instructions;F();F();F();
}void F(void){
int B = 1;static int C = 1;B++;C++;
}
La variable C est statique dans la fonction F()
![Page 54: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/54.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°23 :
void Echange(int A, int B){
int C = A;A = B;B = C;
}int main(){
int X1 = 1, X2 = 2;Echange(X1,X2);instructions;
}
Paramètres formels
Paramètres réels passés par valeur
![Page 55: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/55.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°24 :
void Echange2(const int &A, int &B){
int C = A;A = B;B = C;
}int main(){
int X1 = 1, X2 = 2;Echange2(X1,X2);instructions;
}
Equivalent au mode IN OUT
![Page 56: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/56.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°25 : tst.cpp
#include "lib.h"main(){
int A=1, B=2;int C;C=F(A,B);
}
Exemple n°26 : lib.cpp Exemple n°27 : lib.h
#include "lib.h"int F(int, int);
#include "lib.h"int F(int X1, int X2){
instructions;}Ou
#include "lib.h"
int F(int, int=0);
![Page 57: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/57.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
On remet ça à plus tard
![Page 58: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/58.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°28 :
inligne int Min(int A, int B){
return A<B?A:B;}
-> Récursivité impossible
-> Pas instanciée donc pas de pointeur
-> Le compilateur ne respecte pas forcement ce mot clé!
![Page 59: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/59.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Deux fonctions portant le même nom seront disctinctes :
-> si elles n'ont pas le même nombre de paramètres
-> si le type de paramètres est différents
![Page 60: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/60.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
Exemple n°29 :
int F(){
int C;C=1;return C;
}
Il ne faut pas mettre &C
Il faut éviter de retourner une référence à une variable locale
![Page 61: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/61.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
extern int A;void F1();static void F2 ();void F3();int main;{
F1();F2();
}void F1(){
static int X=1;int Y=1;
}static void F2(){
instructions;}
Déclaration de la variable A
Déclaration de la fonction F1()
Déclaration de F2() mais pas instanciable à partir d'un autre fichier
Déclaration de la fonction F3()
Définition de la fonction F1()
Définition de la fonction F2()
Déclaration et affectation de la variable statique X
Exemple n°30 :
![Page 62: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/62.jpg)
Déclaration de la variable globale A
Déclaration de la variable statique globale B
Déclaration de la variable globale C
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
int A;static int B;int C;void F3();static void F4;void F3(){
instructions;}static void F4(){
instructions;}
Exemple n°31 :
} Définition de la fonction F3()
} Définition de la fonction F4()
![Page 63: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/63.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
#include <stdio.h>int main(void){
int i, som, nbm ;double moy ;int t[20] ;for (i=0 ; i<20 ; i++){
printf ("donnez la note numéro %d : ", i+1) ; scanf ("%d", &t[i]) ;
}for(i=0, som=0 ; i<20 ; i++)
som += t[i] ;moy = som / 20 ;printf ("\n\n moyenne de la classe : %f\n", moy) ;for (i=0, nbm=0 ; i<20 ; i++ )
if (t[i] > moy) nbm++ ;
printf ("%d élèves ont plus de cette moyenne", nbm) ;return 0 ;
}
Exemple n°32 :
![Page 64: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/64.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
L'affectation globale de tableau est impossible en C :
int Tab1[10], Tab2[10];for(int i=0;i<10;i++)
Tab1[i]=1;Tab1 = Tab2;
Interdit car pas géré pas le language C
Un indice peut prendre la forme de n'importe quelle expression arithmétique de type entier (ou caractère, compte tenu des règles de conversion systématique).
Exemple n°33 :
![Page 65: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/65.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
La dimension d'un tableau (son nombre d'éléments) ne peut être qu'une constante ou une expression constante.
Aucun contrôle de "débordement d'indice" n'est mis en place par la plupart des compilateurs.
![Page 66: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/66.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
int tab1[5][3];tab1[i-3][i +j]=1;
Exemple n°34 :
Création d'un tableau de 5 lignes et 3 colonnes
Assignation de la valeur 1 à la cellule placé à la (i-4)ème ligne et la (i+j-1)ème colonne du tableau d'entiers Tab1
![Page 67: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/67.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
int * ad;int n;n = 20;ad = &n;*ad = 30;
Exemple n°35 :
réserve une variable nommée ad comme étant un "pointeur" sur des entiers
cette instruction place dans la variable ad l'adresse de la variable n
affecter à la lvalue *ad la valeur 30
![Page 68: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/68.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
int * ad1, * ad2;int n = 10, p = 20;ad1 = &n; ad2 = &p;ad1 = * ad2 + 2;*ad1 +=3 ;
Exemple n°36 :
Les variables ad1, ad2 et ad sont donc des pointeurs sur des entiers
On place dans ad1 et ad2 les adresses de n et p
affecte à *ad1 la valeur de l'expression : *ad2 + 2
une déclaration telle que:int * ad
réserve un emplacement pour un pointeur sur un entier. Elle ne réserve pas en plus un emplacement pour un tel entier.
![Page 69: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/69.jpg)
Chapitre 2 : Introduction au langage C++
AP4 - Programmation Orientée Objet
*ad++; incrémente la valeur sur laquelle pointe ad
ad++; incrémente l'adresse contenue dans ad de manière qu'elle désigne l'objet suivant
![Page 70: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/70.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Un nom de tableau est un pointeur constant :
int Tab[10];int *Ptr;Ptr=Tab;
Tab:
0 9
![Page 71: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/71.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Un nom de tableau est un pointeur constant :
int Tab[10];int *Ptr;Ptr=Tab;
Tab:
0 9
Ptr
![Page 72: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/72.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
Un nom de tableau est un pointeur constant :
int Tab[10];int *Ptr;Ptr=Tab;
Tab:
0 9
Ptr
![Page 73: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/73.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10];for(int i=0;i<10;i++)
Tab[i]=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
1ère solution :
Tab:
0 9
i
![Page 74: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/74.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10];for(int i=0;i<10;i++)
Tab[i]=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
1ère solution :
Tab:
0 9
0
i
![Page 75: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/75.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10];for(int i=0;i<10;i++)
Tab[i]=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
1
1ère solution :
Tab:
0 9
0
i
![Page 76: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/76.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10];for(int i=0;i<10;i++)
Tab[i]=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
1
1ère solution :
Tab:
0 9
0
i
1
![Page 77: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/77.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10];for(int i=0;i<10;i++)
Tab[i]=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
1 1
1ère solution :
Tab:
0 9
0
i
1
![Page 78: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/78.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10], *Ptr;Ptr=Tab;for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
2ère solution :
Tab:
0 9
Ptr
![Page 79: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/79.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10], *Ptr;Ptr=Tab;for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
2ère solution :
Tab:
0 9
Ptr
![Page 80: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/80.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10], *Ptr;Ptr=Tab;for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
1
2ère solution :
Tab
0 9
0
i
Ptr
![Page 81: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/81.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10], *Ptr;Ptr=Tab;for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
1
2ère solution :
Tab
0 9
0
i
Ptr
1
![Page 82: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/82.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10], *Ptr;Ptr=Tab;for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
1 1
2ère solution :
Tab
0 9
0
i
Ptr
1 2
![Page 83: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/83.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10], *Ptr;Ptr=Tab;for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
1 1 1
2ère solution :
Tab
0 9
0
i
Ptr
1 2
![Page 84: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/84.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[10], *Ptr;Ptr=Tab;for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :
2ère solution :
*Ptr=1; Tab[i]=1; *(Tab+i)=1;
![Page 85: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/85.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[3][4];Int *Ptr;Ptr=Tab;
Tab:
0 3
0
2
Tab[2][3]
Lignes Colonnes
![Page 86: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/86.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[3][4];Int *Ptr;Ptr=Tab;
Tab:
Ptr
0 3
0
2
![Page 87: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/87.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[3][4];Int *Ptr;Ptr=Tab;
Tab:
Ptr
0 3
0
2
![Page 88: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/88.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int Tab[3][4];Int *Ptr;Ptr=Tab;
Tab:
Ptr
0 3
0
2(Tab+1):
(Tab+2):
![Page 89: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/89.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
void Remplir(int T[])// ou void Remplir(int T[10])// ou void Remplir(int *T){
for(int i=0;i<10;i++)T[i]=5;
}int main(){
int Tab[10];Remplir(Tab);
}
a) Tableaux de taille fixe : En pile :
}mainTab
0 9
![Page 90: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/90.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
void Remplir(int T[])// ou void Remplir(int T[10])// ou void Remplir(int *T){
for(int i=0;i<10;i++)T[i]=5;
}int main(){
int Tab[10];Remplir(Tab);
}
a) Tableaux de taille fixe : En pile :
}}
main
Remplir
Tab
0 9
iT
0
![Page 91: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/91.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
void Remplir2(int T[], int Nb){
for(int i=0;i<Nb;i++)T[i]=5;
} int main(){
int Tab[10], N=10;Remplir2(Tab, N);
}
b) Tableaux à nombre d'éléments variable
En pile :
}mainTab
0 N-1
Ajouter un 2ème paramètre : la taille du tableau N
![Page 92: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/92.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
b) Tableaux à nombre d'éléments variable
En pile :
}}
main
Remplir2
Tab
0 N-1
NbT
10
Ajouter un 2ème paramètre : la taille du tableau N
void Remplir2(int T[], int Nb){
for(int i=0;i<Nb;i++)T[i]=5;
} int main(){
int Tab[10], N=10;Remplir2(Tab, N);
}
![Page 93: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/93.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
void Toto(bool T[][15]){
T[1][4]=true;}int main(){
bool Tab[10][15];Toto(Tab);
}
Obligatoire
![Page 94: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/94.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int A=1, B=1, C=1;F(A, B, &C);
}void F(int X, int &Y, int *Z){
X=2;Y=2;*Z=2;
}
Passage par valeur (IN)
Passage par référence (IN/OUT)
Passage par adresse (IN/OUT)
En pile :
}}
main
F
X Y Z
A B C
1 1 1
![Page 95: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/95.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int A=1, B=1, C=1;F(A, B, &C);
}void F(int X, int &Y, int *Z){
X=2;Y=2;*Z=2;
}
Passage par valeur (IN)
Passage par référence (IN/OUT)
Passage par adresse (IN/OUT)
En pile :
}}
main
F
X Y Z
A B C
1 1 1
1
![Page 96: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/96.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int A=1, B=1, C=1;F(A, B, &C);
}void F(int X, int &Y, int *Z){
X=2;Y=2;*Z=2;
}
Passage par valeur (IN)
Passage par référence (IN/OUT)
Passage par adresse (IN/OUT)
En pile :
}}
main
F
X Y Z
A B C
1 1 1
1
![Page 97: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/97.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int A=1, B=1, C=1;F(A, B, &C);
}void F(int X, int &Y, int *Z){
X=2;Y=2;*Z=2;
}
Passage par valeur (IN)
Passage par référence (IN/OUT)
Passage par adresse (IN/OUT)
En pile :
}}
main
F
X Y Z
A B C
1 1 1
1
![Page 98: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/98.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int A=1, B=1, C=1;F(A, B, &C);
}void F(int X, int &Y, int *Z){
X=2;Y=2;*Z=2;
}
Passage par valeur (IN)
Passage par référence (IN/OUT)
Passage par adresse (IN/OUT)
En pile :
}}
main
F
X Y Z
A B C
1 1 1
1
2
![Page 99: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/99.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int A=1, B=1, C=1;F(A, B, &C);
}void F(int X, int &Y, int *Z){
X=2;Y=2;*Z=2;
}
Passage par valeur (IN)
Passage par référence (IN/OUT)
Passage par adresse (IN/OUT)
En pile :
}}
main
F
X Y Z
A B C
1 1 1
1
2
2
![Page 100: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/100.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int A=1, B=1, C=1;F(A, B, &C);
}void F(int X, int &Y, int *Z){
X=2;Y=2;*Z=2;
}
Passage par valeur (IN)
Passage par référence (IN/OUT)
Passage par adresse (IN/OUT)
En pile :
}}
main
F
X Y Z
A B C
1 1 1
1
2
2 2
![Page 101: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/101.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int N;cout << "Nb d'étudiants";cin >> N;//Allocation mémoire d'un tableau de N
entiersint *Tab, *Pi;Pi=(int *)malloc(sizeof(int));Tab=(int *)malloc(N*sizeof(int));if ((Pi != NULL) && (Tab != NULL)){
//instructions;free(Pi);free(Tab);
}}
C'est de l'allocation d'espace mémoire à l'exécution et non à la compilation
En C:
![Page 102: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/102.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int N;cout << "Nb d'étudiants";cin >> N;//Allocation mémoire d'un tableau de N
entiersint *Tab, *Pi;Pi=new int;Tab=new int[N];if ((Pi != NULL) && (Tab != NULL)){
//instructions;delete Pi;delete[] Tab;
}}
C'est de l'allocation d'espace mémoire à l'exécution et non à la compilation
En C ++:
![Page 103: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/103.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int N;cout << "Nb d'étudiants";cin >> N;//Allocation mémoire d'un tableau de N
entiersint *Tab, *Pi;Pi=new int;Tab=new int[N];if ((Pi != NULL) && (Tab != NULL)){
//instructions;delete Pi;delete[] Tab;
}}
C'est de l'allocation d'espace mémoire à l'exécution et non à la compilation
En C ++:
Attention :
Ne pas croiser les allocations/libérations dynamiques entre les instructions C et C++
![Page 104: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/104.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
quand on rencontrera ça on remplacera jusqu'aux bout de la ligne
#define AffChaine(Ch) cout << Ref++ << "—" << Ch << endl#define AffCarac(Car) cout << Ref++ << "—" << Car << endl;
int main (){
char Ref='A';char *Chaine="merci";char Mot[]="beaucoup";char *p=Mot;char *Tab[3]={"ZERO","UN","DEUX"};AffChaine(Chaine);AffCarac(Chaine[23]);;AffChaine(Mot+3);AffCarac(*++p);AffCarac(++*++p);AffChaine(Tab[1]);AffChaine(Tab[2]+1);Tab[0][2]='\Ø';AffChaine (*Tab);retour 0;
}
![Page 105: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/105.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
#define AffChaine(Ch) cout << Ref++ << "—" << Ch << endl#define AffCarac(Car) cout << Ref++ << "—" << Car << endl;
int main (){
char Ref='A';char *Chaine="merci";char Mot[]="beaucoup";char *p=Mot;char *Tab[3]={"ZERO","UN","DEUX"};AffChaine(Chaine);AffCarac(Chaine[23]);;AffChaine(Mot+3);AffCarac(*++p);AffCarac(++*++p);AffChaine(Tab[1]);AffChaine(Tab[2]+1);Tab[0][2]='\Ø';AffChaine (*Tab);retour 0;
}
quand on rencontrera ça on remplacera jusqu'au bout de la ligne
obligatoire car il n'y en pas à la fin du #define
inutile car le ";" est déjà présent a la fin du #define
1
2
3
![Page 106: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/106.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
En Mémoire :
Ref 'A'
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \ØMot mot
+1mot+2
mot+3
p
Tab Z E R O \Ø
U N \Ø
D E U X \Ø
Affichage :
![Page 107: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/107.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
En Mémoire :
Ref 'A' 'B'
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \ØMot mot
+1mot+2
mot+3
p
Tab Z E R O \Ø
U N \Ø
D E U X \Ø
Affichage :
A--merci
![Page 108: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/108.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
En Mémoire :
Ref 'A' 'B'
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \ØMot mot
+1mot+2
mot+3
p
Tab Z E R O \Ø
U N \Ø
D E U X \Ø
Affichage :
A -- merci
B -- r
![Page 109: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/109.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
En Mémoire :
Ref 'A' 'B' 'C'
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \ØMot mot
+1mot+2
mot+3
p
Tab Z E R O \Ø
U N \Ø
D E U X \Ø
Affichage :
A -- merci
B -- r
C -- ucoup
![Page 110: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/110.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
En Mémoire :
Ref 'A' 'B' 'C' 'D'
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \ØMot mot
+1mot+2
mot+3
p
Tab Z E R O \Ø
U N \Ø
D E U X \Ø
Affichage :
A -- merci
B -- r
C -- ucoup
D -- e
![Page 111: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/111.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
En Mémoire :
Ref 'A' 'B' 'C' 'D' 'E'
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \ØMot mot
+1mot+2
mot+3
p
Tab Z E R O \Ø
U N \Ø
D E U X \Ø
Affichage :
A -- merci
B -- r
C -- ucoup
D -- e
E -- b
b
![Page 112: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/112.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
En Mémoire :
Ref 'A' 'B' 'C' 'D' 'E' 'F'
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \ØMot mot
+1mot+2
mot+3
p
Tab Z E R O \Ø
U N \Ø
D E U X \Ø
Affichage :
A -- merci
B -- r
C -- ucoup
D -- e
E -- b
F -- UN
b
![Page 113: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/113.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
En Mémoire :
Ref 'A' 'B' 'C' 'D' 'E' 'F' 'G'
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \ØMot mot
+1mot+2
mot+3
p
Tab Z E R O \Ø
U N \Ø
D E U X \Ø
Affichage :
A -- merci
B -- r
C -- ucoup
D -- e
E -- b
F -- UN
G -- EUX
b
![Page 114: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/114.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
En Mémoire :
Ref 'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H'
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \ØMot mot
+1mot+2
mot+3
p
Tab Z E R O \Ø
U N \Ø
D E U X \Ø
Affichage :
A -- merci
B -- r
C -- ucoup
D -- e
E -- b
F -- UN
G -- EUX
H -- ZE
b
\Ø
![Page 115: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/115.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
-La chaine de caractère se termine par le caractère '\Ø' de code ASCII 0
-Chaque caractère est stocké sur un octet
![Page 116: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/116.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
char p*;p="Toto";while (*p)
cout << *p++;
p
T o t o \Ø
![Page 117: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/117.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
char p*;p="Toto";while (*p)
cout << *p++;
p
T o t o \Ø
![Page 118: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/118.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
char p*;p="Toto";while (*p)
cout << *p++;
p
T o t o \Ø
![Page 119: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/119.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
char p*;p="Toto";while (*p)
cout << *p++;
p
T o t o \Ø
![Page 120: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/120.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
char p*;p="Toto";while (*p)
cout << *p++;
p
T o t o \Ø
code ASCII = 0
![Page 121: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/121.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
char p[20];p="Toto";
char p[20]="Toto";
// ou
char p[20]={'T','o','t','o','\Ø'};char p[]="Toto";
p0 19
![Page 122: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/122.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
char p[20];p="Toto";
char p[20]="Toto";
// ou
char p[20]={'T','o','t','o','\Ø'};char p[]="Toto";
p0 19
p T o t o \Ø
0 19
![Page 123: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/123.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
char *Tab[7]={"lundi","mardi",...}; Tab l u n d i \Ø
m a r d i \Ø
![Page 124: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/124.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
char *Tab[7][9];Tab
0 8
0 l u n d i \Ø
m a r d i \Ø
6
![Page 125: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/125.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
#include <cstring> // en c#include <string.h> // en c++
char Ch[]="Toto";
int lg=strlen(Ch);
strcat(ch1;ch2);
strcat(ch1;ch2;lgmax);
strcmp(ch1;ch2);
strcpy(ch1;ch2);
renvoie la longueur de la chaine
![Page 126: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/126.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
#include <cstring> // en c#include <string.h> // en c++
char Ch[]="Toto";
int lg=strlen(Ch);
strcat(ch1;ch2);
strcat(ch1;ch2;lgmax);
strcmp(ch1;ch2);
strcpy(ch1;ch2);
renvoie la longueur de la chaine
opère la concaténation de la ch2 dans la ch1
![Page 127: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/127.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
#include <cstring> // en c#include <string.h> // en c++
char Ch[]="Toto";
int lg=strlen(Ch);
strcat(ch1;ch2);
strcat(ch1;ch2;lgmax);
strcmp(ch1;ch2);
strcpy(ch1;ch2);
renvoie la longueur de la chaine
opère la concaténation de la ch2 dans la ch1
compare lexicographique ch1 et ch2 et retourne :0 si ch1 et ch2 identiques<0 si ch1 < ch2>0 si ch1 > ch2
![Page 128: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/128.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
#include <cstring> // en c#include <string.h> // en c++
char Ch[]="Toto";
int lg=strlen(Ch);
strcat(ch1;ch2);
strcat(ch1;ch2;lgmax);
strcmp(ch1;ch2);
strcpy(ch1;ch2);
renvoie la longueur de la chaine
opère la concaténation de la ch2 dans la ch1
compare lexicographique ch1 et ch2 et retourne :0 si ch1 et ch2 identiques<0 si ch1 < ch2>0 si ch1 > ch2
copie la ch2 dans la ch1
![Page 129: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/129.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
cout << "qu'elle heure est-il?"; cin >> Heure;
int main(){
int A=12;float B=123.4567;cout << setw(5) << A;
}
Opérateur dans iostream
![Page 130: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/130.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
cout << "qu'elle heure est-il?"; cin >> Heure;
int main(){
int A=12;float B=123.4567;cout << setw(5) << A;
}
Opérateur dans iostream
setw() définie l'espace d'affichagesetprecision() précision après la virgule
![Page 131: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/131.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int main(){
int A;float B;char C;char D[10];printf("bonjour, %d, %f toto %c %s\n",A,B,C,D);scanf("%d %f %c %s", &A, &B, &C, D);
}
spécification de format ici entier réel caractère chaine de caractère
![Page 132: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/132.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
FILE *file; File=fopen("NomPhysique",Mode);fclose(File);//File == NomLogique
Ne pas oublier d'inclure :cstdio en C++
oustdio.h en C
"a" appened ouverture d'un fichier existant pour ajouter à la suite. si le fichier n'existe pas il sera créé
"r" read only
"r+" ouverture en lecture/écriture d'un fichier existant
"w+" création et ouverture en lecture et écriture. Si existant remplacement
"w" création et ouverture en write only. Si existant remplacement
Modes Disponibles
Attention : fopen() retourne NULL si le fichier n'a pu être ouvert
![Page 133: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/133.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int fgetc(File *Flux);int fputc(int c, File *Flux);char * fgets(char*Ch; int n, FILE * Flux);int fputs(const char * Ch, File * Flux);fscanf(File * Flux, char * Ch);fprintf(File * Flux, char * Ch);fwrite(&Client,sizeof(enregistrement),NbElement,File * Flux);fread(&Client,sizeof(enregistrement),NbElement,File * Flux);fseek(FILE * Flux , long Dplcmt, int Origine);rewind(File * Flux);ftell(File * Flux);
Lien pour plus d'infos sur ces fonctions :
http://www.cplusplus.com/ref/cstdio/
![Page 134: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/134.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
int A=1;Tab[10]={1,2,3,4,5,6,7,8,9,10};File * Fich;Fich=fopen("Essai","w");if (Fich){
fwrite(tab,sizeof(int),7,Fich);fwrite(&A,sizeof(int),1,Fich); //fprintf(Fich,"%d",A);fclose(Fich);
}fseek(Fich,Deplacement,Origine);//fonction de positionnementrewind(Fich); //reviens au débutint ftell(Fich); //positionnement dans le fichier par rapport au début
![Page 135: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/135.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
![Page 136: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/136.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
![Page 137: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/137.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
namespace ProjetA{
int i;int j=10;void f(){cout << "lol";};void g();}
int main(){
using namespace ProjetA;j++;
}
![Page 138: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/138.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
#...........
#define LONG 100#define LARG (LONG-10)#define MAX(a,b) ((a)<(b)?(a):(b))
int main(){
int B = 2*LARG}
![Page 139: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/139.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
#undef LONG
#include <. . . .> // Fichiers de base du langage#include ". . . ." // Fichiers autres
![Page 140: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/140.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
#if........#endif#ifdef.......#endif#ifndef......#endif
#if LANGUE==1#define ERREUR"Faute"
#elseif LANGUE==2#define ERREUR"Error"
#endif
"include "Pile.h"
int main(){ F();}
#include "Pile.h"
void F(){ cout << "Salut";}
#ifndef PILE_H #define PILE_H void F();
#endif
test.cpp Pile.cpp Pile.h
![Page 141: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/141.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
struct Client{ char Nom[25]; int Age;};
Indispensable!!
Client UnClient;Client * PtrClient;PtrClient= & UnClient;UnClient.Age=20;PClient->Age=30;//(*PClient).Age=30;
UnClient
Nom
Age
PtrClient
![Page 142: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/142.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
struct Elt{ Client Donnees; Elt *pSuivant;};
pDebut
Client1
![Page 143: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/143.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
struct Elt{ Client Donnees; Elt *pSuivant;};
pDebut
Client1 Client2
![Page 144: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/144.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
struct Elt{ Client Donnees; Elt *pSuivant;};
pDebut
Client1 Client2
![Page 145: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/145.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
struct Elt{ Client Donnees; Elt *pSuivant;};
pDebut
Client1 Client2 Client3
![Page 146: AP4 - Programmation Orientée Objet Chapitre 2 : Introduction au langage C++ C C++ Principales Améliorations : Ajout du type booléen](https://reader036.vdocuments.mx/reader036/viewer/2022070309/551d9dcc497959293b8e5091/html5/thumbnails/146.jpg)
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++
struct Elt{ Client Donnees; Elt *pSuivant;};
pDebut
Client1 Client2 Client3