This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
in204:tds:sujets:td1:part1 [2019/09/23 15:48] bmonsuez [Question n°1] |
in204:tds:sujets:td1:part1 [2022/11/18 10:50] (current) |
||
---|---|---|---|
Line 3: | Line 3: | ||
[[in204:tds:sujets:td1|TD1]] | [[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 46: | Line 46: | ||
}; | }; | ||
</code> | </code> | ||
+ | |||
+ | <hidden Correction> | ||
+ | |||
+ | Le fichier ''counter.hpp'' contient a priori le code suivant : | ||
+ | |||
+ | <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> | ||
+ | |||
+ | 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 72: | 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 81: | Line 168: | ||
<hidden Correction> | <hidden Correction> | ||
- | Nous créons deux fichiers, un fichier ''.hpp'' et un fichier ''.cpp''. | + | Nous modifions les deux fichiers, le fichier ''counter.hpp'' et un fichier ''counter.cpp''. |
- | Le fichier ''.hpp'' contient le code suivant : | + | Le fichier ''counter.hpp'' contient le code suivant : |
<code cpp> | <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> | </code> | ||
+ | |||
+ | Le fichier ''counter.cpp'' contient le code suivant. | ||
<code cpp> | <code cpp> | ||
- | #include"counter.cpp" | + | #include"counter.hpp" |
unsigned MyCounter::getCounter() const | unsigned MyCounter::getCounter() const |