Polymorphism
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows methods or operators to perform different tasks based on the context. In OOP, polymorphism manifests mainly through function overloading, operator overloading, and function overriding, each of which enables flexibility and modularity in code design.
Function Overloading
Function overloading is a type of compile-time polymorphism where multiple functions can have the same name but differ in parameters. The compiler distinguishes these functions based on the number, type, or order of parameters. This concept allows a single function name to handle different types of inputs or operations, making the code more readable and organized.
Example:
#include <iostream>
using namespace std;
// Overloaded functions to handle different types of parameters
int multiply(int a, int b) {
return a * b;
}
double multiply(double a, double b) {
return a * b;
}
int multiply(int a, int b, int c) { // Additional overload with 3 parameters
return a * b * c;
}
int main() {
cout << "Multiplying integers (2, 3): " << multiply(2, 3) << endl;
cout << "Multiplying doubles (2.5, 3.5): " << multiply(2.5, 3.5) << endl;
cout << "Multiplying three integers (2, 3, 4): " << multiply(2, 3, 4) << endl;
return 0;
}
Multiplying integers (2, 3): 6
Multiplying doubles (2.5, 3.5): 8.75
Multiplying three integers (2, 3, 4): 24
Operator Overloading
Operator overloading is another form of compile-time polymorphism that allows existing operators (such as +
, -
, *
, etc.) to be redefined to work with user-defined types. It essentially means giving operators a new meaning when applied to custom classes, thus improving code readability.
Example :
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize complex number
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
// Overloading the '+' operator to add two Complex numbers
Complex operator + (const Complex& other) {
return Complex(real + other.real, imag + other.imag);
}
// Function to display the complex number
void display() const {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
// Creating two Complex objects
Complex c1(3.4, 5.6);
Complex c2(1.2, 3.8);
// Adding two complex numbers using the overloaded '+' operator
Complex c3 = c1 + c2;
// Displaying the result
cout << "Complex number 1: ";
c1.display();
cout << "Complex number 2: ";
c2.display();
cout << "Sum of Complex numbers: ";
c3.display();
return 0;
}
Complex number 1: 3.4 + 5.6i
Complex number 2: 1.2 + 3.8i
Sum of Complex numbers: 4.6 + 9.4i
Function Overriding
Function overriding is a type of runtime polymorphism that occurs when a derived class provides a specific implementation of a function that is already defined in its base class. Function overriding requires an inheritance relationship, where a method in the derived class has the same signature as a method in the base class. The derived class method "overrides" the base class method, allowing specific behavior for objects of the derived class.
Example :
Summary
- Function Overloading: Same function name with different parameters (compile-time).
- Operator Overloading: Redefines operators to work with user-defined types (compile-time).
- Function Overriding: Derived class redefines a base class function with the same signature (runtime).
No comments:
Post a Comment