Question: /////////////////////////Directions////////////////////////////////// Goals : Understanding operator overloading Design and implement TimeSpan class which represents a duration in hours, minutes and seconds. Integer multiplication allows us to
/////////////////////////Directions//////////////////////////////////
Goals: Understanding operator overloading
Design and implement TimeSpan class which represents a duration in hours, minutes and seconds.
Integer multiplication allows us to multiply a TimeSpan with an integer and is not commutative. The other operators work on two given TimeSpan values and are commutative
TimeSpan Ts3(1.5, 30.5, -90); TimeSpan Ts4; Ts4 = Ts3 * 5; // OK // below line won't compile, explained in class // Ts4 = 5 * Ts3;
All functions in the .h file and in the .cpp files MUST have at least one line of documentation.
/////////////////////////////////////Given Starter Files////////////////////////////////////
********************timespan.h*****************************
#ifndef ASS2_TIMESPAN_H #define ASS2_TIMESPAN_H
#include
using namespace std;
class TimeSpan { friend ostream& operator<<(ostream& out, const TimeSpan& ts);
public: // explicit TimeSpan(int Hour = 0, int Minute = 0, int Second = 0); explicit TimeSpan(double hour = 0, double minute = 0, double second = 0);
// add TimeSpan operator+(const TimeSpan& ts) const;
// subtract TimeSpan operator-(const TimeSpan& ts) const;
// check equality bool operator==(const TimeSpan& ts) const;
// check if not equal bool operator!=(const TimeSpan& ts) const;
// multiply timespan by an unsigned number TimeSpan operator*(unsigned int number) const;
// TODO(student) // to add operator+=, operator-=, operator<, operator>, operator<=, operator>=
// hour component of timespan int getHour() const;
// minute component of timespan int getMinute() const;
// second component of timespan int getSecond() const;
// true if timespan is 0 or larger bool isPositive() const;
private: };
#endif // ASS2_TIMESPAN_H *****************************************************timespan.cpp*************************************************
#include "timespan.h"
ostream& operator<<(ostream& out, const TimeSpan& ts) { return out; }
// explicit TimeSpan(int Hour = 0, int Minute = 0, int Second = 0); TimeSpan::TimeSpan(double hour, double minute, double second) {}
// hour component int TimeSpan::getHour() const { return 0; }
// minute component int TimeSpan::getMinute() const { return 0; }
// second component int TimeSpan::getSecond() const { return 0; }
// true if timespan is 0 or larger bool TimeSpan::isPositive() const { return true; }
// add TimeSpan TimeSpan::operator+(const TimeSpan& ts) const { TimeSpan tsSum; return tsSum; }
// subtract TimeSpan TimeSpan::operator-(const TimeSpan& ts) const { TimeSpan tsSub; return tsSub; }
// multiply with an integer TimeSpan TimeSpan::operator*(unsigned int number) const { TimeSpan tsLarge; return tsLarge; }
// equality == bool TimeSpan::operator==(const TimeSpan& ts) const { return true; }
// inequality != bool TimeSpan::operator!=(const TimeSpan& ts) const { return true; }
****************************************************************************main.cpp***********************************************
#include
#include "timespan.h"
using namespace std;
// testing constructor void test1() { TimeSpan ts(1, 20, 30); stringstream ss; ss << ts; assert(ss.str() == "1:20:30");
TimeSpan ts2(4, -20, -90); ss.str(""); ss << ts2; assert(ss.str() == "3:38:30");
TimeSpan ts3(1.5, 30.5, -90); ss.str(""); ss << ts3; assert(ss.str() == "1:59:00");
TimeSpan ts4(0, 0.07, 0); ss.str(""); ss << ts4; assert(ss.str() == "0:00:04"); cout << "test1 complete" << endl; }
// testing equality, addition, subtraction, multiplication void test2() { TimeSpan ts(1, 20, 30); TimeSpan ts2(1, 20, 30); TimeSpan ts3(0, 0, 0); assert(ts == ts2); assert(!(ts != ts2)); assert(ts != ts3); assert((ts + ts + ts) == (ts2 * 3)); assert((ts * 5) == (ts2 * 4) + ts2); assert((ts * 5) == (ts2 * 6) - ts2); assert((ts + ts - ts) == ((ts2 * 2) - ts)); assert((ts - ts2) == ts3); assert((ts3 * 5) == ts3); cout << "test2 complete" << endl; }
void test3() { TimeSpan ts0(0, 0, 0); TimeSpan ts1(0, 0, 10); TimeSpan ts2 = ts0 - ts1; assert(ts1.isPositive() && !ts2.isPositive()); stringstream ss; ss << ts2; assert(ss.str() == "-0:00:10"); cout << "test3 complete" << endl; }
int main() { test1(); test2(); test3(); cout << "Done." << std::endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
