-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_Time_displays_in_HH_MM_SS_format.cpp
64 lines (64 loc) · 1.31 KB
/
class_Time_displays_in_HH_MM_SS_format.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
#include<iostream>
#include<iomanip>
using namespace std;
class Time
{
int min,sec,hour;
public:
void getdata()
{
cout<<"Enter seconds : ";
cin>>sec;
cout<<"Enter minutes : ";
cin>>min;
cout<<"Enter hour : ";
cin>>hour;
if(sec>59)
{
min+=sec/60;
sec=sec%60;
}
if(min>59)
{
hour+=min/60;
min=min%60;
}
if(hour>23)
hour=hour%24;
}
Time operator+(Time&o1)
{
Time temp;
temp.sec=o1.sec+sec;
temp.min=o1.min+min;
temp.hour=o1.hour+hour;
if(temp.sec>60)
{
temp.min+=temp.sec/60;
temp.sec=temp.sec%60;
}
if(temp.min>59)
{
temp.hour+=temp.min/60;
temp.min=temp.min%60;
}
if(temp.hour>23)
temp.hour=temp.hour%24;
return temp;
}
void display()
{
cout<<setfill('0') << setw(2) << hour<<":"<<setfill('0') << setw(2) << min<<":"<<setfill('0') << setw(2) << sec<<endl;
}
};
int main()
{
Time o1,o2,o3;
o1.getdata();
o2.getdata();
o3=o1+o2;
o1.display();
o2.display();
o3.display();
return 0;
}