If you are saying that I/O must be performed using operators then that is not true. Operators such as "<<" and ">>" are just conveniences; they are not requirements and they do not prevent us from doing anything. For example, try:
They both work. Note that the "<<" and ">>" operators provide formatting, which can be a significant convenience, but if your requirements are more advanced than what the "<<" and ">>" operators can provide then you just need to learn more about how C++ works.
I don't think I've ever seen an 'official' answer to this question, but I can think of three possible reasons:
1. << and >> are briefer to write than function names such as output and input respectively.
2. It's easy to use operators multiple times in the same line of code.
3. You can overload these operators to provide 'inserters' or 'extractors' for your own classes which work in an analogous way to the basic stream operations.
EDIT:
I've just checked Bjarne Stroustrup's book, The C++ Programming Language (3rd edition) and it says, in section 21.2 Output, that the use of functions would be repetitive.
"Overloading the operator << to mean 'put to' gives a better notation and lets the programmer output a sequence of objects in a single statement."
It also goes on to say:
"This style can be used as long as x is of a type for which operator << is defined and a user can trivially define operator << for a new type."
I can't find any explanation about the use of >> for input but, logically, if you use an operator for output you should do the same for input.
So it looks like my answer on this was not wide of the mark.
Sam HobbsPosted Jun 3, 2013, 1:46 AM
std::wcout << L"Hello\n";
std::wcout.write(L"Hello again\n", 12);
They both work. Note that the "<<" and ">>" operators provide formatting, which can be a significant convenience, but if your requirements are more advanced than what the "<<" and ">>" operators can provide then you just need to learn more about how C++ works.
VulpesPosted Jun 2, 2013, 10:28 AM
1. << and >> are briefer to write than function names such as output and input respectively.
2. It's easy to use operators multiple times in the same line of code.
3. You can overload these operators to provide 'inserters' or 'extractors' for your own classes which work in an analogous way to the basic stream operations.
EDIT:
I've just checked Bjarne Stroustrup's book, The C++ Programming Language (3rd edition) and it says, in section 21.2 Output, that the use of functions would be repetitive.
"Overloading the operator << to mean 'put to' gives a better notation and lets the programmer output a sequence of objects in a single statement."
It also goes on to say:
"This style can be used as long as x is of a type for which operator << is defined and a user can trivially define operator << for a new type."
I can't find any explanation about the use of >> for input but, logically, if you use an operator for output you should do the same for input.
So it looks like my answer on this was not wide of the mark.