C++ Polymorphism
C++ Polymorphism
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.
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.
Types of Polymorphism
Compile-time Polymorphism
Runtime Polymorphism
Types of 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 data type or 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 that 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.
// CompileTimePolymorphism.cpp
// EXAMPLE 01: C++ program to demonstrate
// function overloading or Compile-time Polymorphism
#include <bits/stdc++.h>
C++ Polymorphism
class example {
public:
// Function with one int parameter
void func(int x) {
cout << "value of x is " << x << endl;
}
// main function
int main() {
example obj1;
Output
value of x is 7
value of x is 9.132
value of x and y is 85, 64
Explanation: In the above example, a single function named function func() acts differently in three
different situations, which is a property of polymorphism.
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.
// OperatorOverloading.cpp
// example 02 : C++ program to demonstrate
// Operator Overloading or Compile-Time Polymorphism
#include <iostream>
using namespace std;
C++ Polymorphism
class Complex {
private:
int real, imag; //private data member
public:
Complex(int r = 0, int i = 0) { // constructor with parameter
real = r; // initialize to zero
imag = i; // initialize to zero
} // end of constructor
void print() {
cout << real << " + i" << imag << endl;
} // end of print ()
}; // end of class Complex
// main function
int main() {
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
} // end of main
Output
12 + i9
Explanation: In the above example, the operator ‘+’ is overloaded. Usually, this operator
is used to add two numbers (integers or floating point numbers), but here the operator is
made to perform the addition of two imaginary or complex numbers.
2. Runtime 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.
C++ Polymorphism
Class Parent {
Public :
void func() {
Statements or body of func;
}
}; //end of class Parent
Int main () {
Child child_obj;
Child_obj.func();
Return 0;
Runtime Polymorphism cannot be achieved by data members in C++. Let’s see an example
where we are accessing the field by reference variable of parent class which refers to the
instance of the derived class.
// FunctionOverriding.cpp
// example 03: C++ program for function overriding with data members
#include <bits/stdc++.h>
using namespace std;
// main function
int main(void) {
Animal d = Dog(); // accessing the field by reference
// variable which refers to derived
cout << d.color;
} // end of class
C++ Polymorphism
Output
Black
We can see that the parent class reference will always refer to the data member of the parent class.
B. Virtual Function
A virtual function is a member function that is declared in the base class using the keyword
virtual and is re-defined (Overridden) in the derived class.
// VirtualFunction.cpp
// example 04 : C++ Program to demonstrate the Virtual Function
#include <iostream>
using namespace std;
public:
virtual void display() { // virtual function
cout << "Called virtual Base Class function"
<< "\n\n";
}
void print() {
cout << "Called example_Base print function"
<< "\n\n";
}
}; //end of Parent Class
public:
void display() {
cout << "Called example_Child Display Function"
<< "\n\n";
} // end of display
void print() {
cout << "Called example_Child print Function"
<< "\n\n";
} //end of print
}; // end of sub class
// main function
int main() {
example_Base *base; // Create a reference of class example_Base
C++ Polymorphism
example_Child child;
base = &child;
base -> example_Base::display();// This will call
// the virtual function
base -> print(); // this will call the non-virtual function
} // end main
Output
Example 2:
// VirtualFunctionOverriding.cpp
// example 05 : C++ program for virtual function overriding
#include <bits/stdc++.h>
using namespace std;
class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}
// main function
int main() {
base* b_ptr;
derived d;
b_ptr = &d;
return 0;
}
Output