This is an old revision of the document!
Ajouter à la classe MyBiDiCounter
une nouvelle méthode :
increment(unsigned value) si counter + value <= max counter <- counter + value sinon counter = (counter + value) mod max
<hidden Correction>
class MyBiDiCounter: public MyCounter { public: MyBiDiCounter(): MyCounter() {} explicit MyBiDiCounter(unsigned theMax): MyCounter(theMax) {} MyBiDiCounter(unsigned theCounter, unsigned theMax): MyCounter(theCounter, theMax) {} MyBiDiCounter(const MyBiDiCounter& anotherCounter): MyCounter(anotherCounter.counter) {} void decrement() { if(counter == 0) counter = max; else counter --; } void increment(unsigned value) { if(counter + value < max) counter += value; else counter = (counter + value) % max; } void print() const { std::cout << "Compteur: " << counter << "/" << max << std::endl; } };
Tester le bon fonctionnement de cette classe à partir du code suivant :
void testNewIncMethod() { MyBiDiCounter bidiCounter1(0, 5); for(unsigned i = 0; i <= 5; i++) { bidiCounter1.increment(5); bidiCounter1.print(); } }
Tester le code suivant.
void testOldIncMethod() { MyBiDiCounter bidiCounter1(0, 5); for(unsigned i = 0; i <= 5; i++) { bidiCounter1.increment(); bidiCounter1.print(); } }
Expliquer pourquoi cela ne fonctionne pas ? Proposer une modification de l’appel pour que cela puisse fonctionner.
Modifier la classe MyBiDiCounter
de manière à ce que les deux méthodes soient accessibles, à la fois la méthode increment()
et la méthode increment(unsigned)
.
Tester ensuite que le code initial de la fonction testOldIncMethod()
.