Wednesday 6 November 2013

Polymorphism

The word polymorphism is a combination f two word and morphism . Poly means many and morphism means form.In object-oriented programming, polymorphism is the ability of object different types to respond to function of the same name. The user dos not have to know the exact type of of the object in advance.The behavior of the object can be implemented at run time. It is called late binding or dynamic binding.Polymorphism is implemented b using virtual functions.
                                           Pointer to objects
A ointer can also refer to an object of a class.The member of an object can be accessed through pointer by using he symbol->.The symbol is known as member access operator.

  • Syntax
                             The syntax of referencing an object with pointer is as follows:
                         
                                     ptr->member
                                      ptr                  It is the name of the pointer that reference an object.
                                      ->                   It is the member access operator that is used to access                                                                                    a member of object.
                                     member            It is the name of the class member to be  accessed.



                                                               Array of Pointers to objects
An can store same type of data.An array of pointers can store memory address of the object of same class.It allow the to create a large number of objects in memory using new operator and process them easily using loops.Each element of array will refer in a different object tn the memory.
                                                                Pointers and inheritance
Pointers have very important capability of storing the address of different objects.A pointer can store the of object whose type is same as the type pointer.Suppose there are three classes A,A and C. The class A is he parent class and class B is the child and class C is the child .A pointer ptr of class class A can store the address all of a object of A as well B and C.
           
                                                                  Virtual Functions
Virtual maens exiting in effect but not in reality .A type of function that appears to exist in some part of program but dose not exist really is called  virtual function.Virtual function are used to implement polymorphism.They enable the user to execute completely different function by the same function call.

                                                                   Early Binding
The assignment of type to variable and experssion at compilation time is known as early binding.It is also called static binding.The early binding occurs every thing required to call a function is known as compile time.Early binding enable the compiler to know executly which function will be called when a certain statement is executed.
When a program is compiled,the the compiler check the function calls and decides which which function to be executed.This process takes during compilation process in normal program with function.

                                                                     Late Binding
The assignment of type to variables and expressions at execution time is known as late binding.It is also called dynamic binding.The late binding occurs when some information to call a function  is decided execution time .The compiler dose not known at compile time which function will be executed.It provide more flexibility.
The use of virtual function to implement  polymorphism is an example of late binding.In virtual function, the compiler dose not know at compile time which object is referred  by the pointer.The compiler executes the function depending on the contents of the pointer rather than type of compiler.
                                                         Pure Virtual Function
A type of  virtual function that has no body is known as pure virtual function . A function can be declared as pure virtual function by adding two things.

  • The Keyword virtual at the start function declared.
  • The=0 at the end of function declared.
The pure virtual function are used int he classes which are not use directly.The user can inherit the class and then override the pure virtual function in the child class.
                                                            Abstract Classes
A type of class that contains any pure virtual function is called abstract class. An abstract class cannot u be used directly.It means the no object of an abstract class can be created .However, a child class inherit an abstract class and use it overriding its pure virtual function.
The  abstract classes are used to create a model class.Any user who creates a new class by inheriting a new abstract class has to override the pure virtual functions.The child also become a abstract class if any of the pure virtual function is not overridden in child class.
Syntax
The abstract  of declared pure virtual function ia as follows


                                 Virtual return-type function-name()=0;

                                                   virtual Base Classes
A class that  is niherit by child class using virtual keyword is called virtual base class.The virtual base  classes are necessary  in a situation where the member of base class is duplicated in multilevel inheritance.


                                 

1 comment: