increment.cpp

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc2040/fall2026/notes/words/code/increment.cpp
 
#include <iostream>

using namespace std;

class FooT {
   public:
      int Data()const{
         return data;
      }
      void Data(int newValue)  {
         data = newValue;
      }

      FooT & operator ++(){
         ++data;
         return *this;
      }

      FooT operator ++(int) {
         FooT originalValue{*this};
         ++data;
         return originalValue;
      }

   private:
      int data {0};
};

ostream & operator << (ostream & s, const FooT & other) {
    s << other.Data();
    return s;
}

FooT & GoodFunction(FooT & other){
    return other;
}

FooT & BadFunction(FooT other){
    return other;
}

int main() {
    //int i{10};
    FooT i;
    cout << "j = i" << endl;
    //int j{i};
    FooT j{i};
    cout << " i = " << i << " j = " << j << endl << endl;

    cout << "j = ++i" << endl;
    j = ++i;
    cout << " i = " << i << " j = " << j << endl << endl;

    cout << "j = i++" << endl;
    j = i++;
    cout << " i = " << i << " j = " << j << endl << endl;

    return 0;
}