Overload relational operator 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 ++
4 ★ | 1 Vote
You should read it
May be interested
- The '+' operator in SQL Serverlearn how to use the + operator in sql server to join two or more strings into a string.
- Load the operator stack to call the function () in C ++c ++ function call () can be overloaded for objects of class type. when you overload (), you are not creating a new way to call a function.
- Convert XML into relational datain this tutorial we will show you some methods of converting xml documents into rows in relational tables.
- How to fix CPU overload error 99%, 100% simple, effective!how to fix cpu overload error 99%, 100%? this article will help you solve the problem of computer overload quickly and effectively!
- Convert XML into relational data used in DB2in this next article we will show you another way to shred xml documents into relational tables.
- Fix CPU and 99% CPU overload errors on Windowsif your computer's fan is constantly running at high speed, your cpu is overloaded. this means your computer will slow down. that is really annoying if you don't fix it.
- What do you know about NoSQL Database?nosql was born as a patch for the shortcomings and shortcomings of the rdbms relational data model. so how much do you know about nosql? try your knowledge with quantrimang through the following multiple choice questions!
- How to fix OneDrive error makes CPU overload on Windows 10the error of opening onedrive causes cpu overload to occur frequently on windows 10 creators, causing the device to be in a state of 'stall' continuously.
- Load the Input / Output operator stack in C ++c ++ is able to input and output existing data types by using thread extraction operators >> and thread insertion operators
- How to Get Compensation from a BUI Accidentif you were injured in a boating accident, then you can sue the boat operator who caused the injury. for example, the operator may have hit your own boat, or you may have been in the boat with the operator who was intoxicated. either way,...