Vous êtes sur la page 1sur 146

Chapitre 2 : Introduction au langage C++

Principales Améliorations :

Ajout du type booléen

C C++

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Programme
simple

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Fichier
Interface

Programme
simple

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Fichier
Interface

Programme
simple

Fichier
d’implémentation

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Extension :

Fichier .h
Interface
(Header)
Programme
simple

.c
Fichier Ou
d’implémentation
.cpp

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Une fonction principale main()

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Une fonction principale main()

Exemple n°1 :

int main()
{

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Une fonction principale main()

Exemple n°1 :

int main() en tête


{

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Une fonction principale main()

Exemple n°1 :

int main() en tête


{
Équivalent
du begin et
end en ;
ADA
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Une fonction principale main()

Exemple n°1 :

int main() en tête

}
{
Équivalent
du begin et Bloc d’instructions
end en ;
ADA
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Une fonction principale main()

Exemple n°1 :

int main() en tête

}
{
Équivalent
du begin et Bloc d’instructions
end en ;
ADA
Instruction
} vide

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Une fonction principale main()

Exemple n°1 :

int main() en tête

}
{
Équivalent
du begin et Bloc d’instructions
end en ;
ADA
}
Instruction
vide

Quelques règles

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

2 méthodes :

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

2 méthodes :

Exemple n°2 :

/* Voila comment ajouter


des commentaires à vos
programmes */

Cette méthode permet de


faire un commentaires sur
plusieurs lignes

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

2 méthodes :

Exemple n°2 :
Exemple n°3 :

/* Voila comment ajouter int main() // Voila comment


des commentaires à vos
programmes */ //ajouter des commentaires à
vos //programmes

Cette méthode permet de Cette méthode permet de


faire un commentaires sur faire un commentaires sur
plusieurs lignes une seule ligne

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++)

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°4 :

