Question
Write in C++ Begin by writing the implementation for all the functions in the Runtime class. Please note that when we add, subtract, multiply and
Write in C++
Begin by writing the implementation for all the functions in the Runtime class. Please note that when we add, subtract, multiply and divide the runs we do these operations to both the time and the distance. Equality (==) means that both time and distance are the same. Also, doing equality of floating point numbers can be problematic, so it might be good to have the distance equal function call all distances that are within a tenth of a mile equal.
#include
#include "MyTime.h"
#ifndef RUNTIME_H
#define RUNTIME_H
class Runtime{
public:
Runtime();
// accessors
double get_distance()const{return distance;}
MyTime get_time()const{return rtime;}
// mutators
void set_time(const MyTime& myt){rtime = myt;}
void set_distance(const double& d){distance = d;}
// average pace
MyTime pace(){return rtime/distance;}
// overloaded operators - do the math on both time and distance
Runtime operator +(const Runtime& rhs)const;
Runtime operator -(const Runtime& rhs)const;
Runtime operator *(const double& rhs) const;
Runtime operator /(const double& rhs) const;
// Full equality means time and distance are the same
bool operator == (const Runtime& rhs)const;
bool operator != (const Runtime& rhs)const;
// Equality of doubles can be problematic - this returns true if they are
// within a tenth of a mile
bool distance_equal(double d) const;
// Input - Output
void input(std::istream& ins);
void output(std::ostream& outs)const;
private:
MyTime rtime;
double distance;
};
// Non-member functions that work with this class
// This first one reverses the arguments for multiplication
Runtime operator *(const double& lhs, const Runtime& rhs);
// Stream operators
std::ostream& operator <<(std::ostream& outs,
const Runtime& rhs);
std::istream& operator >>(std::istream& ins,
Runtime& rhs);
#endif
******************************************************************************************
MyTime.cc
// The implementation file for the MyTime class
#include "MyTime.h"
#include
#include
#include
using namespace std;
// Constructor
MyTime::MyTime(int h, int m,int s){
hours = h;
minutes = m;
seconds = s;
}
void MyTime::Reset(int h, int m,int s){
hours = h;
minutes = m;
seconds = s;
}
void MyTime::simplify(){
minutes += seconds/60;
seconds = seconds%60;
hours += minutes/60;
minutes = minutes%60;
}
MyTime operator + (const MyTime& t1, const MyTime& t2){
MyTime tmp;
tmp.hours = t1.hours + t2.hours;
tmp.minutes = t1.minutes + t2.minutes;
tmp.seconds = t1.seconds + t2.seconds;
tmp.simplify();
return tmp;
}
MyTime operator - (const MyTime& t1, const MyTime& t2){
MyTime tmp;
tmp.seconds =
abs((t1.hours*3600+t1.minutes*60+t1.seconds)
- (t2.hours*3600+t2.minutes*60+t2.seconds));
tmp.simplify();
return tmp;
}
MyTime operator / (const MyTime& t1, double num){
MyTime tmp;
tmp.seconds = t1.hours*3600 + t1.minutes*60 + t1.seconds;
tmp.seconds /= num;
tmp.simplify();
return tmp;
}
MyTime operator * (const MyTime& t1, int num){
MyTime tmp;
tmp.seconds = t1.hours*3600 + t1.minutes*60 + t1.seconds;
tmp.seconds *= num;
tmp.simplify();
return tmp;
}
//This second multiplication operator is to make the operation commutative
MyTime operator * (int num, const MyTime& t1){
MyTime tmp;
tmp.seconds = t1.hours*3600 + t1.minutes*60 + t1.seconds;
tmp.seconds *= num;
tmp.simplify();
return tmp;
}
bool operator == (const MyTime& t1, const MyTime& t2){
return t1.hours == t2.hours && t1.minutes == t2.minutes
&&t1.seconds == t2.seconds;
}
bool operator < (const MyTime& t1, const MyTime& t2){
return (t1.hours*3600 + t1.minutes*60 + t1.seconds) <
(t2.hours*3600 + t2.minutes*60 + t2.seconds);
}
bool operator <=(const MyTime& t1, const MyTime& t2){
return t1 == t2 || t1 < t2;
}
void MyTime::input(istream&ins){
char junk;
ins>>hours;
ins.get(junk);
ins>>minutes;
ins.get(junk);
ins>>seconds;
simplify();
}
void MyTime::output(ostream& outs)const{
outs< outs<<':'< } ostream& operator <<(ostream& outs, const MyTime& t1){ t1.output(outs); return outs; } istream& operator >> (istream& ins, MyTime& t1){ t1.input(ins); return ins; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started