Switch to full style
C++ code examples
Post a reply

Function Overloading

Thu Nov 13, 2008 1:59 pm

Function overloading in C++
cpp 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.

Which of the following may be added?

cpp code
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);




Post a reply
  Related Posts  to : Function Overloading
 overloading << and >>     -  
 What is Operator Overloading? !!!     -  
 operator overloading     -  
 unary operator overloading     -  
 Operator overloading easy code     -  
 php function     -  
 The isset() Function     -  
 Function Recursion     -  
 finalize() function     -  
 srand function example     -  

Topic Tags

C++ OOP