techmore.in

C++ Polymorphism

In C++, polymorphism is a fundamental concept that allows objects to be treated as instances of their base class rather than their actual derived class. It enables a single function or operator to operate in different ways depending on the type of object that it is called on. Polymorphism is crucial for achieving runtime flexibility and abstraction in object-oriented programming.

Types of Polymorphism in C++

  1. Compile-Time Polymorphism (Static Polymorphism):

    • Achieved through function overloading and operator overloading.
    • Function Overloading: Multiple functions with the same name but different parameter lists.
    • Operator Overloading: Custom definitions of operators for user-defined types.
  2. Run-Time Polymorphism (Dynamic Polymorphism):

    • Achieved through virtual functions and inheritance.
    • Virtual Functions: Functions in a base class that can be overridden in derived classes to provide specific implementations.

Examples

Compile-Time Polymorphism

Function Overloading:

cpp
#include using namespace std; // Function to display an integer void display(int i) { cout << "Displaying integer: " << i << endl; } // Function to display a double void display(double d) { cout << "Displaying double: " << d << endl; } // Function to display a string void display(string s) { cout << "Displaying string: " << s << endl; } int main() { display(10); // Calls display(int) display(3.14); // Calls display(double) display("Hello"); // Calls display(string) return 0; }

Operator Overloading:

cpp
#include using namespace std; class Complex { private: float real; float imag; public: Complex(float r = 0, float i = 0) : real(r), imag(i) {} // Overload + operator Complex operator + (const Complex& obj) { return Complex(real + obj.real, imag + obj.imag); } void display() { cout << "Real: " << real << " Imaginary: " << imag << endl; } }; int main() { Complex c1(3.5, 2.5), c2(1.5, 4.5); Complex c3 = c1 + c2; // Calls overloaded + operator c3.display(); return 0; }

Run-Time Polymorphism

Virtual Functions:

cpp
#include using namespace std; // Base class class Base { public: virtual void show() { // Virtual function cout << "Base class show function" << endl; } }; // Derived class class Derived : public Base { public: void show() override { // Override the base class function cout << "Derived class show function" << endl; } }; int main() { Base* b; Derived d; b = &d; // Calls Derived's show() function b->show(); return 0; }

Key Points

  1. Virtual Functions:

    • Mark a function in the base class as virtual to allow derived classes to override it.
    • The function to be called is determined at runtime based on the type of object pointed to by the base class pointer.
  2. Pure Virtual Functions:

    • A pure virtual function is declared by assigning 0 in the base class.
    • It makes the base class abstract and forces derived classes to implement the function.
    cpp
    class AbstractBase { public: virtual void pureVirtualFunction() = 0; // Pure virtual function };
  3. Dynamic Binding:

    • The process of determining the method to call at runtime based on the object type.
  4. Inheritance:

    • Necessary for polymorphism. Derived classes can extend or modify the behavior of base classes.

Summary

Polymorphism in C++ provides flexibility and reusability in code by allowing the same interface to be used for different underlying forms (data types). By leveraging compile-time and run-time polymorphism, you can create more generic and maintainable code.