This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
in202:seance_2:td_2:part_i [2021/04/01 10:02] bmonsuez [Liens vers les éléments syntaxiques C++] |
in202:seance_2:td_2:part_i [2025/04/07 07:49] (current) bmonsuez [Question n° 1.1] |
||
---|---|---|---|
Line 27: | Line 27: | ||
Ajouter les fonctions membres (méthodes) ''decrement()'' et ''print()''. Indiquer si ces méthodes modifient les champs de l'objet ou pas en ajoutant le qualificateur ''const''. | Ajouter les fonctions membres (méthodes) ''decrement()'' et ''print()''. Indiquer si ces méthodes modifient les champs de l'objet ou pas en ajoutant le qualificateur ''const''. | ||
+ | |||
+ | <hidden Correction> | ||
+ | |||
+ | <code cpp> | ||
+ | class MyCounter | ||
+ | { | ||
+ | protected: | ||
+ | int _counter; | ||
+ | int _max; | ||
+ | |||
+ | public: | ||
+ | MyCounter(): _counter(0), _max(0) {} | ||
+ | explicit MyCounter(int theMaxValue): _counter(0), _max(theMaxValue) {} | ||
+ | MyCounter(int theCounterValue, int theMaxValue): | ||
+ | _counter(0), _max(theMaxValue) | ||
+ | {} | ||
+ | MyCounter(const MyCounter& anotherCounter): | ||
+ | _counter(anotherCounter._counter), | ||
+ | _max(anotherCounter._max) | ||
+ | {} | ||
+ | |||
+ | int getCounter() const { return _counter; } | ||
+ | int getMax() const { return _max; } | ||
+ | |||
+ | void increment() | ||
+ | { | ||
+ | _counter = _counter < _max ? _counter + 1 : 0; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | class MyBiDiCounter: public MyCounter | ||
+ | { | ||
+ | public: | ||
+ | void decrement() | ||
+ | { | ||
+ | _counter = _counter > 0 ? _counter -1 : _max; | ||
+ | } | ||
+ | |||
+ | void print() | ||
+ | { | ||
+ | std::cout << "(" << _counter << "/" << _max << ")" << std::endl; | ||
+ | } | ||
+ | }; | ||
+ | </code> | ||
+ | |||
+ | </hidden> | ||
+ | |||
==== Question n° 1.2 ==== | ==== Question n° 1.2 ==== |