UMBC CMSC 202 CSEE | 202 | current 202

Overloading Summary

We now have three ways to overload (some) operators
  1. As a member function (required for some operators)
  2. As a non-member function
  3. As a (non-member) friend function
but some restrictions apply and some guidelines dictate which way is best (but not always).

Restrictions

  1. Not all operators can be overloaded.
  2. You can't make up your own operators.
  3. You can't overload operators for primitive types.
  4. You can't change the precedence of an operator.
  5. You can't change the associativity of an operator.

Good Programming Practice

  1. Overload operators so that they mimic the behavior of primitive data types.
  2. Overloaded binary arithmetic operators should
  3. Overload unary operators as member functions.
  4. Always overload<<
  5. Overload operator= if your class uses dynamic memory allocation

Exercises for the student

Using the code for the Money class from the text, write the prototypes and implementations for other overloaded operators. Be sure to try comparison operators like <, >, !=, etc. and arithemtic operators like += and -=. Also try some unary operators such as ++ and --.


Last Modified: Monday, 28-Aug-2006 10:15:53 EDT