-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy path05_homework_06_answer.cpp
102 lines (83 loc) · 1.97 KB
/
05_homework_06_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <bits/stdc++.h>
using namespace std;
class Time {
private:
int hours;
int minutes;
int seconds;
public:
Time(int hours, int minutes, int seconds) {
SetTime(hours, minutes, seconds);
}
void SetTime(int hours, int minutes, int seconds) {
this->hours = hours;
this->minutes = minutes;
this->seconds = seconds;
}
int GetTotalSeconds() {
return hours * 60 * 60 + minutes * 60 + seconds;
}
int GetTotalMinutes() {
return hours * 60 + minutes;
}
void PrintHHMMSS() {
cout<<ToSring(":")<<"\n";
}
string ToSring(string seperator = "-") {
ostringstream oss;
oss << hours << seperator << minutes << seperator << seconds;
return oss.str();
}
int GetHours() {
return hours;
}
Time& SetHours(int hours) {
if (hours < 0)
hours = 0;
this->hours = hours;
return *this;
}
int GetMinutes() {
return minutes;
}
Time& SetMinutes(int minutes) {
if (minutes < 0)
minutes = 0;
this->minutes = minutes;
return *this;
}
int GetSeconds() {
return seconds;
}
Time& SetSeconds(int seconds) {
if (seconds < 0)
seconds = 0;
this->seconds = seconds;
return *this;
}
};
/*
what does it mean to do
1) t.SetHours(5).Something?
.Something is a function, then it needs object
then t.SetHours(5) must return object of type time NOT void
So we need to return object from these setters
2) Now each object has pointer to itself this
*this is the object
We can return this but there is a problem
It will be a copy, so NOT same
This means we are modifying some temporary object NOT t itself!
To solve that, return the object by reference: Time&T
So overall
Time& SomeSetter
return *this
Q: Was it possible / More OOP to add const to return?:
const Time& SetMinutes(int minutes)
*/
int main() {
Time t(3, 1, 2);
t.PrintHHMMSS(); // 3:1:2
t.SetHours(5).SetMinutes(45).SetSeconds(13);
t.PrintHHMMSS(); // 5:45:13
return 0;
}