Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++
  • C++ Classes and Objects
  • C++ Polymorphism
  • C++ Inheritance
  • C++ Abstraction
  • C++ Encapsulation
  • C++ OOPs Interview Questions
  • C++ OOPs MCQ
  • C++ Interview Questions
  • C++ Function Overloading
  • C++ Programs
  • C++ Preprocessor
  • C++ Templates
Open In App
Next Article:
Overloading Relational Operators in C++
Next article icon

Overloading Relational Operators in C++

Last Updated : 29 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, operator overloading is used to redefine the behavior of already existing operators. Similarly, overloading the relational operators is commonly used to compare the instances of user-defined classes. By overloading these operators we can easily define the behavior of comparisons for the objects of a given class.

In this article, we will see how to overload relational operators for a class in C++.

Relational Operator Overloading

Overloading a relational operator is similar to any other operator overloading. We can easily implement it by overloading "==" operator and "<" operator as member functions, and other relational operators can be derived from these.

C++ Program to Implement Relational Operator Overloading

The below example demonstrates how the overloading of relational operators is done in C++.

C++
// C++ program to demonstrate how to overload relational // operators.  #include <iostream> using namespace std;  class MyClass { private:     int value; // Private member to store the value  public:     // Constructor to initialize MyClass objects     MyClass(int val)         : value(val)     {     }      // Overloading the equality operator (==)     bool operator==(const MyClass& other) const     {         // Compare the value of this object with the value         // of 'other'         return value == other.value;     }      // Overloading the inequality operator (!=)     bool operator!=(const MyClass& other) const     {         // Utilize the already overloaded '==' operator         return !(*this == other);     }      // Overloading the less than operator (<)     bool operator<(const MyClass& other) const     {         // Compare the value of this object with 'other' for         // less than         return value < other.value;     }      // Overloading the greater than operator (>)     bool operator>(const MyClass& other) const     {         // Compare the value of this object with 'other' for         // greater than         return value > other.value;     }      // Overloading the less than or equal to operator (<=)     bool operator<=(const MyClass& other) const     {         // Utilize the already overloaded '>' operator         return !(*this > other);     }      // Overloading the greater than or equal to operator     // (>=)     bool operator>=(const MyClass& other) const     {         // Utilize the already overloaded '<' operator         return !(*this < other);     } };  int main() {     MyClass obj1(20);     MyClass obj2(20);      // Using overloaded relational operators     if (obj1 == obj2) {         cout << "obj1 is equal to obj2" << endl;     }     else {         cout << "obj1 is not equal to obj2" << endl;     }      if (obj1 < obj2) {         cout << "obj1 is less than obj2" << endl;     }     else {         cout << "obj1 is not less than obj2" << endl;     }      // Using overloaded '!=' operator     if (obj1 != obj2) {         cout << "obj1 is not equal to obj2" << endl;     }     else {         cout << "obj1 is equal to obj2" << endl;     }      // Using overloaded '>' operator     if (obj1 > obj2) {         cout << "obj1 is greater than obj2" << endl;     }     else {         cout << "obj1 is not greater than obj2" << endl;     }      // Using overloaded '<=' operator     if (obj1 <= obj2) {         cout << "obj1 is less than or equal to obj2"              << endl;     }     else {         cout << "obj1 is not less than or equal to obj2"              << endl;     }      // Using overloaded '>=' operator     if (obj1 >= obj2) {         cout << "obj1 is greater than or equal to obj2"              << endl;     }     else {         cout << "obj1 is not greater than or equal to obj2"              << endl;     }      return 0; } 

Output
obj1 is equal to obj2  obj1 is not less than obj2  obj1 is equal to obj2  obj1 is not greater than obj2  obj1 is less than or equal to obj2  obj1 is greater than or equal to obj2      

Explanation: In the above program the overloading of all the relational operators (==, !=, <, >, <=, >=) available in C++ is done for a custom class named MyClass. It shows that how we can compare instances of user defined class with the help of these operators in a manner way like using any primitive data types, which store an integer value.


Next Article
Overloading Relational Operators in C++

A

avinashw5if
Improve
Article Tags :
  • C++ Programs
  • C++
  • cpp-operator-overloading
  • C++-Operator Overloading
  • Operator Overloading
  • cpp-overloading
  • CPP Examples
Practice Tags :
  • CPP

Similar Reads

    Overloading the Comma Operator
    In C++, we can overload the comma operator using Operator Overloading. For Example: For "Send the query X to the server Y and put the result in variable Z", the "and" plays the role of the comma. The comma operator (, ) is used to isolate two or more expressions that are included where only one expr
    5 min read
    How to Overload the (+) Plus Operator in C++?
    In C++, operator overloading is a feature of the OOPs concept that allows you to redefine the behavior for different operators when they are used with objects of user-defined classes. The plus operator (+) is a binary operator generally used for addition. In this article, we will learn how to overlo
    2 min read
    Relational Operators on STL Array in C++
    The article illustrates the working of the different relational operator on the array STL. The equality comparison ( == ) is performed by comparing the elements sequentially using operator ( == ), stopping at the first mismatch. The less-than comparison ( < ) or greater-than comparison ( > ) b
    3 min read
    How to Overload the Less-Than (<) Operator in C++?
    In C++ we have an operator called less than operator (<) which checks if the left side operand is smaller than the right side operand or not. In this article, we will learn how to overload the less-than operator in C++. Overloading Less-Than Operator in C++In C++, we can overload the less-than op
    2 min read
    Overloading of function-call operator in C++
    In this article, we will discuss the Overloading of the function-call operators in C++. The function call operator is denoted by “()” which is used to call function and pass parameters. It is overloaded by the instance of the class known as a function object.When the function call operator is overlo
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences