C++ Program : To show the working of Function overloading




#include<iostream.h>
#include<conio.h>
class Rectangle
{
   float l,b,h;
    public:
   void get(float x)
   {
      l=b=h=x;
   }
   void get(float x, float y)
   {
      l=x;
      b=y;
      h=0;
   }
   void get(float x, float y, float z)
   {
      get(x,y);
      h=z;
   }
   float area()
   {
      return(l*b);
   }
   float volume()
   {
      return(l*b*h);
   }
};


int main()
{
   float a,b,c;
   clrscr();
   cout<<"\tSelect Option\n1.Cube\n2.Rectangle(Lenght,breath)\n3.Rectangle(Length,Breath,Height)";
   int op;
   cin>>op;
   Rectangle ob;
   switch(op)
   {
      case 1: cout<<"Enter the Side:";
         cin>>a;
         ob.get(a);
         break;
      case 2: cout<<"Enter the Length and breath:";
         cin>>a>>b;
         ob.get(a,b);
         break;
      case 3: cout<<"Enter the Length, breath and Height:";
         cin>>a>>b>>c;
         ob.get(a,b,c);
         break;
      default:cout<<"Invalid option";
         getch();
         return 0;
   }

   cout<<"\nArea="<<ob.area();
   cout<<"\nVolume="<<ob.volume();
   getch();
   return 0;
}


Class diagram 

(click image to view large)

 

 

Related Posts:

0 comments:

Post a Comment