// // File: Money.cpp. C++ file of method definitions for // Money class // #include using namespace std; #include "Money.h" // // Default Money constructor for $0.00 // Pre: none // Post: The object $0.00 has been constructed. // Money::Money() { dollarsAndCents = 0; } // // Money constructor for $dollars.cents // Pre: dollars is the long dollar amount. // cents is the int cents amount. // Post: The object $dollars.cents has been constructed. // Money::Money(long dollars, int cents) { dollarsAndCents = dollars * 100 + cents; } // // Money constructor for double argument // Pre: amount is a double amount. // Post: The corresponding (rounded) Money object was constructed. // Money::Money(double amount) { dollarsAndCents = round(amount * 100); } // // Function to assign $dollars.cents to Money object // Pre: dollars is the long dollar amount. // cents is the int cents amount. // Post: This object represents $dollars.cents. // void Money::assignMoney(long dollars, int cents) { dollarsAndCents = dollars * 100 + cents; } // // Function to assign double argument to Money object // Pre: amount is a double amount. // Post: This object represents (rounded) $amount. // void Money::assignMoney(double amount) { dollarsAndCents = round(amount * 100); } // // Function to add a Money parameter to this object // Pre: cash is a Money object. // Post: cash has been added to this object. // void Money::addTo(Money cash) { dollarsAndCents = dollarsAndCents + cash.dollarsAndCents; } // // Function to multiply this object by a double number // Pre: number is a double number. // Post: This object has been multiplied by number and rounded. // void Money::multiplyBy(double number) { dollarsAndCents = round(dollarsAndCents * number); } // // Function to return the quotient of this object and // a Money parameter // Pre: cash is a Money object. // Post: The double quotient of this object divided by cash // has been returned. // double Money::quotient(Money cash) { return static_cast(dollarsAndCents)/cash.dollarsAndCents; } // // Boolean function to return true if this object less // than a Money parameter // Pre: cash is a Money object. // Post: If this object is less than cash, true has been // returned, false otherwise. // bool Money::lessThan(Money cash) { bool returnValue; if (dollarsAndCents < cash.dollarsAndCents) returnValue = true; else returnValue = false; return returnValue; } // // Boolean function to return true if this object is // equal to a Money parameter // Pre: cash is a Money object. // Post: If this object is equal to cash, true has been // returned, false otherwise. // bool Money::equalTo(Money cash) { bool returnValue; if (dollarsAndCents == cash.dollarsAndCents) returnValue = true; else returnValue = false; return returnValue; } // // Function to display this object // Pre: none // Post: The decimal value of this object has been displayed. // void Money::display() { int cents; cout << dollarsAndCents / 100 << "."; cents = dollarsAndCents % 100; if (cents < 10) cout << "0"; cout << cents; } // // Function to round a floating point number // Pre: num is a double number. // Post: The function returned the value of num rounded to // the nearest integer. // long Money::round(double num) { long roundedNum; // rounded num if (num >= 0) roundedNum = static_cast(num + .5); else roundedNum = static_cast(num - .5); return roundedNum; }