answersLogoWhite

0


Best Answer

The following code demonstrates function overloading to calculate volumes of cubes, cylinders and rectangular boxes (cuboids).

#include // required for input/output.

#include // required for atan() function.

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:

  1. Code does not compile.
  2. Code strays from the question by including a non-essential class.
  3. Code is not platform-independent.
  4. Code does not cater for invalid input.
User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#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);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program to calculate the volume of cube cylinder and rectangular box using the concept of function overloading?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is function overloading in c language of computer?

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.


How operator overloading differ from function overloading?

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.


What are the similarities between constructor overloading and 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.


When to create a template function?

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.


Rules for function overloading?

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.

Related questions

define function overloading?

Defining several functions with the same name with unique list of parameters is called as function overloading.


Is it possible to do operator overloading in c?

No. Operator and/or function overloading is only a C++ thing.


What is function overloading in c language of computer?

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.


What is the c plus plus program to calculate the area of a circle using function overloading?

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; }


How operator overloading differ from function overloading?

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.


Write a program to calculate the area of a cylinder using a function?

give an example of calculation of mathematics


How the compiler compiles overloaded method?

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.


What is an operator overloading in C?

one function but multiple behaviours depending on the parameters


A rectangular equation that is a function will be a function in any coordinate system?

false


What are the similarities between constructor overloading and 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.


What is the function of the graduated cylinder in the laboratory?

The function of a graduated cylinder in the laboratory is to measure volumes.


What does REC function stand for in a scientific calculator?

Rectangular It converts co-ordinates from a polar form to rectangular (Cartesian) form. It is the opposite of the Pol (or Polar) function.