polymorphism definition
The word “polymorphism” means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form in c++.
A real-life example of polymorphism is a person who at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. So the same person exhibits different behavior in different situations. This is called polymorphism. Polymorphism is considered one of the important features of Object-Oriented Programming.
types of polymorphism
- Compile-time Polymorphism.
- Run-time Polymorphism.
1) compile time polymorphism
This type of polymorphism is achieved by function overloading or operator overloading.
(A) Function Overloading
When there are multiple functions with the same name but different parameters, then the functions are said to be overloaded, hence this is known as Function Overloading. Functions can be overloaded by changing the number of arguments or/and changing the type of arguments. In simple terms, it is a feature of object-oriented programming providing many functions to have the same name but distinct parameters when numerous tasks are listed under one function name. There are certain Rules of Function Overloading that should be followed while overloading a function.
Below is the C++ program to show function overloading or compile-time polymorphism:
#include <iostream> using namespace std; class Addition { public: int add(int x,int y) // Function with parameter { return x+y; // this function is performing addition of two Integer value } int add() // Function with same name but without parameter { string a= "hello"; string b="sam"; // in this function concatenation is performed string c= a+b; cout<<c<<endl; } }; int main(void) { Addition obj; // Object is created cout<<obj.add(128, 15)<<endl; //first method is called obj.add(); // second method is called return 0; }
Output
143 hellosam
(B) Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can make use of the addition operator (+) for string class to concatenate two strings. We know that the task of this operator is to add two operands. So a single operator ‘+’, when placed between integer operands, adds them and when placed between string operands, concatenates them.
Below is the C++ program to demonstrate operator overloading :
#include <iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0,int i = 0) { real = r; imag = i; } Complex operator+(Complex const& obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } void print() { cout << real << " + i" << imag << endl; } }; int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 + c2; c3.print(); getch(); }
Output
12 + i9
2) run time polymorphism
This type of polymorphism is achieved by Function Overriding. Late binding and dynamic polymorphism are other names for runtime polymorphism. The function call is resolved at runtime in runtime polymorphism. In contrast, with compile time polymorphism, the compiler determines which function call to bind to the object after deducing it at runtime.
A) Function Overriding
Function Overriding occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
#include <iostream> using namespace std; class Animal { public: void function() { cout<<"Eating..."<<endl; } }; class Man: public Animal { public: void function() { cout<<"Walking ..."<<endl; } }; int main(void) { Animal A =Animal(); A.function(); //parent class object Man m = Man(); m.function(); // child class object return 0; }
Output
Eating ... Walking ...
Also Read This :- encapsulation in c++
virtual function in c++
Virtual functions are Dynamic in nature.
They are defined by inserting the keyword “virtual” inside a base class and are always declared with a base class and overridden in a child class.
A virtual function is called during Runtime.
Below is the C++ program to demonstrate virtual function :
Program using without virtual Function :
#include <iostream> using namespace std; class Add { int x=5, y=20; public: void display() //overridden function { cout << "Value of x is : " << x+y<<endl; } }; class Substract: public Add { int y=10, z=30; public: void display() //overridden function { cout << "Value of y is : " <<y-z<<endl; } }; int main() { Add *m; //base class pointer. it can only access the base class members Substract s; // making object of derived class m = &s; m->display(); // Accessing the function by using base class pointer return 0; }
Output
Value of x is: 25
pure virtual function in c++
When the function has no definition, we call such functions as “Do-nothing function or Pure virtual function”. The declaration of this function happens in the base class with no definition.
Program using Pure virtual Function :
#include <iostream> using namespace std; class Animal { public: virtual void show() = 0; //Pure virtual function declaration. }; class Man: public Animal { public: void show() { cout << "Man is the part of animal husbandry " << endl; } }; int main() { Animal *aptr; //Base class pointer Man m; //derived class object creation. aptr = &m; aptr->show(); return 0; }
Output
Man is the part of animal husbandry
This is a topic which is close to my heart… Many thanks! Where are your contact details though?