Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟣 OOP  ·  Lesson 47

Polymorphism in C++

In this lesson: Function overloading, operator overloading and virtual functions — both types of polymorphism in C++ with code. In Hindi & English.

What is polymorphism?

Polymorphism means "many forms." It is the idea that one interface — a function name, an operator — can behave differently depending on the object it acts on. It lets you treat a whole family of related objects the same way, while each responds in its own manner.

Two kinds

  • Compile-time (static) — resolved by the compiler: function overloading and operator overloading.
  • Run-time (dynamic) — resolved while the program runs: virtual functions with inheritance.

Compile-time polymorphism

The compiler picks the right version by the signature. Same name, different parameters:

C++
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
// add(2,3) → int version; add(2.5,1.5) → double version

See function overloading and operator overloading.

Run-time polymorphism

Here the choice happens while the program runs. A base-class function is marked virtual, derived classes override it, and calling through a base pointer picks the right override based on the actual object:

C++
class Shape {
public:
    virtual void draw() { cout << "Shape\n"; }   // virtual
};
class Circle : public Shape {
public:
    void draw() override { cout << "Circle\n"; }
};

A run-time example

C++
#include <iostream>
using namespace std;
class Shape { public: virtual void draw() { cout << "Shape\n"; } };
class Circle : public Shape { public: void draw() override { cout << "Circle\n"; } };
class Square : public Shape { public: void draw() override { cout << "Square\n"; } };

int main() {
    Shape *s;
    Circle c; Square sq;
    s = &c; s->draw();    // Circle
    s = &sq; s->draw();   // Square
    return 0;
}
Output:
Circle
Square

The same call s->draw() runs different code depending on what s points to — that is run-time polymorphism. It relies on virtual functions.

Compile-time vs run-time

Compile-timeRun-time
Also calledStaticDynamic
Achieved byOverloadingVirtual functions
ResolvedAt compile timeWhile running
SpeedFasterSlight overhead

Common mistakes

  • Expecting run-time behaviour without marking the base function virtual.
  • Calling through an object (not a pointer/reference), which does not use dynamic dispatch.
  • Forgetting a virtual destructor in a polymorphic base class.
  • Confusing overloading (compile-time) with overriding (run-time).
🏋️ Practice

Make an Animal base with a virtual speak(), and derive Dog and Cat. Store pointers to different animals in an array and loop over it calling speak() — watch each print its own sound.

Summary

  • Polymorphism means one interface, many forms.
  • Compile-time polymorphism uses overloading, resolved by the compiler.
  • Run-time polymorphism uses virtual functions and inheritance.
  • Run-time dispatch needs a base pointer/reference and virtual.
  • It makes code flexible and easy to extend with new types.

Frequently Asked Questions

What is polymorphism in C++?
Polymorphism means "many forms" — the ability of one interface to behave differently depending on the object using it. In C++ it lets the same function name or operator act in different ways, so code can treat related objects uniformly while each responds in its own way.
What are the two types of polymorphism in C++?
There are compile-time (static) polymorphism and run-time (dynamic) polymorphism. Compile-time is achieved with function overloading and operator overloading, resolved by the compiler; run-time is achieved with virtual functions and inheritance, resolved while the program runs.
What is the difference between compile-time and run-time polymorphism in C++?
Compile-time polymorphism is decided by the compiler based on the function signature, as with overloading, and is fast. Run-time polymorphism is decided while the program runs, using virtual functions and base-class pointers, allowing behaviour to depend on the actual object type at that moment.
How is run-time polymorphism achieved in C++?
Run-time polymorphism is achieved by declaring a base-class function virtual and overriding it in derived classes, then calling it through a base-class pointer or reference. The correct override is chosen at run time based on the object the pointer actually points to.
Why is polymorphism useful in C++?
Polymorphism lets you write flexible, extensible code that works with a whole family of related types through a common interface. You can add new derived classes without changing the code that uses the base type, which keeps programs easier to grow and maintain.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicVirtual Functions Your progress: 47 / 81 (58%) · Lesson 48 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free