0% found this document useful (0 votes)
22 views9 pages

Function Overloading

Function overloading allows multiple functions with the same name but different parameter types or counts within the same scope, enhancing program efficiency and implementing polymorphism. Examples of overloaded functions include different definitions for calculating area and volume based on varying parameters. The document also provides sample code demonstrating function overloading for calculating areas and volumes of various shapes.

Uploaded by

pranavamtvm2021
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)
22 views9 pages

Function Overloading

Function overloading allows multiple functions with the same name but different parameter types or counts within the same scope, enhancing program efficiency and implementing polymorphism. Examples of overloaded functions include different definitions for calculating area and volume based on varying parameters. The document also provides sample code demonstrating function overloading for calculating areas and volumes of various shapes.

Uploaded by

pranavamtvm2021
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

Function overloading

A function name having several definitions in the same scope that are
differentiable by their number and type of arguments is said to be an
overloaded function.
The process of creating an overloaded function is called function
overloading.
It not only implies polymorphism but also reduces the number of
comparisons in a program and thereby makes a program run fast.
The term polymorphism is a combination of Greek words 'poly ‘ and
‘morphos' where the term poly means many and ‘morphos’ means
forms when combined together means many forms.

Definition
The logical method of defining various functions by the same name which
are differentiated by their data type or number of arguments is known
as function overloading.
Eg:
public void divide(int a ,int b)
public void divide(double a, doublet b)

These two functions have the same name but different parameter
[Link] you call divide() with int parameters,the divide()
function that expects int parameters will be [Link] you call
divide() with double parameters,the divide() method that expects
double parameters will be [Link] is referred to as function
overloading and the function divide() is said to be overloaded.
Polymorphism
It is a feature of oops(object oriented programming language).It means that
the same function or operation may behave differently in different
[Link] overloading implements polymorphism.
Which of the following function definitions can be overloaded
with the method given below?
I) public void area(int a,int b)
1. public void area(int c)
2. public void area(int p,int q,int r)
3. public void area(double k)
4. public void area(int x,int y)
● Ans:option 1,2,3 can be overloaded and option 4 cannot be
because it has got the same no: and type of arguments
II) public int sum(int p,int q)
1. public int sum(float a,float b)
2. public float sum(int m,float n)
3. public double sum(int d,int e)
4. public void sum(int k)
Ans:options 1,2,4 can be overloaded .Option 3 can’t be
overloaded because it has got the same no: and type of
arguments

[Link] a program to find the area of a rectangle,square and a


circle using function overloading.
[Link] void area(int l,int b)
[Link] void area(int s)
[Link] void area(float r)
public class qwerty
{
public void area(int l,int b)
{
int a1=l*b;
[Link]("Area of rectangle="+a1);
}
public void area(int s)
{
int a2=s*s;
[Link]("Area of square="+a2);
}
public void area(float r)
{
double a3=3.14*r*r;
[Link]("Area of circle="+a3);
}
}
2. Write a pgm to find the volume of cylinder,cone,cuboid and
hemisphere using function overloading.

public class funoverld3


{
public void volume(int r,int h)
{
double v1=3.14*r*r*h;
[Link]("volume of cylinder="+v1);
}
public void volume(int r1,float h1)
{
double v2=1.0/3.0*3.14*r1*r1*h1;
[Link]("volume of cone="+v2)
}
public void volume(int l,int b,int h2)
{
double v3=l*b*h2;
[Link]("volume of cuboid="+v3);
}
public void volume(int r2)
{
double v4=2.0/3.0*3.14*r2*r2*r2;
[Link]("volume of hemisphere="+v4);
}}
3. Design a class to overload a function volume()as follow:
[Link] Volume(double R)with a radius (R) as an argument
returns the volume of a sphere using formula
V1=4/3*22/7*R*R*R
[Link] Volume(double H,double R)with a height(H),radius(R)
as an argument returns the volume of a cylinder using
formula.V2=22/7*R*R*H
[Link] Volume(double H,double B,double L)with a
height(H),radius(R) as an argument returns the volume of a
cuboid using formula.V3=L*B*H */
​ public class funoverload1
{
public double Volume(double R)
{
double V1=4.0/3.0*[Link]*R*R*R;
return V1;
}
public double Volume(double R,double H)
{
double V2=[Link]*R*R*H;
return V2;
}
public double Volume(double L,double B,double H)
{
double V3=L*B*H;
return V3;
}
}
4. Design a class to overload a function area( ) as follows:​
(i) double area (double a, double b, double c) with three double
arguments, returns the area of a scalene triangle

(ii) double area (int a, int b, int height) with three integer arguments,
1
returns the area of a trapezium( 2 height x(a+b))​

(iii) double area (double diagonal1, double diagonal2) with two double
arguments, returns the area of a rhombus using the formula :​
1
area = ( 2 diagonal1 x diagonal2)

public class Overload

double area(double a, double b, double c)

double s = (a + b + c) / 2;

double x = s * (s-a) * (s-b) * (s-c);

double result = [Link](x);

return result;
}

double area (int a, int b, int height)

double result = (1.0 / 2.0) * height * (a + b);

return result;

double area (double diagonal1, double diagonal2)

double result = 1.0 / 2.0 * diagonal1 * diagonal2;

return result;



You might also like