-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime.cpp
48 lines (42 loc) · 1.11 KB
/
Time.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
#include "Time.h"
// Constructor
Time::Time(int h, int m) : hour(h), minute(m) {
// Call overflow and underflow handlers to normalize time
handleOverflow();
handleUnderflow();
}
// Overload + operator
Time Time::operator+(const Time& t) const {
Time temp;
temp.hour = hour + t.hour;
temp.minute = minute + t.minute;
temp.handleOverflow();
return temp;
}
// Overload - operator
Time Time::operator-(const Time& t) const {
Time temp;
temp.hour = hour - t.hour;
temp.minute = minute - t.minute;
temp.handleUnderflow();
return temp;
}
// Function to handle time overflow (when minutes exceed 60)
void Time::handleOverflow() {
if (minute >= 60) {
hour += minute / 60;
minute %= 60;
}
}
// Function to handle time underflow (when minutes are negative)
void Time::handleUnderflow() {
if (minute < 0) {
int borrowHours = (-minute + 59) / 60;
hour -= borrowHours;
minute += borrowHours * 60;
}
}
// Display the time
void Time::display() const {
std::cout << "Time: " << hour << " hours, " << minute << " minutes" << std::endl;
}