-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathq4.cpp
59 lines (54 loc) · 1.05 KB
/
q4.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
// WAP to overload unary operator(++, --)
#include <iostream>
using namespace std;
class Time
{
private:
int hour, minute, second;
public:
Time()
{
hour = minute = second = 0;
}
Time(int _hour, int _minute, int _second)
{
hour = _hour;
minute = _minute;
second = _second;
}
void display()
{
cout << "Hour: " << hour << endl;
cout << "minute: " << minute << endl;
cout << "second: " << second << endl;
}
Time operator++()
{
return Time(++hour, ++minute, ++second);
}
Time operator--(int)
{
return Time(hour--, minute--, second--);
}
friend Time operator++(Time &, int);
friend Time operator--(Time &);
};
Time operator++(Time &obj, int)
{
return Time(obj.hour++, obj.minute++, obj.second++);
}
Time operator--(Time &obj)
{
return Time(--obj.hour, --obj.minute, --obj.second);
}
int main()
{
Time t1(1,2,3);
t1++;
t1.display();
t1--;
t1.display();
Time t3 = t1--;
t3.display();
t1.display();
}