How to Overload the Less-Than (<) Operator in C++? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 operator using the operator function declared as shown: Syntax to Overload Less-Than Operatorbool operator<(const className& right) const;Here, className is the name of the class and right is the name of the right-hand side operand. C++ Program to Overload Less-Than OperatorThe below program demonstrates how we can overload less-than operators using operator overloading in C++. C++ // C++ program to overload the less than < operator #include <iostream> using namespace std; // creating class Coordinates class Coordinates { public: // constructor Coordinates(int x, int y) : x(x) , y(y) { } // Overloading the less-than operator for the Point // objects bool operator<(const Coordinates& other) const { return (x < other.x) || ((x == other.x) && (y < other.y)); } private: int x, y; }; int main() { // initializing two coordinates p1 and p2 Coordinates p1(1, 2); Coordinates p2(3, 4); // checking if p1 is less than p2 if (p1 < p2) { cout << "p1 is less than p2." << endl; } else { cout << "p1 is not less than p2." << endl; } return 0; } Outputp1 is less than p2. Explanation: In the above example, we are overloading the less than (<) operator to compare between the given points for that we first compare the x coordinates of two Points p1 and p2 and if they are equal then we compare the y coordinates, and return the smaller one. Comment More infoAdvertise with us Next Article How to Overload the Less-Than (<) Operator in C++? S subramanyasmgm Follow Improve Article Tags : C++ Programs C++ C++-Operator Overloading Operator Overloading CPP-OOPs CPP Examples +2 More Practice Tags : CPP Similar Reads 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 How to Overload the Multiplication Operator in C++? In C++, the multiplication operator is a binary operator that is used to find the product of two numeric values. In this article, we are going to learn how to overload the multiplication operator for a class in C++. Overloading Multiplication Operator in C++C++ provides the functionality of operator 2 min read How to Overload the Function Call Operator () in C++? In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi 2 min read 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 Overloading Relational Operators in C++ 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 obje 4 min read Like