Sunday, 8 April 2018

Polymorphism

Polymorphism is one of the crucial features of oop. It is simply means 'one name , multiple forms'. Any operation may exhibit different behaviours in different instances.

Definition:

The ability to take more than one form is known as polymorphism. An operation may exhibit different behaviors in different instances.

Ex: Consider the operation of addition , for two numbers , the operation will generate a sum. If two operands are strings then the operation would produce a third string by concatenation.

Types of polymorphism:
 1. Compiler time polymorphism or Early Binding or Static BInding
 2. Run time polymorphism or Late Binding or dynamic Binding

Compile Time Polymorphism
 1. Selecting a function in normal way during compilation time is known as compile time polymorphism or static binding.

 2. During compilation time , the cpp compiler determines which function is used , based on parameters passed to the function.

 3. Static binding is achieved by using function overloading and operator overloading.

 4. Overloading refers to the same thing for different purpose , in function overloading. We can use the same function name to perform variety of task.

#include<iostream.h>
#include<conio.h>
double volume (double l, double b, double h )
{
return(l*b*h);
}
double volume(double r, double h)
{
return(3.14*r*r*h);
}
void main( )
{
cout<<"\n\tVolume of the box is→"<<volume(1.2,2.2,3.4);
cout<<"\n\tVolume of the cylinder is→"<<volume(1.2,4.2);
getch( );
}

Output:-
Volume of the box is→8.976
Volume of the cylinder is→18.99072

Run Time Polymorphism

 . In some situation, where the function name and the prototype is the same in both the base and derived classes, since the prototype of function is the same in both the places, the function is not overloaded and therefore static binding does not apply.

2. In such cases , the appropriate member function is selected while program is running and this is known as run time polymorphism.

3. Virtual function is used to achieve run time polymorphism.

4. At run time , when it known what class objects are  under consideration the appropriate function is done dynamically at run time.

5. Dynamic binding requires the use of pointers to objects.

0 Comments:

Post a Comment

Popular Posts

Categories

AI (32) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (888) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses