-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOver_loading_of_unary_operator_0.cpp
50 lines (47 loc) · 1.21 KB
/
Over_loading_of_unary_operator_0.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
/* author: jaydattpatel
working of unary operator overloading
unary operators are - the increment (++) and decrement (--) operators, the unary minus (-) operator and the logical not (!)
operator.
In case of overloading of unary operators, the calling operand can be either
left or right side of the operator as in case of increment and decrement
operators. While defining the operator functionality for the class the keyword
operator is used.
*/
#include <iostream>
using namespace std;
class counter
{
unsigned int c;
public:
counter()
{c=0;}
int getcount()
{return c;}
/*
void operator ++()
{ c++;}
*/
counter operator ++()
{
c++;
return counter(c);
}
counter(int num)
{c=num;}
};
int main()
{
counter c1,c2;
++c1;
++c1;
cout<<c1.getcount()<<endl;
/*
c1++;
c2 = c1++;
above c1++ gives error
*/
c2 = ++c1;
cout<<c1.getcount()<<endl;
cout<<c2.getcount();
return 0;
}