Introduction to C++ | Polymorphism

C++ Polymorphism

Polymorphism is one of the key features of Object-Oriented Programming (OOP). The term "polymorphism" means "many forms." In C++, polymorphism allows a single interface to represent different underlying forms (data types). It enables objects to behave differently based on their type or class.

Types of Polymorphism

Polymorphism in C++ is mainly divided into two types:

Compile-Time Polymorphism

In compile-time polymorphism, the function to be called is determined at compile time. It is achieved through:

Function Overloading

Function overloading allows multiple functions with the same name but different parameter lists. The compiler determines which function to call based on the arguments.

Code Example: Function Overloading


        #include <iostream>
        using namespace std;
        
        // Function to add two integers
        int add(int a, int b) {
            return a + b;
        }
        
        // Function to add three integers
        int add(int a, int b, int c) {
            return a + b + c;
        }
        
        int main() {
            cout << "Sum of 2 and 3: " << add(2, 3) << endl;
            cout << "Sum of 2, 3, and 4: " << add(2, 3, 4) << endl;
            return 0;
        }
                    

Output:

Sum of 2 and 3: 5
Sum of 2, 3, and 4: 9

Operator Overloading

Operator overloading allows you to redefine the behavior of operators for user-defined types (e.g., classes).

Code Example: Operator Overloading


        #include <iostream>
        using namespace std;
        
        class Complex {
            int real, imag;
        
        public:
            Complex(int r, int i) : real(r), imag(i) {}
        
            Complex operator+(const Complex& obj) {
                return Complex(real + obj.real, imag + obj.imag);
            }
        
            void display() {
                cout << real << " + " << imag << "i" << endl;
            }
        };
        
        int main() {
            Complex c1(2, 3), c2(4, 5);
            Complex c3 = c1 + c2; // Using overloaded + operator
            c3.display();
            return 0;
        }
                    

Output:

6 + 8i

Run-Time Polymorphism

In run-time polymorphism, the function to be called is determined during runtime. It is achieved through inheritance and virtual functions.

Virtual Functions

Virtual functions allow derived classes to override a function in the base class. The function to be executed is determined by the type of object being referred to, not the type of reference or pointer.

Code Example: Virtual Functions


        #include <iostream>
        using namespace std;
        
        class Base {
        public:
            virtual void show() {
                cout << "Base class show function" << endl;
            }
        };
        
        class Derived : public Base {
        public:
            void show() override {
                cout << "Derived class show function" << endl;
            }
        };
        
        int main() {
            Base* basePtr;
            Derived derivedObj;
            basePtr = &derivedObj;
        
            // Call the show function
            basePtr->show();  // Calls Derived's show function due to polymorphism
            return 0;
        }
                    

Output:

Derived class show function

Advantages of Polymorphism:

Pro Tip:

💡 Pro Tip

Polymorphism is a powerful feature of OOP. Use compile-time polymorphism when the behavior of functions or operators is known during compile time. Use run-time polymorphism when behavior needs to be determined dynamically during execution.