int main()
{
int A, B; En C, pas de déclarations possible
double C; après la première instruction
B=A;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°4 :

int main()
{
int A, B; En C, pas de déclarations possible
double C; après la première instruction
B=A;
}

Exemple n°5 :

int main()
{
int A, B;
En C++, tout est possible !!
B=A;
double C;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°6 :

Const int A=2;

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

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°7 :

int main() Possible :


{
A+B int
int A, B;
double C; C double
C = A + B; Et double > int
A = C + 1;
char D = 'b';
D = D + 1;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°7 :

int main() Possible :


{
C sera tronqué
int A, B;
double C; Mieux vaut écrire ça :
C = A + B; A = (int)C+ 1;

A = C + 1;
char D = 'b';
D = D + 1;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°7 :

int main() Possible :


{ Code ASCII de 'b' = 98
int A, B; Donc 98 + 1 = 99
double C; 99 soit 'c'
C = A + B; Donc D = 99
A = C + 1;
char D = 'b';
D = D + 1;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

== Égal
!=
différent
Exemple n°8 :

if ( A == B ) À évité :
{
Si B est égale à 2 alors
A++;
(B==2) vaudra 1 et donc A
C = A; vaudra 1*4=4
}
A=(B==2)*4

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

&& ET logique
|| OU logique
! NON logique

Exemple n°9 :
if (A < 1 && B > 3)
{
instructions;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°10 :

A = B + 3;
B + C = A; INTERDIT!!!

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

i = i + 1;  i++; ou ++i;
i = i – 1;  i--; ou --i;
Exemple n°11 :

int i = 3; Incrémentation avant


affectation
int n;
n = ++i – 3; // n vaut 1, i vaut 4 Incrémentation après
n = i++ - 3; // n vaut 0, i vaut 4 affectation

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

i = i + 2;  i += 2;
Exemple n°12 :
A = 3 * A;  A *= 3;
B = B / 3;  B /= 3;
C = C - 2;  C -= 2;

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

A = A << 1; A *= 2;
A = A >> 1; A /= 2;
~ Complément à 1
& ET logique
| OU logique
^ OU Exclusif logique

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Catégorie d'opérateurs Opérateurs Assoc


fonction, tableau, membre de structure,
() [] . -> G=>D
pointeur sur un membre de structure
opérateurs unaires - ++ -- ! ~ * & sizeof (type) D=>G
multiplication, division, modulo * / % G=>D
addition, soustraction + - G=>D
opérateurs binaires
<< >> G=>D
de décalage
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
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++

Un bloc est une suite d'instructions qui est délimité par { et }

Exemple n°13 :

{
int X=1;
{
X *= 2;
}
cout << X;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Un bloc est une suite d'instructions qui est délimitée par { et }

Exemple n°14 :

{
int X=1;
{
X *= 2; Bloc "Père"
}
cout << X;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Un bloc est une suite d'instructions qui est délimité par { et }

Exemple n°14 :

{
int X=1;
{
X *= 2; Bloc "Fils"
}
cout << X;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Structure générale : Exemple n°13 :

if (Condition) if (C==0)
{ {
cout << "perdu" << endl;
instructions; C = 1;
} }
else
else cout << "gagné << endl;
{
instructions;
}

Les {} ne sont pas obligatoire si il n'y


a qu'une seule instructions dans le
bloc
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++

Structure générale : Exemple n°13 :

if (Condition) if (C==0)
{ {
cout << "perdu" << endl;
instructions; C = 1;
} }
else
else cout << "gagné << endl;
{
instructions; Exemple n°14 :

}
if (C==0)
i++;
Le bloc else n'est pas obligatoire si il
ne contient pas d'instructions.

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°15 :

if (i==0)
if (A>10)
{
instructions;
}
else
instructions;
else
instructions;

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°16 :

int i = 2; Toujour en char ou int


switch (i)
{
case 0 : instructions;
break;
case 1 : instructions;
break;
case 2 : instructions;
break;
default : instructions;
break;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°16 :

int i = 2;
switch (i)
{
case 0 : instructions;
break;
On termine toujours par break si on ne
case 1 : instructions; souhaite pas que les instructions des
break; autres case soit executer .
case 2 : instructions;
break;
default : instructions;
break;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°16 :

int i = 2;
switch (i)
{ Le default n'est pas obligatoire
case 0 : instructions;
break;
case 1 : instructions;
break;
case 2 : instructions;
La valeur de i n'est
break; évaluer qu'une seule fois
default : instructions;
break;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°17 :

p = i = 1;
do { La répétitive est exécutée
p = p * i; au moins une fois
i++;
} while (i != 10);

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°18 :

p = i = 1;
while (i != 10)
Pas d'obligation d'exécution
{
p *= i;
i++;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°18 :

{ Partie d'initialisation
int i, Somme = 0;
for ( i = 1; i <= 10 ; i++ )
{
Somme += i;
}
A = 0;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°18 :

{ Partie d'initialisation
int i, Somme = 0;
for ( i = 1; i <= 10 ; i++ )
{
Somme += i;
}
A = 0;
}

Condition de continuité

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°18 :

{ Partie d'initialisation
int i, Somme = 0;
for ( i = 1; i <= 10 ; i++ ) Partie Incrémentation
{
Somme += i;
}
A = 0;
}

Condition de continuité

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°18 :

{
int i, Somme = 0; 1 2 4 3
for ( i = 1; i <= 10 ; i++ )
{ 1 2 3 2 4 3 2
Somme += i;
} 4
A = 0;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°19 :

float F1 (int, char, int, float); Peut s'écrire :


main () void F2(void);
{
Mais pas
A = F1(3,'c',2,3.14);
void F2;
void F2();
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°20 :

float F1 (int, char, int, float); Équivalent à :


{ if (A > B)
instructions; M = A;
return A; else
} M = B;
int Max (int A, int B, int C)
{
int M = (A > B)? A : B;
return (M > C)? M : C;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Une Fonction peut être appelée que si sa déclaration est accessible

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°21 :

void F();
int A = 0; Variable Globale
main()
{
int B = 0;
float A;
F();
{
char C;
instructions;
}
A++;
}
void F();
{
int C;
instructions;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°21 :

void F();
int A = 0; Variable Globale
main()
{
int B = 0; Variables locales à main()
float A;
F();
{
char C;
instructions;
}
A++;
}
void F();
{
int C;
instructions;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°21 :

void F();
int A = 0; Variable Globale
main()
{
int B = 0; Variables locales à main()
float A;
F();
{
char C;
Variables locales à ce bloc
instructions;
}
A++;
}
void F();
{
int C;
instructions;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°21 :

void F();
int A = 0; Variable Globale
main()
{
int B = 0; Variables locales à main()
float A;
F();
{
char C;
Variables locales à ce bloc
instructions;
}
A++;
}
void F();
{
Variables locales à F()
int C;
instructions;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°22 :

int A;
main ()
{
instructions;
F();
F();
F();
}
void F(void)
{
int B = 1;
static int C = 1;
La variable C est statique dans la
B++; fonction F()
C++;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°23 :

void Echange(int A, int B)


{ Paramètres formels
int C = A;
A = B;
B = C;
}
int main()
{
int X1 = 1, X2 = 2;
Echange(X1,X2); Paramètres réels passés par valeur
instructions;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°24 :

void Echange2(const int &A, int &B) Equivalent au mode IN OUT


{
int C = A;
A = B;
B = C;
}
int main()
{
int X1 = 1, X2 = 2;
Echange2(X1,X2);
instructions;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°25 : tst.cpp Exemple n°26 : lib.cpp Exemple n°27 : lib.h

#include "lib.h" #include "lib.h" #include "lib.h"


main() int F(int, int); int F(int X1, int X2)
{ {
instructions;
int A=1, B=2; Ou }
int C;
C=F(A,B); #include "lib.h"
} int F(int, int=0);

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

On remet ça à plus tard 

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°28 :
-> Récursivité impossible
inligne int Min(int A, int B)
{ -> Pas instanciée donc pas de
return A<B?A:B; pointeur
}
-> Le compilateur ne respecte pas
forcement ce mot clé!

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

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

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Il faut éviter de retourner une référence à une variable locale

Exemple n°29 :

int F()
{ Il ne faut pas mettre &C
int C;
C=1;
return C;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°30 :

extern int A; Déclaration de la variable A


void F1();
static void F2 (); Déclaration de la fonction F1()
void F3();
int main; Déclaration de F2() mais pas
{ instanciable à partir d'un autre fichier
F1();
F2(); Déclaration de la fonction F3()
}
Déclaration et affectation de la variable statique X
void F1()
{ Définition de la fonction F1()
static int X=1;
int Y=1; Définition de la fonction F2()
}
static void F2()
{
instructions;
AP4 - }
Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++

Exemple n°31 :

int A; Déclaration de la variable globale A


static int B;
int C; Déclaration de la variable statique globale B
void F3();
static void F4; Déclaration de la variable globale C

}
void F3()
{ Définition de la fonction F3()
instructions;
}

}
static void F4()
{ Définition de la fonction F4()
instructions;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°32 :

#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 ;
}
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++

L'affectation globale de tableau est impossible en C :


Exemple n°33 :

int Tab1[10], Tab2[10];


for(int i=0;i<10;i++)
Tab1[i]=1; Interdit car pas géré pas le language C
Tab1 = Tab2;

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).

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

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.

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°34 :

int tab1[5][3]; Création d'un tableau de 5 lignes et 3 colonnes


tab1[i-3][i +j]=1;
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

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°35 :

int * ad; réserve une variable nommée ad comme étant un


int n; "pointeur" sur des entiers
n = 20;
ad = &n; cette instruction place dans la variable ad l'adresse de
*ad = 30;
la variable n

affecter à la lvalue *ad la valeur 30

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Exemple n°36 :

int * ad1, * ad2; Les variables ad1, ad2 et ad sont donc des pointeurs
int n = 10, p = 20; sur des entiers
ad1 = &n; ad2 = &p;
ad1 = * ad2 + 2; On place dans ad1 et ad2 les adresses de n et p
*ad1 +=3 ;
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.

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

*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

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Un nom de tableau est un pointeur constant :

int Tab[10];
0 9
int *Ptr; Tab:
Ptr=Tab;

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Un nom de tableau est un pointeur constant :

int Tab[10];
0 9
int *Ptr; Tab
Ptr=Tab; :
Ptr

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Un nom de tableau est un pointeur constant :

int Tab[10];
0 9
int *Ptr; Tab
Ptr=Tab; :
Ptr

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


1ère solution :
int Tab[10];
for(int i=0;i<10;i++)
0 9
Tab[i]=1; Tab
:
i

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


1ère solution :
int Tab[10];
for(int i=0;i<10;i++)
0 9
Tab[i]=1; Tab
:
i
0

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


1ère solution :
int Tab[10];
for(int i=0;i<10;i++)
0 9
Tab[i]=1; Tab
: 1
i
0

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


1ère solution :
int Tab[10];
for(int i=0;i<10;i++)
0 9
Tab[i]=1; Tab
: 1
i
0 1

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


1ère solution :
int Tab[10];
for(int i=0;i<10;i++)
0 9
Tab[i]=1; Tab
: 11
i
0 1

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


2ère solution :
int Tab[10], *Ptr;
Ptr=Tab;
0 9
for(int i=0;i<10;i++,Ptr++) Tab
*Ptr=1; :

Ptr

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


2ère solution :
int Tab[10], *Ptr;
Ptr=Tab;
0 9
for(int i=0;i<10;i++,Ptr++) Tab
*Ptr=1; :

Ptr

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


2ère solution :
int Tab[10], *Ptr;
Ptr=Tab;
0 9
for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Tab
1
i
0

Ptr

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


2ère solution :
int Tab[10], *Ptr;
Ptr=Tab;
0 9
for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Tab
1
i
0 1

Ptr

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


2ère solution :
int Tab[10], *Ptr;
Ptr=Tab;
0 9
for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Tab
11
i
0 1 2

Ptr

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


2ère solution :
int Tab[10], *Ptr;
Ptr=Tab;
0 9
for(int i=0;i<10;i++,Ptr++)
*Ptr=1;
Tab
111
i
0 1 2

Ptr

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

Rédiger un pgrm qui définit et remplit un tableau de 10 entiers avec la valeur 1 :


2ère solution :
int Tab[10], *Ptr;
Ptr=Tab;
for(int i=0;i<10;i++,Ptr++) *Ptr=1;  Tab[i]=1;  *(Tab+i)=1;
*Ptr=1;

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

int Tab[3][4]; 0 3
Int *Ptr; Tab
:
Ptr=Tab;
0
Lignes Colonnes
2
Tab[2][3]

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

int Tab[3][4]; 0 3
Int *Ptr; Tab
:
Ptr=Tab;
Ptr
0

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

int Tab[3][4];
Tab
0 3
Int *Ptr;
:
Ptr=Tab;
Ptr
0

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

int Tab[3][4];
Tab
0 3
Int *Ptr;
:
Ptr=Tab;
Ptr
0
(Tab+1)
:
(Tab+2)
2
:

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

a) Tableaux de taille fixe : En pile :

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()
{ Tab
int Tab[10]; main
Remplir(Tab);
} 0 9

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

a) Tableaux de taille fixe : En pile :

void Remplir(int T[])

}
// ou void Remplir(int T[10])
// ou void Remplir(int *T) T i
{ Remplir
for(int i=0;i<10;i++) 0
T[i]=5;
}

}
int main()
{ Tab
int Tab[10]; main
Remplir(Tab);
} 0 9

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

b) Tableaux à nombre d'éléments variable


Ajouter un 2ème paramètre : la taille du tableau N En pile :
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); Tab
main
}
N-
0 1

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

b) Tableaux à nombre d'éléments variable


Ajouter un 2ème paramètre : la taille du tableau N En pile :
void Remplir2(int T[], int Nb)

}
{
for(int i=0;i<Nb;i++) T Nb
Remplir2
T[i]=5;
10
}
int main()

}
{
int Tab[10], N=10;
Remplir2(Tab, N); Tab
main
}
N-
0 1

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

void Toto(bool T[][15]) Obligatoire


{
T[1][4]=true;
}
int main()
{
bool Tab[10][15];
Toto(Tab);
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En pile :
int main()
{
int A=1, B=1, C=1;
F(A, B, &C);

}
}
void F(int X, int &Y, int *Z)
X Y Z
{
X=2; F
Y=2;
*Z=2;
}

}
Passage par valeur (IN)
A B C
Passage par référence (IN/OUT) main
1 1 1
Passage par adresse (IN/OUT)

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En pile :
int main()
{
int A=1, B=1, C=1;
F(A, B, &C);

}
}
void F(int X, int &Y, int *Z)
X Y Z
{
X=2; 1 F
Y=2;
*Z=2;
}

}
Passage par valeur (IN)
A B C
Passage par référence (IN/OUT) main
1 1 1
Passage par adresse (IN/OUT)

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En pile :
int main()
{
int A=1, B=1, C=1;
F(A, B, &C);

}
}
void F(int X, int &Y, int *Z)
X Y Z
{
X=2; 1 F
Y=2;
*Z=2;
}

}
Passage par valeur (IN)
A B C
Passage par référence (IN/OUT) main
1 1 1
Passage par adresse (IN/OUT)

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En pile :
int main()
{
int A=1, B=1, C=1;
F(A, B, &C);

}
}
void F(int X, int &Y, int *Z)
X Y Z
{
X=2; 1 F
Y=2;
*Z=2;
}

}
Passage par valeur (IN)
A B C
Passage par référence (IN/OUT) main
1 1 1
Passage par adresse (IN/OUT)

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En pile :
int main()
{
int A=1, B=1, C=1;
F(A, B, &C);

}
}
void F(int X, int &Y, int *Z)
X Y Z
{
X=2; 1 F
Y=2;
2
*Z=2;
}

}
Passage par valeur (IN)
A B C
Passage par référence (IN/OUT) main
1 1 1
Passage par adresse (IN/OUT)

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En pile :
int main()
{
int A=1, B=1, C=1;
F(A, B, &C);

}
}
void F(int X, int &Y, int *Z)
X Y Z
{
X=2; 1 F
Y=2;
2
*Z=2;
}

}
Passage par valeur (IN)
A B C
Passage par référence (IN/OUT) main
1 1 2 1
Passage par adresse (IN/OUT)

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En pile :
int main()
{
int A=1, B=1, C=1;
F(A, B, &C);

}
}
void F(int X, int &Y, int *Z)
X Y Z
{
X=2; 1 F
Y=2;
2
*Z=2;
}

}
Passage par valeur (IN)
A B C
Passage par référence (IN/OUT) main
1 1 2 1 2
Passage par adresse (IN/OUT)

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

