-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphism.cpp
More file actions
44 lines (33 loc) · 2.03 KB
/
Copy pathPolymorphism.cpp
File metadata and controls
44 lines (33 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*Polymorphism is an ability of a C++ object to take many forms.
The most important use of polymorphism in C++ OOPs occurs
when we want to bind functions to the objects we defined
in C++ during compile-time or run-time.
Polymorphism in C++
“Poly” means several and “morphism” means form.
So we can say that polymorphism is something that has several forms
or we can say it as one name and multiple forms.
There are two types of polymorphism:
Compile-time polymorphism
Run time polymorphism
Compile Time Polymorphism
We need to first compile a program then we can run it
In compile-time polymorphism, it is already known which function will run. Compile-time polymorphism is also called early binding, which means that you are already bound to the function call and you know that this function is going to run. There are two types of compile-time polymorphism:
Function Overloading
This is a feature that lets us create more than one function and the functions have the same names
but their parameters need to be different.
If function overloading is done in the program and
function calls are made the compiler already knows that which functions to execute.
Operator Overloading
This is a feature that lets us define operators working for some specific tasks.
For example, we can overload the operator “+” and define its functionality to add two strings.
Operator overloading is also an example of compile-time polymorphism because the compiler already knows at
the compile time which operator has to perform the task.
Run Time Polymorphism
In the run-time polymorphism, the compiler doesn’t know already what will happen at run time.
Run time polymorphism is also called late binding.
The run time polymorphism is considered slow because function calls are decided at run time.
Run time polymorphism can be achieved from the virtual function.
Virtual Function
A function that is in the parent class but redefined in the child class is called a virtual function.
“virtual” keyword is used to declare a virtual function.
*/