User Tools

Site Tools


in204:tds:sujets:td2:part3

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
in204:tds:sujets:td2:part3 [2019/09/23 17:08]
bmonsuez [Partie III – Surcharge & Masquage]
in204:tds:sujets:td2:part3 [2022/11/18 10:49] (current)
Line 15: Line 15:
         counter <- counter + value         counter <- counter + value
     sinon     sinon
-        counter = counter + value mod max+        counter = (counter + valuemod max
  
 </​code>​ </​code>​
 +
 +<hidden Correction>​
 +
 +<code cpp>
 +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;
 +    }
 +};
 +
 +</​code>​
 +
 +</​hidden>​
 +
  
 ==== Question n°2 ==== ==== Question n°2 ====
Line 27: Line 70:
     for(unsigned i = 0; i <= 5; i++)      for(unsigned i = 0; i <= 5; i++) 
     {     {
-   ​bidiCounter1.increment(5);​+ bidiCounter1.increment(5);​
         bidiCounter1.print();​         bidiCounter1.print();​
     }     }
Line 49: Line 92:
  
 </​code>​ </​code>​
- 
  
 Expliquer pourquoi cela ne fonctionne pas ? Proposer une modification de l’appel pour que cela puisse fonctionner. Expliquer pourquoi cela ne fonctionne pas ? Proposer une modification de l’appel pour que cela puisse fonctionner.
-Question n°4 :  ​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().+ 
 +<hidden Correction>​ 
 + 
 +La nouvelle méthode ''​increment(unsigned)''​ cache l'​ensemble des méthodes ''​increment''​ définies dans la classe de base ''​MyCounter''​. Il faut dans ce cas ajouter à la classe : 
 + 
 +<code cpp> 
 +public: 
 +    using MyCounter::​increment;​ 
 +</​code>​ 
 + 
 +Ce qui nous donne la classe suivante : 
 + 
 +<code cpp> 
 +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 --; 
 +    } 
 +    using MyCounter::​increment;​ 
 +    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;​ 
 +    } 
 +}; 
 + 
 + 
 +</​code>​ 
 + 
 +</​hidden>​ 
 + 
 + 
 +==== Question n°4 ==== 
 + 
 +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()''​.
  
in204/tds/sujets/td2/part3.1569258518.txt.gz · Last modified: 2019/09/23 17:08 by bmonsuez