#include #include using namespace std; template ::min(), T max=numeric_limits::max()> class Modular { public: explicit Modular(T n) : val{n} { if ( n < min || n > max ) val = min; } Modular & operator=(T const & n) { if ( n < min || n > max) throw domain_error{"Value out of range"}; val = n; return *this; } operator T() const { return val; } template Modular operator+(Modular const & rhs) const { Modular m{adjust(val+rhs)}; return m; } Modular & operator++() { if ( val == max ) val = min; else ++val; return *this; } Modular operator++(int) { auto tmp = *this; ++*this; return tmp; } private: T adjust(T n) const { if ( n < max && n > min ) { return n; } else if (n > max ) { return min + n - max - 1; } return max + n - min + 1; } T val; }; int main() { Modular m{3}; cout << m << endl; try { m = 1; } catch ( std::domain_error const & e) { cout << e.what() << endl; } Modular m2 {5}; m = m + m2; cout << m << endl; m = m + Modular{-10}; cout << "Should be 7: " << m << endl; cout << "Should print 7 9: " << m++ << " "; cout << ++m; Modular mc{'b'}; ++mc; cout << "\nShould print a: " << ++mc << endl; }