Joined: Sun May 25, 2008 5:34 pm Posts: 95 Has thanked: 2 time Have thanks: 1 time
Code:
#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(17); fred("this"); 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.