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 ++
Update 25 May 2019
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
Greatly transforming loose-fitting jeans into extremely simple prints at home To protect the hearing, clean the earpiece Instructions for making backup batteries from an orange, charging nearly 40% of the battery for iPhone Scientists develop new tissue regeneration technology Elon Musk released 8 photos of the first human journey to Mars in the future Watch Elon Musk's presentation on the plan to conquer Mars