C++ Function Overloading

Introduction

We can have multiple functions with the same name but with different parameter types.

This is called function overloading.

A simple explanation: when the function names are the same, but the parameter types differ, then we have overloaded functions.

Example of a function overload declarations:

void myprint(char param); 
void myprint(int param); 
void myprint(double param); 

Then we implement function definitions and call each one:

#include <iostream> 

void myprint(char param); 
void myprint(int param); 
void myprint(double param); 

int main() /*  ww w.  j  a v  a 2s.  co m*/
{ 
    myprint('c');        // calling char overload 
    myprint(123);        // calling integer overload 
    myprint(456.789);    // calling double overload 
} 

void myprint(char param) 
{ 
    std::cout << "Printing a character: " << param << '\n'; 
} 

void myprint(int param) 
{ 
    std::cout << "Printing an integer: " << param << '\n'; 
} 

void myprint(double param) 
{ 
    std::cout << "Printing a double: " << param << '\n'; 
} 

When calling our functions, a proper overload is selected based on the type of argument we supply.

In the first call to myprint('c'), a char overload is selected because literal 'c' is of type char.

In a second function call myprint(123), an integer overload is selected because the type of an argument 123 is int.

In our last function call myprint(456.789), a double overload is selected by a compiler as the argument 456.789 is of type double.

Yes, literals in C++ also have types, and the C++ Standard precisely defines what type that is.

Some of the literals and their corresponding types:

'c'      -    char 
123      -    int 
456.789  -    double 
true     -    boolean 
"Hello"  -    const char[6] 

Two functions with the same name are different if at least one of the following is true:

  • The functions have different numbers of parameters.
  • At least one pair of corresponding parameters are of different types.
#include <iostream>
#include <string>
#include <vector>
using std::string;
using std::vector;

// Function prototypes
double largest(const double data[], int count);
double largest(const vector<double>& data);
int largest(const vector<int>& data);
string largest(const vector<string>& words);

int main()/* w w w.  j  a v a  2  s . c  o m*/
{
  double values[] {1.5, 44.6, 13.7, 21.2, 6.7};
  vector<int> numbers {15, 44, 13, 21, 6, 8, 5, 2};
  vector<double> data {3.5, 5, 6, -1.2, 8.7, 6.4};
  vector<string> names {"C", "E", "J", "H", "A"};
  std::cout << "The largest of values is " << largest(values, sizeof(values)/sizeof(values[0])) << std::endl;
  std::cout << "The largest of numbers is " << largest(numbers) << std::endl;
  std::cout << "The largest of data is " << largest(data) << std::endl;
  std::cout << "The largest of names is " << largest(names) << std::endl;
}

// Finds the largest of an array of double values
double largest(const double data[], int count)
{
  int index_max {};
  for (int i {1} ; i < count ; ++i)
    if (data[index_max] < data[i])
      index_max = i;
  return data[index_max];
}

// Finds the largest of a vector of double values
double largest(const vector<double>& data)
{
  double max {data[0]};
  for (auto value : data)
    if (max < value) max = value;

  return max;
}

// Finds the largest of a vector of int values
int largest(const vector<int>& data)
{
  int max {data[0]};
  for (auto value : data)
    if (max < value) max = value;

  return max;
}
// Finds the largest of a vector of string objects
string largest(const vector<string>& words)
{
  string max_word {words[0]};
  for (auto& word : words)
    if (max_word < word) max_word = word;

  return max_word;
}
#include <iostream>
using namespace std;
void repchar();             //declarations
void repchar(char);
void repchar(char, int);
int main()/*from   w  w  w  .  j a  v  a  2s.  c  om*/
{
   repchar();
   repchar('=');
   repchar('+', 30);
   return 0;
}
// displays 45 asterisks
void repchar()
{
   for(int j=0; j<45; j++)  // always loops 45 times
      cout << '*';          // always prints asterisk
   cout << endl;
}
// displays 45 copies of specified character
void repchar(char ch)
{
   for(int j=0; j<45; j++)  // always loops 45 times
      cout << ch;           // prints specified character
   cout << endl;
}
// displays specified number of copies of specified character
void repchar(char ch, int n)
{
   for(int j=0; j<n; j++)   // loops n times
      cout << ch;           // prints specified character
   cout << endl;
}



PreviousNext

Related