C'est de l'allocation d'espace mémoire à l'exécution et non à la compilation

En C:
int main()
{
int N;
cout << "Nb d'étudiants";
cin >> N;
//Allocation mémoire d'un tableau de N
entiers
int *Tab, *Pi;
Pi=(int *)malloc(sizeof(int));
Tab=(int *)malloc(N*sizeof(int));
if ((Pi != NULL) && (Tab != NULL))
{
//instructions;
free(Pi);
free(Tab);
}
}
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++

C'est de l'allocation d'espace mémoire à l'exécution et non à la compilation

En C ++:
int main()
{
int N;
cout << "Nb d'étudiants";
cin >> N;
//Allocation mémoire d'un tableau de N
entiers
int *Tab, *Pi;
Pi=new int;
Tab=new int[N];
if ((Pi != NULL) && (Tab != NULL))
{
//instructions;
delete Pi;
delete[] Tab;
}
}
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++

C'est de l'allocation d'espace mémoire à l'exécution et non à la compilation

En C ++:
int main()
{ Attention :
int N;
cout << "Nb d'étudiants";
cin >> N;
//Allocation mémoire d'un tableau de N Ne pas croiser les
entiers
int *Tab, *Pi;
allocations/libérations
Pi=new int; dynamiques entre les
Tab=new int[N]; instructions C et C++
if ((Pi != NULL) && (Tab != NULL))
{
//instructions;
delete Pi;
delete[] Tab;
}
}
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'; quand on rencontrera ça on
char *Chaine="merci"; remplacera jusqu'aux bout de la
char Mot[]="beaucoup"; ligne
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;
}
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'; quand on rencontrera ça on
char *Chaine="merci"; remplacera jusqu'au bout de la ligne
char Mot[]="beaucoup";
char *p=Mot;
obligatoire car il n'y en pas à la fin
char *Tab[3]={"ZERO","UN","DEUX"};
AffChaine(Chaine);
du #define
AffCarac(Chaine[23]);; inutile car le ";" est déjà présent a la
AffChaine(Mot+3); fin du #define
AffCarac(*++p);
AffCarac(++*++p); 1
AffChaine(Tab[1]);
2
AffChaine(Tab[2]+1);
Tab[0][2]='\Ø'; 3
AffChaine (*Tab);
retour 0;
}
AP4 - Programmation Orientée Objet
Chapitre 2 : Introduction au langage C++

