le langage c++ et la programmation obje
TRANSCRIPT
![Page 1: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/1.jpg)
Le Langage C++ et
La Programmation Objet
![Page 2: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/2.jpg)
Organisation
l 4 Cours Magistraux de 2h l 3 séances sur machines (TPs) de 4h l Evaluation
l Un examen écrit (50% note finale) l Une remise en fin de trimestre (50% note finale)
l Compte-rendu l Code source
![Page 3: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/3.jpg)
Organisation
Page du cours et des TDs: http://manao.inria.fr/perso/~pac/cpp.php
è A consulter Régulièrement !
![Page 4: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/4.jpg)
l Cours 1 - Généralités - Le processus de création d'un programme - Tour d'horizon du C++ - La programmation impérative en C++
l Cours 2 l Cours 3 l Cours 4
![Page 5: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/5.jpg)
l Cours 1 - Généralités - Le processus de création d'un programme - Tour d'horizon du C++ - La programmation impérative en C++
l Cours 2 l Cours 3 l Cours 4
![Page 6: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/6.jpg)
Historique C, C++, Java et C#, Python
C
C++
![Page 7: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/7.jpg)
Historique C, C++, Java et C#, Python
1963
1998
2002
1978
1967 1995
2011
2011
1973
Travaux pré. Ritchie & Thomson
@Bell Labs C Travaux de l'ANSI
GCC
C99 C11
1999
Noyau Unix C
K&R C-Book
ISO C++11
1987
1983
Simula
1979
Java Python
1994
C#
1983
Travaux pré C++
Stroustrup @Bell Labs
1985
1st Release
2nd Release
1989
1989/90
C89
C99
ISO C++98
2003 ISO
C++03
C++
![Page 8: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/8.jpg)
Historique C, C++, Java et C#, Python
1963
1998
2002
1978
1967 1995
2011
2011
1973
Travaux pré. Ritchie & Thomson
@Bell Labs C Travaux de l'ANSI
GCC
C99 C11
1999
Noyau Unix C
K&R C-Book
ISO C++11
1987
1983
Simula
1979
Java Python
1994
C#
1983
Travaux pré C++
Stroustrup @Bell Labs
1985
1st Release
2nd Release
1989
1989/90
C89
C99
ISO C++98
2003 ISO
C++03
C++
![Page 9: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/9.jpg)
Design du C++
l Syntaxe épurée du C → Conserver le savoir-faire != compatibilité du code
l Améliorer la productivité du progammeur - Simplifier le langage à la place du compilateur
l Code aussi portable et efficace que le C - « Zero-overhead » principle
l Liberté : responsabiliser le programmeur - e.g., gestion de la mémoire
![Page 10: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/10.jpg)
Design du C++
l Limitations historique du C - Assembleur de haut niveau != langage de prog.
impératif - L'absence
l de typage des macros l de passage par adresse
- La rarification des noms d'identificateurs
![Page 11: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/11.jpg)
Caratéristiques générales du C++
l Langage Compilé l Syntaxe ala C - Transition facilitée des développeurs
l Typage statique l C++ - N'est pas une extension objet du C - Un programme C ne compile pas forcément en C++ - rmes en appliquant la norme ISO.
![Page 12: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/12.jpg)
Caratéristiques générales du C++
l « Grande Comptabilité » avec le C - Conversion librairies C pour utilisation depuis C++
l Gestion des erreurs avec des exceptions l Multiples paradigmes de programmation l Multiplateformes en appliquant la norme ISO.
![Page 13: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/13.jpg)
Caractéristiques générales du C++
Paradigmes de programmation : - Impérative
l haut/bas niveau - Programmation par objet O - Générique (templates) - Meta-templates....
→ Langage « hybride »
![Page 14: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/14.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique
![Page 15: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/15.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique LANGAGE C
![Page 16: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/16.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique LANGAGE C
Standard Templates Library
(STL)
![Page 17: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/17.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique
Autres : Qt, Boost,...
LANGAGE C
Standard Templates Library
(STL)
![Page 18: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/18.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique
Autres : Qt, Boost,...
LANGAGE C
Standard Templates Library
(STL)
Librairie C (Fichier,
Mémoire,...)
![Page 19: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/19.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique
Autres : Qt, Boost,...
LANGAGE C
Standard Templates Library
(STL) Autres Librairies
GTK+, Glib,...
Librairie C (Fichier,
Mémoire,...)
![Page 20: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/20.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique
Autres : Qt, Boost,...
LANGAGE C
Standard Templates Library
(STL) Autres Librairies
GTK+, Glib,...
Librairie C (Fichier,
Mémoire,...)
Programmeur C++
![Page 21: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/21.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique
Autres : Qt, Boost,...
LANGAGE C
Standard Templates Library
(STL) Autres Librairies
GTK+, Glib,...
Librairie C (Fichier,
Mémoire,...)
Programmeur C++ Utilisation
![Page 22: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/22.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique
Autres : Qt, Boost,...
LANGAGE C
Standard Templates Library
(STL) Autres Librairies
GTK+, Glib,...
Librairie C (Fichier,
Mémoire,...)
Encapsulation/Utilisation
Programmeur C++ Utilisation
![Page 23: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/23.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique
Autres : Qt, Boost,...
LANGAGE C
Standard Templates Library
(STL) Autres Librairies
GTK+, Glib,...
Librairie C (Fichier,
Mémoire,...)
Encapsulation/Utilisation
Programmeur C++
Possible sous ce certaines
conditions
Utilisation
![Page 24: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/24.jpg)
Langage et librairie standard C vs C++
LANGAGE C++ Core Language
l Syntaxe & Sémantique
Autres : Qt, Boost,...
LANGAGE C
Standard Templates Library
(STL) Autres Librairies
GTK+, Glib,...
Librairie C (Fichier,
Mémoire,...)
Encapsulation/Utilisation
Programmeur C++ Utilisation
A PROSCRIRE
Possible sous ce certaines
conditions
![Page 25: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/25.jpg)
Standardisations/Normes du C++ l C++98 - 1998 : ISO/IEC 14882:1998
l C++03 - 2003 : ISO/IEC 14882:2003
l C++TR1 - 2007 : ISO/IEC TR 19768:2007
l C++11 (avant C++0x)
- 2011 : ISO/IEC 14882:2011 • C++ 2014 - ISO/IEC 14882:2014
![Page 26: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/26.jpg)
Objectifs du cours et des TD l Maîtrise des concepts clés
- Référence (C++) - Constness (C++) - Programmation Objet (C++ mais commun à Java, C#,…)
l Initiation en C++
- Standard Templates Library (STL) l Conteneurs l Algorithmes
- Templates - Design Patterns
![Page 27: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/27.jpg)
l Cours 1 - Généralités - Le processus de création d'un programme - Tour d'horizon du C++ - La programmation impérative en C++
l Cours 2 l Cours 3 l Cours 4
![Page 28: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/28.jpg)
l Cours 1 - Généralités - Le processus de création d'un programme - Tour d'horizon du C++ - La programmation impérative en C++
l Cours 2 l Cours 3 l Cours 4
![Page 29: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/29.jpg)
Le processus de création d'un programme
l Programmation / Développement → Programmeur 1
l Traduction en langage machine → Compilateur + Editeur de lien (linker)
l Test du programmeur → Programmeur 1 ou autre personne
l Déploiement / Installation → Programmeur 1 ou 2
![Page 30: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/30.jpg)
Le processus de création d'un programme
l Programmation / Développement → Programmeur 1
l Traduction en langage machine → Compilateur + Editeur de lien (linker)
l Test du programmeur → Programmeur 1 ou autre personne
l Déploiement / Installation → Programmeur 1 ou 2
![Page 31: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/31.jpg)
Traduction en langage machine
l Langages interprétés (e.g., Scheme, Perl, ...) - code source → activité et/ou instructions machines - Interpréteur et programme « fits » en mémoire L - Vitesse d’exécution réduite - Débogage facilité
l Langages compilés (e.g., C, C++, …) - code source → instructions machines - Taille mémoire plus faible pour le code compilé J - Compilation séparée des fichiers sources
→Création de librairie
![Page 32: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/32.jpg)
Processus de génération de code machine
1. Compilation du code source 2. Edition de lien et création du binaire
![Page 33: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/33.jpg)
33
Compilation et Edition de lien
hello.cpp
![Page 34: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/34.jpg)
34
Compilation et Edition de lien
Compilation : production d'instructions machines
hello.cpp hello.o Compilation
![Page 35: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/35.jpg)
35
Compilation et Edition de lien
Compilation : production d'instructions machines Edition de lien : l Vérification que les liens pointent sur des instructions
valides l Ajout de code pour l'éxécution du programme
hello.cpp hello.o hello.bin Compilation Linking
Librairies Standard
![Page 36: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/36.jpg)
Librairies
l Librairies - ensembles de fonctions/classes/procedures - pas de point d'entrée (i.e., main(...) ) != application
l Facilité par la compilation séparée l But: Partager les fonctionnalités développés
dans la librairie pour différentes applications l Deux type de librairies - Dynamiques (.so, .dll, .dylib) - Statiques (.a, .lib )
![Page 37: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/37.jpg)
Illustration d'utilisation de Librairies
Librairie
l GUI l XML
l Thread l ....
![Page 38: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/38.jpg)
Illustration d'utilisation de Librairies
Librairie
l GUI l XML
l Thread l ....
![Page 39: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/39.jpg)
Illustration d'utilisation de Librairies
Librairie
l GUI l XML
l Thread l ....
![Page 40: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/40.jpg)
Illustration d'utilisation de Librairies
VLC
NUKE Librairie
l GUI l XML
l Thread l ....
![Page 41: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/41.jpg)
La compilation l Pré-processeur
l Remplacement de toutes les macros.
l Création de l'arbre représentant le code l GCC GENERIC: Language-independent
representation(generated by Front Ends )
l Vérification du type (type-safe checking) statique en C++ l Passe globale d'optimisation
l GCC GIMPLE
è Tuple representation used by Tree SSA optimizers
l Génération des instructions machines (IR)
![Page 42: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/42.jpg)
42
Création de librairies
opti.cpp opti.o Compilation
COMPILATION SEPARÉE : - minimise les besoins en mémoire - optimisée sur machine multi-coeur
vec.cpp
matrix.cpp
algebra.cpp
vec.o
matrix.o
algebra.o
Compilation
Compilation
Compilation
![Page 43: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/43.jpg)
43
Création de librairies
opti.cpp opti.o Compilation
COMPILATION SEPARÉE : - minimise les besoins en mémoire - optimisée sur machine multi-coeur
mylib.so
vec.cpp
matrix.cpp
algebra.cpp
vec.o
matrix.o
algebra.o
Compilation
Compilation
Compilation
![Page 44: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/44.jpg)
44
Création de librairies
opti.cpp opti.o
hello.bin
Compilation
COMPILATION SEPARÉE : - minimise les besoins en mémoire - optimisée sur machine multi-coeur
mylib.so
vec.cpp
matrix.cpp
algebra.cpp
vec.o
matrix.o
algebra.o
Compilation
Compilation
Compilation
hello.cpp
hello.o
Edition de Lien
![Page 45: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/45.jpg)
45
Déclaration vs Définition
l Déclaration : - Introduction du nom de l'identifiant au compilateur - Identifiant = nom de variables,classes, fonctions,... - Nécessaire pour la compilation de fichier séparé
l Définition : - Allocation d'espace mémoire pour un identifiant - Une définition peut être aussi une déclaration
l One-Definition-Rule : - Déclarations multiples MAIS une seule définition !
![Page 46: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/46.jpg)
46
Déclaration et définition de fonction
// Déclaration de func1intfunc1(int, int); // Déclaration de func2int func2(int width, int length);
// Définition//Implémentation de func2intfunc2( int width, int length){ return 2 * (width+length) ;}
functions.hpp functions.cpp
![Page 47: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/47.jpg)
47
Déclarations de fonctions En C++ En C
// Déclaration de func2int func2() ;
// Déclaration de func2int func2() ;Combien d'arguments pour func2 ?
![Page 48: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/48.jpg)
48
Déclarations En C++ En C
// Déclaration de func2int func2() ;
// Déclaration de func2int func2() ;Combien d'arguments pour func2 ?
N'importe quel nombre
ET type
![Page 49: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/49.jpg)
49
Déclarations En C++ En C
// Déclaration de func2int func2() ;
// Déclaration de func2int func2() ;Combien d'arguments pour func2 ?
AUCUN
N'importe quel nombre
ET type
![Page 50: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/50.jpg)
50
Déclarations de variables
int a ;
vars.hpp
![Page 51: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/51.jpg)
51
Déclarations de variables
int a ; // != Déclaration. Déclaration ET Définition
vars.hpp
![Page 52: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/52.jpg)
52
Déclarations de variables
int a ; // != Déclaration. Déclaration ET Définition
extern int a ; // == Déclaration
vars.hpp
Remarques : l extern est optionel pour la déclaration de fonction l extern rend le code plus (+) lisible l extern « promet » une définition ailleurs dans ou hors du fichier
![Page 53: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/53.jpg)
53
Exemple de déclarations et définitions
extern int i;float b ;extern float f(float) ;int i ;int h(int x) { return x+1 ;}float f(float x) { return x*x;}int main(int argc, char** argv) { i = 2 ; b = -3.14159f ; f(b) ; int foo = h(i) ; return EXIT_SUCCESS ;}
exs.cpp
![Page 54: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/54.jpg)
54
Exemple de déclarations et définitions
extern int i;float b ;extern float f(float) ;int i ;int h(int x) { return x+1 ;}float f(float x) { return x*x;}int main(int argc, char** argv) { i = 2 ; b = -3.14159f ; f(b) ; int foo = h(i) ; return EXIT_SUCCESS ;}
exs.cpp //Déclaration sans définition
![Page 55: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/55.jpg)
55
Exemple de déclarations et définitions
extern int i;float b ;extern float f(float) ;int i ;int h(int x) { return x+1 ;}float f(float x) { return x*x;}int main(int argc, char** argv) { i = 2 ; b = -3.14159f ; f(b) ; int foo = h(i) ; return EXIT_SUCCESS ;}
exs.cpp //Déclaration sans définition//Déclaration et définition
![Page 56: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/56.jpg)
56
Exemple de déclarations et définitions
extern int i;float b ;extern float f(float) ;int i ;int h(int x) { return x+1 ;}float f(float x) { return x*x;}int main(int argc, char** argv) { i = 2 ; b = -3.14159f ; f(b) ; int foo = h(i) ; return EXIT_SUCCESS ;}
exs.cpp //Déclaration sans définition//Déclaration et définition//Déclaration de fonction
![Page 57: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/57.jpg)
57
Exemple de déclarations et définitions
extern int i;float b ;extern float f(float) ;int i ;int h(int x) { return x+1 ;}float f(float x) { return x*x;}int main(int argc, char** argv) { i = 2 ; b = -3.14159f ; f(b) ; int foo = h(i) ; return EXIT_SUCCESS ;}
exs.cpp //Déclaration sans définition//Déclaration et définition//Déclaration de fonction//Déclaration et définition
![Page 58: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/58.jpg)
58
Déclarations de variables
int a ; // != Déclaration. Déclaration ET Définition
extern int a ; // == Déclaration
vars.hpp
Remarques : l extern est optionnel pour la déclaration de fonction l extern rend le code plus (+) lisible
![Page 59: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/59.jpg)
59
Compilation et linking sous Unix
l Compilation: g++ toto.cpp -c l Edition de lien: - ld hello.o -o hello.exe -lcrt1.10.6.o -lstdc++ -lSystem -
lgcc l En pratique (2 en 1): - g++ toto.c -o toto.exe
l Librairies partagée et statique vues en TD
![Page 60: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/60.jpg)
l Cours 1 - Généralités - Le processus de création d'un programme - Tour d'horizon du C++ - La programmation impérative en C++
l Cours 2 l Cours 3 l Cours 4
![Page 61: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/61.jpg)
l Cours 1 - Généralités - Le processus de création d'un programme - Tour d'horizon du C++ - La programmation impérative en C++
l Cours 2 l Cours 3 l Cours 4
![Page 62: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/62.jpg)
62
Premier exemple en C++ 1: #include <iostream>2: int main(int argc, char** argv ) {
3: std::cout << " HOWDY !!! " << std::endl;4: return 0;
}
![Page 63: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/63.jpg)
63
Inclusion des fichiers en-tête (1/2) // Header type <...>#include <iostream> // Header lib STL (C++)#include < iostream > // Ne marchera PAS
La syntaxe <...> l Utiliser pour la librairie standard l Abstraction du système de fichier (nom, répertoire) de l'OS l Le préprocesseur se charge :
l De trouver quel fichier correspond au nom (iostream ici) l De remplacer la directive d'inclusion par le contenu du fichier
trouvé (copy & paste)
![Page 64: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/64.jpg)
64
Inclusion des fichiers en-tête (2/2) // Header type "…" #include "myvect.hpp" // Depuis dir. courant #include "math/myvect.hpp" // Chemin vers rep. math La syntaxe "..." l Le préprocesseur se charge :
l d'inclure le fichier spécifié à partir du répertoire courant d'appel
du compilateur/pré-processeur l De remplacer la directive d'inclusion par le contenu du fichier
trouvé (copy & paste)
![Page 65: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/65.jpg)
65
Les namespaces
Problématique :
float normInf( float, float, float );float normL2( float, float, float );
float normInf( float, float, float);float fastNormL2( float, float, float);
norm.hpp
norm_comp.hpp
![Page 66: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/66.jpg)
66
Les namespaces
Problématique :
float normInf( float, float, float );float normL2( float, float, float );
float normInf( float, float, float);float fastNormL2( float, float, float);
norm.hpp
norm_comp.hpp
main.cpp #include "norm_comp.hpp"#include "norm.hpp"int main(int argc, char** argv ){ float b=fastNormL2(3.0f,4.0f,5.0f); float z = normInf(1.0f,2.0f,-3.0f); std::cout << a << " " << b; return 0;}
![Page 67: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/67.jpg)
67
Les namespaces
Problématique :
float normInf( float, float, float );float normL2( float, float, float );
float normInf( float, float, float);float fastNormL2( float, float, float);
norm.hpp
norm_comp.hpp
main.cpp #include "norm_comp.hpp"#include "norm.hpp"int main(int argc, char** argv ){ float b=fastNormL2(3.0f,4.0f,5.0f); float z = normInf(1.0f,2.0f,-3.0f); std::cout << a << " " << b; return 0;}
?
![Page 68: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/68.jpg)
namespace ioa_math { float normInf(float, float, float); float fastNormL2(float, float, float);}
namespace iogs_math { float normInf(float, float, float ); float normL2(float, float, float );}
68
#include "norm_comp.hpp"#include "norm.hpp"int main(int argc, char** argv ){ float b=fastNormL2(3.0f,4.0f,5.0f); float z = normInf(1.0f,2.0f,-3.0f); std::cout << a << " " << b; return 0;}
Les namespaces
norm.hpp
norm_comp.hpp main.cpp
![Page 69: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/69.jpg)
namespace ioa_math { float normInf(float, float, float); float fastNormL2(float, float, float);}
namespace iogs_math { float normInf(float, float, float ); float normL2(float, float, float );}
69
#include "norm_comp.hpp"#include "norm.hpp"using namespace ioa_math;int main(int argc, char** argv ){ float b=fastNormL2(3.0f,4.0f,5.0f); float z = normInf(1.0f,2.0f,-3.0f); std::cout << a << " " << b; return 0;}
Les namespaces
norm.hpp
norm_comp.hpp main.cpp
![Page 70: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/70.jpg)
namespace ioa_math { float normInf(float, float, float); float fastNormL2(float, float, float);}
namespace iogs_math { float normInf(float, float, float ); float normL2(float, float, float );}
70
#include "norm_comp.hpp"#include "norm.hpp"using namespace ioa_math;int main(int argc, char** argv ){ float b=fastNormL2(3.0f,4.0f,5.0f); float z = normInf(1.0f,2.0f,-3.0f); std::cout << a << " " << b; return 0;}
Les namespaces
norm.hpp
norm_comp.hpp main.cpp
![Page 71: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/71.jpg)
namespace ioa_math { float normInf(float, float, float); float fastNormL2(float, float, float);}
namespace iogs_math { float normInf(float, float, float ); float normL2(float, float, float );}
71
#include "norm_comp.hpp"#include "norm.hpp"using namespace ioa_math;int main(int argc, char** argv ){ float b=fastNormL2(3.0f,4.0f,5.0f); float z = normInf(1.0f,2.0f,-3.0f); std::cout << a << " " << b; return 0;}
Les namespaces
norm.hpp
norm_comp.hpp main.cpp
![Page 72: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/72.jpg)
namespace ioa_math { float normInf(float, float, float); float fastNormL2(float, float, float);}
namespace iogs_math { float normInf(float, float, float ); float normL2(float, float, float );}
72
#include "norm_comp.hpp"#include "norm.hpp"//using namespace ioa_math;int main(int argc, char** argv ){ float b=fastNormL2(3.0f,4.0f,5.0f); float z = normInf(1.0f,2.0f,-3.0f); std::cout << a << " " << b; return 0;}
Les namespaces
norm.hpp
norm_comp.hpp main.cpp
![Page 73: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/73.jpg)
namespace ioa_math { float normInf(float, float, float); float fastNormL2(float, float, float);}
namespace iogs_math { float normInf(float, float, float ); float normL2(float, float, float );}
73
#include "norm_comp.hpp"#include "norm.hpp"//using namespace ioa_math;int main(int argc, char** argv ){ float b= iogs_math::fastNormL2(3.0f,4.0f,5.0f); float z = ioa_math::normInf(1.0f,2.0f,-3.0f); std::cout << a << " " << b; return 0;}
Les namespaces
norm.hpp
norm_comp.hpp main.cpp
![Page 74: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/74.jpg)
74
Retour sur le premier exemple en C++ 1: #include <iostream>2:3: 4:int main(int argc, char** argv )
5:{6: std::cout << " HOWDY !!! " << std::endl;7: return 0;8:}
![Page 75: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/75.jpg)
75
Retour sur le premier exemple en C++ 1: #include <iostream>2:3: using namespace std;4:int main(int argc, char** argv )
5:{6: cout << " HOWDY !!! " << endl;7: return 0;8:}
![Page 76: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/76.jpg)
76
Les namespaces
l La directive using namespace - S'applique uniquement à la portée courante
l Fichier l Bloc { }
- Ne doit PAS s'utiliser dans les fichiers en-tête è pollution et effet de bord
l Les blocs { } namespace sont utilisées dans
les .cpp et les .hpp
![Page 77: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/77.jpg)
77
Kit de survie : les entrées-sorties
l En ligne de commande - Sorties. Avec l'objet cout (console ouput) - Entrées. Avec l'objet cin (console input
l Chaîne de caractères (string) l Pour les fichiers - Lecture - Ecriture
![Page 78: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/78.jpg)
78
Kit de survie : sorties formatées #include <iostream> //déclaration des stream (cout,cin,...) using namespace std;int main(int argc, char** argv){ cout << "nb en decimal" << dec << 15 << endl; cout << "nb en octal" << oct << 15 << endl; cout << "nb en hexa" << hex << 15 << endl; cout << "nb flottant" << 3.14159 << endl; cout << "caracteres non affiche" << char(27) << endl; return 0 ;}
1:2:3:4:5:6:7:8:9:10:
![Page 79: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/79.jpg)
79
Kit de survie : sorties concaténées #include <iostream> //déclaration des stream (cout,cin,...) using namespace std;int main(int argc, char** argv){ cout << " En C++ il est tout a fait possible " " sur plusieurs lignes tant qu il n y a pas de"
" caracteres de ponctuation \n " ; cout << " Si non peut écrire aussi, " << "caracteres non affiche" << endl; return 0 ;}
1:2:3:4:5:6:7:8:9:10:
![Page 80: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/80.jpg)
80
Kit de survie : entrées #include <iostream> //déclaration des stream (cout,cin,...) using namespace std;int main(int argc, char** argv){ int number; cin >> number; cout << "nb en octal = 0" << oct << number << endl; cout << "nb en hexa = 0x" << hex <<number << endl; return 0 ;}
1:2:3:4:5:6:7:8:9:10:
![Page 81: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/81.jpg)
81
Kit de survie : string #include <string> // declaration des string#include <iostream> // declaration des stream (cout,cin,...) using namespace std;int main(int argc, char** argv) { string s1, s2; // String vide string s3 = "Hello World !" ; // Initialisation string s4(". How is it going "); // Initialisation aussi s1 = s3 + " " + s4; // Concatenation s1 += " today ? " + s2; // Ajout a la fin cout << s1 + " !" << endl ; return 0 ;}
1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:
![Page 82: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/82.jpg)
82
Kit de survie : les fichiers #include <fstream> //stream fichiers (ifstream,ofstream...)#include <string> // objet string #include <iostream> // stream (cout,cin,...) using namespace std;int main(int argc, char** argv) { ifsteam in("toto.txt"); // Ouverture pour lecture ofstream out("toto-c.txt"); // Ouverture pour écriture string s ; while( getline(in,s) ) // Enleve le char newline out << s << "\n" ; // On le remet return 0 ;}
1:2:3:4:5:6:7:8:9:10:11:12:
![Page 83: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/83.jpg)
#include <fstream> //stream fichiers (ifstream,ofstream...)#include <string> // objet string #include <vector> // objet vecteur using namespace std;int main(int argc, char** argv) { vector<string> v ; // Initialisation v est vide ifstream in("toto.txt"); // Ouverture pour lecture string a_line; while( getline(in,a_line) ) // Lecture ligne par ligne v.push_back(a_line); // Stockage ligne dans v cout << "nombre d element du vecteur" << v.size() << endl ; return 0 ;}
83
Kit de survie : les vecteurs 1:2:3:4:5:6:7:8:9:10:11:12:
![Page 84: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/84.jpg)
l Cours 1 - Généralités - Le processus de création d'un programme - Tour d'horizon du C++ - La programmation impérative en C++
l Cours 2 l Cours 3 l Cours 4
![Page 85: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/85.jpg)
l Cours 1 - Généralités - Le processus de création d'un programme - Tour d'horizon du C++
l Bonnes pratiques de programmation - La programmation impérative en C++
l Cours 2 l Cours 3 l Cours 4
![Page 86: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/86.jpg)
86
Bonnes Pratiques de Programmation
l Séparation des fichiers en-tête et sources l Rendre le compilateur verbeux l De la bonne utilisation des macros l Assertion dans le code l Outils de compilation séparée (Qmake en TP) l Conventions de codage (vues TP)
![Page 87: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/87.jpg)
87
Séparation des fichiers sources et en-tête
l Fichier en-tête - .hpp (convention) - Déclarations
l Fichier source - .cpp (convention) - Définitions
![Page 88: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/88.jpg)
88
Contenu d'un fichier en-tête (.hpp)
l .hpp, le standard autorise:
The C++ Programming Language. B.Stroustrup. 1997.
![Page 89: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/89.jpg)
89
Contenu d'un fichier en-tête (.hpp)
l .hpp, le standard n'autorise PAS:
The C++ Programming Language. B.Stroustrup. 1997.
![Page 90: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/90.jpg)
90
Macro guards
#ifndef TOTO_CLASS_HPP
#define TOTO_CLASS_HPP
//CODE C++
// DECLARATIONS VIENNENT ICI
#endif
Toto.hpp
Objectifs: - Faciliter le pre-processing - Accélérer la compilation
- A partir de C++ 2011: #pragma once
![Page 91: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/91.jpg)
91
Contenu fichier source (.cpp)
Règles de base : - Ce qu'il n'y a pas dans les .hpp - L'implémentation = la définition des fonctions et des
variables à l'exception de celles définies dans les .hpp (e.g. inline )
- Inclusion des .hpp pour avoir accès à la déclaration des fonctions, classes,...
![Page 92: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/92.jpg)
92
De la bonne utilisation des macros
l Ne pas Utiliser les macros - Pour définir des constantes
→ mot clé const - Pour définir des fonctions
→ mot clé inline
l Utiliser les macros pour des constantes de compilation - NDEBUG,DEBUG
![Page 93: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/93.jpg)
93
Options de compilation
l Définir des constantes de compilation - g++ -DDEBUGING_MODE hello.cpp -o hello.exe
l Contrôler son code: - -Wall - -ansi -pedantic - -Wunused -Wextra -Wswitch-default
l Spécifier une version du langage - -std=c++98
![Page 94: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/94.jpg)
94
Deboguer son code
l Définir des constantes de compilation - g++ -DDEBUGING_MODE hello.cpp -o hello.exe
l Dans le code : #ifdef DEBUGING_MODE cout << " a= " << a << endl; #endif
l Pas de constante è Pas de pénalité à l'éxecution
è Code éliminé par le pré-processeur
è Deux versions d'un programme debug & release
![Page 95: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/95.jpg)
95
Les assertions
l Proviennent du C. l Verification d'une condition à l'execution sinon
arrêt l Peuvent être désactivées avec NDEBUG #include <cassert>
int main(int argc, char** argv) {
int i = 100 ;
assert( i!= 100 ) ; // Condition fausse => FIN EXECUTION
}
![Page 96: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/96.jpg)
l Cours 1 - Généralités - Le processus de création d'un programme - Tour d'horizon du C++ - La programmation impérative en C++
l Cours 2 l Cours 3 l Cours 4
![Page 97: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/97.jpg)
l Cours 1 - Généralités - Le processus de création d'un programme - Tour d'horizon du C++ - La programmation impérative en C++
l Cours 2 l Cours 3 l Cours 4
![Page 98: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/98.jpg)
La programmation impérative en C++
l Proche du C pour la syntaxe - Declaration des variables - Structure de contrôle - Boucles
l ! Sémantique parfois différente du C → Deux langages différents!
![Page 99: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/99.jpg)
Créer des fonctions (1/2)
// Définition. // Nommage args obligatoire si utilisation dans fctfloat translate( float x, float y, float z ){ return x + y + z;}// Warning du CC si nommage mais non utilisationfloat translate( float x, float y, float z ){ return x + y;}// Absence de nommage possible alors.float translate( float x, float y, float ){ return x + y ;}
![Page 100: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/100.jpg)
Créer des fonctions (2/2) // void = RIEN void fooBar(void) ; // eq. void fooBar()// return n'est PAS optionnel// quand le type de retour est différent // de voidint factoriel( int n ){ return (n != 0) ? (factoriel(n-1)):(1) ;}
![Page 101: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/101.jpg)
l Trouver dans la documentation le header où est déclaré la fonction → www.cppreference.com
l Inclure le header correspondant l Par défaut, le CC et l'éditeur de lien cherche
automatiquement dans la librairie standard et connaisse où elle est installée
Utilisation des fonctions STL (C++)
#include <fstream>
![Page 102: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/102.jpg)
l Trouver dans la documentation le header où est déclaré la fonction → www.cppreference.com
l Inclure le header correspondant l Par défaut, le CC et l'éditeur de lien cherche
automatiquement dans la librairie standard et connaisse où elle est installée
Utilisation des fonctions de la librairie C
#include <stdlib.h> // Forme mauvaise #include <cstdlib> // Forme correcte
![Page 103: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/103.jpg)
Type booléen #include <iostream> using namespace std;int main(int argc, char** argv) { bool a = true ; bool b = false ; bool c = (a && b ) and (!b or a ) ; bool d = !b; bool e = a || b; cout << " a= " << a << "b = " << b << "c = "<< c << "d = " << d << " e = " << e << endl; return 0 ;}
1:2:3:4:5:6:7:8:9:10:11:12:
![Page 104: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/104.jpg)
Type booléen #include <iostream> using namespace std;int main(int argc, char** argv) { bool a = true ; bool b = false ; bool c = (a && b ) and (!b or a ) ; bool d = !b; bool e = a || b; cout << " a= " << a << "b = " << b << "c = "<< c << "d = " << d << " e = " << e << endl; return 0 ;}
1:2:3:4:5:6:7:8:9:10:11:12:
![Page 105: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/105.jpg)
Structure de contrôle if-else 1: #include <iostream> 2: using namespace std;3: int main(int argc, char** argv) {4: int i ;5: cin >> i ;6: if ( i < 5 ) {7: cout << "strictement plus petit que 5" << endl ;8: } else {9: if ( i > 5 ) {10: cout << "strictement plus grand que 5" << endl ;11: } else {12: cout << "egal a 5" << endl ;13: }14: }15: return 0 ;16: }
![Page 106: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/106.jpg)
Boucle for #include <iostream> using namespace std;int main(int argc, char** argv) { for ( int i = 0 ; i < 128*argc ; i=i+1 ) { // INSTRUCTIONS } return 0 ;}
![Page 107: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/107.jpg)
Boucle for #include <iostream> using namespace std;int main(int argc, char** argv) { for ( int i = 0 ; i < 128*argc ; i=i+1 ) { // INSTRUCTIONS } return 0 ;}
Initialisation Condition d'arrêt Pas d'incrémentation
![Page 108: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/108.jpg)
Boucle for #include <iostream> using namespace std;int main(int argc, char** argv) { for ( int i = 0 ; i < 128*argc ; i=i+2 ) { if( i!= 26 ) // Devinez/Testez pq! { cout << " Value i= " << i << " char(i) = " << char(i) // Conversion de type << endl ; } } return 0 ;}
1:2:3:4:5:6:7:8:9:10:11:12:
![Page 109: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/109.jpg)
Boucle while #include <iostream> using namespace std;int main(int argc, char** argv) { int i = 0; while ( (i != 26) && (i < 128*argc) ) { cout << " Value i= " << i << " char(i) = " << char(i) // Conversion de type << endl ; i += 5; } return 0 ;}
1:2:3:4:5:6:7:8:9:10:11:12:
![Page 110: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/110.jpg)
Mot-clés break et continue #include <iostream> using namespace std;int main(int argc, char** argv) { int i = 0; while ( true ) { if( i % 2 ) { continue; } cout << " Value i= " << i << " char(i) = " << char(i) // Conversion de type << endl ; i += 5; if( i > 100 ) { break;} } return 0 ;}
1:2:3:4:5:6:7:8:9:10:11:12:
![Page 111: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/111.jpg)
Introduction aux opérateurs
l Opérateurs classiques : +, -, *, \, =, %, &, <<, >>,
l Opérateur = appel « spécial » de fonction l Règles de précédence - Simple pour expression arithmétique ( *, \, %, +, - ) - Complète (cf. annexe)
l Opérateurs d'incrémentation et décrémentation
![Page 112: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/112.jpg)
Introduction aux opérateurs #include <iostream> using namespace std;int main(int argc, char** argv) { int i = 0; int j = 0 ; cout << ++i << endl ; // pre-incrementation cout << j++ << endl ; // post-incrementation cout << --i << endl ; // pre-decrementation cout << j-- << endl ; // post-decrementation return 0 ;}
1:2:3:4:5:6:7:8:9:10:11:12:13:14:
![Page 113: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/113.jpg)
Types de données
l Types primitifs (fondamentaux) - Très proches du langage C - Types signés et non-signés (unsigned) != Java - Compris intrinsèquement par le compilateur
l Types abstrait - Classes créés par le programmeur - Appris à la « volée » par le compilateur
![Page 114: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/114.jpg)
Types primitifs
l Norme C++ - Ne spécifie pas le nombre de bits → sizeof()- Valeurs minimales et maximales que le type
l #include <climits> l #include <cfloat>
l 4 types de base
- char, int, float, double
![Page 115: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/115.jpg)
Types primitifs
l 4 types de base
- char, int, float, double
l bool
- Valeurs : true or false
- Conversion du compilateur vers int
→ mauvais usage (historique)
![Page 116: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/116.jpg)
Modificateurs de types primitifs
l 4 modificateurs
- long, short,unsigned, signed
l Hiérarchie
- sur les entiers : short int, int, int, long int
- sur les flottants : float, double, long double
l signed
- Implicite sur tous les types sauf char
![Page 117: Le Langage C++ et La Programmation Obje](https://reader035.vdocuments.pub/reader035/viewer/2022062522/62b3e1e4850af509b75ef1b2/html5/thumbnails/117.jpg)
Exercice sur les typres primitifs
l Afficher la taille de tous les types
- en utilisant l'opérateur sizeof
- avec et sans modificateurs