Implement polymorphism by demonstrating function overloading and function overriding
🔷 Understanding Polymorphism in C++: Function Overloading and Overriding
Polymorphism is one of the core principles of Object-Oriented Programming (OOP). In C++, it allows functions to behave differently based on the context, enabling flexibility and reusability in code.
There are two main types of polymorphism in C++:
1. Compile-time Polymorphism – Achieved using function overloading and operator overloading.
2. Run-time Polymorphism – Achieved using function overriding through inheritance and virtual functions.
Let’s explore both with examples.
✅ Function Overloading (Compile-time Polymorphism)
Function overloading means having multiple functions with the same name but different parameter lists. The compiler decides which function to call at compile time based on the number or type of arguments.
✅ Function Overriding (Run-time Polymorphism)
Function overriding happens when a derived class provides a specific implementation of a function that is already defined in its base class. This is typically used with virtual functions, allowing dynamic dispatch at runtime.
🧑💻 C++ Program Demonstrating Function Overloading & Overriding
#include <iostream>
using namespace std;
// Compile-time Polymorphism: Function Overloading
class Calculator {
public:
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: Function Overriding
class Animal {
public:
virtual void sound() {
cout << "This is a generic animal sound." << endl;
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "The dog barks." << endl;
}
};
int main() {
// Demonstrate function overloading
Calculator calc;
cout << "Addition of 2 and 3: " << calc.add(2, 3) << endl;
cout << "Addition of 5.5 and 4.3: " << calc.add(5.5, 4.3) << endl;
cout << "Addition of 1, 2 and 3: " << calc.add(1, 2, 3) << endl;
// Demonstrate function overriding
Animal* a; // Base class pointer
Dog d; // Derived class object
a = &d; // Pointing to derived class
a->sound(); // Calls Dog's overridden method
return 0;
}
🖨️ Output
Addition of 2 and 3: 5
Addition of 5.5 and 4.3: 9.8
Addition of 1, 2 and 3: 6
The dog barks.
📝 Explanation
Function Overloading:
- The class `Calculator` has three `add()` methods with different parameter types/lengths.
- The compiler picks the appropriate method based on the input arguments.
Function Overriding:
- `Animal` defines a virtual function `sound()`.
- `Dog` overrides it with its own implementation.
- The `Animal` pointer points to a `Dog` object, and due to runtime polymorphism, the `Dog`’s `sound()` method is invoked.
🎯 Conclusion
Understanding and applying function overloading and function overriding are essential for mastering polymorphism in C++. These features help in writing clean, modular, and reusable code that behaves correctly in various contexts. Overloading gives flexibility at compile time, while overriding brings dynamic behavior to your applications.
Comments
Post a Comment