
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Modifier Types
- C++ Storage Classes
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Pointers
- C++ Pointers
- C++ Dereferencing
- C++ Modify Pointers
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
Function Overloading in C++
Function overloading in C++ allows you to define multiple functions with the same name but different parameters. Function overloading is used to achieve polymorphism which is an important concept of object-oriented programming systems.
Syntax for Overloaded Functions
Consider the following two function declarations having the same name but different parameters −
return_type function_name(parameter1); return_type function_name(parameter2);
Example of Function Overloading
In the following example, we are defining three different functions with the same name but different parameters. This example demonstrates the implementation of function overloading −
#include<iostream> using namespace std; // Adding two integers (Function definition 1) int addition(int a, int b) { return a + b; } // Adding three integers (Function definition 2) int addition(int a, int b, int c) { return a + b + c; } // Adding two floating-point numbers (Function definition 3) float addition(float a, float b) { return a + b; } int main() { cout<<addition(10.5f, 20.3f)<<endl; cout<<addition(10, 20, 30)<<endl; cout<<addition(10, 20)<<endl; return 0; }
Output
30.8 60 30
How Function Overloading Works?
In the case of different functions with the same name (function overloading), when the compiler reaches a specific function call, it checks with the different function definition based on the parameters type, order, or number of arguments, and executes the matched function definition.
Example
Let suppose, there are 3 different function definitions to add numbers with different parameters −
// Adding two integers (Function definition 1) int addition(int a, int b) { return a + b; } // Adding three integers (Function definition 2) int addition(int a, int b, int c) { return a + b + c; } // Adding two floating-point numbers (Function definition 3) float addition(float a, float b) { return a + b; }
And, you call the function in the following order −
addition(10.5f, 20.3f); // Function call 1 addition(10, 20, 30); // Function call 2 addition(10, 20); // Function call 3
In the above function calls, function definition will be called in the following order −
- Function call 1 will execute Function definition 3, because here we are passing two float values.
- Function call 2 will execute Function definition 2, because here we are passing three integer values.
- Function call 3 will execute Function definition 1, because here we are passing two integer values.
Function Overloading Based on Number of Parameters
This method involves defining multiple functions with the same name but a different number of parameters.
Syntax
void display(int a); // takes one parameter void display(int a, double b); // takes two parameters
Example
The following example demonstrates the function overloading based on the number of parameters −
#include <iostream> using namespace std; // Function overloads based on the number of parameters void display(int a) { cout << "Display with one integer: " << a << endl; } void display(int a, double b) { cout << "Display with an integer and a double: " << a << " and " << b << endl; } int main() { // Calls the first overload display(10); // Calls the second overload display(10, 3.14); double: 10 and 3.14 return 0; }
Output
Display with one integer: 10 Display with an integer and a double: 10 and 3.14
Function Overloading Based on Different Parameter Types
This method involves defining multiple functions with the same name but different types of parameters.
Syntax
void show(int a); // parameter with int type void show(double a); // parameter with double type
Example
The following example demonstrates the function overloading based on the different parameter types −
#include <iostream> using namespace std; // Function for integer input void show(int a) { cout << "Integer value: " << a << std::endl; } // Function for double input void show(double a) { cout << "Double value: " << a << std::endl; } int main() { show(10); show(3.14); return 0; }
Output
Integer value: 10 Double value: 3.14
Function Overloading Based on Different Parameter Order
This method involves defining multiple functions with the same name but different sequences of parameters.
Syntax
// integer followed by a double, // can be any datatype(bool, char etc) void display(int a, double b) { cout << "Integer and Double: " << a << ", " << b << endl; } // double followed by an integer void display(double a, int b) { cout << "Double and Integer: " << a << ", " << b << endl; }
Example
The following example demonstrates the function overloading based on the different parameter order −
#include <iostream> using namespace std; void display(int a, double b) { cout << "Integer and Double: " << a << ", " << b << endl; } void display(double a, int b) { cout << "Double and Integer: " << a << ", " << b <<endl; } int main() { display(10, 3.14); display(3.14, 10); return 0; }
Output
Integer and Double: 10, 3.14 Double and Integer: 3.14, 10
Use Cases for Function Overloading
Function overloading in C++ is a powerful feature that allows you to use the same function name to perform different tasks based on different parameter lists. This can lead to more readable and maintainable code. Here are some common scenarios and examples where function overloading is useful −
- Different Data Types − Function overloading is useful for handling various data types with a common function name.
- Different Number of Parameters − Function overloading provides flexibility with functions which have varying numbers of parameters.
- Parameter Type and Order − Function overloading handles different parameter types or their order.
- Different Operations − Function overloading supports similar operations for different data types or contexts.
- Variant Contexts − Function overloading provides variations of a function to suit different requirements or levels of detail.