Relational Operators Overloading in C++

Posted in /  

Relational Operators Overloading in C++
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    Relational operators are used for comparing two data objects, and by using the class operator overloading methods, we can overload a relational operator for class-based objects. There are various relational operators in C++ such as <, >, <=, >=, ==, etc. which are overloadable.

    This write-up will help you become familiar with relational operators overloading in C++. Here we have overloaded two relational operators < and >. However,  you can overload other relational operators too, if you want to.

    Relational Operators Overloading in C++ Syntax

    return_type operator operator_symbol(Class_name Object_name)
    {
    //redefining body
    }

    Example

    #include <iostream>
    #include<string.h>
    using namespace std;
    
    class Displacement
    {
       private :
          int x;
          char n[20];
    
       public:
            Displacement(int initialize, char name[]) //constructor
               {
                    x=initialize;
                    strcpy(n,name);
                    cout<<"you have created an object "<<n<<" which need to displace  "<< x <<" units";
                    cout<<endl;
                }           
          void operator>(Displacement obj) //operator overloading for > operator
              {
                   if (x>obj.x)
                    {
                     cout<<"True--> object "<<n << " is greater than object "<<obj.n;
                    }
                   else
                    {
                      cout<<"False--> object "<<obj.n << "is greater than object "<<n;
                    }
                      cout<<endl;
              }
    
           void operator<(Displacement obj) //operator overloading for > operator
              {
                   if (x<obj.x)
                    {
                     cout<<"True--> object "<<n << " is Smaller than object "<<obj.n;
                    }
                   else
                   {                                                                    
                     cout<<"False--> object "<<obj.n << " is greater than object "<<n;
                   }
                   cout<<endl;
              }
    };
    
    int main()
    {
         Displacement d1(200,"d1"); // Displacement object
         Displacement d2(30,"d2");  // Displacement object 
         
         d1>d2;
         d1<d2;
         return 0;
    }

    Output

    you have created an object d1 which need to displace  200 units
    you have created an object d2 which need to displace  30 units
    True--> object d1 is greater than object d2
    False--> object d2 is greater than object d1

    Behind the Code

    In this above example, by using the void operator<(Displacement obj) and void operator>(Displacement obj) member functions, we have overloaded or redefined the task of < and > operators for Displacement class objects.

    To Sum it Up

    As you have observed, overloading relational operators is quite easy. If you have a clear understanding of relational operators and the concept of overloading, you can easily overload relational operators to your advantage.

    People are also reading:

    Leave a Comment on this Post

    0 Comments