User Tools

Site Tools


in204:tds:td2:counter

Code ''MyCounter''

#ifndef MYCOUNTER_HPP
#define MYCOUNTER_HPP
 
#include<iostream>
 
class MyCounter
{
protected:
    unsigned counter;
    unsigned max;
 
public:
    unsigned getCounter() const { return counter; }
    unsigned getMax() const { return max; }
    void increment()
    {
       if(counter == max)
           counter = 0;
        else
           counter ++;
    }
    void reset() { counter = 0; }
    void set(unsigned value) { counter = value; }
    void setMax(unsigned value)
    {
        max = value;
        if(value > counter)
            counter = counter % max;
    }
 
    MyCounter(): counter(0), max(0) {}
    MyCounter(unsigned theCounter,
        unsigned theMax): counter(theCounter), max(theMax)
    {}
    explicit MyCounter(unsigned theMax):
        max(theMax), counter(0)
    {}
    MyCounter(const MyCounter& anotherCounter):
        counter(anotherCounter.counter),
        max(anotherCounter.max)
    {}
    ~MyCounter()
    {}
};
in204/tds/td2/counter.txt · Last modified: 2022/11/18 10:48 (external edit)