MC logo
Function Overloading
[^] Code Examples
<<Default Parameters over.cc Reference Parameters>>
#include <iostream>
#include <string>

using namespace std;

void fred(int a)
        cout << "fred the first, " << a << endl;

void fred(int a, string b)
        cout << "I am the second fred: " << a << " " << b << endl;

void fred(string c, string d = "ding!")
        cout << "Lo, I am fred tertiary.  " << c << " " << d << endl;

int main()
        fred(24, "hours");
        fred("some", "day");

C++ function overloading is much like Java's. Overloaded functions must differ by parameter type; differences in return type won't help. Default parameters provide an additional wrinkle, since a function with default parameters essentially counts as several overloaded functions, one for each signature with which it can be called.

Which of the following may be added?

int fred(int x, string y);

int fred(double d);

double fred(string z, int q);

void fred(string cc);

int fred(int m, double d = 0.0);

Reading: pp. 125-144 exc. arrays
<<Default Parameters Reference Parameters>>