// // File: money.cpp. C++ file of method definitions for // money class // #include #include "money.h" // // Default money constructor for $0.00 // Pre: none // Post: The object $0.00 has been constructed. // money::money(void) { 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 $amount (rounded). // 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 double(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. // boolean_t money::LessThan(money cash) { boolean_t 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. // boolean_t money::EqualTo(money cash) { boolean_t 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(void) { 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 = long(num + .5); else RoundedNum = long(num - .5); return RoundedNum; }