En Mémoire : Affichage :
Ref 'A'
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \Ø
Mot mot mot mot
+1 +2 +3

Tab Z E R O \Ø
U N \Ø
D E U X \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En Mémoire : Affichage :
Ref 'A' 'B'
A--merci
Chaine M E R C I \Ø
0 1 2
Mot b e a u c o u p \Ø
Mot mot mot mot
+1 +2 +3

Tab Z E R O \Ø
U N \Ø
D E U X \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En Mémoire : Affichage :
Ref 'A' 'B'
A -- merci
Chaine M E R C I \Ø
B -- r
0 1 2
Mot b e a u c o u p \Ø
Mot mot mot mot
+1 +2 +3

Tab Z E R O \Ø
U N \Ø
D E U X \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En Mémoire : Affichage :
Ref 'A' 'B' 'C'
A -- merci
Chaine M E R C I \Ø
B -- r
0 1 2
C -- ucoup
Mot b e a u c o u p \Ø
Mot mot mot mot
+1 +2 +3

Tab Z E R O \Ø
U N \Ø
D E U X \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En Mémoire : Affichage :
Ref 'A' 'B' 'C' 'D'
A -- merci
Chaine M E R C I \Ø
B -- r
0 1 2
C -- ucoup
Mot b e a u c o u p \Ø
Mot mot mot mot D -- e
+1 +2 +3

Tab Z E R O \Ø
U N \Ø
D E U X \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En Mémoire : Affichage :
Ref 'A' 'B' 'C' 'D' 'E'
A -- merci
Chaine M E R C I \Ø
B -- r
0 1 2
C -- ucoup
Mot b e ab u c o u p \Ø
Mot mot mot mot D -- e
+1 +2 +3

E -- b
p

Tab Z E R O \Ø
U N \Ø
D E U X \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En Mémoire : Affichage :
Ref 'A' 'B' 'C' 'D' 'E' 'F'
A -- merci
Chaine M E R C I \Ø
B -- r
0 1 2
C -- ucoup
Mot b e ab u c o u p \Ø
Mot mot mot mot D -- e
+1 +2 +3

E -- b
p
F -- UN
Tab Z E R O \Ø
U N \Ø
D E U X \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En Mémoire : Affichage :
Ref 'A' 'B' 'C' 'D' 'E' 'F' 'G'
A -- merci
Chaine M E R C I \Ø
B -- r
0 1 2
C -- ucoup
Mot b e ab u c o u p \Ø
Mot mot mot mot D -- e
+1 +2 +3

E -- b
p
F -- UN
Tab Z E R O \Ø G -- EUX
U N \Ø
D E U X \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

En Mémoire : Affichage :
Ref 'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H'
A -- merci
Chaine M E R C I \Ø
B -- r
0 1 2
C -- ucoup
Mot b e ab u c o u p \Ø
Mot mot mot mot D -- e
+1 +2 +3

E -- b
p
F -- UN

Tab Z E R O \Ø G -- EUX
U N \Ø H -- ZE
D E U X \Ø

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

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

p
char p*;
p="Toto";
while (*p)
cout << *p++;
T o t o \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

p
char p*;
p="Toto";
while (*p)
cout << *p++;
T o t o \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

p
char p*;
p="Toto";
while (*p)
cout << *p++;
T o t o \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

p
char p*;
p="Toto";
while (*p)
cout << *p++;
T o t o \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

p
char p*;
p="Toto";
while (*p)
cout << *p++;
T o t o \Ø

code ASCII = 0

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

char p[20];
p="Toto"; p
0 19

char p[20]="Toto";

// ou

char p[20]={'T','o','t','o','\Ø'};
char p[]="Toto";

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

char p[20];
p="Toto"; p
0 19

char p[20]="Toto";
p T o t o \Ø
// ou
0 19
char p[20]={'T','o','t','o','\Ø'};
char p[]="Toto";

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 \Ø

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 \Ø

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

renvoie la longueur de la chaine

#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);

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

renvoie la longueur de la chaine

#include <cstring> // en c opère la concaténation de la ch2 dans


#include <string.h> // en c++ la ch1

char Ch[]="Toto";

int lg=strlen(Ch);

