Thursday, April 30, 2020

Polymorphism


Polymorphism is supported by C++ both at compile time and at run time. Compile time polymorphism is achieved by function overloading and operators. Run time polymorphism is accomplished by using inheritance and virtual function.
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. Therefore compile is able to select the appropriate function for a particular call at the compile time itself. This is called static binding or static linking. Consider a situation where the function name and prototype is same in the both the base and the derived classes. The apparate member function would be selected while the program is running. This is known as run time polymorphism.
C++ support a mechanism known as virtual function to achieve the run time polymorphism. This function is linked with particular class much later after the compilation. This process is term as dynamic binding.
Early and late binding
Early binding refers to whose events occur at compile time. Early binding occurs when all information needed to call a function is known at compile time. Early binding means that an object and the function call are bound during compilation. It is called late or dynamic binding because the appropriate function is selected dynamically at run time. Dynamic binding requires use of pointer to object and is one of the powerful features of C++.
Example of early bindings are overloaded function, call and overloaded operator.
The main advantage of early binding is efficiency because all information necessary to call a function is determine at compile time, this type of function call are very fast.
The opposite of early binding  is late binding. Late binding refers to function calls that are not resolved until run time. virtual function are used to achieve late binding. When access is via a base pointer or reference, the virtual function actually called is determined by the type of object pointer to by a pointer. Because in most cases, this can’t be determined at compile time, the object and function are not linked until run time. This main advantage of late binding is flexibility.
Unlike early binding, late binding allows you to create programs that can respond to event occurring while program execution. Since the function call is not late binding can make for some what slower execution time.

Pointer to object

#include<iostream.h>
#include<conio.h>
class item
{
int code;
float price;
public:
void getdata (int a, float b)
{
code=a;
price=b;
}
void display( )
{
cout<< “code=”<< code<< “\n”;
cout<< “price=”<<price<< “\n”;
}
};
void main( )
{
item x;
item *ptr;
ptr=&x;
ptr->getdata(35,20.5)
ptr->display( );
getch( );
}
 
#include<iostream.h>
#include<conio.h>
class Base
{
public:
int a;
void display ( )
{
cout<< “\n”<<a;
}
};
void main( )
{
Base b;
Base *p;
P=&b;
p->b=10;
p->display( );
getch( );
}

No comments:

Post a Comment