The World of Mayukh Bose

<< Back to Main Page Mayukh's world: e-mail me | about
Mayukh's World: Operator Overloading With C++ Thursday, November 23, 2017
Index
  • Introduction
  • What Operators?
  • Rules
  • Assignment Operator
  • More on Assignment
  • Arithmetic Operators
  • Arithmetic with Globals
  • Increment/Decrement
  • Operator-Assignment
  • Unary Operators
  • Relational Operators
  • Bitshift/Extraction
  • Subscript Operator
  • Function Call Operator
  • Bit and Logical Ops
  • Comma Operator
  • Pointer to Member
  • new and delete Ops
  • Credits and Thanks
  • My Free Software
  • Delphi/C++ Builder
  • Pocket PC
  • FreeSpeech Chat
  • C/C++ Freebies
  • Perl
  • Python
  • Ruby
  • C++ Operator Overloading Tutorial e-mail me

    Relational Operators

    There are six relational (or comparision) operators in C++:
    • Equality (==)
    • Less than (<)
    • Less than or equal to (<=)
    • Greater than (>)
    • Greater than or equal to (>=)
    • Inequality (!=)
    Of these six operators, it is only necessary to implement two of them: the equality operator and either the less than or greater than operator. The others can be implemented using these two operators. For example, if we have overloaded the == and < operators, then we can use the following identities for the other operators:
        Expression           Implementation
        ----------           --------------
          x != y             return !(x == y);
          x > y              return (y < x);
          x <= y             return !(y < x);
          x >= y             return !(x < y);
        
    Let's go ahead and implement == and < operators using member functions first.
    	  class Complex {
    	  private:
    	    double real, imag;
    	  public:
    	    Complex() { real = imag = 0; }	
    	    ....
    	    bool operator==(const Complex& rhs);
    	    bool operator<(const Complex& rhs);
    	  };
    
    	  bool Complex::operator==(const Complex& rhs) {
    	    return (real == rhs.real && imag == rhs.imag);
    	  }
    
              bool Complex::operator<(const Complex& rhs) {
                double magnitude1 = sqrt(real * real + imag * imag);
                double magnitude2 = sqrt(rhs.real * rhs.real  + rhs.imag * rhs.imag);
                if (magnitude1 < magnitude2)
                  return true;
                else
                  return false;
              }
        
    The same functionality implemented using global functions instead of member functions:
    	  class Complex {
    	  private:
    	    double real, imag;
    	  public:
    	    Complex() { real = imag = 0; }	
    	    ....
    	    // Commented out -- bool operator==(const Complex& rhs);
    	    // Commented out -- bool operator<(const Complex& rhs);
    	  };
    
    	  bool operator==(const Complex& lhs, const Complex& rhs) {
    	    return (lhs.GetReal() == rhs.GetReal() && lhs.GetImag() == rhs.GetImag());
    	  }
    
    	  bool operator<(const Complex& lhs, const Complex& rhs) {
                double magnitude1 = sqrt(lhs.real * lhs.real + lhs.imag * lhs.imag);
                double magnitude1 = sqrt(rhs.real * rhs.real + rhs.imag * rhs.imag);
                if (magnitude1 < magnitude2)
                  return true;
    	    else
    	      return false;
    	  }	  
        
    You can of course verify that these functions do work as advertised. As noted previously, the variables are named lhs and rhs to denote that where they appear in the expression (Left Hand Side and Right Hand Side). This was done for clarity and you can name those variables anything you like.

    As for implementing the remaining comparision operators, we can use the identities at the start of this page to implement them. Luckily for us, the C++ standards committee has already taken the step of implementing those identities as template functions. Hence, the only thing we will need to do is include a certain header file, define a namespace and presto! We will have all six operators overloaded. The magic incantation to do this is to add the following lines at the top of the file:
        #include <utility>
        using namespace std::rel_ops;
        
    That's all there is to it. Thanks to the magic of templates and the C++ standards committee, we can leave the work of writing the remaining functions to the compiler! Now you can verify that !=, >, >= and <= work for complex numbers.


    <<Previous: Unary Operators ^Up to Mayukh's World^ Next: Bitshift/Extraction Operators >>

    Copyright © 2004 Mayukh Bose. All rights reserved.
    This work may be freely reproduced provided this copyright notice is preserved.
    OPTIONAL: Please consider linking to this website (http://www.mayukhbose.com/) as well.