Syntactic Sugar





Which Operators?





Syntax for Non-Members

Define using keyword operator and function definition syntax:

const Money operator+ (const Money& a, const Money& b) { return Money( a.GetDollars() + b.GetDollars(), a.GetCents() + b.GetCents() ); }



Syntax for Members

const Money Money::operator+ (const Money& b) const { return Money( m_dollars + b.m_dollars, m_cents + b.m_cents); }



Why return by const value?





Can we return a reference?

Yes, but ...





Automatic Type Conversion





Automatic Type Conversion





Outward Type Conversions





Overloading Unary Operators





Overloading <<





Overloading <<

ostream& operator<< (ostream& sout, const Money& a) { sout << "$" << a.dollars << "." << a.cents ; return sout; }



Overloading >>

istream& operator>>(istream& sin, Money& a) { char dot ; sin >> a.dollars >> dot >> a.cents ; return sin ; }



Final Remarks