⬅ Previous Next ➡

Polymorphism

Polymorphism in C++
  • Polymorphism means “many forms” — same function/operator name behaves differently in different situations.
  • C++ supports:
    • Compile-time polymorphism (early binding): function overloading, operator overloading
    • Runtime polymorphism (late binding): function overriding using virtual functions

1) Compile-time Polymorphism (Early Binding)

  • Decision is made at compile time.
  • Main methods:
    • Function Overloading
    • Operator Overloading
1.1) Function Overloading Example
#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

int main() {
    cout << add(10, 20) << endl;
    cout << add(2.5, 3.5) << endl;
    return 0;
}
1.2) Operator Overloading Example
  • Redefines operator behavior for user-defined types (classes).
#include <iostream>
using namespace std;

class Point {
public:
    int x, y;

    Point(int a = 0, int b = 0) {
        x = a;
        y = b;
    }

    Point operator + (const Point &p) {
        return Point(x + p.x, y + p.y);
    }
};

int main() {
    Point p1(2, 3), p2(4, 1);
    Point p3 = p1 + p2;

    cout << "Point: (" << p3.x << ", " << p3.y << ")" << endl;
    return 0;
}

2) Runtime Polymorphism (Late Binding)

  • Decision is made at runtime using base class pointer/reference.
  • Requires:
    • Inheritance
    • Function overriding
    • virtual function in base class
2.1) Virtual Function Example
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() {
        cout << "Base show()" << endl;
    }
};

class Derived : public Base {
public:
    void show() override {
        cout << "Derived show()" << endl;
    }
};

int main() {
    Base *ptr;
    Derived d;

    ptr = &d;     // base pointer to derived object
    ptr->show();  // calls Derived show() due to virtual

    return 0;
}

3) Pure Virtual Functions

  • A pure virtual function has no body in base class and must be implemented in derived classes.
  • Syntax: virtual void fun() = 0;
#include <iostream>
using namespace std;

class Shape {
public:
    virtual void draw() = 0; // pure virtual
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing Circle" << endl;
    }
};

int main() {
    Shape *s = new Circle();
    s->draw();
    delete s;
    return 0;
}

4) Abstract Classes

  • A class that contains at least one pure virtual function is called an abstract class.
  • We cannot create objects of an abstract class directly.
  • Used to provide a common interface for derived classes.
// Shape is abstract because it has a pure virtual function:
// class Shape {
// public:
//     virtual void draw() = 0;
// };

5) Key Points (Interview Notes)

  • Compile-time polymorphism = overloading, operator overloading.
  • Runtime polymorphism = overriding using virtual.
  • virtual enables late binding using base pointer/reference.
  • Pure virtual makes a class abstract and forces implementation in child class.
  • Use override to avoid mistakes in overriding.
⬅ Previous Next ➡