9: public:
10: Counter();
11: ~Counter(){}
12: int GetItsVal()const { return itsVal; }
13: void SetItsVal(int x) {itsVal = x; }
14: const Counter& operator++ (); // prefix
15: const Counter operator++ (int); // postfix
16:
17: private:
18: int itsVal;
19: };
20:
21: Counter::Counter():
22: itsVal(0)
23: {}
24:
25: const Counter& Counter::operator++()
26: {
27: ++itsVal;
28: return *this;
29: }
30:
31: const Counter Counter::operator++(int theFlag)
32: {
33: Counter temp(*this);
34: ++itsVal;
35: return temp;
36: }
37:
38: int main()
39: {
40: Counter i;
41: cout << “The value of i is “ << i.GetItsVal() << endl;
42: i++;
43: cout << “The value of i is “ << i.GetItsVal() << endl;
44: ++i;
45: cout << “The value of i is “ << i.GetItsVal() << endl;
46: Counter a = ++i;
47: cout << “The value of a: “ << a.GetItsVal();
48: cout << “ and i: “ << i.GetItsVal() << endl;
49: a = i++;
50: cout << “The value of a: “ << a.GetItsVal();
51: cout << “ and i: “ << i.GetItsVal() << endl;
52: return 0;
53: }
312 Day 10
LISTING10.12 continued