Pages

Widgets

FUNCTION OVERLOADING

Posted by sudheer | Posted in | Posted on 1:30 PM



  • Design a C++ program to implement the concept of function overloading.


      C++ program for function overloading. Function overloading means two or more functions can have the same name but either the number of arguments or the data type of arguments has to be different.In this program i am finding volume of a cube, cylinder, rectangle. For this I take three functions with same name (vol) but difference in parameters.

Below is the program....




#include<iostream.h>
#include<conio.h>
class volume
{
public:
void vol(int a);                    //volume of cube=a cubed

void vol(float r=0,int h=0);          //volume of cylinder = pi*r*r*h

void vol(float l=0,float b=0);              //volume of rectangular =l*b
};

void volume::vol(int a)
{
cout<<"\ncube of "<<a<<" is:"<<a*a*a;
}
void volume::vol(float r,int h)
{
float pi=3.14;
cout<<"\nVolume of cylinder is : "<<pi*r*r*h;
}
void volume::vol(float l,float b)
{
cout<<"\nVolume of rectangular is :"<<l*b;
}
/***********************************************************************/
void main()
{
clrscr();
volume v;
float l,b,r;
int ch,a,h,i=0;
cout<<"\nEnter length,breadth,height,radius:\n";
cin>>l>>b>>h>>r;
do
{
cout<<"\n\nSelect 1.cube\n\t2.Volume of a cylinder\n\t3.volume of a rectangle\n\t4.Exit\n";
cout<<"Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter area of a cube : ";
cin>>a;
v.vol(a);
break;
case 2:
v.vol(r,h);
break;
case 3:
v.vol(l,b);
break;
default:
cout<<"\nExit.";
i=6;
}
i++;
}while(i<=4);
getch();
}


Comments (0)

Post a Comment