"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Sunday, March 14, 2010

Write a program with overloaded prototypes of volume(), a function that returns volumes of different structures . Write 3 versions : one for cube's volume that takes one float side of the cube, one for cylinder's volume that takes float radius and float height of the cylinder, and one rectangular box's volume that takes float length, float breadth and float height of the box.

A function name having several definitions that are differentiable by the number or types of their arguments(signature), is known as function overloading.

#include<iostream.h>
#include<conio.h>


// Volume of a cube

double volume(float s)
{
double vol;
vol = (double) ( s * s * s);
return vol ;}


// volume of a cylinder
double volume(float r, float h)
{
double vol = (double)(3.14 * r * r * h);
return vol;
}

// volume of a rectangular box
double volume( float l, float b, float h)


{
double vol = (double) (l * b * h );
return vol;
}

void main()
{
double volume(float s);
double volume(float r, float h);
double volume ( float l, float b, float h);

/* Volume of a cube with side 3.6 unit */
double v = volume(3.6);
cout<< "\n The Volume of a cube = " << v << "Unit Cube";

/* Volume of a cylinder with radius 2.5 unit, height = 6 unit */
v = volume(2.5, 6);
cout<< "\n The Volume of a cylinder = " << v << "Unit Cube";

/* Volume of a rectangular box of l = 10, b =6 , h =4 units , respectively */
v = volume(10, 6, 4);
cout<< "\n The Volume of a rectangular box = " << v << "Unit Cube";

getch();
}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.