Relational Operator Overloading in C++

Relational operator overloading

Relational operator overloading in C++ is frequently used to compare many of the built-in data types in C++. We can overload relational operators like >,<,>=etc to directly manipulate the object of a class.

relational operator overloading

Relational Operator Overloading in C++

There are various relational operators supported by C++ language like (<, >, <=, >=, ==, etc.) which can be used to compare C++ built-in data types.

You can overload any of these operators, which can be used to compare the objects of a class.

Syntax:

return_type operator symbol(const ClassName& object)
{
   // statements
}
// Here operator is a keyword and relational operator 
// symbol is the operator to be overloaded

Example:

bool operator >(const Student& stObj)
{
   // function definition
}

Example 1

Here we are Overloading > i.e. greater than operator

To know the taller between two students.

Run

#include <iostream>    
using namespace std;

class Student{
    int feet = 0; //can be b/w 0 & infinity
    int inches = 0; // can be b/w 0 & 12
    
    public:
    void getHeight(int f, int i){
        feet = f;
        inches = i;
    }

    // const and & added check explanation above(Before code) why
    bool operator > (const Student& s2)
    {
        if(feet > s2.feet)
            return true;
        
        else if(feet == s2.feet && inches > s2.inches)
            return true;
         
        return false;
    }
};

int main()
{
    Student s1,s2;
    
    s1.getHeight(5,10);
    s2.getHeight(6,1);
    
    if(s1 > s2)
        cout << "Student 1 is taller" << endl; else if(s2 > s1)
        cout << "Student 2 is taller" << endl;
    else
        cout << "Both have equal height" << endl;
    

    return 0;
}

Output –

Student 2 is taller

Example 2

Overloading comparison Operator (==)

Here we are writing program to check if two clocks are showing the same time or not –

Run

#include <iostream>  
using namespace std;

class Time
{
    int hour, mins, secs;
    public:
    Time(){
        hour=0, mins=0; secs=0;
    }
    
    // parameterized constructor
    Time(int h, int m, int s)
    {
        if(h>=0 && h <=23 && m >=0 && m <=59 && s >0 && s <=59)
            { hour=h, mins=m; secs=s; }
        else
            cout << "Invalid time format, values would be 00:00:00 by default" << endl;
    }
    
    // returns true if both times are same
    bool operator == (const Time& t2)
    {
        return (hour == t2.hour 
        && mins == t2.mins 
        && secs == t2.secs);
    }
};


int main()
{
    Time t1(7,11,30);
    Time t2(3,30,41);

    if(t1 == t2)
        cout << "Clocks show same time";
    else 
        cout << "Clocks show different times";

    return 0;
}

Output

Clocks show different times

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription