# Syntactic Sugar

• If we had functions, SUM, DIFF, PRODUCT and QUOTIENT, we could write: A = QUOTIENT(PRODUCT(SUM(B,C),DIFF(D,E)),F) ;

• Much cleaner to use: A = ((B + C) * (D - E)) / F ;

# Which Operators?

• C++ allows you to define the result of operators such as: + - * / % [] ++ -- += < > ==

• Cannot overload these four operators: :: .* . ?:

• Bad idea to overload && and ||: no short circuit evaluation.

• Cannot redefine behavior of operators on built-in types.

• Precedence and associativity are same as for built-in types.

# 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() ); }
• Money has two members: dollars and cents.

• Alternate constructor Money(a,b).

• Returns const Money.

• Temporary Money object created.

# Syntax for Members

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

• Only one parameter present. The left hand side (lhs) is implicit.

• a + b is equivalent to a.operator+(b).

• m_dollars and m_cents belong to the host object a.

• Is a const member function.

# Why return by const value?

• (a + b) is a temporary Money object.

• (a + b) = c is allowed without const

• d = (a + b) = c looks really strange.

# Can we return a reference?

Yes, but ...

• Should not return a reference to local variable.

• Return value can be bounded to a reference variable during its definition: Money& X = (a + b) ; // if + returned a reference

• Return value copied in other situations: Money A ; Money& X = A ; X = (a + b) ; // return value of + copied to X

# Automatic Type Conversion

• Money has an alternate constructor: Money (int dollars) ;

• This type of constructor is also called a type converter.

• Rules for overloading functions apply, including type conversion. Allows syntax like: Money a ; Money d = a + 10 ;

• Compiler calls constructor to convert 10 from int to Money.

# Automatic Type Conversion

• If operator+ is defined as a member function, cannot have: Money a ; Money d = 10 + a ;

• Compiler looks for converter from Money to int in int "class".

• If operator+ defined as a non-member function, then either operand, lhs and rhs, can be converted automatically.

• Non-member overloaded operators often need to be declared a friend of the class to allow access to private data members.

# Outward Type Conversions

• A constructor with one parameter of a different type is considered an "inward" type converter. For example: Money (float amount) ;

• To convert Money to float we need an "outward" type converter. Define a member function: operator float () ; // note weird syntax!

• Write a function that returns a float value.

• Not in your textbook AFAIK.

• For example: -a    ++a    a++

• Same syntax, one less operand.

• Dummy parameter distinguishes pre-increment and post-increment: Money Money::operator++( ) ; // pre-inc Money Money::operator++( int dummy ) ; // post-inc

• Technical term for this is a hack.

• For int, ++n returns a reference and n++ returns a value, but textbook recommends always returning a value.

• Non-member prototype: ostream& operator<< (ostream& sout, const Money& a) ;

• Can't (wouldn't want to) re-define iostream class.

• The intention is for sout to be the return value.

• The << operator associates left. For example: cout << "Henry paid me " << a << "today\n" ; uses 2 different overloaded << operators.

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

• Uses sout instead of cout.

• Must be a friend of Money class to access private data members dollar and cents.

• Preferable to use public member functions GetDollar() and GetCents().

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

• Returns sin as istream reference.

• Must be a friend of Money class or use public mutator functions.

# Final Remarks

• It is syntactic sugar.

• Great when the operator has intuitive meaning.
(E.g., + for string concatenation.)