-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathIncDecOptOverloading-POSTFIX.cpp
71 lines (44 loc) · 1.24 KB
/
IncDecOptOverloading-POSTFIX.cpp
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
using namespace std;
//overloading INCREMENT AND DECREMENT OPERATORS in CPP in POSTFIX form- we need to pass and argumene to the operator function
// --x; means first decrement , then do something -this is prefix form
// ++,-- are UNARY operators as they are performed on a single operand
class Marks {
int mark;
public:
Marks() {
mark=0;
}
Marks(int m) {
mark=m;
}
void getMark() {
cout<<"Marks are: "<<mark<<endl;
}
Marks operator++(int) {
Marks duplicate(*this); //making a duplicate object which is copy of current object
mark += 1;
return duplicate;
}
friend Marks operator--(Marks &m,int) ;
};
Marks operator--(Marks &m,int) {
Marks duplicate(m);
m.mark -= 1;
return duplicate;
}
int main()
{
Marks m(90);
cout<<"Initially ";
m.getMark();
cout<<"----------------------"<<endl;
cout<<"Marks increased by one :"<<endl;
(m++).getMark(); //first old value is printed, then it is increased
m.getMark(); //will now print incremented value
cout<<"-----------------------------------"<<endl;
cout<<"Marks decreased by one : "<<endl;
(m--).getMark();//first old value printed , then decremented
m.getMark();//now decremented value is printed
return 0;
}