-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOverloading.cpp
More file actions
72 lines (57 loc) · 1.12 KB
/
Copy pathPlusOverloading.cpp
File metadata and controls
72 lines (57 loc) · 1.12 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
61
62
63
64
65
66
67
68
69
70
71
72
// operator+ overloading in cpp
#include <iostream>
using namespace std;
class Time
{
private:
int h,m,s;
public:
void setTime(int h,int m,int s)
{
this->h = h;
this->m = m;
this->s = s;
}
void showTime()
{
cout<<"Time "<<h<<":"<<m<<":"<<s<<endl;
}
//this is for desire time format
void normalize()
{
m = m+s/60;
s = s%60;
h = h+m/60;
m = m%60;
}
//overloading of operator+
Time operator+(Time t)
{
Time temp;
temp.s = s+t.s;
temp.m = m+t.m;
temp.h = h+t.h;
temp.normalize();
return temp;
}
};
int main()
{
int H,M,S;
Time t1,t2,t3;
cout<<"\tTime addition"<<endl;
cout<<"Enter Time 1 "<<endl;
cin>>H>>M>>S;
t1.setTime(H,M,S);
cout<<"Enter Time 2 "<<endl;
cin>>H>>M>>S;
t2.setTime(H,M,S);
cout<<"Time fist "<<endl;
t1.showTime();
cout<<"Time second "<<endl;
t2.showTime();
cout<<"adding time "<<endl;
//t3 = t1.add(t2);
t3 = t1+t2; //this is instead of t1.operator+(t2);
t3.showTime();
}