#include #include "distance.h" using namespace std; void DistanceT::Reset(void) { inch = 0; foot = 0; yard = 0; mile = 0; return; } void DistanceT::Reduce(void){ foot += inch/INCH_TO_FOOT; inch %= INCH_TO_FOOT; yard += foot/FOOT_TO_YARD; foot %= FOOT_TO_YARD; mile += yard/YARD_TO_MILE; yard %= YARD_TO_MILE; return; } DistanceT::DistanceT(){ Reset(); return; } DistanceT::DistanceT(int inches){ Reset(); inch = inches; Reduce(); return; } DistanceT::DistanceT(int feet, int inches){ Reset(); inch = inches; foot = feet; Reduce(); return; } DistanceT::DistanceT(int yards, int feet, int inches){ Reset(); inch = inches; foot = feet; yard = yards; Reduce(); return; } DistanceT::DistanceT(int miles, int yards, int feet, int inches){ Reset(); inch = inches; foot = feet; yard = yards; mile = miles; Reduce(); return; } DistanceT::~DistanceT(){ if (VERBOSE) { cout << "In the destructor" << endl; } return; } void DistanceT::CopyData(const DistanceT & other) { inch = other.inch; foot = other.foot; yard = other.yard; mile = other.mile; } DistanceT::DistanceT(const DistanceT & other){ if (VERBOSE) { cout << "In the copy constructor " << endl; } CopyData(other); return; } DistanceT & DistanceT::operator = (const DistanceT & other){ if (this != &other) { CopyData(other); } return *this; } DistanceT DistanceT::Add(const DistanceT & other) const{ DistanceT rv(other); rv.inch += inch; rv.foot += foot; rv.yard += yard; rv.mile += mile; rv.Reduce(); return rv; } DistanceT DistanceT::operator + (const DistanceT & other) const{ DistanceT rv(other); rv.inch += inch; rv.foot += foot; rv.yard += yard; rv.mile += mile; rv.Reduce(); return rv; } string DistanceT::Cat(int measure, string name) const { if (measure != 0) { return to_string(measure) + name + " "; } return ""; } string DistanceT::DistanceString(void) const{ string rv; rv += Cat(mile, " mi"); rv += Cat(yard, " yd"); rv += Cat(foot, " ft"); rv += Cat(inch, " in"); if (rv == "") { // return something rv = "0 in"; } else { // strip off the last space. rv = rv.substr(0,rv.size()-1); } return rv; }