Need help with a C++ assignment I have three files main.cpp, time.cpp and time.h
ID: 3677249 • Letter: N
Question
Need help with a C++ assignment
I have three files main.cpp, time.cpp and time.h
I need help implementing the operator overrides in the Time.h file, use the main() that instantiates the Time class and add additional tests to cover the overridden operators.
Here is the time.h file:
#ifndef TIME_H_
#define TIME_H_
#include <iostream>
#include <string>
/**
* Time class
*
* The Time class contains time as hours:minutes:seconds:milliseconds
*/
class Time
{
public:
Time();
Time(long time);
Time(int hours, int minutes, int seconds, int milli);
virtual ~Time();
/**
* Return time as a long
*/
long asLong() const;
/**
* Display as a string in the format hours:minutes:seconds:milliseconds
* The time is displayed as 24 hours if the 24 hour flag is set true.
*/
std::string toString() const;
/**
* Enable/disable 24 hour time display
*/
void set24Hour(bool value);
/**
* Return true if 24 hour time display is enabled
*/
bool is24Hour() const;
/**
* Output the time to an output stream as hours:minutes:seconds:milliseconds
*/
friend std::ostream& operator <<(std::ostream&, const Time&); // Output a Time to an output stream
/**
* Define ordering relationships
*/
friend bool operator <(const Time&, const Time&);
friend bool operator >(const Time&, const Time&);
friend bool operator ==(const Time &a, const Time &b);
/**
* Define addition and subtraction
*/
Time operator +(const Time&) const;
Time operator -(const Time&) const;
private:
/**
* Private members go here
*/
};
#endif /* TIME_H_ */
here is the time.cpp file:
/*
* Time.cpp
*
* Created on: Mar 12, 2016
* Author: student
*/
#include "Time.h"
#include <stdexcept>
#include <sstream>
#include <cmath>
using namespace std;
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 values as int
{
if( h > 23 || h < 0) error("Bad Input!");
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 and sets variables accordingly
{
individualValues(time);
}
Time::Time() // Sets default time to zeros
{
hours = 0;
minutes = 00;
seconds = 00;
milliseconds = 00;
pmflag = false;
}
Time::Time( const Time & t) // Copies the values of time t to a 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 to the values of another time object
{
if(this == & rhs)
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)
{
if (time > 99999999 || time < 0) error("Bad Input!");
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
{
long longValue;
longValue = (long) hours;
longValue = (longValue * 60) + minutes;
longValue = (longValue * 60) + seconds;
longValue = (longValue * 1000) + milliseconds;
return longValue;
}
string Time::toString() const
{
ostringstream s1;
string timeOfDay;
if(is24Hour() == true || pmflag == true)
timeOfDay = "PM";
else
timeOfDay = "AM";
s1 << hours << ":" << minutes << ":" << seconds << ":" << milliseconds << " " << timeOfDay << ' ';;
return s1.str();
}
void Time::set24Hour(bool value)
{
if( value == false )
{
if(hours > 12)
{
hours -= 12;
pmflag = true;
}
}
if( value == true )
{
if( hours < 12 && pmflag == true)
hours += 12;
}
}
bool Time::is24Hour() const
{
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 << ' ';;
return a;
}
/**
* Define ordering relationships
*/
bool operator <(const Time&a, const Time&b)
{
long t1 = a.asLong(); // Convert to a long value
long t2 = b.asLong();
if( t1 < t2 )
return true;
else
return false;
}
bool operator ==(const Time &a, const Time &b)
{
long t1 = a.asLong(); // Convert to a long value
long t2 = b.asLong();
if( t1 == t2 )
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)
{
t1.milliseconds -= 1000;
t1.seconds += 1;
}
while( t1.seconds > 59)
{
t1.seconds -= 60;
t1.minutes += 1;
}
while( t1.minutes > 59)
{
t1.minutes -= 60;
t1.hours += 1;
}
while( t1.hours > 23)
{
t1.hours -= 23;
}
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) // Loop as long as values are not positive
{
while( t1.hours < 0)
{
t1.hours += 23;
}
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 main.cpp file:
#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);
} catch (out_of_range& e) {
cout << "Testing range for hours: " << ' ';
cerr << "Sorry, " << e.what() << ' ';
}
try // Over 24
{
Time t31(25, 0, 0, 0);
} catch (out_of_range& e) {
cerr << "Sorry, " << e.what() << ' ';
}
try // Under 0
{
Time t32(0, -1, 0, 0);
} catch (out_of_range& e) {
cout << "Testing range for minutes: " << ' ';
cerr << "Sorry, " << e.what() << ' ';
}
try // Over 60
{
Time t33(0, 61, 0, 0);
} catch (out_of_range& e) {
cerr << "Sorry, " << e.what() << ' ';
}
try // Under 0
{
Time t34(0, 0, -1, 0);
} catch (out_of_range& e) {
cout << "Testing range for seconds: " << ' ';
cerr << "Sorry, " << e.what() << ' ';
}
try // Over 60
{
Time t35(0, 0, 61, 0);
} catch (out_of_range& e) {
cerr << "Sorry, " << e.what() << ' ';
}
try // Under 0
{
Time t36(0, 0, 0, -1);
} catch (out_of_range& e) {
cout << "Testing range for milliseconds: " << ' ';
cerr << "Sorry, " << e.what() << ' ';
}
try // Over 1000
{
Time t37(0, 0, 0, 1001);
} catch (out_of_range& e) {
cerr << "Sorry, " << e.what() << ' ';
}
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" << ' ';
cerr << "Sorry, " << e.what() << ' ';
}
try // Over 60
{
Time t39(1, 0, 0, 0);
t39.setHours(60);
} catch (out_of_range& e) {
cerr << "Sorry, " << e.what() << ' ';
}
try // Under 0
{
Time t39(0, 0, 0, 0);
t39.setMinutes(-3);
} catch (out_of_range& e) {
cout << "Testing bounds for setMinutes" << ' ';
cerr << "Sorry, " << e.what() << ' ';
}
try // Over 60
{
Time t38(1, 0, 0, 0);
t38.setMinutes(64);
} catch (out_of_range& e) {
cerr << "Sorry, " << e.what() << ' ';
}
try // Under 0
{
Time t38(1, 0, 0, 0);
t38.setSeconds(-3);
} catch (out_of_range& e) {
cout << "Testing bounds for setSeconds" << ' ';
cerr << "Sorry, " << e.what() << ' ';
}
try // Over 60
{
Time t38(1, 0, 0, 0);
t38.setSeconds(100);
} catch (out_of_range& e) {
cerr << "Sorry, " << e.what() << ' ';
}
try // Under 0
{
Time t38(1, 0, 0, 0);
t38.setMilliseconds(-3);
} catch (out_of_range& e) {
cout << "Testing bounds for setMilliseconds" << ' ';
cerr << "Sorry, " << e.what() << ' ';
}
try // Over 60
{
Time t38(1, 0, 0, 0);
t38.setMilliseconds(6000);
} catch (out_of_range& e) {
cerr << "Sorry, " << e.what() << ' ';
}
cout << "***Now testing the Getters***" << ' '; // Testing the Getters
cout << "***Testing Getters***" << ' ';
Time t28(2, 4, 5, 600);
ostringstream s2;
cout << "Starting Value: " << t28 << ' ';
s2 << t28.getHours() << t28.getMinutes() << t28.getSeconds()
<< t28.getMilliseconds() << ' ';
cout << "Ending Values: " << s2.str() << ' ';
cout << "***Now testing the Setters***" << ' '; // Testing the Setters
cout << "***Testing Setters***" << ' ';
Time t29(1, 0, 0, 0);
ostringstream s3;
cout << "Starting Value: " << t29 << ' ';
t29.setHours(3);
t29.setMinutes(14);
t29.setSeconds(25);
t29.setMilliseconds(523);
s3 << t29.getHours() << t29.getMinutes() << t29.getSeconds()
<< t29.getMilliseconds() << ' ';
cout << "Ending Values: " << s3.str() << ' ';
cout << "***Now testing the Copy constructor***" << ' '; // Testing the copy constructor
cout << "***Testing constructor***" << ' ';
Time t22(1000);
cout << "Starting Value: " << t22 << ' ';
Time t23(t22);
cout << "Ending Values: " << t23 << "and " << t22 << ' ';
cout << "***Now testing the Copy assignment***" << ' '; // Testing the copy assignment operator
cout << "***Testing assignment***" << ' ';
Time t24(1500);
cout << "Starting Value: " << t24 << ' ';
Time t25 = t24;
cout << "Ending Values: " << t25 << "and " << t24 << ' ';
cout
<< "***Now testing the Copy assignment using a different assignment method***"
<< ' '; // Testing the copy assignment operator
cout << "***Testing assignment***" << ' ';
Time t26(6, 10, 10, 10);
cout << "Starting Value: " << t26 << ' ';
Time t27;
t27 = t26;
cout << "Ending Values: " << t27 << "and " << t26 << ' ';
cout << "***Now testing the overloaded operators***" << ' ';
cout << "***Testing addition***" << ' '; // Testing the addition properties
Time t1(1);
Time t2(2);
cout << "Starting value: " << t1 << ' ';
cout << "Starting value: " << t2 << ' ';
Time t3 = t1 + t2;
cout << "Ending Value: " << t3.toString() << " " << ' ';
cout << "***Testing subtraction***" << ' '; // Testing the subtraction properties
Time t4(16);
Time t5(19);
cout << "Starting value: " << t5 << ' ';
cout << "Starting value: " << t4 << ' ';
Time t6 = t5 - t4;
cout << "Ending Value: " << t3.toString() << " " << ' ';
cout << "***Testing ostream***" << ' '; // Testing the << operator
ostringstream s1;
Time t7(15, 0, 16, 32);
cout << "Starting value: " << t7 << ' ';
s1 << t7;
cout << "Ending Value: " << s1.str();
cout << "***Testing < (true)***" << ' '; // Testing the < operator, true
Time t8(10000);
Time t9(4, 35, 45, 465);
cout << "Starting value: " << t8 << ' ';
cout << "Starting value: " << t9 << ' ';
cout << "Ending Value: " << (t8 < t9) << " " << ' ';
cout << "***Testing < (false)***" << ' '; // Testing the < operator, false
cout << "Starting value: " << t9 << ' ';
cout << "Starting value: " << t8 << ' ';
cout << "Ending Value: " << (t9 < t8) << " " << ' ';
cout << "***Testing == (true)***" << ' '; // Testing the == operator, true
Time t10(15000);
Time t11(15000);
cout << "Starting value: " << t10 << ' ';
cout << "Starting value: " << t11 << ' ';
cout << "Ending Value: " << (t10 == t11) << " " << ' ';
cout << "***Testing == (false)***" << ' '; // Testing the == operator, false
cout << "Starting value: " << t9 << ' ';
cout << "Starting value: " << t10 << ' ';
cout << "Ending Value: " << (t9 == t10) << " " << ' ';
cout << "***Testing wrap-around with addition***" << ' '; // Wrap-around addition
Time t12(13, 40, 59, 900);
Time t13(10, 22, 14, 504);
cout << "Starting value: " << t12 << ' ';
cout << "Starting value: " << t13 << ' ';
Time t14 = t12 + t13;
cout << "Ending Value: " << t14 << ' ';
cout << "***Testing wrap-around with subtraction***" << ' '; // Wrap-around subtraction
Time t15(3, 10, 10, 20);
Time t16(13, 20, 12, 30);
cout << "Starting value: " << t15 << ' ';
cout << "Starting value: " << t16 << ' ';
Time t17 = t15 - t16;
cout << "Ending Value: " << t17 << ' ';
// Basic functions test
Time t18(13, 45, 52, 125);
cout << "***Now testing the basic functionality***" << ' ';
cout << "Time as long: " << t18.asLong() << ' '; // Check asLong
t18.set24Hour(false);
cout << "Time 24 hour flag: " << t18.is24Hour() << ' ';
cout << "Time as 12 hour: " << t18.toString() << ' ';
t18.set24Hour(true);
cout << "Time 24 hour flag: " << t18.is24Hour() << ' ';
cout << "Time as 24 hour: " << t18.toString() << " " << ' ';
Time t19(13, 45, 52, 125);
Time t20;
Time t21(14457600);
cout << "Starting Value: " << t19 << ' ';
cout << "Time as long: " << t19.asLong() << ' '; // Check 24 hour flag and toString
cout << "24 Hour Flag: " << t19.is24Hour() << ' ';
t19.set24Hour(false);
cout << "24 Hour Flag set to false: " << t19.toString() << " " << ' ';
cout << "Starting Value: " << t20 << ' ';
cout << "Time as long: " << t20.asLong() << ' ';
cout << "24 Hour Flag: " << t20.is24Hour() << ' ';
t20.set24Hour(false);
cout << "24 Hour Flag set to false: " << t20.toString() << " " << ' ';
cout << "Starting Value: " << t21 << ' ';
cout << "Time as long: " << t21.asLong() << ' ';
cout << "24 Hour Flag: " << t21.is24Hour() << ' ';
t21.set24Hour(false);
cout << "24 Hour Flag set to false: " << t21.toString() << " " << ' ';
cout << "***Now testing the Copy constructor again***" << ' '; // Testing the copy constructor again, to make sure values are still present
cout << "***Testing constructor***" << ' ';
cout << "Ending Values: " << t23 << "and " << t22 << ' ';
cout << "***Now testing the Copy assignment again***" << ' '; // Testing the copy assignment operator again, to make sure values are still present
cout << "***Testing assignment***" << ' ';
cout << "Ending Values: " << t25 << "and " << t24 << ' ';
return 0;
}
Explanation / Answer
Time.h
#ifndef TIMELAB_SRC_TIME_H_
#define TIMELAB_SRC_TIME_H_
#include <iostream>
#include <string>
using namespace std;
/**
* Time class
*
* The Time class contains time as hours:minutes:seconds:milliseconds (AM/PM).
*/
class Time
{
public:
/**
* Constructor with zero values
*/
Time();
Time(long time);
Time(int hours, int minutes, int seconds, int milli);
virtual ~Time();
/**
* Return time as a long
*/
long asLong() const;
/**
* Display as a string in the format hours:minutes:seconds:milliseconds.
* For example 1:45:30:56 PM
*
* The time is displayed as 24 hours if the 24 hour flag is set true.
*/
std::string toString() const;
/**
* Enable/disable 24 hour time display
*/
void set24Hour(bool value);
/**
* Return true if 24 hour time display is enabled
*/
bool is24Hour() const;
/**
* Output the time to an output stream as hours:minutes:seconds:milliseconds.microseconds
*/
friend std::ostream& operator <<(std::ostream&, const Time&);
/**
* Define ordering relationships
*/
friend bool operator <(const Time&, const Time&);
friend bool operator ==(const Time &a, const Time &b);
/**
* Define addition and subtraction
*/
Time operator +(const Time&) const;
Time operator -(const Time&) const;
/**
* Copy constructor
*/
Time(const Time & t);
/**
* Copy assignment
*/
Time& operator =( const Time& rhs );
/**
* Getters
*/
int getHours() const;
int getSeconds() const;
int getMinutes() const;
int getMilliseconds() const;
/**
* Setters
*/
void setHours(long x);
void setMinutes(long x);
void setSeconds(long x);
void setMilliseconds(long x);
private:
/**
* Private members go here
*/
long hours;
long minutes;
long seconds;
long milliseconds;
bool PMflag = false;
void individualValues(long time);
void error(string s);
};
#endif /* TIMELAB_SRC_TIME_H_ */
Time.cpp
#ifndef TIMELAB_SRC_TIME_H_
#define TIMELAB_SRC_TIME_H_
#include <iostream>
#include <string>
using namespace std;
/**
* Time class
*
* The Time class contains time as hours:minutes:seconds:milliseconds (AM/PM).
*/
class Time
{
public:
/**
* Constructor with zero values
*/
Time();
Time(long time);
Time(int hours, int minutes, int seconds, int milli);
virtual ~Time();
/**
* Return time as a long
*/
long asLong() const;
/**
* Display as a string in the format hours:minutes:seconds:milliseconds.
* For example 1:45:30:56 PM
*
* The time is displayed as 24 hours if the 24 hour flag is set true.
*/
std::string toString() const;
/**
* Enable/disable 24 hour time display
*/
void set24Hour(bool value);
/**
* Return true if 24 hour time display is enabled
*/
bool is24Hour() const;
/**
* Output the time to an output stream as hours:minutes:seconds:milliseconds.microseconds
*/
friend std::ostream& operator <<(std::ostream&, const Time&);
/**
* Define ordering relationships
*/
friend bool operator <(const Time&, const Time&);
friend bool operator ==(const Time &a, const Time &b);
/**
* Define addition and subtraction
*/
Time operator +(const Time&) const;
Time operator -(const Time&) const;
/**
* Copy constructor
*/
Time(const Time & t);
/**
* Copy assignment
*/
Time& operator =( const Time& rhs );
/**
* Getters
*/
int getHours() const;
int getSeconds() const;
int getMinutes() const;
int getMilliseconds() const;
/**
* Setters
*/
void setHours(long x);
void setMinutes(long x);
void setSeconds(long x);
void setMilliseconds(long x);
private:
/**
* Private members go here
*/
long hours;
long minutes;
long seconds;
long milliseconds;
bool PMflag = false;
void individualValues(long time);
void error(string s);
};
#endif /* TIMELAB_SRC_TIME_H_ */
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;
}