Skip to main content

Polymorphism

Understanding Polymorphism in C++

Polymorphism is a fundamental concept in Object-Oriented Programming (OOP) and C++. It allows objects of different classes related by inheritance to respond differently to the same function call. This tutorial will guide you through an in-depth understanding of this concept.

What is Polymorphism?

In Greek, 'Poly' means 'many' and 'morph' means 'forms'. Thus, Polymorphism refers to the ability of a variable, function or object to take on multiple forms. In C++, it is mainly divided into two types: compile-time polymorphism (Function Overloading and Operator Overloading) and runtime polymorphism (Virtual Functions).

Compile-time Polymorphism

Function Overloading

Function overloading is when multiple functions have the same name but different parameters. The correct function will be used based on the arguments. Here's an example:

#include<iostream>
using namespace std;

void display(int var) {
cout << "Integer number: " << var << endl;
}

void display(double var) {
cout << "Double number: "<< var << endl;
}

void display(char* var) {
cout << "String: " << var << endl;
}

int main() {
display(5);
display(500.263);
display("Function overloading");
return 0;
}

Operator Overloading

Operator overloading allows operators to have user-defined meanings on user-defined types (classes). Here's an example:

#include<iostream>
using namespace std;

class Complex {
private:
float real;
float imag;
public:
Complex(): real(0), imag(0) {}
void input() {
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}

// Overload the + operator
Complex operator + (Complex c2) {
Complex temp;
temp.real = real + c2.real;
temp.imag = imag + c2.imag;
return temp;
}

void output() {
if(imag < 0)
cout << "Output Complex number: "<< real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag << "i";
}
};

int main() {
Complex c1, c2, result;

cout << "Enter first complex number:\n";
c1.input();

cout << "Enter second complex number:\n";
c2.input();

// Call operator function
result = c1 + c2;
result.output();

return 0;
}

Runtime Polymorphism

Runtime polymorphism is achieved by Function Overriding. Function Overriding means having two or more methods with the same name, same signature but in different classes. It is related to dynamic (late) binding.

#include<iostream>
using namespace std;

class base {
public:
virtual void print() {
cout << "print base class" << endl;
}

void show() {
cout << "show base class" << endl;
}
};

class derived:public base {
public:
void print() {
cout << "print derived class" << endl;
}

void show() {
cout << "show derived class" << endl;
}
};

int main() {
base *baseptr;
derived d;
baseptr = &d;

//virtual function, binded at runtime
baseptr->print();

// Non-virtual function, binded at compile time
baseptr->show();

return 0;
}

In the above code, print() is a virtual function in the base class, and is overridden in the derived class. The call to this function is resolved dynamically at runtime.

Conclusion

Understanding polymorphism is crucial for effective object-oriented programming in C++. It allows you to write more flexible and dynamic code. The concepts of function overloading, operator overloading, and function overriding are key to implementing polymorphism in C++. Practice using these concepts to improve your C++ programming skills.