/* * Copyright (c) 2002 by Cliff Green. All rights reserved. Individual files * may be covered by other copyrights (as noted in the file itself). * * Redistribution and use in source and binary forms are permitted * provided that this entire copyright notice is duplicated in all such * copies. * * This software is provided "as is" and without any expressed or implied * warranties, including, without limitation, the implied warranties of * merchantibility and fitness for any particular purpose. */ //---------------------------------------------------------------------- // Source file: money.h // Written by: Cliff Green, 2002 // Compiler: Metrowerks CodeWarrior Pro 6, g++ 3.0.4 // History: // Modified: Mar. 24, 2002 // By: Cliff Green // Comments: Fixed bug in streamOut, when amt < $1.00 (thanks to // Dante diTommaso for discovering it). Also added // operator>, although not strictly necessary. //---------------------------------------------------------------------- // This commentheader supports documentation tools such as Doc++ and // Doxygen: http://www.doxygen.org/ //---------------------------------------------------------------------- /// Money class, very simple for illustration purposes. /** * Ctor possibilities: default, double, dollars and cents. * Internally value is stored as a scaled integer. * * Please don't use this class for any real-life uses, as it is not * accurate in many ways. It is meant to be an illustration of concepts * such as operator overloading, value classes, explicit and implicit * construction, and friendship versus member functions. */ #ifndef MONEY_H #define MONEY_H #include #include #include class Money { public: Money () : mAmt(0) { } Money (double val) : mAmt(static_cast(val * 100.0)) { } // explicit not wanted, implicit conversions ok Money (int dollars, unsigned int cents) : mAmt(dollars*100 + cents) { } // ~Money() { } // implicit dtor // Money (Money const&); // implicit copy ctor // Money& operator= (Money const&) // implicit copy assign op std::ostream& streamOut (std::ostream& os) const { unsigned int cents ((mAmt < 0 ? -mAmt : mAmt) % 100); // must add -0 for negative amts, i.e. < $1 if (-100 < mAmt && mAmt < 0) { os << "-"; } return os << (mAmt / 100) << '.' << std::setw(2) << std::setfill('0') << cents; } // for better accuracy, read each part in as int instead of // as double, but for now, do it the easy way std::istream& streamIn (std::istream& is) { double tmp; is >> tmp; mAmt = static_cast(tmp*100.0); return is; } Money& operator+= (Money rhs) { mAmt += rhs.mAmt; return *this; } Money operator-() { return Money(-mAmt); } friend Money operator+(Money, Money); friend Money operator-(Money, Money); friend Money operator*(Money, Money); friend Money operator*(Money, double); friend bool operator<(Money, Money); friend bool operator>(Money, Money); private: int mAmt; private: explicit Money (int val) : mAmt(val) { } // only avail to friends }; std::ostream& operator<< (std::ostream&, Money const&); inline std::ostream& operator<< (std::ostream& os, Money const& rhs) { return rhs.streamOut(os); } std::istream& operator>> (std::istream&, Money&); inline std::istream& operator>> (std::istream& is, Money& rhs) { return rhs.streamIn(is); } Money operator+ (Money lhs, Money rhs) { return Money(lhs.mAmt + rhs.mAmt); } Money operator- (Money lhs, Money rhs) { return Money(lhs.mAmt - rhs.mAmt); } Money operator* (Money lhs, Money rhs) { return Money((lhs.mAmt * rhs.mAmt)/100); } Money operator* (Money lhs, double rhs) { return (double(lhs.mAmt) / 100.0 * rhs); } bool operator< (Money lhs, Money rhs) { return lhs.mAmt < rhs.mAmt; } bool operator> (Money lhs, Money rhs) { return lhs.mAmt > rhs.mAmt; } #endif // end header