Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class named Time that contains integer fields for hours and minutes. St

ID: 3544367 • Letter: C

Question

Create a class named Time that contains integer fields for hours and minutes. Store the hours in military time. Add a function that displays the fields, using a colon to separate hours and minutes. Add another function that takes an argument that represents minutes to add to the time. The function updates the time based on the number of minutes added. For example, 12:30 plus 15 is 12:45, 14:50 plus 20 is 15:10, and 23:59 plus 2 is 0:01. The Time constructor requires an argument for hours. The argument for minutes is optional; the value defaults to 0 if no argument is supplied. The constructor ensures that the hours field is no greater than 23 and that the minutes field is not greater than 59; default to these maximum values if the arguemnts to the constructor are out of range. Create a main() function that instaniates an array of at least four Time objects and demonstrates that they display correctly both before and after varying amounts of time have been added to them.


NOTE: Do not create an array of four objects as specified in the book. Create four separate

scalar variables.

Explanation / Answer

#include
using namespace std;

class Time
{
private:
int hr;
int min;
int sec;

public:
Time(); // constructor
Time(int,int,int); // constructor
void get();
void show();
Time operator + (Time);
Time operator - (Time);
};

int main()
{
Time t1;
Time t2;
cout<<"Enter first time:";
t1.get();
cout<<"Enter second time:";
t2.get();
Time t_sum = t1 + t2;
Time t_diff = t1 - t2;
cout<<"Sum ";
t_sum.show();
cout<<"Diff ";
t_diff.show();
system("pause");
}

Time::Time()
{
hr=0;
min=0;
sec=0;
}

Time::Time(int h,int m,int s)
{
hr=h;
min=m;
sec=s;
}

void Time::get()
{
cout<<"nEnter hrs:";
cin>>hr;
cout<<"Enter min:";
cin>>min;
cout<<"Enter sec:";
cin>>sec;
}

void Time::show()
{
cout<<"Time is "<



Time Time::operator + ( Time t)
{
int h,m,s;
int sum;
sum = (hr + t.hr)*3600 + (min + t.min)*60 + sec + t.sec;
s = sum % 60;
sum = sum / 60;
m = sum % 60;
h = sum / 60;
return Time(h,m,s);
}

Time Time::operator - ( Time t)
{
int h,m,s;
int sum1,sum2,sum;
sum1 = (hr)*3600 + (min )*60 + sec ;
sum2 = (t.hr)*3600 + (t.min)*60 + t.sec;
if (sum1>sum2)
sum = sum1-sum2;
else
sum = sum2-sum1;
s = sum %60;
sum = sum/60;
m = sum % 60;
h = sum/60;
return Time(h,m,s);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote