-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq19.cpp
72 lines (60 loc) · 1.58 KB
/
q19.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
/* WAP to add two time objects where the data members are hours, minutes,
* seconds. Make use of validation check while entering the time. */
#include <iostream>
using namespace std;
class Time {
public:
int hour;
int min;
int sec;
void validity_check();
void add_time (Time time1, Time time2);
Time() {
hour = 0;
min = 0;
sec = 0;
}
};
void Time::validity_check() {
if (sec > 59) {
min += sec / 60;
sec = sec % 60;
}
if (min > 59) {
hour += min / 60;
min = min % 60;
}
}
void Time::add_time (Time time1, Time time2) {
sec = time1.sec + time2.sec;
min = time1.min + time2.min;
hour = time1.hour + time2.hour;
validity_check();
}
int main() {
Time t1, t2, t3;
cout << "Enter details of time object 1 : " << endl;
cout << "Seconds: \t";
cin >> t1.sec;
cout << "Minutes: \t";
cin >> t1.min;
cout << "Hours: \t\t";
cin >> t1.hour;
cout << "Enter details of time object 2 : " << endl;
cout << "Seconds: \t";
cin >> t2.sec;
cout << "Minutes: \t";
cin >> t2.min;
cout << "Hours: \t\t";
cin >> t2.hour;
t1.validity_check();
t2.validity_check();
cout << "Time 1: \t" << t1.hour << "h " << t1.min << "m " << t1.sec << "s ";
cout << endl;
cout << "Time 2: \t" << t2.hour << "h " << t2.min << "m " << t2.sec << "s ";
cout << endl;
t3.add_time(t1,t2);
cout << "Addition : \t" << t3.hour << "h " << t3.min << "m " << t3.sec << "s ";
cout << endl;
return 0;
}