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&);

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education