GP OOPS C++ Polymorphism
GP OOPS C++ Polymorphism
Polymorphism-
Polymorphism is considered one of the important features of Object-Oriented
Programming. Polymorphism is a concept that allows you to perform a single action in
different ways. Polymorphism is the combination of two Greek words. The poly means
many, and morphs means forms. So polymorphism means many forms. Let’s
understand polymorphism with a real-life example.
Real-life example: A person at the same time can have different characteristics. Like a
man at the same time is a father, a husband, and an employee. So the same person
possesses different behavior in different situations. This is called polymorphism.
1
a) Function overloading:
When there are multiple functions in a class with the same name but different
parameters, these functions are overloaded. The main advantage of function
overloading is that it increases the program’s readability. Functions can be overloaded
by using different numbers of arguments or by using different types of arguments. We
have already discussed function overloading in detail in the previous module.
b) Operator Overloading:
C++ also provides options to overload operators. For example, we can make the
operator (‘+’) for the string class to concatenate two strings. We know that this is the
addition operator whose task is to add two operands. When placed between integer
operands, a single operator, ‘+,’ adds them and concatenates them when placed
between string operands.
2
Example: Perform the addition of two imaginary or complex numbers.
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator + (Complex const & b) {
Complex a;
a.real = real + b.real;
a.imag = imag + b.imag;
return a;
}
void print() {
cout << real << " + i" << imag << endl;
}
};
int main() {
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
c3.print();
}
Output:
12 + i9
❖ Runtime polymorphism:
Runtime polymorphism is also known as dynamic polymorphism. Method overriding is a
way to implement runtime polymorphism.
3
Method overriding:
Method overriding is a feature that allows you to redefine the parent class method in
the child class based on its requirement. In other words, whatever methods the parent
class has by default are available in the child class. But, sometimes, a child class may not
be satisfied with parent class method implementation. The child class is allowed to
redefine that method based on its requirement. This process is called method
overriding.
Example:
#include<iostream>
using namespace std;
class Parent {
public:
void show() {
cout << "Inside parent class" << endl;
}
};
class subclass1: public Parent {
public: void show() {
cout << "Inside subclass1" << endl;
}
};
class subclass2: public Parent {
public: void show() {
cout << "Inside subclass2";
}
};
int main() {
subclass1 o1;
subclass2 o2;
o1.show();
o2.show();
}
4
Output:
Inside subclass1
Inside subclass2