Â
C++ Polymorphism :Â Â
Polymorphism is one of the most important OOP concept. Polymorphism means ability to take more than one form.Â
Polymorphism is extensively used in inheritance.Â
For example :Â
A Lady whose name is Mansi she is a teacher in a school and that same lady when she is at her home she's mother of someone sister of someone and wife of someone.Â
So in the above example we can say that Mansi is the same lady everywhere but having different forms in different places and also plays different roles in different places.Â
This is the best example of Polymorphism which can be depicted from real life for better understanding.Â
Polymorphism also plays an important role in allowing objects having different internal structure to share the same external interface.Â
The above statement in simple technical term means that say example :Â
we create function inside a parent class and then we create another function inside an inherited class with same name and properties but the purpose of the function will be different in different classes like it will print different things or will perform different operations etc and in this way the internal structure will be different but the external structure will be same.
We can see Polymorphism in C++ in these type of programs like :Â
1) Function OverloadingÂ
2) Operator Overloading
3) Function Overriding
4) InheritanceÂ
5) Virtual Function
Polymorphism can also be classified into two types i.e.
a) Compile time Polymorphism : In this type of polymorphism the function that is to be executed is invoked during compilation of the program, in simple terms the output of the function which is to be displayed on the screen is decided during the compile time of the program.Â
Compile time polymorphism can be seen in function overloading, operator overloading and Inheritance etc.Â
b) Run time Polymorphism :Â In this type of polymorphism the function that is to be executed is invoked while running the program, in simple terms the output of the function which is to be displayed on the screen is decided during the run time of the program.Â
Run time polymorphism can be seen in function overriding, virtual functions, pointers etc.Â
Compile time polymorphism program in C++Â
#include <iostream>
using namespace std;
class Overload
{
public:
void func(int x)
{
cout << "value of x is " << x << endl;
}
void func(double x)
{
cout << "value of x is " << x << endl;
}
void func(int x, int y)
{
cout << "value of x and y is " << x << ", " << y << endl;
}
};
int main()
{
Overload obj1;
obj1.func(7);
obj1.func(3.142);
obj1.func(69, 420);
return 0;
}
In the above prog we can see that the function "func"Â has been used in different classes for different purposes with different output and also the parameters here are different and hence it comes under Compile time Polymorphism.
Run time polymorphism program in C++Â
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n" ;
}
};
// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee \n" ;
}
};
// Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n" ;
}
};
int main()
{
Animal A;
Pig P;
Dog D;
A.animalSound();
P.animalSound();
D.animalSound();
return 0;
}
OUTPUT:-
In the above prog we can see that the function "animalsound"Â had the same external interface but was used in different classes for different purposes with different output.
                     #ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP