What does this code mean exactly?
My book doesn't have the best explanation in the world and I would appreciate it if someone would break this down with as much detail as they can stand, but to give the lazy ones ;) some general guide lines ... what I'm mainly concerned w/ here are the references and operator overloading, but as a new programmer I could benefit from any extra explanations you'd want to add.
#include <iostream>
using namespace std;
class Counter
{
public:
Counter();
~Counter(){}
int GetItsVal() const {return itsVal; }
void SetItsVal(int x) {ivsVal = x; }
const Counter& operator++ (); //prefix
const Counter operator++ (int); //postfix
private:
int itsVal;
};
Counter::Counter:
itsVal(0)
{}
const Counter& Counter::operator++()
{
++itsVal;
return *this;
}
const Counter Counter::operator++(int theFlag)
{
Counter temp(*this);
++itsVal;
return temp;
}
int main()
{
Counter i;
cout << "The value of i is " << i.GetItsVal() << endl;
i++;
cout << The Value of i is " << i.GetItsVal() << endl;
++i;
cout << The Value of i is " << i.GetItsVal() << endl;
Counter a = ++i;
cout << "The value of a: " << a.GetItsVal();
cout << " and i: " << i.GetItsVal() ,, endl:
a = i++
cout << "The value of a: " << a.GetItsVal();
cout << " and i: " << i.GetItsVal() << endl;
return 0;
}