Overload relational operator in C ++
There are many different relational operators supported by C ++, such as: (, =, ==, ...) that can be used to compare the data types available in C ++.
There are many different relational operators supported by C ++, for example: (<,>, <=,> =, ==, .) that can be used to compare data types with available in C ++.
You can overload any relational operator, which can be used to compare objects of a class.
The following example explains how the
#include using namespace std ; class KhoangCach { private : int met ; int centimet ; public : // phan khai bao cac constructor can thiet KhoangCach (){ met = 0 ; centimet = 0 ; } KhoangCach ( int m , int c ){ met = m ; centimet = c ; } // nap chong toan tu < bool operator <( const KhoangCach & k ) { if ( met < k . met ) { return true ; } if ( met == k . met && centimet < k . centimet ) { return true ; } return false ; } }; int main () { KhoangCach K1 ( 23 , 15 ), K2 ( 17 , 46 ); if ( K1 < K2 ) { cout << "K1 la ngan hon K2 " << endl ; } else { cout << "K2 la ngan hon K1 " << endl ; } return 0 ; }
Compiling and running the above C ++ program will produce the following results:
K2 la ngan hon K1
According to Tutorialspoint
Previous article: Overloading binary operators in C ++
Next lesson: Load the Input / Output operator stack in C ++
You've just finished reading the article "Overload relational operator in C ++" edited by the TipsMake team. You can save overload-relational-operator-in-c-.pdf to your computer here to read later or print it out. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.
- Operator overload and Load overlap in C ++
- Overload subscript operator [] in C ++
- Load operator ++ and - in C ++
- Operator overloading in C #
- Overload class member access operator (->) in C ++
- Overload binary operators in C ++
- What is an Operator? Everything you need to know about AI-based browser agents.
- Operator in C ++
- The '+' operator in SQL Server
- Load the stack of assignment operators in C ++
- Load the operator stack to call the function () in C ++
- Convert XML into relational data
- Convert XML into relational data used in DB2
- How to fix CPU overload error 99%, 100% simple, effective!