![]() |
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++
Output
p1 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. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |