Function Overloading

How Function Overloading Works

It is possible to create two or more functions that have the same name but have completely different implementations.  This is accomplished through function overloading.  C++ compilers distinguish between overloaded functions by their parameters. Overloaded functions MUST differ in the parameters they receive: either in type, number or order.  For example, the following code will NOT compile:

#include <iostream>               //iostream.h is being phased out, be sure to use iostream (without the .h)

using namespace std;            //what is a namespace?

int foo(void);                            //function prototype

void foo(void);                        //function prototype, note same parameters and name, different return type

 

int foo(void)
{
    cout << "foo" << endl;

    return 1;
}

void foo(
void)
{
    cout << "Hello." << endl;
}

void main(void)
{
    foo();
}

The following error is generated:

error C2556: 'void __cdecl foo(void)' : overloaded function differs only by return type from 'int __cdecl foo(void)'

The compiler is complaining that the only thing different about the declaration of the two overloaded functions is the return type, but we just learned that functions MUST have different parameters in order to be overloaded.  It is clear why this is an error when you look at how the function is called.  When foo is called from main, the compiler looks for a function named foo that takes no parameters, but there are 2 functions that fit that description!  What is a compiler to do?

Here is the code with correct function overloading:

#include <iostream>

using namespace std;

int foo(void);

void foo(int);  //now takes an int

 

int foo(void)
{
    cout << "foo" << endl;

    return 1;
}

void foo(int bar)
{
    cout << "bar" << endl;
}

void main(void)
{
    foo();
}

Now it is completely clear to the compiler which function should be called.  There are 2 instances of foo but one MUST take an int while the other MUST take no parameters.  In main, int foo() (the foo which takes no parameters) is called and the output is: "foo".  

Fun With Functions

So far the examples have had different return types.  The next example demonstrates that function overloading works with functions which have the same return type.

#include <iostream>

using namespace std;

int foo(bool);

int foo(int);  //notice - same return type, different parameter type


int foo(bool goo)
{
    return -1;
}

int foo(int bar)
{
    return 1;
}

void main(void)
{
    cout << "Calling foo, passing a bool." << endl;
    cout << "foo returned " << foo(true) << endl << endl;


    cout << "Calling foo, passing an int." << endl;
    cout << "foo returned " << foo(2) << endl;
}

The output will be:

"Calling foo, passing a bool."

"foo returned -1"

"Calling foo, passing an int."

"foo returned 1"

Here is one final example which demonstrates three functions that are overloaded.  Notice that two of them have the same parameters and return type but the ORDER of the parameters is different.

#include <iostream>

using namespace std;

int foo(bool, int);

int foo(int, bool);  //notice - same return and parameter types, different order

void foo(char);


int foo(bool goo, int boo)
{
    cout << "Function ";

    return 0;
}

int foo(int bar, bool gar)
{
    cout << "overloading ";

    return 0;
}

void foo(char coo)
{
    cout << "is powerful!";
}

void main(void)
{
    foo(true, 1);

    foo(1, true);

    foo('a');
}

The output is:

"Function overloading is powerful!"

At this point you may be thinking: "Powerful?  Yeah right, all I see is some text on the screen, I could do that with one function call."  True, these examples are extremely simplistic, the real power of overloading and polymorphism comes later, patience grasshopper.  For now just understand that many functions can share a name but execute completely different code.

 

Quick Reference

The following chart shows what attributes affect the ability of a function to be overloaded:

Attribute Affects Overloading?
return type NO
function implementation (code between the { } ) NO
function name YES (must be the same)
type of parameters YES (must be different)
order of parameters YES (must be different)
number of parameters YES

 

Test Your Might

There are no "trick" questions here, if there is an error please let me know.  A link to the answers is found after the last question (no fair using your compiler!).

1.  What is the output of the following code:

#include <iostream>

using namespace std;

int function(void);
char function(int);
void function(int, bool);
void function(int, char);

int function()
{
    cout << "I think I know this one!" << endl;
    return 0;
}

char function(int x)
{
    return 'A';
}

void function(int x, bool y)
{
    if(y)
        cout << x << endl;
}

void function(int x, char y)
{
    cout << x << y << endl;
}

void main(void)
{
    function();
    function(100, true);
    cout << function(25) << endl;
}


 

2.  Why is the following set of functions not overloaded properly?

   int function(void);

    char function(void);

    int functions(int);

 

3.  Write four overloaded functions (prototypes only, no implementation) so that a different behavior could be implemented if the function was called with (an integer), (a boolean), (a character and an integer), or (an empty parameter).

 

 

ANSWERS (you didn't use your compiler, did you?)