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();
                   }



Thursday, November 29, 2012

Inline Function, Macros and Recursion.



Inline function is the optimization  technique used by the compilers.  Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration. While invoking an inline method does not generate a function call, but rather expands the body at the point of call. Use inline definitions only for methods that are short.  Use of too many inline function may cause thrashing in the memory. It also save overhead of variables on the stack.

The inline functions are similar to macros but inline functions are parsed by the compiler, whereas macros are expanded by the preprocessor.




Example of Macros : Largest of two no.




#include <iostream.h>

#define max(a,b) (a>b?a:b)  //Macro defined
  void main()
      {
           int x,y;
           cout<<"\n\tEnter two no ";
           cin>>x>>y;
           cout<<"\n\tLarges of - "<<x<<" and " <<y<<" is "<<max(x,y);
      }





Example of Inline Fuction : Largest of two no.




#include <iostream.h>

  inline int max(int x,int y)
      {
           return x>y?x:y;
      }
  void main()
      {
           int x,y;
           cout<<"\n\tEnter two no ";
           cin>>x>>y;
           cout<<"\n\tLarges of - "<<x<<" and " <<y<<" is "<<max(x,y);
      }



When function called itself to perform an finite iteration its called recursion. The Recursive functions call themselves to work towards a solution to a problem. While a recursive function perform, it is keep creating a new stack frame on top of the current, a stack is a special area of memory.  Whenever a method is called, an item is placed on the stack for each local variable that we passing to the method.

Tail recursive is method has the recursive call as the last statement in the method and the recursive methods that are not tail recursive are known as non-tail recursive. Tail Recursive functions are very easy to convert to an iterative one. Recursive functions are occupy more space and slow than normal iterative process but sometime when we need multiple loop use of recursion is very handy.



Example of Non-tail Recursive function : Bubble sort to call two recursive function for two loops.



//Bubble Sort

#include <iostream.h>

class rec_sort
{
 private: int Arr[100],n;
 public:
  rec_sort()
      {
           n = 0;
      }
   void accept()
      {
           int i;
           cout<<"\n\tEnter the value of n:";
           cin>>n;
           cout<<"\n\tEnter "<<n<<" Values \n";
           for(i=0;i<n;i++)
              {
                   cin>>Arr[i];
              }
      }
   void disp()
      {
           int i;
           for(i=0;i<n;i++)
              {
                   cout<<Arr[i]<<"  ";
              }
      }
  int sort(int i)
      {
           if(i>=n)
              {
                   return 0;
              }
           else
              {
                   chks(i,i+1);
                   sort(i+1);
              }
      }

  int chks(int i,int j)
      {
           int tmp;
              if(j>=n)
                   {
                        return 0;
                   }
              else
                {
                        if(Arr[i]>Arr[j])
                            {
                                tmp=Arr[i];
                                Arr[i]=Arr[j];
                                Arr[j]=tmp;
                            }
                        chks(i,j+1);
                }
      }
 };

  void main()
      {
          rec_sort RS;
          int tmp;
          RS.accept();
          cout<<"\nOriginal No : ";
          RS.disp();
          cout<<"\nSorted No   : ";
          tmp=RS.sort(0);
          RS.disp();
      }





Example of Tail Recursion – Sum of ‘N’ natural number.




#include <iostream.h>

//Sum of first N natural No.
  int sum(int n)
      {
           if(n<=1)
              {
                   return n;
              }
           else
              {
                   return n+sum(n-1);
              }
      }
  void main()
      {
           int s=0,n;
           cout<<"\n\tEnter the value of n  ";
           cin>>n;
           s=sum(n);
           cout<<"\n\tSum = "<<s;
      }




Example of Tail Recursion – GCD of two No.




#include <iostream.h>
  //GCD of two No.

  int gcd(int,int);
  inline int max(int x,int y)
      {
           return x>y?x:y;
      }

  inline int min(int x,int y)
      {
           return x<y?x:y;
      }

  void main()
      {
           int m,n,hcf;
           cout<<"\n\tEnter two No. ";
           cin>>m>>n;

           hcf=gcd(max(m,n),min(m,n));
           cout<<"\n\tGCD of "<<m<<" and "<<n<<" = "<<hcf;
      }
  int gcd(int m,int n)
      {

           if(n<=0)
              {
                   return m;
              }
           else
              {
                   return gcd(n,m%n);
              }
      }