
Download this note as PDF at no cost
If any AD appears on download click please wait for 30sec till it gets completed and then close it, you will be redirected to pdf/ppt notes page.
Polymorphism means “many forms”. In C++, the same name (function/operator) can behave differently depending on context.
Example idea:
add(2,3) and add(2.5, 3.2) → same name, different types+ works for integers and also for strings (conceptually)Polymorphism is important because it provides:
Decided by compiler during compilation.
Decided during execution.
Function overloading: same function name but different parameter list.
Overloading can differ by:
Overloading cannot be done only by changing:
Why useful:
Operator overloading lets us give user-defined meaning to operators for class objects.
Why needed:
a + b for complex numbersImportant: operator overloading does not change precedence/associativity rules; it only changes behavior.
Most operators can be overloaded, but some cannot.
Common non-overloadable operators (remember at least 2 for exam):
:: (scope resolution). (member access)?: (ternary)sizeofYou can overload operators by defining a special function operator<symbol>.
Example idea:
Complex operator+(Complex c)It can be a:
A virtual function is a member function in a base class that we expect to override in derived classes.
When we call a virtual function using a base class pointer/reference, C++ performs dynamic binding and calls the derived version.
Why needed:
Concept:
Base *p;p = new Derived;If Base::show() is virtual and Derived overrides it, then p->show() calls Derived::show().
A pure virtual function is written as:
virtual void f() = 0;A class containing a pure virtual function becomes an abstract class.
. or :: (not allowed).virtual keyword in base (then early binding happens).Base pointer p → points to Derived object
Call p->show()
If show() is virtual → run-time decides → Derived::show() runs
Get instant access to notes, practice questions, and more benefits with our mobile app.
Polymorphism means “many forms”. In C++, the same name (function/operator) can behave differently depending on context.
Example idea:
add(2,3) and add(2.5, 3.2) → same name, different types+ works for integers and also for strings (conceptually)Polymorphism is important because it provides:
Decided by compiler during compilation.
Decided during execution.
Function overloading: same function name but different parameter list.
Overloading can differ by:
Overloading cannot be done only by changing:
Why useful:
Operator overloading lets us give user-defined meaning to operators for class objects.
Why needed:
a + b for complex numbersImportant: operator overloading does not change precedence/associativity rules; it only changes behavior.
Access the complete note and unlock all topic-wise content
It's free and takes just 5 seconds
From this topic
Compile-time polymorphism (static binding) is decided during compilation. Examples:
Compiler selects correct function/operator based on signature.
Run-time polymorphism (dynamic binding) is decided during execution using virtual functions.
Example: Base *p = new Derived; p->show(); calls Derived::show() if show() is virtual and overridden.
This ensures correct method is executed for derived objects.
Polymorphism means many forms—the same name can perform different actions depending on context.
Types:
Thus polymorphism improves flexibility and supports reusable, extensible designs.