Page 231 - DCAP404 _Object Oriented Programming
P. 231

Object-oriented Programming




                    Notes          accessed in the same manner even though specific actions associated with each operation may
                                   differ. Polymorphism is extensively used in implementing inheritance as shown below.




                                                                       shape
                                                                      Draw()





                                             Object circle           Object box            Object triangle



                                             Draw(circle)            Draw(box)             Draw(tr  iangle)

                                   Polymorphism can be implemented using operator and function overloading, where the same
                                   operator and function works  differently on different arguments producing different results.
                                   These polymorphisms are brought into effect at compile time itself, hence is known as early
                                   binding, static binding, static linking or compile time polymorphism.
                                   However, ambiguity creeps in when the base class and the derived class both have a function
                                   with same name. For instance, let us consider the following code snippet.
                                   Class  aa
                                   {
                                          Int  x;
                                          Public:

                                                 Void  display()  {……}    //display  in  base  class
                                   };
                                   Class  bb  :  public  aa
                                   {
                                          Int  y;
                                          Public:
                                                 Void  display()  {……}    //display  in  derived  class

                                   };
                                   Since, both the functions aa.display() and bb.display() are same but at in different classes, there
                                   is no overloading, and hence early binding does not apply. The appropriate function is chosen
                                   at the run time – run time polymorphism.
                                   C++ supports run-time polymorphism by a mechanism called virtual function. It exhibits late
                                   binding or dynamic linking.
                                   As stated earlier, polymorphism refers to the property by which objects belonging to different
                                   classes are able to respond to the same message, but in different forms. Therefore, an essential
                                   feature of polymorphism is the ability to refer to objects without any regard to their classes. It
                                   implies that a single pointer variable may refer to object of different classes.
                                   However, a base pointer, even if is made to contain the address of the derived class, always
                                   executes the function in the base class. The compiler  ignores the content of the pointer and





          224                               LOVELY PROFESSIONAL UNIVERSITY
   226   227   228   229   230   231   232   233   234   235   236