Lecture Notes for Introduction to Computer Science II

7 June 2001 - Function Overloading


  1. parts of the function - the signature

    1. function name

    2. number, order, and types of the function arguments

    3. not the return type, not the argument names

  2. two signatures are the same if they agree in every aspect - otherwise, they're different

  3. two functions are the same only if they have the same signatures

    1. two different signatures mean two different functions

  4. overloaded functions agree in name but have different signatures

    1. bool find(int i) and bool find(string s)

  5. used sparingly overloading functions is a useful technique

    1. try to vary the signatures by type, not by number or order

      1. for example find(int), find(string), find(char *)

      2. there's (almost) nothing to remember - easy to use

    2. varying by order can get confusing, particularly with more than two arguments

    3. varying by number can also be confusing

  6. watch out for bad interactions with other c++ features, particularly with type conversions

  7. constructor overloading

    1. constructors are functions, so they can take arguments

      1. the arguments can be used to help determine how class members are initialized

    2. being functions, constructors may be overloaded

    3. the calling syntax puts the parameter list on the variables

    4. for example

      1. class gear { gear(int cogs) { . . . } . . .};

      2. gear g(3);

    5. several different constructors may be called using the function overloading rules

    6. for example

      1. class gear { gear() { . . . } gear(int cogs) { . . . } . . .};

      2. gear g, g2(3);

    7. be careful - it is an extremely annoying error to use gear g(); when you meant gear g;


This page last modified on 6 June 2001.