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 ++
Update 25 May 2019
You should read it
- Operator in C ++
- C Shell operators
- Basic Shell operators
- Korn Shell Operator
- Boolean operators AND and OR in SQL
- Operator in PHP
- ! = and! == What is the difference in JavaScript?
- Load the stack of assignment operators in C ++
- Overload binary operators in C ++
- Comparison operators in SQL Server
- Operator in JavaScript
- Operator overloading in C #
Maybe you are interested
The list of 10 cities can be dangerous for tourists The top 20 most dangerous countries in the world are not for tourists 5 simple ways to help you remember everything in life 20 things to give up to be happier before the new year black hole, white hole, deep hole Detecting the tremors of the 'twin brothers' of the solar system, the key to deciphering early life