Assignment
19
Write a C++ program to find volume of cube,
cylinder and rectangle using function overloading.
Use formula to calculate
Volume as below:
Volume of Cube = a * a * a
Volume of Cylinder = 3.14 * r * h
Volume of Rectangle = l * w * h
Program Code for run:
#include <iostream>
using namespace std;
void volume(float a)
{
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
cout
<< ” \t \t Volume of Cube \n”;
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
cout
<< ” \t Value a = ” << a <<“\n \t Volume =
” << a * a * a << endl;
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
}
void volume(float r, float h)
{
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
cout
<< ” \t \t Volume of Cylinder \n”;
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
cout
<< ” \t Value r = ” << r << “\t h = ”
<< h ;
cout
<<“\n \t \t Volume = ” << 3.14 * r * h << endl;
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
}
void volume(float l, float w, float h)
{
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
cout
<< ” \t \t Volume of Rectangle \n”;
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
cout
<< ” \t Value l = ” << l <<“\t w = ”
<< w << “\t h = ”
<< h ;
cout
<< ” \n \t \t Volume = ” << l * w * h << endl;
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
}
int main()
{
int
n, a, l1, w1, h1, r, h;
while(1)
{
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
cout
<< ” \t Calculate the Volume of Cube, Cylinder and Rectangle ”
<< endl;
cout
<< ” \t Cube : 1 \t\t Cylinder : 2 \n”;
cout
<< ” \t Rectangle : 3 \t\t Exit : Other \n”;
cout
<< ” \t\t Choose Appropriate option \n”;
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
cout
<< “\t”;
cin
>> n;
cout
<< ” * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
” << endl;
switch(n)
{
case 1: cout << ” Enter
the side of Cube ” << endl << ” \t “;
cin
>> a;
volume(a);
break;
case
2: cout << ” Enter the radius of Cylinder ” << endl
<< ” \t “;
cin
>> r;
cout
<< ” Enter the Height of Cylinder ” << endl <<
” \t “;
cin
>> h;
volume(r,
h);
break;
case
3: cout << ” Enter the length of Rectangle ” << endl
<< ” \t “;
cin
>> l1;
cout
<< ” Enter the width of Rectangle” << endl <<
” \t “;
cin
>> w1;
cout
<< ” Enter the height of Rectangle” << endl <<
” \t “;
cin
>> h1;
volume(l1,
w1, h1);
break;
default:
cout << ” Thank You to Use this Program !”;
exit(0);
}
}
}
Output
of Program:
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Calculate the Volume of Cube, Cylinder and
Rectangle
Cube : 1 Cylinder : 2
Rectangle : 3 Exit : Other
Choose Appropriate option
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
1
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Enter the side of Cube
2
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Volume of Cube
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Value a = 2
Volume = 8
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Calculate the of Volume Cube, Cylinder and
Rectangle
Cube : 1 Cylinder : 2
Rectangle : 3 Exit : Other
Choose Appropriate option
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
2
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Enter the radius of Cylinder
5
Enter the Height of Cylinder
3
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Volume of Cylinder
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Value r = 5
h = 3
Volume = 47.1
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Calculate the Volume of Cube, Cylinder and
Rectangle
Cube : 1 Cylinder : 2
Rectangle : 3 Exit : Other
Choose Appropriate option
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
3
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Enter the length of Rectangle
5
Enter the width of Rectangle
7
Enter the height of Rectangle
8
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Volume of Rectangle
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Value l = 5
w = 7
h = 8
Volume = 280
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Calculate the Volume of Cube, Cylinder and
Rectangle
Cube : 1 Cylinder : 2
Rectangle : 3 Exit : Other
Choose Appropriate option
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
5
* * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * *
Thank You to Use this Program !