Please realize the following functions with operator overloading: input and output time with >> and << use += and -= increase and decrease the time ,e.g.Time& operator+=(const Time&);Time& operator-=(const Time&);

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter12: Adding Functionality To Your Classes
Section: Chapter Questions
Problem 1PP
icon
Related questions
Question
100%

【Description】

Write a program to define a class Time, which contains three member variables: hour, minute and second

Please realize the following functions with operator overloading:

  • input and output time with >> and <<

  • use += and -= increase and decrease the time ,e.g.Time& operator+=(const Time&);Time& operator-=(const Time&);

     

【Input】

  • There are two line, each line is the time of a clock

  • Input format:hour minute second。

【Output】

  • There are two lines.

  • The first line is the output of time1 += (time2) ;

  • The second line is the output of time1 -= time2 ;

  • Output format:hour:minute:second;

  • Please read the 

【Input example】

21 10 35

10 15 25

【Output example】

07:26:00

21:10:34

THE SOLUTION YOU SENT ME IS NOT GIVING THE REQUIRED OUTPUTS. PLEASE LOOK AND READ THE INPUT AND THE OUTPUT SAMPLES ABOVE CAREFULLY.

BELOW IS THE SLOTION YOU GAVE ME.

#include<iostream>
using namespace std;
class Time
{
 
inthour;
intminute;
intsecond;
 
public:
Time(){}
 
Time(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
 
 
friendistream&operator >>(istream&in,Time&t)
{
in>>t.hour>>t.minute>>t.second;
return in;
}
 
friendostream&operator<<(ostream&out, Time&t)
{
out<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;
return out;
}
 
 
Timeoperator+(constTime&t1)
{
TimeT;
T.second=t1.second+second;
T.minute=t1.minute+minute;
T.hour=t1.hour+hour;
if(T.second>59)
{
T.second=T.second-60;
T.minute=T.minute+1;
}
if(T.minute>59)
{
T.minute=T.minute-60;
T.hour=T.hour+1;
}
if(T.hour>23)
T.hour=T.hour%24;
returnT;
}
 
 
Timeoperator-(constTime&t2)
{
Timediff;
if(t2.second>second){
--minute;
second+=60;
}
 
second=second-t2.second;
if(t2.minute>minute) {
--hour;
minute+=60;
}
 
minute=minute-t2.minute;
diff.hour=hour-t2.hour;
if(diff.hour<0)
diff.hour+=24;
returndiff;
 
 
}

};




int main(){

Timetime1,time2,sum, diff;
 
cin>>time1;
 
cin>>time2;
sum=time1+time2;
diff=time1-time2;
 
cout<<sum<<endl<<diff<<endl;
}
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning