MC logo
Function Overloading
[^] CSc 220 Home
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Functional Average] [Functional Average II] [Functional Average III] [Default Parameters] [Function Overloading] [Reference Parameters] [Constant References] [Namespaces] [Namespaces I] [Using Namespaces]
#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);

Overloading is C++ only.