OOPS - [C++ Runtime Polymorphism]

♠ Posted by Unknown in at 04:04

Runtime Polymorphism


Polymorphism means ‘one name, multiple forms’. The concept of polymorphism is implemented using the overloaded functions and operators. The overloaded member functions are ‘selected’ for invoking by matching arguments, both type and number. This information is known to the compiler at the compile time and, therefore compiler is able to select the appropriate function for a particular call at the compile time itself. This is called early binding or static binding or static linking. Also known as compile time polymorphism, early binding simply means that an object is bound to its function call at compile time.

If the appropriate member function could be selected while the program is running. This is known as run time polymorphism. C++ supports a mechanism known as virtual function to achieve runtime polymorphism.

At runtime, when it is known what class objects are under consideration, the appropriate version of the function is invoked. Since the functions is linked with a particular class much later after the compilation, this process is termed as late binding. It is also known as dynamic binding because the selection of the appropriate function is done automatically at runtime. Refer following figure.

Image: Run Time Polymorphism


Dynamic binding is one of the powerful features of C++. This requires the use of pointers to objects. The object pointer and virtual functions are used to implement dynamic binding.

Example:

#include<iostream.h>
#include<conio.h>
class Account
{
 protected:
            int acc_no;
 public:
            Account(int ac)
            { acc_no = ac; }
            virtual void display(){ }    //Empty Virtual Function
};

class Saving: public Account
{
            int sav_amount;
 public:
            Saving(int ac, int s_am):Account(ac)
            { sav_amount = s_am; }
            void display();
};
void Saving::display()
{
 cout<<"The Saving Account No : " <<acc_no<<endl;
 cout<<"The Saving Account Amount : "<<sav_amount<<endl;
}

class Current: public Account
{
            int cur_amount;
 public:
            Current(int ac, int c_am): Account(ac)
            { cur_amount = c_am; }
            void display();
};

void Current::display()
{
 cout<<"The Current Account No : "<<acc_no<<endl;
 cout<<"The Current Account Amount : "<<cur_amount<<endl;
}

void main()
{
 Saving sav(01, 5000);
 Current cur(02, 10000);
 clrscr();
 Account *acc; //Base Class Pointer

 acc = &sav;
 acc->display();//display() From Saving Class

 acc = &cur;
 acc->display();//display() From Current Class.
 getch();
}

8 comments:

very good explanation thank you :)

Thank you 4 appreciating my blog....

This is really nice blog. Contents over here are so informative. For more on this topic.. visit here.. Polymorphism – Feature of Object Oriented Programming (OOP)

https://javatopinterviewquestions.blogspot.com/2013/11/runtime-polymorphism-or-dynamic-binding.html?showComment=1533112503935#c8925028453920529011

Post a Comment