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 <
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
May be interested
- How to save command line output to file on Windows, Mac and Linuxwhen you run a terminal command, it will usually print the output in the terminal so you can read it immediately. however, sometimes you will want to save the output for later analysis or combination with another tool.
- Stack operator in Pythonyou can change the meaning of the operator in python depending on the operand used and we call that operator overloading. quantrimang will learn more about this content through the article. invites you to read the track.
- Update the BIOS for the PC motherboard in 5 stepsbios stands for basic input / output system. it will manage the entire system of computer input and output devices. you should update to the latest bios.
- The '+' operator in SQL Serverlearn how to use the + operator in sql server to join two or more strings into a string.
- Load the operator stack to call the function () in C ++c ++ function call () can be overloaded for objects of class type. when you overload (), you are not creating a new way to call a function.
- Stack Overflow hits the hacker face, no significant damage is recordedin a brief report released earlier today, stack overflow has reportedly recorded an attack that led hackers to successfully access their production systems.
- Instructions for entering BIOS on different computersbios (short for basic input / output system - basic input / output system) is the place that contains many groups of commands stored on the motherboard's firmware chip to control basic features of the computer.
- Overload the one-seat operator in C ++the unary operator in c ++ operates on a single operand.
- How to Implement a Stack Data Structure in C++a stack is a basic data structure that is commonly used throughout computer science. for example, a stack is utilized by your web browser to remember the last few pages you were on. developers and programmers should be very familiar with...
- Self-assemble computers, build desktops (P3): BIOS settingsbios stands for basic input-output system (basic input-output system). it is a small program that is stored on a small memory chip in the motherboard and runs on your computer before the operating system works, thereby helping you to set up everything and allowing changes to the settings. basic of the system.