Upload
amir-ali
View
186
Download
3
Embed Size (px)
DESCRIPTION
Citation preview
Roll No# 4823
Name: Amir Ali
Polymorphism
• Polymorphism is a feature of OOP with single interface and multiple methods.
• It also means “One name and multiple forms”.
• After the inheritance it is another most important feature of OOP.
• It helps us to reduce complexity using a single interface and multiple methods.
Polymorphism
• It indicates calling one or more functions using same name.
• It is achieved using overloading of functions and overloading of operators.
• Polymorphism is achieved using overloading of functions using:
Multiple methods Single interface
Multiple Methods:• Multiple methods imply two or more
functions with the same name but with different signatures.
• For example, we have two overloaded add functions as shown below:
// adds two integer valuesadd(int a, int b){ int c =a+b; cout<<c<<endl;}
// adds two floating valuesadd(float a, float b){ float c =a+b; cout<<c<<endl;}
The above two functions are called methods
Single Interface:• With respect to previous example,
whether we find sum of two integers or two floating point values it doesn’t matter for us.
• What is required is “to add two values”• So, it is logical to use add as the function
name. We may call two functions as shown below:
add(10,20); //Invoke the first function add(1.5,3.5); //Invoke the second add function
How can we access the member of class?
• The member of a class can be access through pointer to the class.
• General syntax; P -> member class;
• P is pointer to object.• -> is member access operator.• Member class. It is the member of the
object;
Types of Polymorphism
Static Polymorphism• Also called as static
binding, it binds an object to a function call during compilation time.
• Refers to events that occur during compile time.
• Execution is much faster.• Not flexible since functions
are decided during compilation time.
• Achieved using normal function calls, function overloading and operator overloading.
Dynamic Polymorphism• Also called as dynamic
binding, it binds an object to a function call during run time.
• Refers to events that occur during run time.
• Execution is slower.• Very flexible since
executable functions are decided during run time.
• Achieved using inheritance and virtual functions.