In this post, I have focused on some proficiency in C++. You can solve this easy question using another approach but you will learn something new today.
TOPICS: padding, overloading, modulo use, output formatting, C++ features- friend function
Let’s overload some operators today.
In the given question, you have to use class (named time) , operator overloading to print the object directly(overload << operator).
NOTE: The ahead time can even have a parameter(hour, minutes or second) more than 60.
Don’t use this logic in overloading of + operator(+1 to HH and -60 to MM or so). Think in a better way.
You are given two clocks- A and B. The clocks show time in HH : MM : SS format. One of these clocks denote the time at GMT and another the time it is ahead of GMT. You have a hi-tech clock. Now you are at an island having these two watches. As soon as you add the time in both clocks- A and B and set it to your watch, a message will be sent to the helpers. You want to leave this sinking island.
Now make a program for the above case.
In input, you will get two lines of integers- first denoting the GMT time and second the ahead time.
INPUT:
11 22 57
02 55 07
OUTPUT:
14 18 04
The code is given below:
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
class time1
{
private: int hh,mm,ss;
public:
time1()
{
hh=mm=ss=0;
}
time1(int hh,int mm,int ss)
{
this->hh=hh;
this->mm=mm;
this->ss=ss;
}
time1 operator +(time1 &t)
{
time1 tmp;
tmp.mm+= (ss+ t.ss)/60;
tmp.ss+=(ss+ t.ss)%60;
tmp.hh=hh+ t.hh;
tmp.hh+= (mm+ t.mm)/60;
tmp.mm+= (mm+ t.mm)%60;
return tmp;
}
friend istream &operator >>(istream &input,time1 &t);
friend ostream &operator <<(ostream &output,time1 &t);
};
ostream &operator <<(ostream &output,time1 &t)
{
output<<setw(2)<<t.hh<<” “<<setw(2)<<t.mm<<” “<<setw(2)<<t.ss<<‘\n’;
return output;
}
istream &operator >>(istream &input,time1 &t)
{
cin>>t.hh>>t.mm>>t.ss;
return input;
}
int main()
{
cout.fill(‘0’);
time1 t1;
time1 t2;
time1 t3;
cin>>t1;
cin>>t2;
t3=t1+t2;
cout<<t3;
}