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 (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (113) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (726) Python Coding Challenge (170) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

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