This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
in204:tds:sujets:td1:part1 [2019/09/19 11:49] 77.194.42.247 created |
in204:tds:sujets:td1:part1 [2022/11/18 10:50] (current) |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== Partie I – Création du compteur en C++ ===== | ===== Partie I – Création du compteur en C++ ===== | ||
| + | [[in204:tds:sujets:td1|TD1]] | ||
| - | ==== Question n° 0 ==== | + | ==== Etape 0 : Installation de l'environnement ==== |
| - | Lancer votre environnement de développement préféré. | + | En vous référant à la [[::in202::outils|documentation disponible]] sous ce lien et en n'hésitant pas à demander à l'enseignant de vous accompagner, installer un environnement de développement sur votre ordinateur. |
| ==== Question n°1 ==== | ==== Question n°1 ==== | ||
| Line 43: | Line 44: | ||
| max = value;; | max = value;; | ||
| } | } | ||
| + | }; | ||
| + | </code> | ||
| - | MyCounter() | + | <hidden Correction> |
| - | { | + | |
| - | counter = 0; | + | Le fichier ''counter.hpp'' contient a priori le code suivant : |
| - | max = value ; | + | |
| + | <code cpp> | ||
| + | #ifndef counterHPP | ||
| + | #define counterHPP | ||
| + | |||
| + | struct MyCounter | ||
| + | { | ||
| + | unsigned counter; | ||
| + | unsigned max; | ||
| + | |||
| + | unsigned getCounter() const { | ||
| + | return counter; | ||
| } | } | ||
| - | } | + | unsigned getMax() const { |
| + | return max; | ||
| + | } | ||
| + | |||
| + | void increment() { | ||
| + | counter ++; | ||
| + | if(counter > max) | ||
| + | counter = 0; | ||
| + | } | ||
| + | |||
| + | void reset() { | ||
| + | counter = 0; | ||
| + | } | ||
| + | |||
| + | void set(unsigned value) { | ||
| + | counter = (value <= max) ? value : counter; | ||
| + | } | ||
| + | |||
| + | void setMax(unsigned value) { | ||
| + | if(counter >= value) | ||
| + | counter = 0; | ||
| + | max = value;; | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | #endif | ||
| </code> | </code> | ||
| + | |||
| + | Le fichier ''counter.cpp'' soit ne contient rien, soit se limite à incluer le fichier ''counter.hpp'' comme suit: | ||
| + | |||
| + | <code cpp> | ||
| + | |||
| + | #include"counter.hpp" | ||
| + | |||
| + | </code> | ||
| + | |||
| ==== Question n°2 ==== | ==== Question n°2 ==== | ||
| Line 77: | Line 125: | ||
| Vérifier que le code de ''useObjectA()'' fonctionne correctement. | Vérifier que le code de ''useObjectA()'' fonctionne correctement. | ||
| + | |||
| + | <hidden Correction> | ||
| + | |||
| + | Le fichier ''main.cpp'' doit corresponder au fichier suivant : | ||
| + | |||
| + | <code cpp> | ||
| + | |||
| + | #include<iostream> // Nécessaire pour std::cout | ||
| + | #include"counter.hpp" | ||
| + | |||
| + | void useObjectA() { | ||
| + | MyCounter Counter1; | ||
| + | MyCounter Counter2; | ||
| + | Counter1.setMax(2); | ||
| + | Counter2.setMax(4); | ||
| + | Counter1.reset(); | ||
| + | Counter2.reset(); | ||
| + | for(unsigned i = 0; i <= 5; i++) { | ||
| + | std::cout | ||
| + | << "Valeur des compteurs (" << Counter1.counter | ||
| + | << ", " << Counter2.counter << ")" << std::endl; | ||
| + | Counter1.increment(); | ||
| + | Counter2.increment(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + | useObjectA(); | ||
| + | return 0; | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | </hidden> | ||
| ==== Question n°4 ==== | ==== Question n°4 ==== | ||
| Line 84: | Line 166: | ||
| Modifier le code pour séparer la déclaration des fonctions membres de leurs implantations. | Modifier le code pour séparer la déclaration des fonctions membres de leurs implantations. | ||
| + | <hidden Correction> | ||
| + | |||
| + | Nous modifions les deux fichiers, le fichier ''counter.hpp'' et un fichier ''counter.cpp''. | ||
| + | |||
| + | Le fichier ''counter.hpp'' contient le code suivant : | ||
| + | |||
| + | <code cpp> | ||
| + | #ifndef CounterHPP | ||
| + | #define CounterHPP | ||
| + | |||
| + | struct MyCounter | ||
| + | { | ||
| + | unsigned counter; | ||
| + | unsigned max; | ||
| + | |||
| + | unsigned getCounter(); | ||
| + | unsigned getMax(); | ||
| + | void increment(); | ||
| + | |||
| + | void reset(); | ||
| + | void set(unsigned value); | ||
| + | void setMax(unsigned value); | ||
| + | }; | ||
| + | |||
| + | #endif | ||
| + | </code> | ||
| + | |||
| + | Le fichier ''counter.cpp'' contient le code suivant. | ||
| + | |||
| + | <code cpp> | ||
| + | |||
| + | #include"counter.hpp" | ||
| + | |||
| + | unsigned MyCounter::getCounter() const | ||
| + | { | ||
| + | return counter; | ||
| + | } | ||
| + | |||
| + | unsigned MyCounter::getMax() const | ||
| + | { | ||
| + | return max; | ||
| + | } | ||
| + | |||
| + | void MyCounter::increment() | ||
| + | { | ||
| + | counter ++; | ||
| + | if(counter > max) | ||
| + | counter = 0; | ||
| + | } | ||
| + | |||
| + | void MyCounter::reset() | ||
| + | { | ||
| + | counter = 0; | ||
| + | } | ||
| + | |||
| + | void MyCounter::set(unsigned value) | ||
| + | { | ||
| + | counter = (value <= max) ? value : counter; | ||
| + | } | ||
| + | |||
| + | void MyCounter::setMax(unsigned value) | ||
| + | { | ||
| + | if(counter >= value) | ||
| + | counter = 0; | ||
| + | max = value;; | ||
| + | } | ||
| + | |||
| + | </code> | ||
| + | |||
| + | </hidden> | ||