Dynamic Polymorphism

 Dynamic Polymorphism

Introduction-

  •  Dynamic (run time) polymorphism is the polymorphism existed at run-time. Here, Java compiler does not understand which method is called at compilation time. Only JVM decides which method is called at run-time. Method overloading and method overriding using instance methods are the examples for dynamic polymorphism.

  •  Dynamic polymorphism, which in C++ is called Overriding, allows us to determine the actual function method to be executed at run-time rather than compile time.
  •  For example, if we are using the uC/OS-II RTOS and have developed a Mutex class, e.g.

                class uCMutex

                {

                public:

                    uCMutex();

                    void lock();

                    void unlock();

                private:

                    OS_EVENT* hSem;

                    INT8U err;

                    // not implemented

                    uCMutex( const uCMutex& copyMe );

                    uCMutex& operator=( const uCMutex& rhs );

                };

Pointer To Object-

similar to variables, objects also have an address. A pointer can pointer can point to a specified object. The following program illustrates this:
 
                                    #include<iostream.h>
                                    #include<conio.h>
                                    class Bill 
                                    {
                                        int qty;
                                        float price;
                                        float amount;
                                        public:
                                        void getdata (int a , float b, float c)
                                        {
                                            qty=a;
                                            price=b;
                                            amount=c;
                                        }
                                        void show()
                                       {
                                       cout<<"Quantity : " <<qty <<"\n";
                                       cout<<"Price:"<<price<<"\n";
                                       cout<<"Amount:"<<amount<<"\n";
                                       }
                                `

                                int main()
                                {
             
                                    clrscr();
                                    Bill s;
                                    Bill*ptr=&s;
                                    Ptr->getdata(45,10.25,45*10.25);
                                    (*ptr).show();
                                    return 0;
                                }                         

  Pointers to derived class-  


Virtual functions-

  •  A virtual function a member function which is declared within base class and is re-defined (Overriden) by derived class.When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.
  •  Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call. They are mainly used to achieve Runtime polymorphism.
  •  Functions are declared with a virtual keyword in base class. The resolving of function call is done at Run-time. 

Pure Virtual functions-

  • A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly.

‘this’ pointer in C++ -

 The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).
                           #include<iostream>
                            using namespace std;

                            /* local variable is same as a member's name */
                            class Test
                           {
                            private:
                                int x;
                            public:
                                void setX (int x)
                                {
                                    // The 'this' pointer is used to retrieve the object's
                           x
                                    // hidden by the local variable 'x'
                                    this->x = x;
                                }
                                void print() { cout << "x = " << x << endl; }
                            };

                            int main()
                            {
                                Test obj;
                                int x = 20;
                                obj.setX(x);
                                obj.print();
                                return 0;
                            }
  •  The pointers pointing to objects are referred to as Object Pointers.
  •  C++ Declaration and Use of Object Pointers Just like other pointers, the object pointers are declared by placing in front of a object pointer's name. It takes the following general form :

 class-name ∗ object-pointer ; 
where class-name is the name of an already defined class and object-pointer is the pointer to an object of this class type. For example, to declare optr as an object pointer of Sample class type, we shall write.
 Sample ∗optr ; 
where Sample is already defined class. When accessing members of a class using an object pointer, the arrow operator (->) is used instead of dot operator.

 The following program illustrates how to access an object given a pointer to it. This C++ program illustrates the use of object pointer-

                 /* C++ Pointers and Objects. Declaration and Use
        * of Pointers. This program demonstrates the
        * use of pointers in C++ */

        #include<iostream.h>
        #include<conio.h>
        class Time
        {

                    short int hh, mm, ss;
                    public:
                        Time()
                        {
                            hh = mm = ss = 0;
                        }
                        void getdata(int i, int j, int k)
                        {
                            hh = i;
                            mm = j;
                            ss = k;
                        }
                        void prndata(void)
                        {
                            cout<<"\nTime is
          "<<hh<<":"<<mm<<":"<<ss<<"\n";
                        }
        };
        void main()
       {
                 clrscr();
                Time T1, *tptr;
                cout<<"Initializing data members using the object, with
        values 12, 22, 11\n";
                T1.getdata(12,22,11);
                cout<<"Printing members using the object ";
                T1.prndata();
                tptr = &T1;
                cout<<"Printing members using the object pointer ";
                tptr->prndata();
                cout<<"\nInitializing data members using the object
        pointer, with values 15, 10, 16\n";
                tptr->getdata(15, 10, 16);
                cout<<"printing members using the object ";
                T1.prndata();
                cout<<"Printing members using the object pointer ";
                tptr->prndata();
                getch();
      }

                    



Post a Comment

Previous Post Next Post