Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, November 4, 2024

Polymorphism: function overloading, operator overloading, function Overriding

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;

}


Output :

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;

}


Output :

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 :

#include <iostream>
using namespace std;

// Base class Shape
class Shape {
public:
    // Virtual function to calculate area
    virtual double area() {
        return 0;
    }
};

// Derived class Rectangle
class Rectangle : public Shape {
    double length, width;
public:
    // Constructor to initialize length and width
    Rectangle(double l, double w) : length(l), width(w) {}

    // Overridden function to calculate the area of a rectangle
    double area() override {
        return length * width;
    }
};

// Derived class Circle
class Circle : public Shape {
    double radius;
public:
    // Constructor to initialize radius
    Circle(double r) : radius(r) {}

    // Overridden function to calculate the area of a circle
    double area() override {
        return 3.14159 * radius * radius;
    }
};

int main() {
    // Create objects of Rectangle and Circle
    Shape *shape1 = new Rectangle(5.0, 3.0);   // Rectangle with length 5 and width 3
    Shape *shape2 = new Circle(4.0);           // Circle with radius 4

    // Display areas using overridden functions
    cout << "Area of Rectangle: " << shape1->area() << endl;
    cout << "Area of Circle: " << shape2->area() << endl;

    // Free dynamically allocated memory
    delete shape1;
    delete shape2;

    return 0;
}


Output : 
Area of Rectangle: 15
Area of Circle: 50.2654

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