#include "money.h" // constructor - default money::money(void) { nD = 0; nP = 0; } // constructor - two argument (int, int) money::money(int nPennies,int nDimes) { nD = nDimes; nP = nPennies; } // constructor - copy money::money(const money & other) { nD = other.nD; nP = other.nP; } money::~money(void) { // intentionally left blank unless I need to tidy dynamic memory } // accessor function int money::getDimes(void) const { return nD; } // accessor function int money::getPennies(void) const { return nP; } // accessor function int money::getTotalValue(void) const { return (nD * 10) + nP; } // mutator function money & money::setNumD(int n) { if ( n == nD ) return *this; // same value - no need to process nD = n; return *this; } // mutator function money & money::setNumP(int n) { if ( n == nP ) return *this; // same value - no need to process nP = n; return *this; } money & money::setValues(int cents) { nD = cents / 10; nP = cents % 10; return *this; } // client overloaded operator - // answer will be computed by subtracting total values, // right from left and the answer will be express as ALL pennies money operator-(const money & left, const money & right) { money temp; temp.setValues( left.getTotalValue() - right.getTotalValue() ); // cout << temp.getTotalValue(); return temp; // Try this if the above works: // return temp.setValues( left.getTotalValue() - right.getTotalValue() ); } const money & money::operator = ( const money & r) { (*this).nP = r.nP; this->nD = r.nD; return (*this); } bool money::operator == (const money & r ) const { return ( (this->getDimes() == r.getDimes()) && (this->getPennies() == r.getPennies()) ); } // will return true if the // number of dimes in the invoking instance is the same // as the number of dimes in r AND the number of pennies // in the invoking instance is the same as the number // of pennies in the parameter r bool money::operator != (const money & r) const { return ( this->getTotalValue() != r.getTotalValue() ) ? true : false; } bool money::operator < ( const money & r) const { return ( this->getTotalValue() < r.getTotalValue() ) ? true : false; } // use getTotalValue for comparison money money::operator+(const money & r) const { money temp; temp.setNumD ( this->getDimes() + r.getDimes() ); temp.setNumP ( this->getPennies() + r.getPennies() ); return temp; } // return money object with // corresponding pennies and // dimes added together bool money::operator!(void) const { return ( this->getTotalValue() <= 0 ) ? true : false; } // return true if "BROKE", i.e., no money bool money::oweMoney(void) const { return ( this->getTotalValue() < 0 ) ? true : false; } // return true if you owe money const int money::NUMBER_PENNIES_PER_DIME = 100; ostream & operator << ( ostream & outs, const money & moneyArg) { outs << moneyArg.getPennies() << "P " << moneyArg.getDimes() << "D\n"; return outs; } // xxP yyD istream & operator >> ( istream & ins, money & moneyArg) { int p,d; cout << "\nEnter # of pennies: "; ins >> p; cout << "\nEnter # of dimes: "; ins >> d; moneyArg.setNumP(p); moneyArg.setNumD(d); return ins; } // input pennies dimes values