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

Math Operators Overloaded in C++

In this tutorial, we will learn how to overload math operators for a custom class in C++. Overloading math operators allows you to use these operators naturally and intuitively with instances of your custom classes. We will demonstrate overloading math operators using a simple Fraction class as an example.

  • Create a simple Fraction class:

First, let's create a basic Fraction class with a constructor and a function to display the fraction:

#include <iostream>

class Fraction {
    int numerator;
    int denominator;

public:
    // Constructor
    Fraction(int n, int d) : numerator(n), denominator(d) {}

    // Function to display the fraction
    void display() const {
        std::cout << numerator << "/" << denominator;
    }
};
  • Overload addition operator (+):

To overload the addition operator for the Fraction class, define a member function with one argument representing the right operand. The result should be a new Fraction object representing the sum of the two fractions:

Fraction operator+(const Fraction& rhs) const {
    int n = numerator * rhs.denominator + rhs.numerator * denominator;
    int d = denominator * rhs.denominator;
    return Fraction(n, d);
}
  • Overload subtraction operator (-):

To overload the subtraction operator for the Fraction class, define a member function with one argument representing the right operand. The result should be a new Fraction object representing the difference between the two fractions:

Fraction operator-(const Fraction& rhs) const {
    int n = numerator * rhs.denominator - rhs.numerator * denominator;
    int d = denominator * rhs.denominator;
    return Fraction(n, d);
}
  • Overload multiplication operator (*):

To overload the multiplication operator for the Fraction class, define a member function with one argument representing the right operand. The result should be a new Fraction object representing the product of the two fractions:

Fraction operator*(const Fraction& rhs) const {
    int n = numerator * rhs.numerator;
    int d = denominator * rhs.denominator;
    return Fraction(n, d);
}
  • Overload division operator (/):

To overload the division operator for the Fraction class, define a member function with one argument representing the right operand. The result should be a new Fraction object representing the quotient of the two fractions:

Fraction operator/(const Fraction& rhs) const {
    int n = numerator * rhs.denominator;
    int d = denominator * rhs.numerator;
    return Fraction(n, d);
}
  • Complete example:

Here's the complete Fraction class with overloaded math operators:

#include <iostream>

class Fraction {
    int numerator;
    int denominator;

public:
    // Constructor
    Fraction(int n, int d) : numerator(n), denominator(d) {}

    // Overloading the addition operator
    Fraction operator+(const Fraction& rhs) const {
        int n = numerator * rhs.denominator + rhs.numerator * denominator;
        int d = denominator * rhs.denominator;
        return Fraction(n, d);
    }

    // Overloading the subtraction operator
    Fraction operator-(const Fraction& rhs) const {
        int n = numerator * rhs.denominator - rhs.numerator * denominator;
        int d = denominator * rhs.denominator;
        return Fraction(n, d);
    }

    // Overloading the multiplication operator
    Fraction operator*(const Fraction& rhs) const {
        int n = numerator * rhs.numerator;
        int d = denominator * rhs.denominator;
        return Fraction(n, d);
    }   
  1. Operator overloading for addition, subtraction, multiplication, and division in C++:

    • Description: Illustrates overloading the basic arithmetic operators (+, -, *, /) for a custom class.
    • Example Code:
      #include <iostream>
      
      class Complex {
      private:
          double real, imag;
      
      public:
          Complex(double r, double i) : real(r), imag(i) {}
      
          // Overloading arithmetic operators
          Complex operator+(const Complex& other) const {
              return Complex(real + other.real, imag + other.imag);
          }
      
          Complex operator-(const Complex& other) const {
              return Complex(real - other.real, imag - other.imag);
          }
      
          Complex operator*(const Complex& other) const {
              return Complex(real * other.real - imag * other.imag, real * other.imag + imag * other.real);
          }
      
          Complex operator/(const Complex& other) const {
              double denominator = other.real * other.real + other.imag * other.imag;
              return Complex((real * other.real + imag * other.imag) / denominator,
                             (imag * other.real - real * other.imag) / denominator);
          }
      
          // Display function
          void display() const {
              std::cout << real << " + " << imag << "i" << std::endl;
          }
      };
      
      int main() {
          Complex a(1.0, 2.0);
          Complex b(2.0, 3.0);
      
          // Using overloaded arithmetic operators
          Complex sum = a + b;
          Complex difference = a - b;
          Complex product = a * b;
          Complex quotient = a / b;
      
          // Displaying results
          std::cout << "Sum: ";
          sum.display();
      
          std::cout << "Difference: ";
          difference.display();
      
          std::cout << "Product: ";
          product.display();
      
          std::cout << "Quotient: ";
          quotient.display();
      
          return 0;
      }