L3_IO - C+ IO C+ IO For : COP 3330. Object oriented...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 C++ IO For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar C++ IO | All I/O is in essence, done one character at a time | Concept: I/O operations act on streams (sequences) of ASCII characters C++ IO | cout standard output stream sequence of characters printed to the monitor | cin standard input stream sequence of characters input from the keyboard | both cout and cin are data objects and are defined as classes <iostream> vs <cstdio> | More type-safe: With <iostream>, the type of object being I/O'd is known statically by the compiler. In contrast, <cstdio> uses "%" fields to figure out the types dynamically. | Less error prone: With <iostream>, there are no redundant "%" tokens that have to be consistent with the actual objects being I/O'd. Removing redundancy removes a class of errors. | Extensible: The C++ <iostream> mechanism allows new user-defined types to be I/O'd without breaking existing code. Imagine the chaos if everyone was simultaneously adding new incompatible "%" fields to printf() and scanf()?! | Inheritable: User defined streams possible. #include <iostream> cin ( type istream ) class cout ( type ostream ) class Keyboard Screen executing program input data output data Interactive I/O Namespaces: They provide a way to avoid name collision. Be careful about using this. Example #include <iostream> using namespace std; int main(void){ cout << “Hello World” ; cout << endl; return 0; } Standard IO library for C++. Defines two fundamental types, istream and ostream. Stream: A flow of characters (1 or 2 bytes long). Can flow in and out of Files, strings, etc.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Example #include <iostream> using namespace std; int main(void){ cout << “Hello World” ; cout << endl; return 0; } Ostream object named cout. Equivalent to: operator<< (cout, “Hello World”); Its calling a friend function of ostream with input data. Uses function declaration (approx): Example #include <iostream> using namespace std; int main(void){ cout << endl; return 0; } invokes a manipulator function called endl. endl looks something like this: ostream& endl( ostream& os) { os << '\n'; os.flush(); return os; } Equivalent Compiler statement: std::cout.operator<<( std::endl(std::cout) ); Scope Operator for namespaces. \n new line \t tab \b backspace \r carriage return \' single quote \" double quote \\ backslash Special Output Characters Stream IO headers | iostream -- contains basic information required for all stream I/O operations | iomanip – used for performing formatted I/O with stream manipulators | fstream – used for performing file I/O operations | strstream -- used for performing in-memory I/O operations (i.e., into or from strings in memory) A Stream | A flow of characters. | Buffers: IO to streams goes thru a
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 11

L3_IO - C+ IO C+ IO For : COP 3330. Object oriented...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online