0% found this document useful (0 votes)
222 views

Presentation: Void Functionoverloading Presented To: Prof. Mam Asma Basharat. Presented By: Zain Javaid (071-Bscs08)

Function overloading in C++ allows defining multiple functions with the same name but different parameters. The functions must differ in either the number of parameters, data type of parameters, or order of parameters. This allows a single function name to perform similar tasks depending on the arguments passed. The document provides examples of function overloading by changing argument types, number, and order to illustrate how overloading works in C++.

Uploaded by

AdilLiaqat
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views

Presentation: Void Functionoverloading Presented To: Prof. Mam Asma Basharat. Presented By: Zain Javaid (071-Bscs08)

Function overloading in C++ allows defining multiple functions with the same name but different parameters. The functions must differ in either the number of parameters, data type of parameters, or order of parameters. This allows a single function name to perform similar tasks depending on the arguments passed. The document provides examples of function overloading by changing argument types, number, and order to illustrate how overloading works in C++.

Uploaded by

AdilLiaqat
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction

Presentation:
void FunctionOverloading ( );

Presented to:
prof. Mam Asma Basharat.

Presented by:
Zain Javaid (071-bscs08)
Function Overloading
C++ supports writing more than one function with the
different signature.
This could include:
– Same function name
– Same return type of functions
– Different argument list
– Different argument types
– Different order of arguments

The advantage is that the same apparent function can


be called to perform similar but different tasks. The
following will show an example of this.
Signature of function
The first step in understanding function overloading is to understand what constitutes
a function signature.
int foo(int x, double y)
The signature of the above function is:
foo(int, double)
Now, consider the following two functions:
int foo(int x)
double foo(int x)
The above functions are not considered to be different, their signatures are the
same because the return value of a function is not part of its signature, this will
cause an error when compiling the code:
foo(int x)
foo(int x)
Finally, consider these functions:
int bar(int x, double y)
int bar(int z, double q)
The above functions are not considered to be different, changing the names of
the argument variables does not alter the signature:
bar(int, double)
bar(int, double)
Same function name
To perform function overloading the name of
functions must be same
– void sum( int x, int y);
– void add( int x, int y);
The above functions are not considered to
be
overloaded, because in overloading two or
more function with same name are
needed.
Same return type of functions
To perform function overloading the return type of
functions must be same
– int bar(int x)
– double bar(int x)
The above functions are not considered to be different,
their signatures are the same because the return value
of a function is not part of its signature, this will cause an
error when compiling the code:
– bar(int x)
– bar(int x)
Different argument list
To perform function overloading the
argument list must be different
– int bar(int z)
– int bar(int x, int y)
Although the functions above have the
same name, they are treated as different
functions by the compiler because their
signatures are different:
– bar(int)
– bar(int, int)
Different argument types
To perform function overloading the argument
types must be different
– int bar( int x, float y)
– int bar( int x, int y)
Although the functions above have the same
name, they are treated as different functions by
the compiler because their signatures are
different:
– bar(int, float)
– bar(int, int)
Different order of arguments
To perform function overloading the order of argument
must be different
– int bar( int x, float y)
– int bar( float x, int y)
Although the functions above have the same name,
they are treated as different functions by the
compiler because their signatures are different:
– bar(int, float)
– bar(float, int)
C++ - Sample code for function
overloading
void AddAndDisplay(int x, int y)
{
cout<<" C++ Tutorial - Integer result: "<<(x+y);
}
void AddAndDisplay(double x, double y)
{
cout<< " C++ Tutorial - Double result: "<<(x+y);
}
void AddAndDisplay(float x, float y)
{
cout<< " C++ Tutorial - float result: "<<(x+y);
}
C++ - Sample code for function
overloading
Here are example Function overloading
class FuncOver {
public:
// Constructors can also be overloaded.
FuncOver();
// Overloaded constructor.
FuncOver(int i);
// Functions example.
int sum(int a int b);
int sum(float a float b);
};

You might also like