11

Polymorphism

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Polymorphism
Page 2: Polymorphism

Roll No# 4823

Name: Amir Ali

Page 3: Polymorphism
Page 4: Polymorphism

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.

Page 5: Polymorphism

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

Page 6: Polymorphism

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

Page 7: Polymorphism

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

Page 8: Polymorphism

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;

Page 9: Polymorphism

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.

Page 10: Polymorphism
Page 11: Polymorphism