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

I am Converting the \".h\" file (bold one) and the \".cpp\" file (non bold) from

ID: 3828116 • Letter: I

Question

I am Converting the ".h" file (bold one) and the ".cpp" file (non bold) from structure to class. I have converted the ".h" file to class, please check, but i am having difficulty changing the ".cpp" part. Can you please explain and convert it for me? Thank you

#ifndef TIME_H
#define TIME_H

#include <iostream>

class Time
{
public:

void init(Time &t, int hours, int minutes, int seconds);
void normalize(Time &t);
Time operator +(const Time &t1, const Time &t2);
Time &operator +=(Time &t1, const Time &t2);
Time operator -(const Time &t1, const Time &t2);
Time &operator -=(Time &t1, const Time &t2);
bool operator ==(const Time &t1, const Time &t2);

std::ostream &operator <<(std::ostream &os, const Time &t);
std::istream &operator >>(std::istream &is, Time &t);

private:

int hours, minutes, seconds;
};

#endif

#include <iostream>
#include <iomanip>

#include "time.h"

using namespace std;

void Time :: init(Time &t, int hours, int minutes, int seconds) {
   if (hours < 0 || hours > 23) throw string("init - Bad hour value");
   if (minutes < 0 || minutes > 59) throw string("init - Bad minute value");
   if (seconds < 0 || seconds > 59) throw string("init - Bad second value");
   t.hours = hours;
   t.minutes = minutes;
   t.seconds = seconds;
}

void Time :: normalize(Time &t) {
   if (t.seconds < 0) {
       t.seconds += 60;
       t.minutes -= 1;
   }
   if (t.minutes < 0) {
       t.minutes += 60;
       t.hours -= 1;
   }
   if (t.hours < 0)
       t.hours += 24;
   t.minutes += t.seconds / 60;
   t.seconds %= 60;
   t.hours = (t.hours + t.minutes / 60) % 24;
   t.minutes %= 60;
}

Time operator +(const Time &t1, const Time &t2) {
   Time result = t1;
   return operator +=(result, t2);
}

Time &operator +=(Time &t1, const Time &t2) {
   t1.hours += t2.hours;
   t1.minutes += t2.minutes;
   t1.seconds += t2.seconds;
   normalize(t1);
   return t1;
}

Time operator -(const Time &t1, const Time &t2) {
   Time result = t1;
   return operator -=(result, t2);
}

Time &operator -=(Time &t1, const Time &t2) {
   t1.hours -= t2.hours;
   t1.minutes -= t2.minutes;
   t1.seconds -= t2.seconds;
   normalize(t1);
   return t1;
}

bool Time :: operator ==(const Time &t1, const Time &t2) {
   return t1.hours == t2.hours && t1 .minutes == t2.minutes && t1.seconds == t2.seconds;
}

ostream Time :: &operator <<(ostream &os, const Time &t) {
   os << setw(2) << setfill('0') << t.hours << ":" << setw(2) << setfill('0') << t.minutes << ":" << setw(2) << setfill('0') << t.seconds;
   return os;
}

istream Time :: &operator >>(istream &is, Time &t) {
   char ch;
   int hours, minutes, seconds;
   is >> hours;
   is >> ch;
   if (ch != ':') throw string(">> - Bad time format");
   is >> minutes;
   is >> ch;
   if (ch != ':') throw string(">> - Bad time format");
   is >> seconds;
   init(t, hours, minutes, seconds);

   return is;
}

Explanation / Answer

#include <iostream>
#include <iomanip>

#include <string>

#include "time.h"

using namespace std;

void Time :: init(Time &t, int hours, int minutes, int seconds) {
   if (hours < 0 || hours > 23) throw string("init - Bad hour value");
   if (minutes < 0 || minutes > 59) throw string("init - Bad minute value");
   if (seconds < 0 || seconds > 59) throw string("init - Bad second value");
   t.hours = hours;
   t.minutes = minutes;
   t.seconds = seconds;
}

void Time :: normalize(Time &t) {
   if (t.seconds < 0) {
       t.seconds += 60;
       t.minutes -= 1;
   }
   if (t.minutes < 0) {
       t.minutes += 60;
       t.hours -= 1;
   }
   if (t.hours < 0)
       t.hours += 24;
   t.minutes += t.seconds / 60;
   t.seconds %= 60;
   t.hours = (t.hours + t.minutes / 60) % 24;
   t.minutes %= 60;
}

Time operator +(const Time &t1, const Time &t2) {
   Time result = t1;
   return operator +=(result, t2);
}

Time &operator +=(Time &t1, const Time &t2) {
   t1.hours += t2.hours;
   t1.minutes += t2.minutes;
   t1.seconds += t2.seconds;
   normalize(t1);
   return t1;
}

Time operator -(const Time &t1, const Time &t2) {
   Time result = t1;
   return operator -=(result, t2);
}

Time &operator -=(Time &t1, const Time &t2) {
   t1.hours -= t2.hours;
   t1.minutes -= t2.minutes;
   t1.seconds -= t2.seconds;
   normalize(t1);
   return t1;
}

bool Time :: operator ==(const Time &t1, const Time &t2) {
   return t1.hours == t2.hours && t1 .minutes == t2.minutes && t1.seconds == t2.seconds;
}

ostream Time :: &operator <<(ostream &os, const Time &t) {
   os << setw(2) << setfill('0') << t.hours << ":" << setw(2) << setfill('0') << t.minutes << ":" << setw(2) << setfill('0') << t.seconds;
   return os;
}

istream Time :: &operator >>(istream &is, Time &t) {
   char ch;
   int hours, minutes, seconds;
   is >> hours;
   is >> ch;
   if (ch != ':') throw string(">> - Bad time format");
   is >> minutes;
   is >> ch;
   if (ch != ':') throw string(">> - Bad time format");
   is >> seconds;
   init(t, hours, minutes, seconds);

   return is;
}