ap4 - programmation orientée objet chapitre 2 : introduction au langage c++ c c++ principales...

Post on 04-Apr-2015

124 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

AP4 - Programmation Orientée Objet

Chapitre 2 : Introduction au langage C++

C C++

Principales Améliorations :

Ajout du type booléen

AP4 - Programmation Orientée Objet

Chapitre 2 : Introduction au langage C++

Programme simple

AP4 - Programmation Orientée Objet

Chapitre 2 : Introduction au langage C++

Programme simple

Fichier Interface

AP4 - Programmation Orientée Objet

Chapitre 2 : Introduction au langage C++

Programme simple

Fichier Interface

Fichier d’implémentation

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

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

int main(){

;

}

Exemple n°1 :

AP4 - Programmation Orientée Objet

Chapitre 2 : Introduction au langage C++

Une fonction principale main()

int main(){

;

}

en tête

Exemple n°1 :

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 :

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 :

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 :

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 :

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 :

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

Cette méthode permet de faire un commentaires sur plusieurs lignes

Exemple n°2 :

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 :

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

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 :

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 :

AP4 - Programmation Orientée Objet

Chapitre 2 : Introduction au langage C++

Const int A=2;

Exemple n°6 :

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

{

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

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 :

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 :

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 :

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 :

Chapitre 2 : Introduction au langage C++

AP4 - Programmation Orientée Objet

A = B + 3;

B + C = A; INTERDIT!!!

Exemple n°10 :

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 :

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 :

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

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

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 :

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 :

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 :

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

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.

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;

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

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 .

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

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

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

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

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é

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

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

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;

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;

Chapitre 2 : Introduction au langage C++

AP4 - Programmation Orientée Objet

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

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

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

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

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

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

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

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

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

Chapitre 2 : Introduction au langage C++

AP4 - Programmation Orientée Objet

On remet ça à plus tard

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é!

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

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

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 :

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

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 :

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 :

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.

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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;

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

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

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

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

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

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

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

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

}

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

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

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

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

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

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

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

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

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:

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

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

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;

}

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

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 :

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

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

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

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

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

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

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

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

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

char p*;p="Toto";while (*p)

cout << *p++;

p

T o t o \Ø

AP4 - Programmation Orientée Objet

Chapitre 2 : Introduction au langage C++

char p*;p="Toto";while (*p)

cout << *p++;

p

T o t o \Ø

AP4 - Programmation Orientée Objet

Chapitre 2 : Introduction au langage C++

char p*;p="Toto";while (*p)

cout << *p++;

p

T o t o \Ø

AP4 - Programmation Orientée Objet

Chapitre 2 : Introduction au langage C++

char p*;p="Toto";while (*p)

cout << *p++;

p

T o t o \Ø

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

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

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

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

6

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

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

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

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

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

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

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

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 positionnementrewind(Fich); //reviens au débutint 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........#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

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

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

top related