-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathIncDecOptOverloading.cpp
65 lines (40 loc) · 1.04 KB
/
IncDecOptOverloading.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
#include<iostream>
//overloading INCREMENT AND DECREMENT OPERATORS in CPP in PREFIX form
// --x; means first decrement , then do something -this is prefix form
// ++,-- are UNARY operators as they are performed on a single operand
using namespace std;
class Marks {
int mark;
public:
Marks() {
mark=0;
}
Marks(int m) {
mark=m;
}
void getmark() {
cout<<"Marks are: "<<mark<<endl;
}
Marks operator ++ () {
mark += 1 ;// mark = mark+1
return *this; //this returns the address of the current object of type Marks
}
friend Marks operator--(Marks &m);
};
Marks operator--(Marks &m) {
Marks temp(m);
temp.mark -= 1;
return temp;
}
int main () {
Marks m(80);
cout<<"Initially ";
m.getmark();
cout<<"----------------------"<<endl;
cout<<"Marks increased by one :"<<endl;
(++m).getmark(); //overloading ++ operator in prefix
cout<<"-----------------------------------"<<endl;
cout<<"Marks decreased by one : "<<endl;
(--m).getmark(); //overloading -- operator in prefix
return 0;
}