Introduction to Polymorphism
Definition
Polymorphism literally means “many forms”.
In programming, it refers to the ability of a function, object, or operator to behave differently in different situations.
Key Idea:
One name → multiple behaviors
Helps reuse code, improve flexibility, and model real-world systems
Real-World Analogy:
Person:
One person can talk, walk, write, play football → same person, many forms of behavior
Printer:
print()function can print text, images, PDF, or document → same function, multiple forms
Advantages of Polymorphism
Code Reusability → Same function name works with different objects
Maintainability → Changes in base class automatically reflect in derived classes
Flexibility → Same interface works for different types
Real-world modeling → Matches hierarchical relationships in real systems
Limitations of Polymorphism
Slightly slower performance for run-time polymorphism (virtual functions)
Can lead to confusion if overused without proper design
Requires careful memory management for object pointers in inheritance
Types of Polymorphism
Compile-Time Polymorphism (Static Binding)
Also called early binding
Determined at compile time
Achieved via:
Function Overloading
Operator Overloading
Example (Function Overloading):
Practice Exercises:
Create a
multiply()function with 2, 3, or double parametersCreate a
volume()function for cube, cuboid, and cylinder
Run-Time Polymorphism (Dynamic Binding)
Also called late binding
Determined at runtime
Achieved via:
Virtual Functions
Function Overriding
Key Concept:
Virtual functions allow derived class methods to replace base class methods at runtime
Example (Virtual Function):
Practice Exercises:
Create
Birdclass with virtualfly()methodOverride in
EagleandParrotUse base class pointer to call
fly()
Function Overloading
Definition
Same function name with different parameters in the same scope
Rules:
Different number of parameters
Different type of parameters
Return type alone cannot differentiate overloaded functions
Real-Life Analogy:
print()function prints text, image, or PDF → same name, multiple forms
Advantages:
Easier to read code
Flexible interfaces
Maintains same function names for related operations
Limitations:
Cannot overload by return type only
Too many overloaded functions can confuse programmers
Example:
Practice Exercises:
Overload a
volume()function for cube, cuboid, cylinder, and sphereOverload
display()to printint,double, andstring
Virtual Functions
Definition
Virtual functions are functions in base class declared with
virtualkeywordCan be overridden in derived class
Decision which function to call → runtime
Memory Concept:
Virtual Table (vtable) in C++ stores addresses of virtual functions
Base class pointer uses vtable to call correct derived function
Example:
Practice Exercises:
Create
Employeebase class with virtualcalculateSalary()Derived classes:
Manager,Developer→ overridecalculateSalary()Use base class pointer to call
calculateSalary()
Advantages of Polymorphism
| Feature | Description |
|---|---|
| Code Reusability | Use same function name for different operations |
| Flexibility | Objects behave differently at runtime |
| Real-World Modeling | Mimics human/real-world behavior |
| Maintainability | Changes in base class automatically reflect in derived classes |
Limitations
| Limitation | Reason |
|---|---|
| Slight runtime overhead | Virtual function pointer lookup |
| Complexity | Harder for beginners to trace |
| Ambiguity | Multiple inheritance + same function names |
| Cannot overload only by return type | Compile-time conflict |
Extra Real-Life Examples
Banking System:
Base: Account → virtual
calculateInterest()Derived: SavingsAccount, CurrentAccount → override method
School System:
Base: Person → virtual
displayInfo()Derived: Student, Teacher → override
Transport:
Base: Vehicle → virtual
start()Derived: Car, Bike → override
Extended Exercises for Students
Polymorphism + Overloading Practice:
Class
Calculatorwith overloadedadd(),subtract(),multiply()Use base class pointer for run-time polymorphism
Virtual Functions Exercise:
Base class
Shapewith virtualarea()Derived:
Circle,Rectangle,Triangle→ overridearea()Use base class pointer to call
area()
Mixed Exercise:
Class hierarchy
Employee → Manager/DeveloperFunction overloading:
bonus()for full-time, part-timeVirtual function:
calculateSalary()