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

Function Overloading Function Overloading in C++: Add (Int A, Float B) Add (Float A, Int B) Add (Float A, Int B, Int C)

Function overloading allows multiple functions to have the same name but different parameters. There are two types of function overloading: 1) functions with the same name but different number of arguments and 2) functions with the same name and number of arguments but different argument types. During compilation, the compiler determines which overloaded function to call based on the number and types of arguments passed.

Uploaded by

Iam Dude
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Function Overloading Function Overloading in C++: Add (Int A, Float B) Add (Float A, Int B) Add (Float A, Int B, Int C)

Function overloading allows multiple functions to have the same name but different parameters. There are two types of function overloading: 1) functions with the same name but different number of arguments and 2) functions with the same name and number of arguments but different argument types. During compilation, the compiler determines which overloaded function to call based on the number and types of arguments passed.

Uploaded by

Iam Dude
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Function overloading

Function Overloading in C++


Function refers to a segment of code to perform a specific task.
Overloading refers to multiple thing of same name but different purposes.
Function overloading is a feature in C++ where two or more functions
can have the same name but different parameters.
In C++ programming, two functions can have same name if number and/or
type of arguments passed are different. That is, you specify more than one
function definition with the same function name
Example:
Add(int a, float b) is different from,
Add(float a, int b) is different from,
Add(float a, int b, int c) but, all the same function name i.e. ‘Add‘.
When you call an overloaded function, the compiler determines the most
appropriate definition to use, by comparing the argument types you have used to
call the function with the parameter types specified in the definitions.
The function to be invoked is determined by checking the number and type
of arguments in a function.
When an overloaded function is called the compiler matches the call with
the prototype having the same number and same type of arguments and then
calls that function (transfer control).
The process of selecting the most appropriate overloaded function is called
overload resolution.
Function overloading can be considered as an example of polymorphism
feature in C++, it is a compile-time polymorphism.

3.1 Function Overloading with Different numbers arguments:


In this type of function overloading we define two or more functions with
same names but different number of parameters of the same type.
Example:
// first definition
int add(int x, int y)
{
cout << x+y;
}
// second overloaded definition
int add(int x, int y, int z)
{
cout << x+y+z;
}
Here add() function is said to overloaded, as it has two definition, one which
accepts two arguments and another which accepts three arguments. Which add()
function will be called, depends on the number of arguments.
int main()
{
add(10, 20); // add() with 2 parameters will be called

add(10, 20, 30); // add() with 3 parameters will be called


}
28
Function overloading
3.2 Function Overloading with Different kinds of arguments:
In this type of overloading we define two or more functions with same name
and same number of parameters, but the type of parameter is different.
Example:
// first definition
int sum(int x, int y)
{
cout<< x+y;
}

// second overloaded defintion


double sum(double x, double y)
{
cout << x+y;
}

Here, we have two sum() function, first one gets two integer arguments and
second one gets two double arguments.

int main()
{
sum (10.5,20.5); // sum() with double parameters will be called
sum (10,20); // sum() with integer parameters will be called
}

Different numbers arguments Different kinds of arguments


#include<iostream.h> #include<iostream.h>
int add(int x, int y) int sum(int x, int y)
{ {
cout << x+y; cout<< x+y;
} }

int add(int x, int y, int z) double sum(double x, double y)


{ {
cout << x+y+z; cout << x+y;
} }

int main() int main()


{ {
add(10, 20); sum (10.5,20.5);
add(10, 20, 30); sum (10,20);
} }

29
Function overloading
/*08 Program to Find area of Circle, Triangle and Rectangle
using Function Overloading */
#include<iostream.h>
#include<conio.h>
int area(int,int);
float area(float);
float area(float,float);
int main()
{
int s,l,b;
float r,bs,ht;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
area(r);
area(bs,ht);
area(l,b);
getch();
return 0;
}
int area(int l, int b)
{
cout<<"\nArea of circle = " <<l*b;
}
float area(float r)
{
cout<<"\nArea of triangle = " << 3.14*r*r;
}
float area(float bs,float ht)
{
cout<<"\nArea of rectangle = "<< (bs*ht)/2;
}

Output:

30

You might also like