As you all know there are libraries using streams such as iostream and fstream.
My question is:
- Why streams? Why didn’t they stick with functions similar to
print,fgetsand so on (for example)?
They require their own operators << and >> but all they do could be implemented in simple functions like above, also the function
printf("Hello World!");
is a lot more readable and logical to me than
cout << "Hello World";
I also think that all of those string abstractions in C++ all compile down to (less efficient) standard function calls in binary.
Streams have better type safety.
For instance
printf("%s", a);can go horribly wrong ifais an integer.cout << a;doesn’t have this problem.Another issue is that streams better conform to Object Oriented design methodologies.
For instance you have a simple application that writes some output and then you want the output to go to a file instead of to the console. With C calls you’ll have to replace all of the calls to
printfto calls tofprintfand take care to maintain theFILE*along the way. With streams you just change the concrete class of the stream you’re using and that’s it, most of the code remains the same like so: