WAP to calculate and display the volume of sphere and cylinder
demonstrating the concept of function overloading
#include<iostream>
#include<cmath>
using namespace std;
const double PI=3.141592;
double ShapeVolume(double rad);
double ShapeVolume(double rad, double heig);
int main()
{
char ch;
do
{
cout<<"\nPlease, select what you want to calculate: sphere (S or s),\n"
<<"cylinder (C or c). Quit program (Q or q):";
cin>>ch;
if(ch=='S'||ch=='s')
{
double rad;
cout<<"Please, enter the radius of sphere:";
cin>>rad;
cout<<"The volume of sphere is "
<<ShapeVolume(rad);
}
else if(ch=='C'||ch=='c')
{
double rad, heig;
cout<<"Please, enter the radius of cylinder:";
cin>>rad;
cout<<"Please, enter the heigh of cylinder:";
cin>>heig;
cout<<"The volume of cylinder is "
<<ShapeVolume(rad,heig);
}
}
while(ch!='Q'&&ch!='q');
}
double ShapeVolume(double rad)
{
return ((4.0/3.0)*PI*pow(rad,3));
}
double ShapeVolume(double rad, double heig)
{
return ((PI*pow(rad,2)*heig));
}
Comments
Leave a comment