Mastering C++ Operator Overloading - CSU1287 - Shoolini U

Operator Overloading

Overloaded Operators or Operator Overloading

Overloaded operators are a powerful feature of C++ that allows you to redefine the behavior of operators for user-defined types. By overloading an operator, you can specify what happens when an operator is used with objects of your class, providing more natural syntax and functionality for your classes.

In C++, most operators can be overloaded, including arithmetic operators, comparison operators, logical operators, and the assignment operator. To overload an operator, you define a special member function with the operator keyword, followed by the operator you want to overload. The function takes one or more arguments, depending on the operator, and returns a value or a reference, depending on the operator and its behavior.

Here's an example of a class that overloads the + operator:

class Vector {
public:
  Vector operator+(const Vector& other) const {
    Vector result;
    result.x = x + other.x;
    result.y = y + other.y;
    result.z = z + other.z;
    return result;
  }
private:
  double x, y, z;
};

In this example, the Vector class has an overloaded + operator. The + operator takes a single argument of type Vector and returns a new Vector object that is the sum of the two vectors. The + operator is defined as a member function of the Vector class, using the operator+() syntax. The const keyword at the end of the function declaration indicates that the function does not modify the current object.

To use the overloaded + operator, you simply use the + operator between two Vector objects, like this:

Vector a(1.0, 2.0, 3.0);
Vector b(4.0, 5.0, 6.0);
Vector c = a + b; // c is now (5.0, 7.0, 9.0)

In addition to overloaded operators, C++ also provides a set of non-member functions that can be used to provide additional functionality to your classes. These functions are known as friend functions, and they are declared using the friend keyword in the class definition.

Here's an example of a class that uses a friend function to overload the << operator for output:

class Complex {
public:
  Complex(double real, double imag) : real(real), imag(imag) {}
  friend std::ostream& operator<<(std::ostream& os, const Complex& c);
private:
  double real, imag;
};

std::ostream& operator<<(std::ostream& os, const Complex& c) {
  os << c.real << "+" << c.imag << "i";
  return os;
}

In this example, the Complex class has a friend function that overloads the << operator for output. The friend function takes two arguments: an output stream object and a const reference to a Complex object. The friend function is defined outside of the class definition, using the operator<<() syntax. The friend keyword in the class definition allows the friend function to access the private data members of the Complex class.

To use the overloaded << operator, you simply use the << operator between an output stream object and a Complex object, like this:

Complex c(1.0, 2.0);
std::cout << "c = " << c << std::endl; // prints "c = 1+2i"

Overall, overloaded operators are a powerful and flexible feature of C++ that allows you to define custom behavior for operators for user-defined types. By overloading operators, you can provide more natural syntax and functionality for your classes, improving code readability and maintainability.