C++ Polymorphism
C++ Polymorphism
In this tutorial, we will learn about polymorphism in C++ with the help of
examples.
The + operator in C++ is used to perform two specific functions. When it is used
with numbers (integers and floating-point numbers), it performs addition.
int a = 5;
int b = 6;
int sum = a + b; // sum = 11
And when we use the + operator with strings, it performs string concatenation.
For example,
. Function overloading
. Operator overloading
. Function overriding
. Virtual functions
#include <iostream>
using namespace std;
int main() {
// Call function with 2 int parameters
cout << "Sum 1 = " << sum(5, 6) << endl;
return 0;
}
Run Code
Output
Sum 1 = 11
Sum 2 = 12.1
Sum 3 = 18
We cannot use operator overloading for basic types such as int , double , etc.
And, depending on the operands, different operator functions are executed. For
example,
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
count1.display();
return 0;
}
Run Code
Output
Count: 6
We have used this overloaded operator to directly increment the value variable
of count1 object by 1 .
In C++ inheritance, we can have the same function in the base class as well as its
derived classes.
When we call the function using an object of the derived class, the function of the
derived class is executed instead of the one in the base class.
So, different functions are executed depending on the object calling the
function.
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
return 0;
}
Run Code
Output
Derived Function
Here, we have used a print() function in the Base class and the same function
in the Derived class
When we call print() using the Derived object derived1 , it overrides the
print() function of Base by executing the print() function of the Derived
class.
It's a runtime polymorphism because the function call is not resolved by the
compiler, but it is resolved in the runtime instead.
Using virtual functions in the base class ensures that the function can be
overridden in these cases.
Thus, virtual functions actually fall under function overriding. For example,
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
return 0;
}
Run Code
Output
Derived Function
Here, we have used a virtual function print() in the Base class to ensure that it
is overridden by the function in the Derived class.
Why Polymorphism?
Polymorphism allows us to create consistent code. For example,
Suppose we need to calculate the area of a circle and a square. To do so, we can
create a Shape class and derive two classes Circle and Square from it.
In this case, it makes sense to create a function having the same name
calculateArea() in both the derived classes rather than creating functions with
different names, thus making our code more consistent.