Operator Overloading - heshawacooray/OOP-Heshawa GitHub Wiki

Definition: Operator overloading allows operators (such as +, -, etc.) to be redefined for user-defined classes, enabling the operators to work with objects in a meaningful way.

Example code:

#include <iostream>
using namespace std;

class Complex {
private:
    int real;
    int imag;

public:
    Complex(int r, int i) : real(r), imag(i) {}

    Complex operator + (const Complex& other) {
        return Complex(real + other.real, imag + other.imag);
    }

    void display() {
        cout << "Real: " << real << ", Imag: " << imag << endl;
    }
};

int main() {
    Complex c1(3, 4);
    Complex c2(1, 2);
    Complex c3 = c1 + c2;

    c3.display();  // Outputs: Real: 4, Imag: 6

    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️