Unary increment (++) and decrement (--) operator overloading program in c++.
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
48
| /*C++ program for unary increment (++) and decrement (--) operator overloading.*/#include<iostream>using namespace std;class NUM{ private: int n; public: //function to get number void getNum(int x) { n=x; } //function to display number void dispNum(void) { cout << "value of n is: " << n; } //unary ++ operator overloading void operator ++ (void) { n=++n; } //unary -- operator overloading void operator -- (void) { n=--n; }};int main(){ NUM num; num.getNum(10); ++num; cout << "After increment - "; num.dispNum(); cout << endl; --num; cout << "After decrement - "; num.dispNum(); cout << endl; return 0;} |
After increment - value of n is: 11
After decrement - value of n is: 10