Operator

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Operator overloading is a powerful feature in object-oriented programming (OOP) that allows

programmers to redefine the behavior of operators such as +, -, *, /, =, and more. This feature
enables objects to behave like built-in types, providing a natural and intuitive syntax for
performing operations. In languages like C++, operator overloading is extensively used to
enhance the readability and expressiveness of code.

The concept of operator overloading is based on the principle of polymorphism, which allows
operators to behave differently based on the types of operands involved. By overloading
operators, programmers can define custom behaviors for operators when applied to objects of
user-defined types.

In C++, operator overloading is implemented by defining special member functions known as


overloaded operators. These functions have special names and syntax that indicate which
operator they overload. For example, the + operator can be overloaded by defining a function
named operator+. Similarly, the - operator is overloaded by defining a function named
operator-, and so on.

#include <iostream>

class Complex {

private:

double real;

double imag;

public:

Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

// Overloading the + operator

Complex operator+(const Complex& other) const {

return Complex(real + other.real, imag + other.imag);


}

// Overloading the - operator

Complex operator-(const Complex& other) const {

return Complex(real - other.real, imag - other.imag);

// Overloading the * operator

Complex operator*(const Complex& other) const {

return Complex(real * other.real - imag * other.imag,

real * other.imag + imag * other.real);

// Overloading the == operator

bool operator==(const Complex& other) const {

return (real == other.real) && (imag == other.imag);

// Overloading the != operator

bool operator!=(const Complex& other) const {

return !(*this == other);

// Overloading the << operator for output

friend std::ostream& operator<<(std::ostream& os, const Complex& c);


};

// Overloading the << operator for output

std::ostream& operator<<(std::ostream& os, const Complex& c) {

os << c.real << " + " << c.imag << "i";

return os;

int main() {

Complex a(2.0, 3.0);

Complex b(1.0, 2.0);

Complex c = a + b;

Complex d = a - b;

Complex e = a * b;

std::cout << "a + b = " << c << std::endl;

std::cout << "a - b = " << d << std::endl;

std::cout << "a * b = " << e << std::endl;

return 0;

In this example, the Complex class represents complex numbers with real and imaginary parts.
We overload the +, -, *, ==, and != operators to perform addition, subtraction, multiplication,
and comparison operations on Complex objects. Additionally, we overload the << operator to
output Complex objects to the console.

Operator overloading provides a concise and natural way to work with user-defined types,
making the code more readable and expressive. However, it should be used judiciously to
maintain clarity and avoid confusion. Overloaded operators should adhere to the expected
behavior of the corresponding built-in operators to ensure consistency and intuitive usage.

You might also like