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 09:59] bmonsuez [Code présenté pendant le cours] |
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 ==== | ||
| Line 89: | Line 136: | ||
| {{https://perso.ensta-paris.fr/~bmonsuez/Media/code/in202/2021/td2/part1/part1.cpp|''part1.cpp''}} Fichier principal contient les fonctions de test et le main. | {{https://perso.ensta-paris.fr/~bmonsuez/Media/code/in202/2021/td2/part1/part1.cpp|''part1.cpp''}} Fichier principal contient les fonctions de test et le main. | ||
| - | {{https://perso.ensta-paris.fr/~bmonsuez/Media/code/in202/2021/td2/part1/MyCounter.hpp.hpp|''MyCounter.hpp''}} Définition de la classe de base ''MyCounter''. | + | {{https://perso.ensta-paris.fr/~bmonsuez/Media/code/in202/2021/td2/part1/MyCounter.hpp|''MyCounter.hpp''}} Définition de la classe de base ''MyCounter''. |
| {{https://perso.ensta-paris.fr/~bmonsuez/Media/code/in202/2021/td2/part1/MyBiDiCounter.hpp|''MyBiDiCounter.hpp''}} Définition de la classe dérivée ''MyBiDiCounter''. | {{https://perso.ensta-paris.fr/~bmonsuez/Media/code/in202/2021/td2/part1/MyBiDiCounter.hpp|''MyBiDiCounter.hpp''}} Définition de la classe dérivée ''MyBiDiCounter''. | ||
| Line 99: | Line 146: | ||
| [[cpp:syntax:class:deriving:methods|Champs & methodes dans les classes dérivées]] | [[cpp:syntax:class:deriving:methods|Champs & methodes dans les classes dérivées]] | ||
| - | [[cpp:syntax:functions:overloard|La surcharge de fonctions en C++]] | + | [[cpp:syntax:functions:overload|La surcharge de fonctions en C++]] |
| ===== Navigation ===== | ===== Navigation ===== | ||
| [[.:part_ii|Deuxième partie]] | [[.:part_ii|Deuxième partie]] | ||