C++ est un langage de programmation puissant et performant, utilisé pour créer des systÚmes d'exploitation, des jeux vidéo, des applications temps réel et bien plus encore.
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}g++ hello.cpp -o hello./hello
#include <iostream>
#include <string>
int main() {
// Types numériques entiers
int age = 25;
long population = 7800000000;
short temperature = -5;
// Types numériques à virgule
float prix = 19.99f;
double pi = 3.14159265359;
// CaractÚres et booléens
char lettre = 'A';
bool estVrai = true;
// ChaĂźnes de caractĂšres
std::string nom = "Alice";
// Affichage
std::cout << "Nom: " << nom << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Prix: " << prix << std::endl;
return 0;
}// Auto : le type est déduit automatiquement
auto x = 42; // int
auto y = 3.14; // double
auto nom = "Alice"; // const char*
// Const : valeur constante (non modifiable)
const int MAX_SIZE = 100;
const double PI = 3.14159;
// MAX_SIZE = 200; // Erreur de compilation !#include <iostream>
#include <string>
int main() {
std::string nom;
int age;
// Demander des informations
std::cout << "Quel est votre nom ? ";
std::getline(std::cin, nom);
std::cout << "Quel Ăąge avez-vous ? ";
std::cin >> age;
// Afficher les résultats
std::cout << "Bonjour " << nom << " !" << std::endl;
std::cout << "Vous avez " << age << " ans." << std::endl;
return 0;
}int a = 10, b = 3;
int somme = a + b; // 13
int difference = a - b; // 7
int produit = a * b; // 30
int quotient = a / b; // 3 (division entiĂšre)
int reste = a % b; // 1 (modulo)
// Incrémentation et décrémentation
a++; // a = 11
b--; // b = 2
// Opérateurs composés
a += 5; // a = a + 5
b *= 2; // b = b * 2int x = 5, y = 10;
bool egal = (x == y); // false
bool different = (x != y); // true
bool superieur = (x > y); // false
bool inferieur = (x < y); // true
bool sup_ou_egal = (x >= y); // false
bool inf_ou_egal = (x <= y); // truebool a = true, b = false;
bool et = a && b; // false (ET logique)
bool ou = a || b; // true (OU logique)
bool non = !a; // false (NON logique)#include <iostream>
int main() {
int age;
std::cout << "Votre Ăąge : ";
std::cin >> age;
if (age >= 18) {
std::cout << "Vous ĂȘtes majeur." << std::endl;
} else if (age >= 13) {
std::cout << "Vous ĂȘtes adolescent." << std::endl;
} else {
std::cout << "Vous ĂȘtes enfant." << std::endl;
}
return 0;
}#include <iostream>
int main() {
int choix;
std::cout << "Choisissez (1-3) : ";
std::cin >> choix;
switch (choix) {
case 1:
std::cout << "Option 1 sélectionnée" << std::endl;
break;
case 2:
std::cout << "Option 2 sélectionnée" << std::endl;
break;
case 3:
std::cout << "Option 3 sélectionnée" << std::endl;
break;
default:
std::cout << "Choix invalide" << std::endl;
}
return 0;
}#include <iostream>
int main() {
// Boucle for classique
for (int i = 0; i < 5; i++) {
std::cout << "Itération " << i << std::endl;
}
// Compter Ă rebours
for (int i = 10; i >= 0; i--) {
std::cout << i << " ";
}
std::cout << "Décollage !" << std::endl;
return 0;
}#include <iostream>
int main() {
int compteur = 0;
while (compteur < 5) {
std::cout << "Compteur: " << compteur << std::endl;
compteur++;
}
return 0;
}#include <iostream>
int main() {
int nombre;
do {
std::cout << "Entrez un nombre positif : ";
std::cin >> nombre;
} while (nombre <= 0);
std::cout << "Merci ! Vous avez entré : " << nombre << std::endl;
return 0;
}#include <iostream>
int main() {
// Déclaration et initialisation
int nombres[5] = {10, 20, 30, 40, 50};
// AccÚs aux éléments
std::cout << "Premier élément : " << nombres[0] << std::endl;
std::cout << "Dernier élément : " << nombres[4] << std::endl;
// Modifier un élément
nombres[2] = 35;
// Parcourir le tableau
for (int i = 0; i < 5; i++) {
std::cout << nombres[i] << " ";
}
std::cout << std::endl;
return 0;
}