January 31, 2013

Polymorphism in C++ (OOPs)


polymorphism sequence and types with description in C++ (OOPs)

Polymorphism is made from two words POLY+MORPHISM which means more the one form. In C++ polymorphism is implemented by overloading a function or an operator. In Function Overloading a single function name is used to perform different tasks. 


Actually different functions with same name are defined. The compiler can distinguish between these functions by the number, type and the parameters passed.


There are mainly two types of Polymorphism:

Compile Time (Static Binding or Early Binding)

As the name says binding of program during compile time. Its mainly observed when we are doing function overloading or operator overloading.

Some of the operators that can't be overloaded
operators that can't be overloaded in C++ (POLYMORPHISM)



/* Program to perform arithmetic function overloading in Compile time */


#include<iostream.h>
#include<conio.h>
class operation
{
      public:
             int a,b,add;
      void sum()
      {
           cout<<"\n Enter any two integer values";
           cin>>a>>b;
           add=a+b;
           cout<<"\n Addition of "<<a<<" and "<<b<<" is "<<add;
           }
      int sum(int c,int d)
      {
          add=c+d;
          cout<<"\n Addition of "<<c<<" and "<<d<<" is "<<add;
          }
      float sum(float *e,float *f)
      {
           float sum=(*e)+(*f);
           cout<<"\n Addition of "<<*e<<" and "<<*f<<" is "<<sum;
            }
};
main()
{
      class operation o;
      o.sum();
      int a,b;
      cout<<"\n\n Enter any two new integer values";
      cin>>a>>b;
      o.sum(a,b);
      float c,d;
      cout<<"\n\n Enter any two real values";
      cin>>c>>d;
      o.sum(&c,&d);
      getch();
      }
output of program to show arithmetic function overloading under compile time, static or early binding in C++ (OOPs)

/* Program to overload increment operator in Compile time*/

#include<iostream.h>
#include<conio.h>
class overload
{
      public:
             int a,b,c,d;
      void enter()
      {
           cout<<"\n Enter any two integer values";
           cin>>a>>b;
           }
      void operator ++()
      {
           c=a++;
           d=b++;
           }
      void display()
      {
         cout<<"\n Increment of "<<c<<" and "<<d<<" is "<<a<<" and "<<b<<" respectively";
           }
};
main()
{
      class overload o1,o2,o3;
      o1.enter();
      
      o1.operator ++();
      o1.display();
      cout<<"\n ";
      o2.enter();
      
      o2.operator ++();
      o2.display();
      cout<<"\n ";
      o3.enter();
     
      o3.operator ++();
      o3.display();
      getch();
      }
output of program to show operator increment overloading under compile, early and static binding in C++ (OOPs)
      

/* Program to overload decrement operator in Compile time*/

#include<iostream.h>
#include<conio.h>
class overload
{
      public:
             int a,b,c,d,e,f;
      void enter()
      {
           cout<<"\n Enter any two intger value";
           cin>>a>>b;
           }
      void operator --()
      {
           c=a--;
           d=b--;
           }
      void display()
      {
           cout<<"\n Decrement of "<<c<<" and "<<d<<" is "<<a<<" and "<<b<<" respectively";
           }
};
main()
{
      class overload o[20];
      int i,n;
      cout<<"\n How many times you want to enter values";
      cin>>n;
      for(i=0;i<n;i++)
      {
                      o[i].enter();
                      o[i].operator --();
                      o[i].display();
                      cout<<"\n ";
                      }
      getch();
      }
output of program to show operator decrement overloading under compile, static and early binding in C++ (OOPs)


Run Time (Dynamic Binding or Late Binding)

As the name says binding of program during its execution stage. It’s mainly observed when virtual functions are taken into existence.


/* Program to show use of dynamic binding using NEW & DELETE*/


#include<iostream.h>
#include<conio.h>
int *w,*h;
class rect
{
      public:
      rect(int a,int b)
      {
                int *w=new int;
                *w=a;
                int *h=new int;
                *h=b;
                int ar=(*w)*(*h);
                cout<<"\n Area of "<<*w<<" and "<<*h<<" is "<<ar;
                }
      ~rect()
      {
            delete w;
            delete h;
            }
};
main()
{
      int a,b;
      cout<<"\n Enter any value of a and b";
      cin>>a>>b;
      class rect r(a,b);
      getch();
      }
output of program to show use of dynamic binding or late binding using new and delete keywords in C++ (OOPs) polymorphism
          


VIRTUAL FUNCTIONS

1. It is a member function.
2. Used to perform special task only employed in run-time polymorphism.
3. Virtual keyword is function signature.
4. Defined in base/parent class.
5. Redefined in derived/child class.
6.     These functions are used to remove ambiguity from the program.


/* Program to show use of virtual function in C++ (OOPs) */


#include<iostream.h>
#include<conio.h>
class vir_func1
{
  public:
  void show()
  {
    cout<<"\n Data is displaying from 1st function under PARENT Class";
    }
  virtual void display()
  {
    cout<<"\n Data is displaying fron 2nd function under PARENT Class";
    }
};
class vir_func2:public vir_func1
{
   public:
   void show()
   {
     cout<<"\n Data is displaying from 1st function under CHILD Class";
     }
   void display()
   {
     cout<<"\n Data is displaying fron 2nd function under CHILD Class";
     }
};
main()
{
      class vir_func1 *p,vf1;
      class vir_func2 vf2;
      p=&vf1;
      p->show();
      p->display();
      cout<<"\n ";
      p=&vf2;
      p->show();
      p->display();
      cout<<"\n\n\n See now you must be clear the what we want to print is only just with the help  of virtual function";
      getch();
      }
output of program to show use of virtual function in dynamic, late and run time binding in C++ (OOPs)



Pure Virtual Function

Its same as that of simple virtual function but varies from it , as it doesn’t have any body.
Syntax:
virtual  return_type function_name()=0;


Abstract CLASS

The class in which pure virtual function exists and does not have any object of that CLASS and obviously no OBJECT can be created if CLASS is empty. 



No comments:

Post a Comment