Explain function overloading resolution and function overloading types in C++

Learn: What is the function overloading and function overloading resolution with its types in C++ programming language?

If you didn’t read about the function overloading, I would recommend please read C++ function overloading before reading this article.

Function overloading resolution

When we call overloaded functions, the decision of which function is to be executed is known as function overloading resolution. We cannot overload functions on the basis of return type. It means function overloading resolution is done on the basis of only signature.

There are following criteria for function overloading resolution:

  • Function call exact match or match using only conversion. Example array name to pointer, function name to pointer to function.
  • Function call match using promotion:
    • Promotion in integral data-type: bool → char → short → int → long int
    • Promotion in floating point type: float → double → long double
  • Function call match using standard conversions:
    • Conversion for standard data types: integral → float type
    • Conversion hierarchy:
      • derived * → base *
      • data type * → void *
  • Function calling match with the help of user define conversions.

Note: When two function call matches are found at same time, then this call is rejected and known as ambiguous call.

Function overloading types

There are different ways to overload function:

  1. Different number of arguments
  2. Different types of arguments
  3. Different order of arguments
  4. Different number and type of arguments both

1) Different number of arguments

We can overload function with the help of number of arguments, argument in function can be zero , one and more than one.

Example:

void print();
void print(char ch);
void print(char ch, int num);

2) Different types of arguments

We can overload function on the basis of different types of arguments. It means every function have argument of different data type.

Example:

void printval(int val);
void printval(char val);
void printval(float val);

3) Different order of arguments

We can overload function on the basis of order of argument.

Example:

void printChar(int num, char ch);
void printChar(char ch, int num);

4) Different number and type of arguments both

We can also combine above type of function overloading. .


Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.