Answer to Question #216896 in C++ for fiend

Question #216896
Add to the time class hat has separate int member data for hours, minutes, and Seconds the ability to subtract two time values using the overloaded (-) operator, and to multiply a time value by a number of type float, using the overloaded (*) operator.
1
Expert's answer
2021-07-13T13:36:44-0400
#include<iostream>
#include<bits/stdc++.h>
using namespace std;


class Time{
	public:
		int hour;
		int minute;
		int second;
		
		Time()
		{
			hour =0;
			minute =0;
			second =0;
		}
		
		Time(int h, int m, int s)
		{
			hour = h;
			minute = m;
			second = s;
		}
		
		Time operator - (Time const &t2){
			
			Time diff;
		    diff.second = second - t2.second;
		    if(t2.minute > minute)
		    {
		        --hour;
		        minute += 60;
		    }
		    diff.minute = minute-t2.minute;
		    diff.hour = hour-t2.hour;
		    return diff;
		}
			
		Time operator * (float k)
		{
			Time mul;
			mul.hour = (hour*k);
			mul.minute = minute;
			mul.second = second;
			
			return mul;
			
		}
		
		void display()
		{
			cout<<"Time : "<<hour<<":"<<minute<<":"<<second<<endl;
		}
};


int main()
{
	Time t1(6,30,0);
	t1.display();
	Time t2(5,0,0);
	t2.display();
	Time t3;
	t3 = t1-t2;
	t3.display();
	Time t4;
	t4 = t1*5;
	t4.display();
	
}





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS