C++ Tutorial

Class and Object

Reference

Inheritance and Derivation

Polymorphism and Virtual Functions

Operator Overloading

Template

Exception

Object Oriented Advanced

Input/Output Stream

File Operations

Operator Overloading in C++

Operator overloading allows you to redefine the behavior of C++ operators for your custom types (classes and structs). By overloading operators, you can make your custom types work with operators more intuitively and naturally. This tutorial will introduce operator overloading and guide you through the process of overloading an operator for a custom class.

  • Basic Syntax:

Here's the general syntax for overloading an operator:

return_type class_name::operator op(arguments) {
    // implementation
}
  • Overloading Unary Operators:

To overload a unary operator, like the negation operator (-), increment operator (++), or decrement operator (--), define a member function without any arguments (except the implicit this pointer). For instance, overloading the negation operator for a custom Number class:

class Number {
    int value;

public:
    // Constructor
    Number(int v) : value(v) {}

    // Overloading the negation operator
    Number operator-() const {
        return Number(-value);
    }
};
  • Overloading Binary Operators:

To overload a binary operator, like the addition operator (+), subtraction operator (-), or multiplication operator (*), define a member function with one argument representing the right operand. For instance, overloading the addition operator for the Number class:

class Number {
    int value;

public:
    // Constructor
    Number(int v) : value(v) {}

    // Overloading the addition operator
    Number operator+(const Number& rhs) const {
        return Number(value + rhs.value);
    }
};
  • Overloading Insertion and Extraction Operators:

To overload the insertion operator (<<) or the extraction operator (>>), you need to use a friend function, as these operators require access to the private members of both the stream object and the custom class. Here's an example of overloading the insertion and extraction operators for the Number class:

#include <iostream>

class Number {
    int value;

public:
    // Constructor
    Number(int v = 0) : value(v) {}

    // Overloading the insertion operator (<<)
    friend std::ostream& operator<<(std::ostream& os, const Number& num) {
        os << num.value;
        return os;
    }

    // Overloading the extraction operator (>>)
    friend std::istream& operator>>(std::istream& is, Number& num) {
        is >> num.value;
        return is;
    }
};
  • Overloading Comparison Operators:

To overload comparison operators, such as the equality operator (==), the less-than operator (<), or the greater-than operator (>), define a member function with one argument and return a boolean value. Here's an example of overloading the equality and less-than operators for the Number class:

class Number {
    int value;

public:
    // Constructor
    Number(int v) : value(v) {}

    // Overloading the equality operator (==)
    bool operator==(const Number& rhs) const {
        return value == rhs.value;
    }

    // Overloading the less-than operator (<)
    bool operator<(const Number& rhs) const {
        return value < rhs.value;
    }
};
  1. How to overload operators in C++:

    • Description: Explains the concept of operator overloading, allowing you to redefine the behavior of operators for user-defined types.
    • Example Code:
      #include <iostream>
      
      class Complex {
      private:
          double real, imag;
      
      public:
          Complex(double r, double i) : real(r), imag(i) {}
      
          // Overloading the + operator
          Complex operator+(const Complex& other) const {
              return Complex(real + other.real, imag + other.imag);
          }
      
          // Overloading the << operator for output
          friend std::ostream& operator<<(std::ostream& os, const Complex& c);
      };
      
      std::ostream& operator<<(std::ostream& os, const Complex& c) {
          os << c.real << " + " << c.imag << "i";
          return os;
      }
      
      int main() {
          Complex a(1.0, 2.0);
          Complex b(2.0, 3.0);
      
          // Using the overloaded + operator
          Complex result = a + b;
      
          // Using the overloaded << operator
          std::cout << "Result: " << result << std::endl;
      
          return 0;
      }