unit 4 opps
1️⃣ Polymorphism – Introduction
🔹 Definition
Polymorphism means “many forms”.
In OOP, polymorphism allows:
One function or object to behave differently in different situations.
🔹 Real Life Example
-
A person behaves differently as:
-
Father at home
-
Employee at office
-
Friend in society
-
Same person → different behavior
👉 This is polymorphism.
2️⃣ Types of Polymorphism in C++
Polymorphism is divided into:
1️⃣ Compile-Time (Static) Polymorphism
2️⃣ Run-Time (Dynamic) Polymorphism
3️⃣ Compile-Time (Static) Polymorphism
It is resolved at compile time.
Achieved by:
-
Function Overloading
-
Operator Overloading
🔹 Method (Function) Overloading
Definition
Same function name but:
-
Different number of parameters
OR -
Different type of parameters
🔹 Example in C++
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
};
int main() {
Calculator obj;
cout << obj.add(5, 3) << endl;
cout << obj.add(2.5, 3.5);
}
Here:
Same function name add()
Different parameters
👉 Compile-time polymorphism
🔹 Why Called Static?
Because:
Compiler decides which function to call before program runs.
4️⃣ Run-Time (Dynamic) Polymorphism
Resolved at runtime.
Achieved by:
-
Method Overriding
-
Virtual Functions
5️⃣ Method Overriding
Definition
When derived class provides its own version of base class function.
Condition:
-
Same function name
-
Same parameters
-
Use
virtualkeyword in base class
🔹 Example in C++
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal makes sound\n";
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks\n";
}
};
int main() {
Animal* ptr;
Dog d;
ptr = &d;
ptr->sound(); // Calls Dog's version
}
Output:
Dog barks
Here:
-
Function call decided at runtime.
-
Base class pointer calls derived class function.
👉 This is runtime polymorphism.
6️⃣ Static vs Runtime Polymorphism (Important Table)
| Feature | Static Polymorphism | Runtime Polymorphism |
|---|---|---|
| Binding | Compile time | Runtime |
| Achieved by | Overloading | Overriding |
| Keyword | No special keyword | virtual |
| Speed | Faster | Slightly slower |
| Flexibility | Less | More |
7️⃣ Function Overloading vs Function Overriding
| Basis | Overloading | Overriding |
|---|---|---|
| Same class | Yes | No |
| Different class | No | Yes |
| Parameters | Different | Same |
| Inheritance required | No | Yes |
| Polymorphism Type | Static | Runtime |
8️⃣ Complete Example (Both Together)
#include <iostream>
using namespace std;
class Shape {
public:
// Overloaded function
int area(int side) {
return side * side;
}
virtual void show() {
cout << "Shape class\n";
}
};
class Circle : public Shape {
public:
void show() override {
cout << "Circle class\n";
}
};
int main() {
Shape obj;
cout << obj.area(5) << endl; // Overloading
Shape* ptr;
Circle c;
ptr = &c;
ptr->show(); // Overriding
}
9️⃣ Advantages of Polymorphism
✅ Code flexibility
✅ Improves extensibility
✅ Supports runtime decision
✅ Reduces complexity
✅ Enhances maintainability
Comments
Post a Comment