Q5. Demonstrate Compile-time and Run-time Polymorphism
Question:
Write program to demonstrate use of compile-time and runtime polymorphism.
My Approach
Polymorphism ka matlab hai same function/method ka alag-alag behavior.
Ye do types ka hota hai:
-
Compile-time Polymorphism (Early Binding)
- Achieved by Function Overloading and Operator Overloading.
- Compiler at compile-time decide karta hai kaunsa function call hoga.
-
Run-time Polymorphism (Late Binding)
- Achieved by Function Overriding using virtual functions.
- At run-time decide hota hai kaunsa function execute hoga based on object type.
Maine niche ek program likha hai jo dono ko demonstrate karta hai.
Program Code
File Name: polymorphism_demo.cpp
#include <iostream>
using namespace std;
// ================= Compile-time Polymorphism =================
class Calculator {
public:
// Function Overloading
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
};
// ================= Run-time Polymorphism =================
class Shape {
public:
// Virtual Function
virtual void draw() {
cout << "Drawing a Shape" << endl;
}
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a Circle" << endl;
}
};
class Rectangle : public Shape {
public:
void draw() override {
cout << "Drawing a Rectangle" << endl;
}
};
// ================= Main Function =================
int main() {
cout << "=== Compile-time Polymorphism Demo ===" << endl;
Calculator calc;
cout << "Add (int, int): " << calc.add(5, 10) << endl;
cout << "Add (double, double): " << calc.add(4.5, 3.2) << endl;
cout << "Add (int, int, int): " << calc.add(1, 2, 3) << endl;
cout << "\n=== Run-time Polymorphism Demo ===" << endl;
Shape* shape; // Base class pointer
Circle c;
Rectangle r;
shape = &c;
shape->draw(); // Calls Circle's draw()
shape = &r;
shape->draw(); // Calls Rectangle's draw()
return 0;
}Sample Input / Output
Output:
=== Compile-time Polymorphism Demo ===
Add (int, int): 15
Add (double, double): 7.7
Add (int, int, int): 6
=== Run-time Polymorphism Demo ===
Drawing a Circle
Drawing a RectangleExplanation
-
Compile-time polymorphism:
Calculatorclass meadd()function overload hua hai.- Alag parameter list hone ki wajah se compile-time par decide hota hai kaunsa function call hoga.
-
Run-time polymorphism:
Shapebase class kadraw()method virtual hai.- Circle aur Rectangle apna apna
draw()override karte hain. - Base class pointer ke through call karne par run-time decide hota hai kaunsa function execute hoga.
Real Time Relevance
-
Compile-time polymorphism:
Example – Calculator apps me different types of addition (integers, floats, 2 numbers, 3 numbers). -
Run-time polymorphism:
Example – Graphic software (Paint, Photoshop) meShapeke alag-alag sub-classes (Circle, Rectangle, Triangle) hote hain aur har ek ka apna draw method hota hai.
Conclusion
Is program me dono types of polymorphism clearly demonstrate hote hain:
- Compile-time → Function Overloading
- Run-time → Virtual Function + Function Overriding
Submitted by:
Intesab Ansari
MCA Student, NMU University
Portfolio: intesab.live