Thursday 15 May 2014

Concepts of OOPs-4 or Characteristics of OOPs

                                       Concepts of OOPs-4
In simple terms, polymorphism means one name and many duties. Polymorphism refers the ability of one thing to take many (Poly) distinct forms (Morphism)
For example, consider the operation of operator (+). Operation is sum if operands are integer type and if operands are strings types then operation is string concatenation.
The process of making an operator to exhibit different behaviors in different instances called as operator overloading.
Overloading refers to the use of the same things for different purposes. Overloading can make programs easier to write and to understand. Overloading is a kind of polymorphism.
C++ also supports function overloading. Function Overloadingrefers to the use of one function name to perform different purposes or tasks.
For example, as we can see in the figure given below which shows a single function name, area (), can be used to find the area of circle, rectangle or triangle depend upon the number of arguments.

Polymorphism is the property by which same message is send to objects of different classes and the objects behave differently, For example, Consider the following two class as given below:
  In this example, we have considered two classes

1.   Class Integer 2. Class String

The same message or function Add () is sent to both the classes. But the result of passing this message to them is different.

Sending Add () message to an object of class integer would give sum of two numbers (i.e. number 1+ number2) whereas, sending Add () message to an object of class string would result in a concatenated string (i.e. name1 + name2)

Static Binding and Dynamic Binding (or Types of Polymorphism)


Binding means connecting the function call to the code to the executed in response to the call. These are of two types.

1. Static Binding or Compile Time Polymorphism

Static Binding means that the code associated with the function call is linked at compile time. Static Binding is also known as early binding or compiles time polymorphism.

For example, When an overloaded function is called, the compiler matches the arguments passed with the formal arguments (or signature) of the various functions with the same name.
Once the match is found. It associates that code of the function with the call. Therefore, there is no confusion and appropriate function is linked at compile time. This is called static binding.
Operator overloading also falls under the same category as function overloading.

2.   Dynamic Binding or Run Time Polymorphism

Dynamic binding means that the code associated with the function call is linked at run time. Dynamic binding is also known as late binding or run time polymorphism.
For example, consider the following figure given below.

In this example, as we can see that there is one base class, namely PERSON and two derived class namely STUDENT and EMPLOYEE. A exactly same function DISPLAY ( ) without any argument is available in base class as well as in both derived class. If, we call this function through the same base class pointer, then compiler will get confused as to which function to refer to for each call. So, compiler does not attach the function to any call and leaves the job to the run-time-system to decide the function to be called, So calling of the appropriate function takes place at run-time and hence it is called dynamic binding.

To implement dynamic binding, we need virtual functions. Virtual functions are special type of member functions which are defined in base class and are redefined in derived classes. When virtual function is called through a pointer to a base class, the derived class function is activated.



No comments:

Post a Comment