Thursday, December 6, 2012

Function Overloading - C++



Polymorphism is the ability to more than one function have same name, but different content, for a related class. These function are called “overloaded functions”.
Overloaded function differentiate by the type or numbers of argument and not on its return type.

In the example we have three functions with same name as “area()”, but all three differ on the basis of arguments. Where first one passed with one Integer argument, second function with two Integer and the third one also having one argument but float type. Therefore, even first and third function both have only one argument but the third is having a different data type than the first one.  Function overloading nothing to do with its return type.



Example of Function Overloading. Three function defined with same name but differ only on the basis of argument.


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

          class test
                   {
                             public:
                             void area(int);
                             void area(int,int);
                             void area(float);
                   };

          void test::area(int S)
                   {
                             int ans=S*S;
                             cout<<"\nThe Area od Square "<<ans;
                   }

          void test::area(int L, int B)
                   {
                             int ans=L*B;
                             cout<<"\nThe Area of Rectangle "<<ans;
                   }
          void test:: area(float radius)
                   {
                             float ans=3.14*pow(radius,2);
                             cout<<"\nThe area of Circle is = "<<ans;
                   }
          void main()
                   {
                             clrscr();
                             int a,b;
                             float r;
                             test t;
                             cout<<"Enter side : ";
                             cin>>a;
                             cout<<"Enter Length & Breadth : ";
                             cin>>a>>b;
                             cout<<"enter the radius  "<<endl;
                             cin>>r;
                             t.area(a);
                             t.area(a,b);
                             t.area(r);
                             getch();
          }



Adding two values of combination integer and float.



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

          class fo
                   {
                             public :
                                      void cal(int,int);
                                      void cal(int,float);
                                      void cal(float,float);
                   };
          void fo::cal(int a,int b)
                   {
                                      cout<<"Sum of"<<a<<"and"<<b<<"="<<a+b<<endl;
                   }
          void fo::cal(int a,float b)
                   {
                             cout<<"\n\tsum of"<<a<<"and"<<b<<"="<<a+b<<endl;
                   }
          void fo::cal(float a,float b)
                   {
                             cout<<"\n\tsum of"<<a<<"and"<<b<<"="<<a+b<<endl;
                   }
          void main()
                   {
                             int a,b;
                             float x,y;
                             cout<<"\n\tEnter two integer value: ";
                             cin>>a>>b;
                             cout<<"\n\tnter two float value:";
                             cin>>x>>y;
                             fo f;
                             f.cal(a,b);
                             f.cal(a,x);
                             f.cal(x,y);
                             getch();
                   }


Using function overloading to create substring function one like in java.


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

          class Fun_Ovl
                   {
                             private:
                             char Str[50];
                             public:
                             void input();
                             void disp();
                             Fun_Ovl substr(int);
                             Fun_Ovl substr(int,int);
                   };
          void Fun_Ovl:: input()
                   {
                             cout<<"\n\tEnter a Strinbg : ";
                             cin.getline(Str,50);
                   }

          Fun_Ovl Fun_Ovl::substr(int S)
                   {
                             Fun_Ovl F1;
                             int i,j=0;
                             for(i=0;i<50;i++)
                                      {
                                                F1.Str[i]='\0';
                                      }
                             for(i=S;Str[i]!='\0';i++)
                                      {
                                                F1.Str[j++]=Str[i];
                                      }
                             return F1;
                   }
          Fun_Ovl Fun_Ovl::substr(int S, int E)
                   {
                             Fun_Ovl F1;
                             int i,j=0;
                             for(i=0;i<50;i++)
                                      {
                                                F1.Str[i]='\0';
                                      }
                             for(i=S-1;i<E;i++)
                                      {
                                                F1.Str[j++]=Str[i];
                                      }
                             return F1;
                   }
          void Fun_Ovl:: disp()
                   {
                             cout<<"\n\t"<<Str<<"\n";
                   }

          void main()
                   {
                             clrscr();
                             int s,e;
                             Fun_Ovl f,f1,f2;
                             f.input();
                             cout<<"\n\tEnter Starting Postion of String : ";
                             cin>>s;
                             cout<<"\n\tEnter Enter End Position of the String : ";
                             cin>>e;
                             f1=f.substr(s);
                             cout<<"\n\tOriginal String : ";
                             f.disp();
                             cout<<"\n\tSubstring from position "<<s<<" to End ";
                             f1.disp();
                             f1=f.substr(s,e);
                             cout<<"\n\tSubstring between "<<s<<" to "<<e;
                             f1.disp();
                             getch();
                   }



No comments: