-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path05_homework_07_answer.cpp
77 lines (70 loc) · 1.93 KB
/
05_homework_07_answer.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
66
67
68
69
70
71
72
73
74
75
76
77
#include <bits/stdc++.h>
using namespace std;
class Time {
private:
int total_seconds;
public:
Time(int hours, int minutes, int seconds) {
// Delegation: give another object/function the task to do it for you
SetTime(hours, minutes, seconds);
}
void SetTime(int hours, int minutes, int seconds) {
total_seconds = 0;
SetHours(hours);
SetMinutes(minutes);
SetSeconds(seconds);
}
int GetTotalSeconds() {
return total_seconds;
}
int GetTotalMinutes() {
return GetHours() * 60 + GetMinutes();
}
void PrintHHMMSS() {
cout << ToSring(":") << "\n";
}
string ToSring(string seperator = "-") {
ostringstream oss;
oss << GetHours() << seperator << GetMinutes() << seperator << GetSeconds();
return oss.str();
}
int GetHours() {
return total_seconds / (60 * 60);
}
void SetHours(int hours) {
if (hours < 0)
hours = 0;
total_seconds += (hours - GetHours()) * 60 * 60;
}
int GetMinutes() {
return (total_seconds % (60 * 60)) / 60;
}
void SetMinutes(int minutes) {
if (minutes < 0)
minutes = 0;
total_seconds += (minutes - GetMinutes()) * 60;
}
int GetSeconds() {
return total_seconds % 60;
}
void SetSeconds(int seconds) {
if (seconds < 0)
seconds = 0;
total_seconds += (seconds - GetSeconds());
}
};
// In previous code, all functions use directly the 3 integers
// It might be a good practice to depend on the available setters and getters
// Imagine a real Time class with 20 functions that use minutes, which now doesn't exist
// If they all use GetMinutes, no one of them will be changed
// Be careful with data members that might change or keep depending on setters/getters
// As you see, we could even provide setters & getters for variables that doesn't exist
// Code "flexibility" is important
int main() {
Time t(0, 0, 0);
t.SetMinutes(1);
t.SetSeconds(60);
t.SetHours(2);
cout << t.GetTotalSeconds();
return 0;
}