All pastes #10732 Raw Edit

Someone

public text v1 · immutable
#10732 ·published 2005-05-01 20:33 UTC
rendered paste body
#include <iostream>
#include <cctype>
using namespace std;

class CCounter 
{
private:
  int f_MaxValue;
  int f_Counter;
  bool f_OverflowFlag;

  bool overflow() 
  {
    return f_OverflowFlag;
  }
  void increment(int inc) {
    int newCounter = f_Counter + inc;
    // if smaller - int limits overflow
    if(newCounter < f_Counter ||
     // if MaxValue is not -1, check for its overflow
       (f_MaxValue != -1 && newCounter > f_MaxValue)) 
	   {
         f_OverflowFlag = true;
         newCounter = newCounter - f_MaxValue;
     }
         f_Counter = newCounter;
  }
public:
  CCounter(int max) : f_MaxValue(max), f_Counter(0), f_OverflowFlag(false)
  {}
  CCounter() : f_MaxValue(-1), f_Counter(0), f_OverflowFlag(false) // no max value
  {}
  ~CCounter() { }

  void reset() 
  {
    f_Counter = 0;
    f_OverflowFlag = false;
  }
  void incrcents() 
  {
    increment(1);
  }
  void incrdimes() 
  {
    increment(10);
  }
  void incrdollars() 
  {
    increment(100);
  }
  void incrtendollars() 
  {
    increment(1000);
  }

  void reportoverflow() 
  {
    if(overflow())
      cout << "Overflow" << endl;
    else
      cout << "No Overflow" << endl;
  }
  void currentamount() 
  {
    cout << f_Counter/100 << " dollars and " << f_Counter%100 << " cents. " << endl;
  }
};

int main()
{
  CCounter asdf;
  asdf.incrcents();
  asdf.incrdimes();
  asdf.incrdollars();
  asdf.incrtendollars();
  return 0;
}//end main