-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-Sub_time_constructor.cpp
More file actions
79 lines (70 loc) · 1.52 KB
/
Add-Sub_time_constructor.cpp
File metadata and controls
79 lines (70 loc) · 1.52 KB
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
72
73
74
75
76
77
#include <iostream>
using namespace std;
class Time
{
private:
int second, minute, hour;
public:
Time(int a, int b, int c)
{
second = a;
minute = b;
hour = c;
}
Time() {}
void display()
{
cout << "Time is: " << hour << ":" << minute << ":" << second << endl;
}
Time add(Time d)
{
Time temp;
temp.second = second + d.second;
temp.minute = minute + d.minute;
temp.hour = hour + d.hour;
if (temp.second >= 60)
{
temp.minute += temp.second / 60;
temp.second = temp.second % 60;
}
if (temp.minute >= 60)
{
temp.hour += temp.minute / 60;
temp.minute = temp.minute % 60;
}
return temp;
}
Time sub(Time t)
{
Time temp;
temp.second = second - t.second;
temp.minute = minute - t.minute;
temp.hour = hour - t.hour;
if (temp.second < 0)
{
temp.minute -= 1;
temp.second += 60;
}
if (temp.minute < 0)
{
temp.hour -= 1;
temp.minute += 60;
}
if(temp.hour < 0)
{
temp.hour += 12;
}
return temp;
}
};
int main()
{
Time t1(50,45,4), t2(20, 40, 3), t3, t4;
t3 = t1.add(t2);
t4 = t1.sub(t2);
cout << "Addition of time: " << endl;
t3.display();
cout << "Subtraction of time: " << endl;
t4.display();
return 0;
}