-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperatoroverloading1.cpp
More file actions
47 lines (41 loc) · 1.07 KB
/
operatoroverloading1.cpp
File metadata and controls
47 lines (41 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Overloading ++ operator
#include <iostream>
using namespace std;
class overload {
private:
int count;
public:
overload(int i) : count(i) {}
// pre/post指的是increment在return前还是后
// return old value, then increment(postfix, i++)
overload operator++(int) { // dummy int isn't used, it just tells the compiler "this is the postfix form"
overload temp = *this;
++count;
return temp; // the value we return is the old value
}
// increment, then return new value(prefix, ++i)
overload& operator++() {
++count;
return *this;
}
void Display() { cout << count << endl; }
};
int main()
{
overload i(5);
overload post(5);
overload pre(5);
pre = ++i; // i.operator++(); for compiler
cout << "i.count = ";
i.Display();
cout << "pre.count = ";
pre.Display();
i++; // just to show diff
i++; // just to show diff
post = i++; // i.operator(0); for compiler
cout << "post.count = ";
post.Display();
cout << "i.count = ";
i.Display();
return 0;
}