Académique Documents
Professionnel Documents
Culture Documents
M1 / S1 / Module : C++
DEVOIR MAISON N2
__________________________
Présenté par :
Nadjib Bounaim
class Personne {
private:
string nom_;
int age_;
public:
void initialiser(string nom, int age) {
nom_ = nom;
age_ = age;
}
void afficherInformations() {
cout << "Nom: " << nom_ << ", Age: " << age_ << " ans" << endl;
}
};
int main() {
Personne personne1;
Personne personne2;
personne1.initialiser("Alice", 25);
personne2.initialiser("Bob", 30);
personne1.afficherInformations();
personne2.afficherInformations();
return 0;
}
Exercice 2 :
l’explication de code :
la correction de code :
La première faute c été ( using namespace std;)
La deuxième Animal()
La troisième private:
Le quatrième string race_;
La derniere animal.afficherInformations();
la correction finale :
#include <iostream>
#include <string>
class Animal{
public:
Animal(string nom, int age) : nom_(nom), age_(age){
cout << "Constructeur appele." << endl;
}
~Animal() {
cout << "Destructeur appele." << endl;
}
void afficherInformations() const{
cout << "Animal:Nom :" << nom_ << ", Age :" << age_ << " ans" << endl;
}
public:
string nom_;
int age_;
};
int main(){
Animal animal("Anima", 5);
animal.afficherInformations();
return 0;
Exercice 3 :
Écriture d’un programme en C++ qui utilise des classes pour représenter les livres et la bibliothèque :
#include <iostream>
#include <vector>
class Livre {
public:
Livre(string titre, string auteur, int annee, string isbn)
: titre_(titre), auteur_(auteur), annee_(annee), isbn_(isbn) {}
private:
string titre_;
string auteur_;
int annee_;
string isbn_;
};
class Bibliotheque {
public:
void ajouterLivre(const Livre& livre) {
livres_.push_back(livre);
}
private:
vector<Livre> livres_;
};
int main() {
Bibliotheque bibliotheque;
while (true) {
cout << "\nMenu:\n1. Ajouter un livre\n2. Afficher tous les livres\n3.
Quitter\n";
int choix;
cin >> choix;
switch (choix) {
case 1: {
string titre, auteur, isbn;
int annee;
return 0;
}