strcat(ch1;ch2);

strcat(ch1;ch2;lgmax);

strcmp(ch1;ch2);

strcpy(ch1;ch2);

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

renvoie la longueur de la chaine

#include <cstring> // en c opère la concaténation de la ch2 dans


#include <string.h> // en c++ la ch1

char Ch[]="Toto"; compare lexicographique ch1 et ch2 et


retourne :
int lg=strlen(Ch);
0 si ch1 et ch2 identiques
strcat(ch1;ch2); <0 si ch1 < ch2
>0 si ch1 > ch2
strcat(ch1;ch2;lgmax);

strcmp(ch1;ch2);

strcpy(ch1;ch2);

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

renvoie la longueur de la chaine

#include <cstring> // en c opère la concaténation de la ch2 dans


#include <string.h> // en c++ la ch1

char Ch[]="Toto"; compare lexicographique ch1 et ch2 et


retourne :
int lg=strlen(Ch);
0 si ch1 et ch2 identiques
strcat(ch1;ch2); <0 si ch1 < ch2
>0 si ch1 > ch2
strcat(ch1;ch2;lgmax);

strcmp(ch1;ch2);
copie la ch2 dans la ch1

strcpy(ch1;ch2);

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

cout << "qu'elle heure est-il?"; 


cin >> Heure;
Opérateur dans iostream

int main()
{
int A=12;
float B=123.4567;
cout << setw(5) << A;
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

cout << "qu'elle heure est-il?"; 


cin >> Heure;
Opérateur dans iostream
setw() définie l'espace d'affichage
int main()
{ setprecision() précision après la virgule
int A=12;
float B=123.4567;
cout << setw(5) << A;
}

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

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

FILE *file;
File=fopen("NomPhysique",Mode);
Ne pas oublier d'inclure :
fclose(File);//File == NomLogique cstdio en C++
ou
stdio.h en C
Modes Disponibles

"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 write only. Si existant  remplacement
"w+"  création et ouverture en lecture et écriture. Si existant  remplacement

Attention : fopen() retourne NULL si le fichier n'a pu être ouvert


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/

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 positionnement
rewind(Fich); //reviens au début
int ftell(Fich); //positionnement dans le fichier par rapport au début

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

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++;
}

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
}

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

#undef LONG

#include <. . . .> // Fichiers de base du langage


#include ". . . ." // Fichiers autres

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

#if..... #if LANGUE==1


... #define ERREUR"Faute"
#endif #elseif LANGUE==2
#ifdef.... #define ERREUR"Error"
... #endif
#endif
#ifndef...
...
#endif

test.cpp Pile.cpp Pile.h


"include "Pile.h" #include "Pile.h" #ifndef PILE_H
#define PILE_H
int main() void F()
{ { void F();
F(); cout << "Salut";
} } #endif

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

struct Client
{ Indispensable!!
char Nom[25];
int Age;
};

Client UnClient;
Client * PtrClient; UnClient PtrClient
PtrClient= & UnClient;
UnClient.Age=20; Nom
PClient->Age=30;//(*PClient).Age=30;
Age

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

struct Elt
{
Client Donnees;
Elt *pSuivant;
};

pDebut
Client1

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

struct Elt
{
Client Donnees;
Elt *pSuivant;
};

pDebut
Client1 Client2

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

struct Elt
{
Client Donnees;
Elt *pSuivant;
};

pDebut
Client1 Client2

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

struct Elt
{
Client Donnees;
Elt *pSuivant;
};

pDebut
Client1 Client2 Client3

AP4 - Programmation Orientée Objet


Chapitre 2 : Introduction au langage C++

struct Elt
{
Client Donnees;
Elt *pSuivant;
};

pDebut
Client1 Client2 Client3

AP4 - Programmation Orientée Objet

Vous aimerez peut-être aussi