 Polymorphism  means “many forms”.
     Polymorphism means ability to take
      more than one form.
     C++ implements polymorphism
      through
              Overloaded functions
              Overloaded operators
              Virtual functions.

JPC and JWD © 2002 McGraw-Hill, Inc.
A function name having several
different     definitions that   are
differentiable by the number or type
of their arguments,is known as an
OVERLOADED FUNCTION and this
process      is    called  FUNCTION
OVERLOADING.
Functions to be overloaded must
 have
 same name.
 their parameter list must vary in:
            1.in terms of number of
              arguments.
          2.in type of arguments.
Different parameter types
void Function(int a) {}
void Function(char a) {}
void Function(double a)
{}
Different number of parameters
int Function(int a) {}
int Function(int a,char b)
{}
int Function(int a,float b,
long c) {}
1.While creating functions with same
 name if the signatures of subsequent
 functions match the previous
 function’s,then the second is treated
 as REDECLARATION of the first.
 For example:
 void square(int a,float b);
 void square(int x,float y);
2.While creating functions with same
 name if the signatures of the two
 functions match exactly but the return
 types differ,the second declaration is
 treated as an erroneous re-declaration
 of the first and is flagged at COMPILE
 time as an error.
 For example:
    float happy(float f);
    double happy(float x);
Class abc {        void main()
        int x;float y;
                            {
        public:                 clrscr();
    int max(int a,int b)        float d;int c;
{                               abc o1,o2;
       if(a>b)                 c=o1.max(5,6);
       return a;               printf("%d",c);
       else
       return b; }         d=o2.max(10.0001);
     float max(float a)       printf("%f",d);
{                             getch();
       if(a>10)               }
       return a;
       else
       return 10.00;}};
class graphics{
      float a,b;
      public:                      void main(){
float area(float r){            float k;
       float x;                 clrscr();
      x=3.14*r*r;               graphics o1,o2;
       return x;}               k=o1.area(10.0);
float area(float l,float        printf("%ft",k);
b) {
       float y;            k=o2.area(10.01,20.01);
       y=l*b;                   printf("%f",k);
       return(y); } };          getch();}
 Can perform variety of
  functions with same function
  name.
 No need to remember name of
  many functions.
 Functions  should have
  different argument lists.
 Member functions cannot be
  overloaded solely on the basis
  of one being static and the
  other being non static.
 C++ Sumita Arora 11th class.
 C++ Sumita Arora 12th class.
 Object –Oriented Modelling and
  Design with
   UML-James RumBaugh.
 Masters in C and C++ .
 OOP with Balaguruswamy.
Compile time polymorphism

Compile time polymorphism

  • 2.
     Polymorphism means “many forms”.  Polymorphism means ability to take more than one form.  C++ implements polymorphism through Overloaded functions Overloaded operators Virtual functions. JPC and JWD © 2002 McGraw-Hill, Inc.
  • 3.
    A function namehaving several different definitions that are differentiable by the number or type of their arguments,is known as an OVERLOADED FUNCTION and this process is called FUNCTION OVERLOADING.
  • 4.
    Functions to beoverloaded must have  same name.  their parameter list must vary in: 1.in terms of number of arguments. 2.in type of arguments.
  • 5.
    Different parameter types voidFunction(int a) {} void Function(char a) {} void Function(double a) {}
  • 6.
    Different number ofparameters int Function(int a) {} int Function(int a,char b) {} int Function(int a,float b, long c) {}
  • 7.
    1.While creating functionswith same name if the signatures of subsequent functions match the previous function’s,then the second is treated as REDECLARATION of the first. For example: void square(int a,float b); void square(int x,float y);
  • 8.
    2.While creating functionswith same name if the signatures of the two functions match exactly but the return types differ,the second declaration is treated as an erroneous re-declaration of the first and is flagged at COMPILE time as an error. For example: float happy(float f); double happy(float x);
  • 11.
    Class abc { void main() int x;float y; { public: clrscr(); int max(int a,int b) float d;int c; { abc o1,o2; if(a>b) c=o1.max(5,6); return a; printf("%d",c); else return b; } d=o2.max(10.0001); float max(float a) printf("%f",d); { getch(); if(a>10) } return a; else return 10.00;}};
  • 12.
    class graphics{ float a,b; public: void main(){ float area(float r){ float k; float x; clrscr(); x=3.14*r*r; graphics o1,o2; return x;} k=o1.area(10.0); float area(float l,float printf("%ft",k); b) { float y; k=o2.area(10.01,20.01); y=l*b; printf("%f",k); return(y); } }; getch();}
  • 13.
     Can performvariety of functions with same function name.  No need to remember name of many functions.
  • 14.
     Functions should have different argument lists.  Member functions cannot be overloaded solely on the basis of one being static and the other being non static.
  • 15.
     C++ SumitaArora 11th class.  C++ Sumita Arora 12th class.  Object –Oriented Modelling and Design with UML-James RumBaugh.  Masters in C and C++ .  OOP with Balaguruswamy.