bài giảng chú giải trình hướng đối tượng bài 5

29 304 0
bài giảng chú giải trình hướng đối tượng bài 5

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Unit-V Streams  The stream is an object flowing from one place to another.  In c++,a stream is used to refer to the flow of data from a particular device to the program’s variable.  The device refer to files,keyborad,memmory arrays,console and so on.  A stream is a series of bytes,which act either as a source from which input data can be extracted or destination to which the output can be sent.  Predefined Console Stream are  cin  cout  cerr  clog Formatted I/O C++ supports a wide variety of features to perform input or output in different formats.They include the following,  ios stream class member functions and flags  Standard manipulators  User-defined manipulators. ios Class Function and Flags: The functions are  width()  precision()  fill()  setf()  unsetf() continue  width() Specifies the required number of fields to be used while displaying the output value. precision() Specifies the number of digits to be displayed after the decimal point. fill() Specifies a character to be used to fill the unused area of a field.By default,fill blank space character. unsetf() Clears the specified flag setf() Sets format flag that control the form of output display. Formatting with flags and bit fields  The member function of the ios class,setf() is used to set flags and bit fields that control the output.  It has the following two forms: 1.long setf(long _setbits,long _field); 2.long setf(long _setbits); where, _setbits is one of the flags defined in the class ios. _fields specifies the group to which the formattong flag belongs.  The flag and bit field table as Flags value Bit field Effect produced ios::left ios::adjustified Left-jusitified ios::scientific ios::floatfield Use exponential floating notation ios::dec ios::basefield Decimal conversion Manipulators  Manipultors are special functions that are specially designed to modify the working of a stream.  Manipulators are broadly categorized into two groups namely  Proceducer is the one which generates output on an output stream  Consumer consumes input from an input stream  All the predefined manipulators are defined in the header file iomanip.h  Predefined Manipulators are of two types namely  Non-parameterized manipulators  Parameterized manipulators  Non-Parameterized manipulator are  dec sets the conversion base to 10  hex sets the conversion base to 16  oct sets the conversion base to 8  ws White space  endl Outputs anewline and flushes stream  ends Outputs a NULL character  flush Flushes the stream. continue  Predefined parameterized manipulators are 1.setw(int width) Sets the field width 2.setprecision(int prec) Sets the floating-point precision 3.setfill(int fchar) sets the fill character 4.setbase(int base) Sets the conversion base 5.setiosflags(long flags) Sets the format flag 6.resetiosflags(long flags) reset the format flag  Custom/User defined manipulators: Users can design their own manipulators to control the appearance of the output depending upon their taste and need. Syntax: ostream & manipulator(ostream &output,arguments_if_any) { ……… ……… return output; } File  A file is a collection of related information defined by its creator.  There are three classes for handling files  ifstream for handling input files  ofstream for handling output files.  fstream for handling files on which both input and output ca be performed  It is necessary to include the header file fstream.h  Manipulation of a file involves the following steps  Name the file on the disk  Open the file to get the file pointer  Process the file(read/write)  Check for error while processing  Closethe file after its complete usage File Modes  C++ provides a mechanism of openning a file in different modes in which case the second parameter must be explicitly passed.the syntax is as follows stream-object.open(“filename”,mode);  List of modes: Mode value Effect on the mode ios::in ios::out ios::ate ios::app ios::trun ios::nocreate ios::noreplace ios::binary Open for reading Open for writing Go to the end of file at opening time Append mode Truncate the ile if it already exists. Open fails if file does not exist. Open fails if file already exists. Open as a binary file. Random Access to a File #include<iostream.h> #include<fstream.h> #define RED 6 Void main() { char reader[RED+1]; fstream fstr(“rand.txt”,ios::binary|ios::in|ios::out); for(int i=0;i<10;i++) fstr<<I; fstr.seekp(2); fstr<<“Rajalakshmi”; fstr.seekg(4); fstr.read(reader,RED); reader[RED]=0; cout<<reader<,endl; } [...]... enabling element iteration and access functionality in a common and general interface  Used as arguments to algorithms  Containers provide several iterators  Begin, End, Reverse Begin, Reverse End  5 categories – each has specific operations  Input, Output, Forward, Bidirectional, Random Iterators  Iterators are similar to pointers  point to element in a container  iterator operators uniform... iterators enables writing generic algorithms which are applicable to any container class that provides a standard iterator More about STL  B Stroustrup, The C++ Programming Language  Part III (427- 657 )  STL examples  http://www.josuttis.com/libbook/toc.html  STL tutorials  just hit Google with "STL Tutorial" . precision 3.setfill(int fchar) sets the fill character 4.setbase(int base) Sets the conversion base 5. setiosflags(long flags) Sets the format flag 6.resetiosflags(long flags) reset the format flag  Custom/User

Ngày đăng: 24/10/2014, 16:24

Từ khóa liên quan

Mục lục

  • Unit-V

  • Streams

  • Formatted I/O

  • continue

  • Formatting with flags and bit fields

  • Manipulators

  • Slide 7

  • File

  • File Modes

  • Random Access to a File

  • Object Serialization

  • Namespaces

  • Standard template Library

  • STL – Why?

  • STL Overview

  • Containers – Overview

  • Slide 17

  • Sequence Containers

  • Associative Containers

  • Containers Common Functionality

Tài liệu cùng người dùng

Tài liệu liên quan