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

Function Overloading

Function overloading in C++ allows multiple functions to have the same name but different parameters, enhancing program readability. It can differentiate functions based on data type, number of parameters, or sequence of parameters. The document provides examples of function overloading to calculate volumes of various shapes, demonstrating its practical application in programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Function Overloading

Function overloading in C++ allows multiple functions to have the same name but different parameters, enhancing program readability. It can differentiate functions based on data type, number of parameters, or sequence of parameters. The document provides examples of function overloading to calculate volumes of various shapes, demonstrating its practical application in programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Function Overloading in C++

Multiple functions having same name but parameters of the functions should be different is known
as Function Overloading.
If we have to perform only one operation and having same name of the functions increases the
readability of the program.
Suppose you have to perform addition of the given numbers but there can be any number of arguments, if
you write the function such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it
may be difficult for you to understand the behavior of the function because its name differs.
Parameters should be different means
1. Data Type of parameter should be different
Eg:
void add(int, int);
void add(double,double);

void add(int a, int b)


{
cout<<"sum ="<<(a+b);
}

void add(double a, double b)


{
cout<<endl<<"sum ="<<(a+b);
}

main()
{
add(10,2);
add(5.3,6.2);
return 0;
}

1
2. Number of parameter should be different
Eg:
void add(int , int);
void add(int , int, int);

#include<iostream>
using namespace std;

void add(int a, int b)


{
cout<<"sum ="<<(a+b);
}

void add(int a, int b,int c)


{
cout<<endl<<"sum ="<<(a+b+c);
}

main()
{

add(10,2);
add(5,6,4);
return 0;
}

3. Sequence of parameter should be different


Eg:
void add(int , double);
void add(double , int);

2
#include<iostream>
using namespace std;

void add(int a, double b)


{
cout<<"sum ="<<(a+b);
}
void add(double a, int b)
{
cout<<endl<<"sum ="<<(a+b);
}

main()
{
add(10,2.5);
add(5.5,6);
return 0;
}

Eg:
void add(int , int); function overloading does not depend onto the return type
int add(int, int); of the method

3
Q: Write a C++ program to find volume of cube, cylinder and rectangular box using
the concept of function overloading.
Volume of cube=s3; volume of cylinder=pi*r2h; and volume of rectangular box=l*b*h
#include<iostream>
using namespace std;
float vol(int,int);
int vol(int);
int vol(int,int,int);
int main()
{
int r,h,a;
int r1;
cout<<"Enter radius and height of a cylinder:";
cin>>r>>h;
cout<<"Enter side of cube:";
cin>>a;
cout<<"Volume of cylinder is "<<vol(r,h);
cout<<"\nVolume of cube is "<<vol(a);

return 0;
}
float vol(int r,int h)
{
return(3.14*r*r*h);
}

int vol(int a)
{
return(a*a*a);
}

4
Q: Write a C++ Program to Find Area of Square, Rectangle and Circle using function
Overloading.

Q: Write a class Volume in C++ program to find volume of cube, cylinder and
rectangular box using the concept of function overloading.
Volume of cube=s3; volume of cylinder=pi*r2h; and volume of rectangular box=l*b*h
#include<iostream>
#include<process.h>
using namespace std;
class Volume
{
public:
float vol(int,int);
int vol(int);
};

float Volume::vol(int r,int h)


{
return(3.14*r*r*h);
}

int Volume::vol(int a)
{
return(a*a*a);
}

int main()
{
Volume v;
int r,h,a;

5
float r1;
int ch;
while(1)
{
cout<<"1]Cube \t 2]Cylinder \t 3]Exit";
cout<<"\nEnter the Choice:";
cin>>ch;
switch(ch)
{

case 1:
cout<<"Enter side of cube:";
cin>>a;
cout<<"\nVolume of cube is "<<v.vol(a); break;
case 2:
cout<<"Enter radius and height of a cylinder:";
cin>>r>>h;
cout<<"Volume of cylinder is "<<v.vol(r,h);
case 3:exit(0);
default: cout<<"Enter the correct choice.";
}
}
return 0;
}

Q: Write a C++ program to compute volume of a sphere, cylinder, cube, and cuboid using

function overloading. (Hint: ,

6
)

#include<iostream>
using namespace std;
#define PI 3.1416

// function prototypes
float volume(float length, float breadth, float height);
float volume(float radius);
float volume(float radius, float height);

int main(){
float cube_l = 40.0, cube_b = 30.0, cube_h = 10.0;
float sphere_r = 2.5;
float cylinder_r = 2.5, cylinder_h = 10.0;
cout<<"Volume of Cube ="<<volume(cube_l, cube_b, cube_h)<<endl;
cout<<"Volume of Sphere ="<<volume(sphere_r)<<endl;
cout<<"Volume of Cylinder ="<<volume(cylinder_r, cylinder_h)<<endl;
return 0;
}
// function defination
float volume(float length, float breadth, float height){
return length * breadth * height;
}
float volume(float radius){
return (4.0/3.0) * PI * radius * radius *radius;
}
float volume(float radius, float height){
return PI * radius *radius * height;
}

Output:

7
Volume of Cube =12000
Volume of Sphere =65.45
Volume of Cylinder =196.35

You might also like