Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: Object Oriented Analysis and Design

Similar presentations


Presentation on theme: "CS212: Object Oriented Analysis and Design"— Presentation transcript:

1 CS212: Object Oriented Analysis and Design
Managing Input Output

2 Streams Input Stream Extraction from input stream Input device Program
Output Stream Insertion into output stream Output device

3 Object Oriented Analysis and Design (CS 212)
C++ stream class ios C++ provides various standard classes for I/O management File stream classes allows easy file handling File stream classes are declared in the fstream header file An object that belongs to a file stream class is known as a file stream istream ostream iostream ifstream ofstream fstream Object Oriented Analysis and Design (CS 212)

4 C++ predefined streams
Meaning Device cin Standard input Keyboard cout Standard output Screen cerr Standard Error clog Buffered version of cerr Error checking in by cin Object Oriented Analysis and Design (CS 212)

5 Unformatted I/O Operation
cin >> [istream] cout << [ostream] c = cin.get() cin.get(c) cout.put(c) cin.getline(line, size) [difference with cin ?] cout.write(line,size) Object Oriented Analysis and Design (CS 212)

6 Object Oriented Analysis and Design (CS 212)
Formatted Console I/O ios class functions and flags Manipulators User defined output functions Object Oriented Analysis and Design (CS 212)

7 Examples of formatted console I/O
cout.width(w) cout.precision(d) cout.fill(ch) Object Oriented Analysis and Design (CS 212)

8 Overloading << and >>
<< and the >> operators are overloaded in C++ to perform I/O for built-in types They can be overloaded to do the same for user-defined types << output operator is referred to as the insertion operator >> input operator is called the extraction operator Object Oriented Analysis and Design (CS 212)

9 Creating Own Inserters
General form of inserter function Function returns a reference to a stream of type ostream First parameter to the function is a reference to the output stream Second parameter is the object being inserted ostream &operator <<(ostream &stream, class_type obj) { // body of inserter return stream; } Object Oriented Analysis and Design (CS 212)

10 Creating Own Extractors
Extractors are the complement of inserters First parameter must also be a reference to a stream of type istream Second parameter must be a reference to an object of the class Function returns a reference to a stream of type istream istream &operator>>(istream &stream, class_type &obj) { // body of extractor return stream; } Object Oriented Analysis and Design (CS 212)

11 Creating Own Manipulator Functions
It is possible to create custom manipulators Consolidate a sequence of several separate I/O operations into one manipulator I/O operations on a nonstandard device Demonstration Object Oriented Analysis and Design (CS 212)

12 Object Oriented Analysis and Design (CS 212)
Functionality Inherit the functionality of their base classes Methods for non-formatted writing and reading of single characters and/or data blocks The operators << or >> for formatted reading and writing from or to files Methods and manipulators for formatting character sequences Object Oriented Analysis and Design (CS 212)


Download ppt "CS212: Object Oriented Analysis and Design"

Similar presentations


Ads by Google