The following code demonstrates function overloading to calculate volumes of cubes, cylinders and rectangular boxes (cuboids).
#include
#include
using namespace std; // required for console input/output.
// User-input control function (avoids garbage input).
unsigned int GetNum( char * prompt )
{
int iResult = 0;
cout << prompt << ": ";
while( !( cin >> iResult ) iResult < 0 )
{
cin.clear();
cin.ignore( BUFSIZ, '\n' ); // If BUFSIZ not defined, use literal constant 512 instead.
cout << "Please enter numeric characters only." << endl << endl;
cout << prompt;
}
return( iResult );
}
// Forward declarations of overloaded functions.
void volume(unsigned int);
void volume(unsigned int,unsigned int);
void volume(unsigned int,unsigned int,unsigned int);
void volume(unsigned int s)
{
unsigned int cube = s*s*s;
cout << "Volume of cube is " << cube << endl << endl;
}
void volume( unsigned int r, unsigned int h)
{
// For the greatest accuracy across all platforms,
// PI is defined as 4 * arctan(1).
float cylinder = (4*atan((float)1)) * r*r*h;
cout<<"Volume of cylinder is " << cylinder << endl << endl;
}
void volume(unsigned int l, unsigned int b, unsigned int h)
{
unsigned int cuboid= l*b*h;
cout << "Volume of cuboid is " << cuboid << endl << endl;
}
int main()
{
unsigned int s;
s = GetNum( "Enter side of cube" );
volume( s );
unsigned int r, h;
r = GetNum( "Enter radius of cylinder" );
h = GetNum( "Enter height of cylinder" );
volume( r, h );
unsigned int l, b;
l = GetNum( "Enter length of cuboid" );
b = GetNum( "Enter breadth of cuboid" );
h = GetNum( "Enter height of cuboid" );
volume( l, b, h );
return 0;
}
OUTPUT:
Enter side of cube: 4
Volume of cube is 64
Enter radius of cylinder: 2
Enter height of cylinder: 3
Volume of cylinder is 37.6991
Enter length of cuboid: 9
Enter breadth of cuboid: 8
Enter height of cuboid: 6
Volume of cuboid is 432
Press any key to continue . . .
[EDIT] Reasons for changes to original answer:
#include<iostream.h>
#include<conio.h>
float volume(float);
float volume(float,float);
float volume(float,float,float);
int main()
{
clrscr();
float loc,roc,hoc,le,br,ht;
cout<<"Enter the length of the cube\n\t";
cin>>loc;
cout<<"Enter the radius and the height of the cylinder\n";
cin>>roc>>hoc;
cout<<"Enter the length ,breadth and height of the rectangular box\n\t";
cin>>le>>br>>ht;
cout<<"\n\n The volume of cube is="<<volume(loc);
cout<<"\n The volume of cylinder is="<<volume(roc,hoc);
cout<<"\n The volume of Rectangle is="<<volume(le,br,ht);
getch();
return 0;
}
float volume(float s)
{
return(s*s*s);
}
float volume(float r,float h)
{
return(3.14*r*r*h);
}
float volume(float m,float n,float o)
{
return(m*n*o);
}
There is no such thing as function overloading in C; that is a feature of C++. Function overloading allows us to provide two or more implementations of the same function. Typically, we use function overloading so that the same function can cater for different types. For instance, we might provide one implementation that is optimised to handle an integer argument while another is optimised to handle a real argument. We can also use function overloading to provide a common implementation of a function which can then be invoked by overloads that handle the low-level type conversions.
Function overloading is multiple definition with different signatures(the parameters should be different) for the same function. The parameter list have to be different in each definition. The compiler will not accept if the return type alone is changed. Operator overloading is defining a function for a particular operator. The operator loading function can not be overloaded through function overloading.
The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.
A template function is used when you want to write some kind of function that can be applied to different data types. It is a form of overloading, but you don't have to actually write all of the overloaded variants.
It's a way by which you use define the same function for different input types. For example, think about the the operator "+" which in java works for adding integers, floating point numbers and even string concatenation. The way such functionality is achieved is by overloading.
Defining several functions with the same name with unique list of parameters is called as function overloading.
No. Operator and/or function overloading is only a C++ thing.
There is no such thing as function overloading in C; that is a feature of C++. Function overloading allows us to provide two or more implementations of the same function. Typically, we use function overloading so that the same function can cater for different types. For instance, we might provide one implementation that is optimised to handle an integer argument while another is optimised to handle a real argument. We can also use function overloading to provide a common implementation of a function which can then be invoked by overloads that handle the low-level type conversions.
Function overloading is used when you want to re-use the same function name with different argument types or a different number of arguments. Calculating the area of a circle isn't the sort of function that requires overloading since the only argument you need is the radius. double area_of_circle (const double radius) { const double pi=4*atan(1); return pi*radius*radius; }
Function overloading is multiple definition with different signatures(the parameters should be different) for the same function. The parameter list have to be different in each definition. The compiler will not accept if the return type alone is changed. Operator overloading is defining a function for a particular operator. The operator loading function can not be overloaded through function overloading.
give an example of calculation of mathematics
method overloading occurs when we use two functions or more with the same name.In function overloading compiler detect which method is call actually throw the parameters passed to the methods.In function overloading parameters of functions are different.
one function but multiple behaviours depending on the parameters
false
The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.
The function of a graduated cylinder in the laboratory is to measure volumes.
Rectangular It converts co-ordinates from a polar form to rectangular (Cartesian) form. It is the opposite of the Pol (or Polar) function.