Polymorphism allows objects of different types to be treated as objects of a general type. There are two main types: static polymorphism achieved through function overloading and operator overloading at compile time, and dynamic polymorphism achieved through virtual functions and overridden functions at runtime. Virtual functions allow late binding where the function called depends on the object type. Pure virtual functions must be defined in derived classes.
2. Topics:
1. What is Polymorphism?
2. What is Polymorphism in OOP?
3. How Polymorphism can be achieved?
4. Types of Polymorphism
5. Function Overloading
6. Function Overriding
7. Early Binding
8. Late Binding and Virtual function
9. Pure Virtual Function
3. What is Polymorphism?
The process of representing one form in multiple form in known as
polymorphism . Typically, polymorphism occurs when there is a
hierarchy of classes and they are related by inheritance.
It means that a call to a member function will cause a different
function to be executed depending on the type of object that
invokes the function.
4. What is Polymorphism in OOP?
After the inheritance it is another most important feature
of OOP.
In polymorphism, the member functions with same name are
define in base class and also in each derived class.
Polymorphism is used to keep the interface of base class to
its derived classes.
Polymorphism can be achieved by mean of virtual functions.
In polymorphism one pointer to a base class object may also
point any object of its derived class.
5. How Polymorphism can be achieved:
Polymorphism can be achieved in Three ways:
1. Function Overloading
2. Function Overriding
3. Dynamic Binding.
6. Types of Polymorphism:
Fig: Types of polymorphism
Polymorphism
Static Polymorphism :
Compile time/
static/Early Binding
Dynamic Polymorphism:
Runtime/Dynamic/Late
Binding
7. Types of Polymorphism:
Static Polymorphism: It takes place during Compile
time. Function Overloading, Operator Overloading
implements static polymorphism.
Dynamic Polymorphism: It takes place during Run
time. The member function whice change Their
behavior during run time called Virtual Functions.
8. Function Overloading
Here we have multiple definitions for the same function name in the same scope. The
definition of the function must differ from each other by the types and/or the numberof
arguments in the argument list.
9. Function Overriding
Overriding of functions happens when one class inherits another class. Here functions of both
class have the same name and there world be same parameter list and return type of the
functions should be same.
10. Early Binding
Events occurring at compile time are known as early binding. In the process of early binding
all info which is required for a function call is known at compile time. Examples of early
binding: function calls, overloaded function calls, and overloaded operators.
11. Late Binding and Virtual Function:
All the Code is understood at the Time of Execution This is called as Late Binding. As we
Know that Late Binding is Performed By using the virtual Functions. Virtual Means Function
must be Override. Virtual Function is a function in base class, which is overrided in the
derived class, and which tells the compiler to perform Late Binding on this function.
12. Pure Virtual Function
The virtual function that is only declare But Not Define In The InThe
Base Class.
General Syntax:
virtual function_name()=0;
The class that contains the pure virtual function exists only to act as
base or parent classes.