Function Overloading
C++ function overloading allows you to define multiple functions with
the same name but different parameters. It is a form of compile time
polymorphism in which a function can perform different jobs based on
the different parameters passed to it. It is a feature of object-oriented
programming that increases the readability of the program.
Example
int add2(int a, int b) {
return a + b;
int add3(int a, int b, int c) {
return a + b + c;
Rules for Function Overloading
Same Function Name: All overloaded functions must have the exact same name.
Different Parameter Lists: The parameter lists of the overloaded functions must
differ. This difference can be in:
o Number of parameters: void func(int a); and void func(int a, int
b);
o Types of parameters: void func(int a); and void func(double a);
o Order of parameters (if types differ): void func(int a, double b); and
void func(double a, int b);
Return Type Alone is NOT Sufficient: You cannot overload functions based solely
on their return type. For example, int func(int a); and double func(int a);
will result in a compilation error because their parameter lists are identical. The
compiler cannot determine which function to call based on the return type alone at the
point of the function call.
typedef and using do not affect overloading: typedef and using declarations
create synonyms for existing types, they don't define new types. Therefore, they don't
affect function overloading. For example, typedef int MyInt; void func(int
a); and void func(MyInt a); are considered to have identical argument lists.
Default Arguments: Be careful when using default arguments with overloaded
functions, as they can lead to ambiguity. If a function call could match multiple
overloads due to default arguments, the compiler will report an error.
Different Ways of Function Overloading
A function in C++ can be overloaded in three different ways:
By having different number of parameters.
By having different types of parameters.
By having both different number and types of parameters.
Different Number of Parameters
We can overload a function by changing the number of parameters that
it accepts.
#include <iostream>
using namespace std;
// Two functions taking different number
// of parameters
int multiply(int a, int b);
int multiply(int a, int b, int c);
int main() {
// Calling multiply function with
// different number of arguments
cout << multiply(10, 2) << endl;
cout << multiply(5, 6, 4);
return 0;
// Definition of functions
int multiply(int a, int b) {
return a * b;
int multiply(int a, int b, int c) {
return a * b * c;
Different Types of Parameters
We can also change the data type of the parameters while keeping the
same function name in function overloading.
#include <iostream>
using namespace std;
// Function with different arguments type
int add(int a, int b);
double add(double a, double b);
int main() {
// Calling add function with different
// argument type
cout << add(10, 2) << endl;
cout << add(5.3, 6.2);
return 0;
// Function definitions
int add(int a, int b) {
return a + b;
double add (double a, double b) {
return a + b;
Different Number and Types of Parameters
Functions can also be overloaded by changing both the number and
types of parameters.
#include<iostream>
using namespace std;
// Functions overloaded with different
// argument number and types
int add(int a, double b);
double add(double a, int b, int c);
int main() {
// Calling both overloaded versions
cout << add(10, 2.5) << endl;
cout << add(5.5, 6, 12);
return 0;
// Function definitions
int add(int a, double b) {
return a + (double)b;
double add(double a, int b, int c) {
return a + (double)b + (double)c;
Functions that Cannot be Overloaded
In C++, we have some specific scenarios in which the functions cannot
be overloaded. As function overloading is primarily based on the
function name and the types or number of its parameters. There could
be cases where the C++ compiler cannot differentiate between
functions and thus cannot be overloaded.
Member function declarations with different access specifier but
same name and same parameter-type-list cannot be overloaded.
Functions with parameter declarations that differ only in a pointer *
versus an array [].
Functions with same parameter but different passing method (i.e.
passed by value and passed by reference).
Function with same parameter but different volatile type specifier.
Aspect Function Overloading Function Overriding
Definition Same function name with Same function with same
different parameters. parameters but different
Aspect Function Overloading Function Overriding
function definition.
To create multiple functions To change or extend the
that perform similar tasks with behavior of an inherited
Purpose varying arguments. function.
Function signature should
Function Function signatures should be
be similar (name, return
different.
Signature type, parameters).
Compile-time
Done at compile time Done at run time
or Runtime
Overloaded functions are in Overridden functions are
Scope same scope. in different scopes.
Default Arguments
A default argument is a value provided for a parameter in a function
declaration that is automatically assigned by the compiler if no value is
provided for those parameters in function call. If the value is passed for
it, the default value is overwritten by the passed value.
Default arguments in C++ allow you to specify a default value for a function parameter. If a caller
doesn't provide an argument for that parameter, the default value is used. This feature enhances
function flexibility and reduces the need for multiple overloaded functions when some parameters
frequently have the same value.
Syntax
A default argument is defined by assigning a value to a function
parameter in its declaration.
return_type name (p1= v1, p2= v2, ...);
Rules to Follow
There are some important rules and best practices to keep in mind
when using default arguments in C++:
1. Default Values Must Be Specified in Function Declarations
The default values for parameters must be specified in the function
declaration (or prototype). If a function is declared and defined
separately, the default values must be in the declaration, not in
definition.
// Declaration with default argument
void func(int x = 10);
// Definition without default argument
void func(int x) {
cout << "Value: " << x << endl;
}
2. Default Arguments Cannot Be Modified
Once default arguments are defined in the declaration, they cannot be
modified in the function definition. If you try to change the default value
in the definition, it will result in a compilation error.
// Declaration
void f(int a = 10);
// This definintion will throw and error
void f(int a = 222) {
// statements
}
3. Default Arguments Must Be Provided from Right to Left
In a function with multiple parameters, default values must be provided
from the rightmost parameter to the left. It means that if a parameter
has a default argument, all parameters to its right must also have
default values.
// Valid
void func(int x, int y = 20);
// Invalid, as `y` does not have a default value
void func(int x = 10, int y);
4. Ambiguity in Function Overloading
If a function containing default arguments is overloaded, then we need
to make sure it is not ambiguous to the compiler, otherwise it will throw
an error. For example,
// Valid
void f(int a = 10, int b = 20);
// Will throw error as the signature is same
void f(int a = 22, int b = 2);
// Will also throw error
void f(int a);
// Will also throw an error
void f(int a, b)
Advantages
Default arguments are useful when we want to increase the
capabilities of an existing function.
It helps in reducing the size of a program.
It provides a simple and effective programming approach.
Default arguments improve the consistency of a program.
Disadvantages
If the default values are not well-documented or understood, it can
lead to confusion about what arguments are being used.
Overloading functions with default arguments can sometimes lead to
ambiguities.
It increases the execution time as the compiler needs to replace the
omitted arguments by their default values in the function call.
Constructor Overloading
We can have more than one constructor in a class with same name, as
long as each has a different list of [Link] concept is known as
Constructor Overloading and is quite similar to function overloading.
In C++, just like regular functions, constructors can also be overloaded. This means you
can have multiple constructors within the same class, each with a different parameter list
(signature). The compiler differentiates between these constructors based on the number,
type, and order of their parameters.
Overloading constructors is a fundamental concept for providing different ways to initialize
objects of a class.
Rules for Overloading Constructors
The rules for overloading constructors are the same as for overloading regular functions:
1. Same Name: All overloaded constructors must have the same name as the class itself.
2. Different Parameter Lists (Signatures): Each constructor must have a unique
parameter list. This difference can be in:
o Number of parameters: MyClass() vs. MyClass(int val)
o Types of parameters: MyClass(int val) vs. MyClass(double val)
o Order of parameters (if types differ): MyClass(int a, double b) vs.
MyClass(double a, int b)
3. Return Type: Constructors do not have a return type, not even void. This is
implicitly handled by the compiler.
Overloaded constructors essentially have the same name (exact
name of the class) and different by number and type of arguments.
A constructor is called depending upon the number and type of
arguments passed.
While creating the object, arguments must be passed to let compiler
know, which constructor needs to be called.
// C++ program to illustrate
// Constructor overloading
#include <iostream>
using namespace std;
class construct
public:
float area;
// Constructor with no parameters
construct()
area = 0;
// Constructor with two parameters
construct(int a, int b)
area = a * b;
void disp()
cout<< area<< endl;
}
};
int main()
// Constructor Overloading
// with two different constructors
// of class name
construct o;
construct o2( 10, 20);
[Link]();
[Link]();
return 1;
Output:
0
200
Operator Overloading
Operator Overloading
C++ has the ability to provide the operators with a special meaning for
a data type, this ability is known as operator overloading. Operator
overloading is a compile-time polymorphism. For example, we can
overload an operator '+' in a class like String so that we can
concatenate two strings by just using +. Other example classes where
arithmetic operators may be overloaded are Complex Numbers,
Fractional Numbers, Big integers, etc.
Example:
int a;
float b,sum;
sum = a + b;
// C++ Program to Demonstrate the
// working/Logic behind Operator
// Overloading
class A {
statements;
};
int main()
A a1, a2, a3;
a3 = a1 + a2;
return 0;
Operators that can be Overloaded in C++
We can overload
Unary operators
Binary operators
Special operators ( [ ], (), etc)
But, among them, there are some operators that cannot be overloaded.
They are
Scope resolution operator (::)(::)
Member selection operator
Member selection through *
Pointer to a member variable
Conditional operator (?:)(?:)
Sizeof operator sizeof()
Operators that can be overloaded Examples
Binary Arithmetic +, -, *, /, %
Unary Arithmetic +, -, ++, --
Assignment =, +=,*=, /=,-=, %=
Operators that can be overloaded Examples
Bitwise & , | , << , >> , ~ , ^
De-referencing (->)
Dynamic memory allocation,
New, delete
De-allocation
Subscript []
Function call ()
Logical &, | |, !
Relational >, < , = =, <=, >=
overloading an operator as a member function
Overloading an operator as a member function in C++ involves defining a
special member function within a class that redefines the behavior of an
operator for objects of that class. This allows you to use standard C++
operators with your custom data types, making the code more intuitive and
readable. ou're essentially defining how that operator should behave when it's applied to objects
of your class. This allows you to use familiar operators (like +, -, *, ==, etc.) with your custom data
types, making your code more intuitive and readable.
Syntax: The operator function is declared within the class using
the operator keyword followed by the operator symbol.
class MyClass {
public:
ReturnType operatorOp(ArgumentList); // For binary operators
ReturnType operatorOp(); // For unary operators
};
ReturnType: The type of value returned by the overloaded operator.
operatorOp: The operator keyword followed by the operator symbol
(e.g., operator+, operator-, operator++).
ArgumentList: For binary operators, this list contains one argument, representing the
right-hand operand. The left-hand operand is implicitly the this object (the object on
which the member function is called). For unary operators, there are no explicit
arguments as the operation applies to the this object.