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 ++
4 ★ | 1 Vote
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
6 ways to fix the error 'PowerPoint found a problem with the content'
Sample painting of Hanoi in my heart, simple landscape
PowerPoint 2016: Working with lists in PowerPoint
How to Paste an Image into a Selection in Photoshop
Europe develops satellite internet system to replace Starlink
Intel releases new driver update package with 6GHz optimization and many other improvements