How in operator-overloading for "<<" explained ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How in operator-overloading for "<<" explained ?

how it works ?

17th Jan 2017, 9:29 AM
Artur Molla
Artur Molla - avatar
2 Answers
+ 2
operator<<() is like a member function operator to give formatted output in C++ See here : www.cplusplus.com/reference/ostream/ostream/operator<<
25th Jan 2017, 4:13 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
0
"<<" was created in C as a bitshift operator. For example, if you have number 2, which is 00000010 in binary, and you apply bitshift by 3 bits (2 << 3) it moves all the bits to the left by 3, and fills end with 0s. So 2 << 3 results 16 in which is 00010000 in binary. In C++ you will mostly see << for printing, like (cout << "Some string"). That is actualy a overloaded << operator in purpose of cleaner code. When you overload << operator (or any other operator) you can make it do anything you want. For example, if you have a class Song, you could overload << to rewind a Song for some number of seconds.
20th Jan 2017, 7:46 AM
Josip Tomić
Josip Tomić - avatar