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
C ++ is able to input and output existing data types by using thread extraction operators >> and thread insertion operators <
Here, it is important to create an operator overloading function for a friend of the class, because it will be called without creating an object.
The following example explains how to overload thread extraction operators >> and thread insertion operators << in C ++ :.
#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 ; } friend ostream & operator <<( ostream & output , const KhoangCach & K ) { output << "nDo dai bang m la: " << K . met << "nVa do dai bang cm la: " << K . centimet ; return output ; } friend istream & operator >>( istream & input , KhoangCach & K ) { input >> K . met >> K . centimet ; return input ; } }; int main () { KhoangCach K1 ( 23 , 14 ), K2 ( 14 , 35 ), K3 ; cout << "Nhap gia tri cua doi tuong K3: " << endl ; cin >> K3 ; cout << "Khoang cach dau tien: " << K1 << endl ; cout << "n==========================n" << endl ; cout << "Khoang cach thu hai: " << K2 << endl ; cout << "n==========================n" << endl ; cout << "Khoang cach thu ba: " << K3 << endl ; return 0 ; }
Compiling and running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous article: Overloading relational operators in C ++
Next lesson: Load the operator ++ and - in C ++
4 ★ | 2 Vote
You should read it
- Stack operator in Python
- Operator overloading in C #
- Operator overload and Load overlap in C ++
- Overload the one-seat operator in C ++
- Operator in programming C
- The '+' operator in SQL Server
- Load operator ++ and - in C ++
- Load the stack of assignment operators in C ++
- Overload subscript operator [] in C ++
- Overload binary operators in C ++
- How to Get Compensation from a BUI Accident
- EXCEPT operator in SQL Server
Maybe you are interested
AI Technology Online Casino Optimization: What Should Operators and Players Expect?
The field of switchboard operators suffered the sweep of the 'AI tsunami'
The difference between the == and === operators in JavaScript
Vietnamese operators accidentally revealed iOS 13.3 release date
How to change WiFi DLink password for network operators
How to get the scratched card back, lost the number of network operators