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/10/01 18:29]
bmonsuez [Question n°1]
in204:tds:sujets:td2:part3 [2022/11/18 10:49] (current)
Line 20: Line 20:
  
 <hidden Correction>​ <hidden Correction>​
 +
 <code cpp> <code cpp>
 class MyBiDiCounter:​ public MyCounter class MyBiDiCounter:​ public MyCounter
Line 57: Line 58:
  
 </​code>​ </​code>​
 +
 +</​hidden>​
 +
  
 ==== Question n°2 ==== ==== Question n°2 ====
Line 66: 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 88: 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.
 +
 +
 +<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>​
  
  
in204/tds/sujets/td2/part3.1569954591.txt.gz · Last modified: 2019/10/01 18:29 by bmonsuez