#ifndef MONEY_H // avoid repeated expansion #define MONEY_H #include /** Represents a monetary amount in dollars and cents */ class Money { public: /** Construct a new money instance. * Automatically normalizes so that cents is between -99 and 99 and so that * signs of the dollar and cents values do not disagree. * @param dollars value in dollars * @param cents value in cents (sign is assumed to match that of dollars) */ explicit Money(int dollars=0, int cents=0); /** Return the amount of dollars. * * If overall value is negative, the returned amount will be appropriately negated. * * @return number of dollars (signed) */ int getDollars() const; /** Return the amount of cents. * * If overall value is negative, the returned amount will be appropriately negated. * * @return number of cents (signed) */ int getCents() const; /** Checks if current value is negative. * @return true if negative, false otherwise */ bool isNegative() const; /** Binary addition of monetary values. * @param other monetary value as second operand * @return sum of monetary values */ Money operator+(const Money& other) const; /** Mutating form of addition for monetary values. * @param other monetary value as second operand */ void operator+=(const Money& other); /** Binary subtraction of monetary values. * @param other monetary value as second operand * @return difference of monetary values */ Money operator-(const Money& other) const; /** Mutating form of subtraction for monetary values. * @param other monetary value as second operand */ void operator-=(const Money& other); /** Unary negation of monetary value. * @return negation of original value */ Money operator-() const; /** Compares two monetary amounts * @param other monetary value as second operand * @return true if first is stricty less than second */ bool operator<(const Money& other) const; /** Compares two monetary amounts * @param other monetary value as second operand * @return true if first is less than or equal to the second */ bool operator<=(const Money& other) const; /** Compares two monetary amounts * @param other monetary value as second operand * @return true if first is stricty greater than second */ bool operator>(const Money& other) const; /** Compares two monetary amounts * @param other monetary value as second operand * @return true if first is greater than or equal to the second */ bool operator>=(const Money& other) const; /** Compares two monetary amounts * @param other monetary value as second operand * @return true if two are equivalent */ bool operator==(const Money& other) const; /** Compares two monetary amounts * @param other monetary value as second operand * @return true if two are non-equivalent */ bool operator!=(const Money& other) const; /** Multiply monetary amount by a constant. * Rounds to nearest cent. * @param factor multiplicative factor * @return new monetary value */ Money operator*(double factor) const; private: int dollars; int cents; void normalize(); }; /** * Output the value in from such as $5.35 for positive value or * -$5.35 for a negative value. * @param os the output stream * @param value monetary value to display */ std::ostream& operator<<(std::ostream& os, const Money& value); /** Multiply monetary amount by a constant. * @param factor multiplicative factor * @param m monetary value * @return new monetary value * Rounds to nearest cent. */ Money operator*(double factor, const Money& m); #endif