MC logo

Function Overloading

  Practice Questions

  1. Under what circumstances may two different C++ functions have the same name?

    Consider

    #include <iostream.h>

    void fred(char *msg, int cnt = 3, char *post = "!")
    {
            cout << msg;
            while(cnt--) cout << post;
            cout << endl;
    }
    void fred(char *msg, char *post)
    {
            fred(msg, 3, post);
    }
    What does each of the following print?

  2. fred("Hi");

  3. fred("Huh", "?");

  4. fred("Never", 10);

  5. fred("Arrrggggg", 5, "h");

  6. Is fred a recursive function?

  7. Why does fred contain fred(msg, 3, post); instead of fred(msg, , post);?

  8. What would happen if we changed void fred(char *msg, char *post) to
    void fred(char *msg, char *post = "!")?

  9. What if we also changed void fred(char *msg, int cnt = 3, char *post = "!") to
    void fred(char *msg, int cnt = 3, char *post)?

<<Access Control Answer Operator Overloading>>