-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeDataStructure.cpp
More file actions
60 lines (52 loc) · 1.17 KB
/
TimeDataStructure.cpp
File metadata and controls
60 lines (52 loc) · 1.17 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
#include<iostream>
using namespace std;
class Time{
private:
int hr, min, sec;
public:
void show_time();
void set_time(int, int, int);
void normalize();
Time operator+(Time);
};
void Time :: set_time(int h, int m, int s){
/* Takes in input of hour, minutes and seconds
from the user and assigns to the variables
hr, min, sec respectively*/
hr = h;
min = m;
sec = s;
normalize();
}
void Time :: show_time(){
/* Outputs the normalized time on screen */
normalize();
cout<<hr<<":"<<min<<":"<<sec<<endl;
}
void Time :: normalize(){
/* Normalizes the time */
int temp = 0;
min += sec/60;
sec = sec % 60;
hr += min/60;
min = min % 60;
}
Time Time :: operator+(Time t2){
/* Performs the addition of two different Time
data srtuctures */
Time t;
t.hr = hr + t2.hr;
t.min = min + t2.min;
t.sec = sec + t2.sec;
t.normalize();
return t;
}
int main(){
Time t1;
t1.set_time(4, 100, 100);
t1.show_time();
Time t2;
t2.set_time(1, 20, 6);
Time t3 = t1+t2;
t3.show_time();
}