Polymorphism is a feature of object-oriented programming that allows a single interface and multiple methods. It helps reduce complexity by allowing a single name to be used for multiple forms or implementations. Polymorphism is achieved through function overloading and operator overloading, which allows different implementations of the same function name based on parameters. This allows a single function call to work with different data types.
4. 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.
5. 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
6. • 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 values
add(int a, int b)
{
int c =a+b;
cout<<c<<endl;
}
// adds two floating values
add(float a, float b)
{
float c =a+b;
cout<<c<<endl;
}
The above two functions are called methods
7. • 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
8. 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;
9. Types of 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.
• 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.