#include /****************************************************** * Please read 4.8 Increment-only counters first. * * This implementation attempts to mimic the behavior * of pointers by using *a, ++a, and a++ for the * operations on a counter that can only be incremented * by one (1). * * In the previous version, we added two member * functions: * * increment(); * value() const; * * The first incremented the counter, while the second * returned its current value. * * We will now replace these member functions by * overloading three operators: * * ++a prefix increment * a++ postfix increment * *a retrieve the current value * * The prefix and postfix increment operators both * increment the counter by one. The difference is in * the value they return: * * ++a increments and returns the new value * a++ increments but returns the old value * * The * operator does not modify the counter and * returns its current value. ******************************************************/ // Class declaration class Increment_only_counter; // Function declaration int main(); // Class definition class Increment_only_counter { public: Increment_only_counter(); unsigned int operator++(); unsigned int operator++( int ); unsigned int operator*() const; private: unsigned int counter_; }; // Function definition int main() { Increment_only_counter a{}; // Here, '*a' calls a.operator*() to retrieve the // current value of the counter. std::cout << "Initially, the counter is " << *a << "." << std::endl; // Demonstrate prefix increment. // // Here, '++a' calls a.operator++(). The counter is // incremented first and the new value is returned. std::cout << "The value returned by ++a is " << ++a << "." << std::endl; std::cout << "The counter is now " << *a << "." << std::endl; // Demonstrate postfix increment. // // Here, 'a++' calls a.operator++( 0 ). The old value // is returned, but the counter is still incremented. std::cout << "The value returned by a++ is " << a++ << "." << std::endl; std::cout << "The counter is now " << *a << "." << std::endl; // Start with a new counter for the actual example. Increment_only_counter count{}; for ( int k{17}; k <= 1532; ++k ) { if ( k%17 == 0 ) { // Prefix increment calls count.operator++(). ++count; } } // Here, '*count' calls count.operator*(). std::cout << "There are " << *count << " numbers divisible by 17 between " << "17 and 1532, inclusive." << std::endl; return 0; } // Member function definitions // Construct an increment-only counter whose initial // value is zero. Increment_only_counter::Increment_only_counter(): counter_{ 0 } { // Empty constructor } // Prefix increment: ++a // // Increment the counter and return the new value. unsigned int Increment_only_counter::operator++() { ++counter_; return counter_; } // Postfix increment: a++ // // The dummy int parameter distinguishes the postfix // increment operator from the prefix increment operator. // The argument is not otherwise used. // // Return the old value and then increment the counter. unsigned int Increment_only_counter::operator++( int ) { unsigned int old_counter{ counter_ }; ++counter_; return old_counter; } // Dereference: *a // // Return the current value of the counter without // modifying the object. unsigned int Increment_only_counter::operator*() const { return counter_; }