+ 2
How in operator-overloading for "<<" explained ?
how it works ?
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<<
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.