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

C++ Help I have a program where I need to implement the operator overrides in th

ID: 3676824 • Letter: C

Question

C++ Help

I have a program where I need to implement the operator overrides in the Time.h file. I need to use the main() that instantiates the Time class to add additional tests to cover the various overidden operators. When implementing time math it needs to correctly wrap around.

Here is what I have so far:

main.cpp

#include <iostream>
#include <sstream>
#include "Time.h"
#include <stdexcept>

using namespace std;
int main()
{
   try   // Testing out of range exceptions with constructors
   {
       Time t30(-1,0,0,0);   // Under 0
   }
   catch(out_of_range& e)
   {
       cout << "Testing range for hours: " << endl;
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Over 24
   {
       Time t31(25,0,0,0);
   }
   catch(out_of_range& e)
   {
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Under 0
   {
       Time t32(0,-1,0,0);
   }
   catch(out_of_range& e)
   {
       cout << "Testing range for minutes: " << endl;
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Over 60
   {
       Time t33(0,61,0,0);
   }
   catch(out_of_range& e)
   {
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Under 0
   {
       Time t34(0,0,-1,0);
   }
   catch(out_of_range& e)
   {
       cout << "Testing range for seconds: " << endl;
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Over 60
   {
       Time t35(0,0,61,0);
   }
   catch(out_of_range& e)
   {
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Under 0
   {
       Time t36(0,0,0,-1);
   }
   catch(out_of_range& e)
   {
       cout << "Testing range for milliseconds: " << endl;
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Over 1000
   {
       Time t37(0,0,0,1001);
   }
   catch(out_of_range& e)
   {
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Testing out of range for all setters
   {
       Time t38(1,0,0,0);   // Under 0
       t38.setHours(-3);
   }
   catch(out_of_range& e)
   {
       cout << "Testing bounds for setHours" << endl;
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Over 60
   {
       Time t39(1,0,0,0);
       t39.setHours(60);
   }
   catch(out_of_range& e)
   {
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Under 0
   {
       Time t39(0,0,0,0);
       t39.setMinutes(-3);
   }
   catch(out_of_range& e)
   {
       cout << "Testing bounds for setMinutes" << endl;
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Over 60
   {
       Time t38(1,0,0,0);
       t38.setMinutes(64);
   }
   catch(out_of_range& e)
   {
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Under 0
   {
       Time t38(1,0,0,0);
       t38.setSeconds(-3);
   }
   catch(out_of_range& e)
   {
       cout << "Testing bounds for setSeconds" << endl;
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Over 60
   {
       Time t38(1,0,0,0);
       t38.setSeconds(100);
   }
   catch(out_of_range& e)
   {
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Under 0
   {
       Time t38(1,0,0,0);
       t38.setMilliseconds(-3);
   }
   catch(out_of_range& e)
   {
       cout << "Testing bounds for setMilliseconds" << endl;
       cerr << "Sorry, " << e.what() << endl;
   }

   try   // Over 60
   {
       Time t38(1,0,0,0);
       t38.setMilliseconds(6000);
   }
   catch(out_of_range& e)
   {
       cerr << "Sorry, " << e.what() << endl;
   }

   cout << "***Now testing the Getters***" << endl;   // Testing the Getters
   cout << "***Testing Getters***" << endl;
   Time t28(2,4,5,600);
   ostringstream s2;
   cout << "Starting Value: " << t28 << endl;
   s2 << t28.getHours() << t28.getMinutes() << t28.getSeconds() << t28.getMilliseconds() << endl;
   cout << "Ending Values: " << s2.str() << endl;

   cout << "***Now testing the Setters***" << endl;   // Testing the Setters
   cout << "***Testing Setters***" << endl;
   Time t29(1,0,0,0);
   ostringstream s3;
   cout << "Starting Value: " << t29 << endl;
   t29.setHours(3);
   t29.setMinutes(14);
   t29.setSeconds(25);
   t29.setMilliseconds(523);
   s3 << t29.getHours() << t29.getMinutes() << t29.getSeconds() << t29.getMilliseconds() << endl;
   cout << "Ending Values: " << s3.str() << endl;

   cout << "***Now testing the Copy constructor***" << endl;   // Testing the copy constructor
   cout << "***Testing constructor***" << endl;
   Time t22(1000);
   cout << "Starting Value: " << t22 << endl;
   Time t23(t22);
   cout << "Ending Values: " << t23 << "and " << t22 << endl;

   cout << "***Now testing the Copy assignment***" << endl;   // Testing the copy assignment operator
   cout << "***Testing assignment***" << endl;
   Time t24(1500);
   cout << "Starting Value: " << t24 << endl;
   Time t25 = t24;
   cout << "Ending Values: " << t25 << "and " << t24 << endl;

   cout << "***Now testing the Copy assignment using a different assignment method***" << endl;   // Testing the copy assignment operator
   cout << "***Testing assignment***" << endl;
   Time t26(6, 10, 10, 10);
   cout << "Starting Value: " << t26 << endl;
   Time t27;
   t27 = t26;
   cout << "Ending Values: " << t27 << "and " << t26 << endl;

   cout << "***Now testing the overloaded operators***" << endl;
   cout << "***Testing addition***" << endl;   // Testing the addition properties
   Time t1(1);
   Time t2(2);
   cout << "Starting value: " << t1 << endl;
   cout << "Starting value: " << t2 << endl;
   Time t3 = t1 + t2;
   cout << "Ending Value: " << t3.toString() << " " << endl;

   cout << "***Testing subtraction***" << endl;   // Testing the subtraction properties
   Time t4(16);
   Time t5(19);
   cout << "Starting value: " << t5 << endl;
   cout << "Starting value: " << t4 << endl;
   Time t6 = t5 - t4;
   cout << "Ending Value: " << t3.toString() << " " << endl;

   cout << "***Testing ostream***" << endl;   // Testing the << operator
   ostringstream s1;
   Time t7(15, 0, 16, 32);
   cout << "Starting value: " << t7 << endl;
   s1 << t7;
   cout << "Ending Value: " << s1.str();

   cout << "***Testing < (true)***" << endl;   // Testing the < operator, true
   Time t8(10000);
   Time t9( 4, 35, 45, 465);
   cout << "Starting value: " << t8 << endl;
   cout << "Starting value: " << t9 << endl;
   cout << "Ending Value: " << ( t8 < t9 ) << " " << endl;

   cout << "***Testing < (false)***" << endl;   // Testing the < operator, false
   cout << "Starting value: " << t9 << endl;
   cout << "Starting value: " << t8 << endl;
   cout << "Ending Value: " << ( t9 < t8 ) << " " <<endl;

   cout << "***Testing == (true)***" << endl;   // Testing the == operator, true
   Time t10(15000);
   Time t11(15000);
   cout << "Starting value: " << t10 << endl;
   cout << "Starting value: " << t11 << endl;
   cout << "Ending Value: " << ( t10 == t11) << " " << endl;

   cout << "***Testing == (false)***" << endl;   // Testing the == operator, false
   cout << "Starting value: " << t9 << endl;
   cout << "Starting value: " << t10 << endl;
   cout << "Ending Value: " << (t9 == t10) << " " << endl;

   cout << "***Testing wrap-around with addition***" << endl;   // Wrap-around addition
   Time t12(13, 40, 59, 900 );
   Time t13(10, 22, 14, 504);
   cout << "Starting value: " << t12 << endl;
   cout << "Starting value: " << t13 << endl;
   Time t14 = t12 + t13;
   cout << "Ending Value: " << t14 << endl;

   cout << "***Testing wrap-around with subtraction***" << endl;   // Wrap-around subtraction
   Time t15(3, 10, 10,20);
   Time t16(13, 20, 12, 30);
   cout << "Starting value: " << t15 << endl;
   cout << "Starting value: " << t16 << endl;
   Time t17 = t15 - t16;
   cout << "Ending Value: " << t17 << endl;

   // Basic functions test
   Time t18(13, 45, 52, 125);

   cout << "***Now testing the basic functionality***" << endl;
   cout << "Time as long: " << t18.asLong() << endl;   // Check asLong
   t18.set24Hour(false);
   cout << "Time 24 hour flag: " << t18.is24Hour() << endl;
   cout << "Time as 12 hour: " << t18.toString() << endl;
   t18.set24Hour(true);
   cout << "Time 24 hour flag: " << t18.is24Hour() << endl;
   cout << "Time as 24 hour: " << t18.toString() << " " << endl;

   Time t19(13, 45, 52, 125);
   Time t20;
   Time t21(14457600);

   cout << "Starting Value: " << t19 << endl;
   cout << "Time as long: " << t19.asLong() << endl;   // Check 24 hour flag and toString
   cout << "24 Hour Flag: " << t19.is24Hour()<< endl;
   t19.set24Hour(false);
   cout << "24 Hour Flag set to false: " << t19.toString() << " " << endl;

   cout << "Starting Value: " << t20 << endl;
   cout << "Time as long: " << t20.asLong() << endl;
   cout << "24 Hour Flag: " << t20.is24Hour()<< endl;
   t20.set24Hour(false);
   cout << "24 Hour Flag set to false: " << t20.toString() << " " << endl;

   cout << "Starting Value: " << t21 << endl;
   cout << "Time as long: " << t21.asLong() << endl;
   cout << "24 Hour Flag: " << t21.is24Hour()<< endl;
   t21.set24Hour(false);
   cout << "24 Hour Flag set to false: " << t21.toString() << " "<< endl;

   cout << "***Now testing the Copy constructor again***" << endl;   // Testing the copy constructor again, to make sure values are still present
   cout << "***Testing constructor***" << endl;
   cout << "Ending Values: " << t23 << "and " << t22 << endl;

   cout << "***Now testing the Copy assignment again***" << endl;   // Testing the copy assignment operator again, to make sure values are still present
   cout << "***Testing assignment***" << endl;
   cout << "Ending Values: " << t25 << "and " << t24 << endl;


   return 0;
}

time.cpp

#include "Time.h"
#include <stdexcept>
#include <sstream>
#include <cmath>

int Time::getHours() const   // Getter for hours
{
   return hours;
}

int Time::getMinutes() const   // Getter for minutes
{
   return minutes;
}

int Time::getSeconds() const   // Getter for seconds
{
   return seconds;
}

int Time::getMilliseconds() const   // Getter for milliseconds
{
   return milliseconds;
}

void Time::setHours(long x)   // Setter for hours
{
   if( x > 23 || x < 0) error("Bad Input!");
   this->hours = x;
}

void Time::setMinutes(long x)   // Setter for minutes
{
   if( x > 59 || x < 0 ) error("Bad Input!");
   this->minutes = x;
}

void Time::setSeconds(long x)   // Setter for seconds
{
   if(x > 59 || x < 0) error("Bad Input!");
   this->seconds = x;
}

void Time::setMilliseconds(long x)   // Setter for milliseconds
{
   if(x > 999 || x < 0) error("Bad Input!");
   this->milliseconds = x;
}

Time::Time(int h, int m, int s, int ms) // Takes input as int, stores individual vales as int
{
   if( h > 23 || h < 0) error("Bad Input!");   // Checking boundaries
   if( m > 59 || m < 0 ) error("Bad Input!");
   if(s > 59 || s < 0) error("Bad Input!");
   if(ms > 999 || ms < 0) error("Bad Input!");
   if(h > 12)
       PMflag = true;

   hours = h;
   minutes = m;
   seconds = s;
   milliseconds = ms;
}

Time::Time(long time)   // Accepts time as long and sets variables accordingly
{
   individualValues(time);
}

Time::Time() // Sets default time to all zeros
{
   hours = 0;
   minutes = 00;
   seconds = 00;
   milliseconds = 00;
   PMflag = false;
}

Time::Time( const Time & t)   // Copies all values of time t to local object
{
   hours = t.hours;
   minutes = t.minutes;
   seconds = t.seconds;
   milliseconds = t.milliseconds;
   set24Hour( is24Hour() );
}

Time& Time::operator =( const Time& rhs )   // Assigns a time object the values of another time object
{
   if(this == & rhs)   // If values are equal, return this
       return *this;
   this->hours = rhs.hours;
   this->minutes = rhs.minutes;
   this->seconds = rhs.seconds;
   this->milliseconds = rhs.milliseconds;
   this->set24Hour(rhs.is24Hour());
   return *this;
}

void Time::individualValues(long time)   // Code segment credit to Bill Hooper
{
   if (time > 99999999 || time < 0) error("Bad Input!");   // More boundary checking

   milliseconds = time % 1000;
   if(milliseconds > 999 || milliseconds < 0) error("Bad Input!");
   time /= 1000;

   seconds = time % 60;
   if(seconds > 59 || seconds < 0) error("Bad Input!");
   time /= 60;

   minutes = time % 60;
   if( minutes > 59 || minutes < 0 ) error("Bad Input!");
   time /= 60;

   hours = time;
   if( hours > 24 || hours < 0 ) error("Bad Input!");
   if ( hours > 12)
       PMflag = true;
}

long Time::asLong() const   // Code segment credit to Bill Hooper
{
   long longValue;

   longValue = (long) hours;
   longValue = (longValue * 60) + minutes;
   longValue = (longValue * 60) + seconds;
   longValue = (longValue * 1000) + milliseconds;
   return longValue;
}

string Time::toString() const   // Creates a string with h, m, s, and ms, displays to console
{
   ostringstream s1;
   string timeOfDay;

   if(is24Hour() == true || PMflag == true)
       timeOfDay = "PM";
   else
       timeOfDay = "AM";
   s1 << hours << ":" << minutes << ":" << seconds << ":" << milliseconds << " " << timeOfDay << endl;
   return s1.str();
}

void Time::set24Hour(bool value)   // If false, time is in standard 12-hour format, PMflag set
{
   if( value == false )
   {
       if(hours > 12)
       {
           hours -= 12;
           PMflag = true;
       }
   }
   if( value == true )
   {
       if( hours < 12 && PMflag == true)
           hours += 12;
   }

}

bool Time::is24Hour() const   // Checks to see if time is in 24-hour format, or standard 12-hour
{
   if( hours > 12)
       return true;
   else
       return false;
}

void Time::error(const string s)   // Throws an error, and does not modify values
{
   //cerr << s;
   throw out_of_range(s);
}


Time::~Time() {
   // TODO Auto-generated destructor stub
}


std::ostream& operator <<(std::ostream&a, const Time&b)
{
   string timeOfDay;   // Takes a time, and puts it into an acceptable ostream object

   if(b.is24Hour() == true || b.PMflag == true)
       timeOfDay = "PM";
   else
       timeOfDay = "AM";
   a << b.hours << ":" << b.minutes << ":" << b.seconds << ":" << b.milliseconds << " " << timeOfDay << endl;
   return a;
}

/**
* Define ordering relationships
*/
bool operator <(const Time&a, const Time&b)
{
   long t1 = a.asLong();   // Convert each time to a long value
   long t2 = b.asLong();

   if( t1 < t2 )   // Compare the values
       return true;
   else
       return false;
}

bool operator ==(const Time &a, const Time &b)
{
   long t1 = a.asLong();   // Convert each time to a long value
   long t2 = b.asLong();

   if( t1 == t2 )   // Check to see if the values are equal
       return true;
   else
       return false;
}

/**
* Define addition and subtraction
*/

Time Time::operator +(const Time&a) const
{
   Time t1;

   t1.hours = this->hours + a.hours;   // Add the hours, minutes, seconds, and/or milliseconds together
   t1.minutes = this->minutes + a.minutes;
   t1.seconds = this->seconds + a.seconds;
   t1.milliseconds = this->milliseconds + a.milliseconds;

   while( t1.milliseconds > 999)   // Wrap around if greater than 24 hours
   {
       t1.milliseconds -= 1000;
       t1.seconds += 1;
   }
   while( t1.seconds > 59)   // Wrap around if greater than 24 hours
   {
       t1.seconds -= 60;
       t1.minutes += 1;
   }
   while( t1.minutes > 59)   // Wrap around if greater than 24 hours
   {
       t1.minutes -= 60;
       t1.hours += 1;
   }
   while( t1.hours > 23)   // Wrap around if greater than 23 hours
   {
       t1.hours -= 23;   // 23 to account for non-existent hour zero
   }
   return t1;
}

Time Time::operator -(const Time&a) const
{
   Time t1;

   t1.hours = this->hours - a.hours;   // Subtract the hours, minutes, seconds, or milliseconds
   t1.minutes = this->minutes - a.minutes;
   t1.seconds = this->seconds - a.seconds;
   t1.milliseconds = this->milliseconds - a.milliseconds;
   while(t1.hours < 0 || t1.minutes < 0 || t1.seconds < 0 || t1.milliseconds < 0)   // While values are not positive, continue looping
   {
       while( t1.hours < 0)   // While value is negative, add 60 (or 23), and subtract from succeeding digit
       {
           t1.hours += 23;   // 23 to account for non-existent, hour zero
       }
       while(t1.minutes < 0)
       {
           t1.minutes += 60;
           t1.hours -= 1;
       }
       while(t1.seconds < 0)
       {
           t1.seconds += 60;
           t1.minutes -= 1;
       }
       while( t1.milliseconds < 0)
       {
           t1.milliseconds += 1000;
           t1.seconds -= 1;
       }
   }
   //t1.individualValues(t1.asLong());
   return t1;
}

and the Time.h file

The only thing that should be modified in the Time.h file is the private members.

Explanation / Answer

Private members means which are accessed only inside the class.

long Time::asLong() const   // Code segment credit to Bill Hooper
{
private long longValue;

   longValue = (long) hours;
   longValue = (longValue * 60) + minutes;
   longValue = (longValue * 60) + seconds;
   longValue = (longValue * 1000) + milliseconds;
   return longValue;
}

string Time::toString() const   // Creates a string with h, m, s, and ms, displays to console
{
private ostringstream s1;
string timeOfDay;

   if(is24Hour() == true || PMflag == true)
       timeOfDay = "PM";
   else
       timeOfDay = "AM";
   s1 << hours << ":" << minutes << ":" << seconds << ":" << milliseconds << " " << timeOfDay << endl;
   return s1.str();
}

Time Time::operator +(const Time&a) const
{
private Time t1;

   t1.hours = this->hours + a.hours;   // Add the hours, minutes, seconds, and/or milliseconds together
   t1.minutes = this->minutes + a.minutes;
   t1.seconds = this->seconds + a.seconds;
   t1.milliseconds = this->milliseconds + a.milliseconds;

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