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
How to Integrate ChatGPT for Siri on iPhone
How to get data from web into Excel
Top best apps to turn men into women, boys into girls
How to blur background images in PowerPoint - Adjust background image display mode
Instructions for creating PowerPoint background color effects - Create a new background for slides
7 Best Pinterest Alternatives