Namespaces, I/O streams. Namespaces Namespace – what’s this? Namespace – what’s this?namespace When do we need the namespace? When do we need the namespace?

  • Published on
    22-Dec-2015

  • View
    213

  • Download
    1

Embed Size (px)

Transcript

<ul><li> Slide 1 </li> <li> Namespaces, I/O streams </li> <li> Slide 2 </li> <li> Namespaces Namespace whats this? Namespace whats this?namespace When do we need the namespace? When do we need the namespace? </li> <li> Slide 3 </li> <li> Namespaces using namespace mine { void printf(int); class file; void fprintf(const file &amp;, int); } mine::printf(44); mine::file infile; fprintf(infile, 31415323);// if at least one function argument comes from // the mine namespace, then the function // fprintf() is looked for in this namespace first // (Koenig search rule) </li> <li> Slide 4 </li> <li> Namespaces using Namespaces may be extended/joined Namespaces may be extended/joined in a single source file in a single source file in many files in many files Any given name (or all the names) may be moved from namespace scope to the global (or local) scope Any given name (or all the names) may be moved from namespace scope to the global (or local) scope using mine::file; using mine::file; using namespace mine; using namespace mine; Avoid using using in the header files! Avoid using using in the header files! why? why? use the scope operator instead. use the scope operator instead. </li> <li> Slide 5 </li> <li> Namespace std The C++ standard library is defined in the std namespace. The C++ standard library is defined in the std namespace. stream classes stream classes string classes string classes STL STL headers with.h and without.h headers with.h and without.h interesting fact: *.hpp and *.hxx are considered obsolete and non-standard interesting fact: *.hpp and *.hxx are considered obsolete and non-standard </li> <li> Slide 6 </li> <li> Standard C++ I/O streams IOStream library (iostream) IOStream library (iostream) its the oldest part of the standard library its the oldest part of the standard library developed by AT&amp;T developed by AT&amp;T support for internationalization support for internationalization string types supported by library string types supported by library char * (compatibility with earlier routines) char * (compatibility with earlier routines) string string iostream is a library of templates, iostream is a library of templates, iostream uses exception mechanism iostream uses exception mechanism </li> <li> Slide 7 </li> <li> IOStream library basics classes classes input stream istream (from template: basic_istream ) input stream istream (from template: basic_istream ) output stream ostream (from template: basic_ostream ) output stream ostream (from template: basic_ostream ) </li> <li> Slide 8 </li> <li> IOStream library basics objects (header file: ) objects (header file: ) istream cin; //stdin, buffered istream cin; //stdin, buffered ostream cout; //stdout, buffered ostream cout; //stdout, buffered ostream cerr; //stderr, not buffered ostream cerr; //stderr, not buffered //by default outputs to console ostream clog; //no C equivalent, buffered ostream clog; //no C equivalent, buffered //by default outputs to console //by default outputs to console </li> <li> Slide 9 </li> <li> IOStream library basics operators &gt; operators &gt; overloaded for fundamental types, including char *, void *, bool, overloaded for fundamental types, including char *, void *, bool, to be overloaded for classes requiring stream i/o to be overloaded for classes requiring stream i/o istream is; ostream os; int i; double d; os &gt;i; os &gt;d; os &gt;i&gt;&gt;d; cout &gt;i&gt;&gt;d; cout </li> <li> IOStream library details ios_base basic_streambuf basic_streambufbasic_ios virtual virtual basic_istream basic_ostream basic_iostream basic_iostream </li> <li> Slide 12 </li> <li> IOStream library details ios_base common interface for stream classes independent on the class/type of the stream elements common interface for stream classes independent on the class/type of the stream elements current condition of the stream current condition of the stream formatting of the data being processed formatting of the data being processed </li> <li> Slide 13 </li> <li> IOStream library details basic_ios common interface for stream classes in extent dependent on the class/type of the stream elements common interface for stream classes in extent dependent on the class/type of the stream elements definition of the stream buffer (class derived from template basic_streambuf for specific class/type of the stream element, definitions of methods actually reading/writting data) definition of the stream buffer (class derived from template basic_streambuf for specific class/type of the stream element, definitions of methods actually reading/writting data) </li> <li> Slide 14 </li> <li> IOStream library details basic_istreambasic_ostream templates for classes of read-only/write-only streams templates for classes of read-only/write-only streams they inherit virtually template basic_ios they inherit virtually template basic_ios for the char template argument theyre derived from istream i ostream respectively for the char template argument theyre derived from istream i ostream respectively headers: and headers: and </li> <li> Slide 15 </li> <li> IOStream library details basic_iostream templates of stream classes capable of reading and writting templates of stream classes capable of reading and writting header header </li> <li> Slide 16 </li> <li> IOStream library details result of stream operations result of stream operations status of the stream (within scope of ios_base, constants of type iostate) status of the stream (within scope of ios_base, constants of type iostate) goodbit// everything ok goodbit// everything ok // bits described below are zeroes // bits described below are zeroes eofbit// end of file reached eofbit// end of file reached failbit// last operation failed failbit// last operation failed // following operations will fail // following operations will fail // until failbit gets set to 0 // until failbit gets set to 0 badbit// failure; stream destroyed badbit// failure; stream destroyed </li> <li> Slide 17 </li> <li> IOStream library details result of stream operations - methods result of stream operations - methods bool good()// everything ok bool good()// everything ok bool eof() // end of file reached bool eof() // end of file reached bool fail() // last operation failed bool fail() // last operation failed bool bad() // failure; stream destroyed bool bad() // failure; stream destroyed rdstate()// read the stream state rdstate()// read the stream state clear()// set the state to goodbit clear()// set the state to goodbit clear(state)// set state to state clear(state)// set state to state setstate(state)//equivalent of setstate(state)//equivalent of // clear(rdstate() | state) </li> <li> Slide 18 </li> <li> IOStream library details result of stream operations conversion operators result of stream operations conversion operators operator void* ()// equivalent of !fail() operator void* ()// equivalent of !fail() if (cin&gt;&gt;x) // or: while(cin&gt;&gt;x) { // x read without an error } operator ! () // equivalent of fail() operator ! () // equivalent of fail() if ( !!(cin&gt;&gt;x)) // or: while( !!(cin&gt;&gt;x)) { // x read without an error } </li> <li> Slide 19 </li> <li> IOStream library details result of stream operations exceptions result of stream operations exceptions to define when to throw exceptions (method of stream class): to define when to throw exceptions (method of stream class):exceptions(flags) to check what may cause throwing exceptions (method of stream class): to check what may cause throwing exceptions (method of stream class):exceptions() if it returns goodbit, then exceptions are never thrown </li> <li> Slide 20 </li> <li> IOStream library details i/o formatted and unformatted i/o formatted and unformatted operators &gt; by default do formatted i/o (specific precision, skipping white spaces etc.) operators &gt; by default do formatted i/o (specific precision, skipping white spaces etc.) methods get*/put*/read*/write* etc. are for unformatted i/o only methods get*/put*/read*/write* etc. are for unformatted i/o only </li> <li> Slide 21 </li> <li> IOStream library details formatted i/o formatted i/o methods for format flags (ios::fmtflags) methods for format flags (ios::fmtflags) setf(flags);// set flags setf(flags);// set flags setiosflags(flags);// equivalent to setf(flags); setiosflags(flags);// equivalent to setf(flags); setf(flags, mask);// set flags within the mask group only setf(flags, mask);// set flags within the mask group only resetiosflags(mask);// equivalent to setiosflags(0, mask); resetiosflags(mask);// equivalent to setiosflags(0, mask); unsetf(flags);// clear flags unsetf(flags);// clear flags flags();// retrn flags currently set flags();// retrn flags currently set flags(flags);// set flags, clear all the remaining flags(flags);// set flags, clear all the remaining copyfmt(stream)// copy flags from stream copyfmt(stream)// copy flags from stream </li> <li> Slide 22 </li> <li> IOStream library details formatted i/o formatted i/o flags and masks flags and masks boolalpha// flag: type bool numeric (0/1) or descriptive boolalpha// flag: type bool numeric (0/1) or descriptive // true/false // true/false // there are also manipulators: boolalpha and noboolalpha adjustfield// mask: alignment (left, right, internal) adjustfield// mask: alignment (left, right, internal) left// flag: align to left left// flag: align to left right// flag: align to right right// flag: align to right internal// flag: for digits align sign to left internal// flag: for digits align sign to left // remaining characters to right // remaining characters to right // there are also manipulators: left, right and internal </li> <li> Slide 23 </li> <li> IOStream library details formatted i/o formatted i/o flags and masks flags and masks showpos// output + for positive numbers showpos// output + for positive numbers // there are also manipulators: showpos and noshowpos uppercase// print (upper case) hexadecimal digits uppercase// print (upper case) hexadecimal digits // there are also manipulators: uppercase and nouppercase basefield// mask: for bases of numerical system (see below) basefield// mask: for bases of numerical system (see below) dec, hex oct// flags for different numerical systems dec, hex oct// flags for different numerical systems (empty)// flag: output as dec, input according to (empty)// flag: output as dec, input according to // actual prefix 0x hex, 0 oct, else: dec // actual prefix 0x hex, 0 oct, else: dec // there are also manipulators: dec hex and oct </li> <li> Slide 24 </li> <li> IOStream library details formatted i/o formatted i/o flags and masks flags and masks showbase// precede numbers with prefix of numerical system showbase// precede numbers with prefix of numerical system // there are also manipulators: showbase and noshowbase floatfield// mask: format of the floating point floatfield// mask: format of the floating point fixed// decimal fraction (eg. 3.14159265323) fixed// decimal fraction (eg. 3.14159265323) scientific// use exponent (eg. 3.14e+0) scientific// use exponent (eg. 3.14e+0) (empty)// pick best one depending on value being oputput (empty)// pick best one depending on value being oputput // there are also manipulators: fixed and scientific showpoint// always output the dot (3. 1415) showpoint// always output the dot (3. 1415) // for the floating point // for the floating point // there are also manipulators: showpoint and noshowpoint </li> <li> Slide 25 </li> <li> IOStream library details formatted i/o formatted i/o precision()// return the precision precision()// return the precision // for the floating point, // by default 6 digits after dot precision(p)// return the precision precision(p)// return the precision // for the floating point // there is also manipulator: setprecision(w) // there is also manipulator: setprecision(w) </li> <li> Slide 26 </li> <li> IOStream library details formatted i/o formatted i/o flags flags skipws// skip white spaces skipws// skip white spaces // there are also manipulators: skipws and noskipws unitbuf// flush the output buffer unitbuf// flush the output buffer // after each </li> <li> IOStream library details formatted i/o formatted i/o width()// return width of the filed (number of characters) width()// return width of the filed (number of characters) // interpreted as: not less than // interpreted as: not less than width(w)// set the width of the filed (number of characters) width(w)// set the width of the filed (number of characters) // interpreted as: not less than // interpreted as: not less than // there is also manipulator: setw(w) char buf[80]; cin &gt;&gt; setw(sizeof(buffer)) &gt;&gt; buffer; fill() // return character that is used for filling the field fill() // return character that is used for filling the field fill(c) // set this character fill(c) // set this character // there is also manipulator: setfill(c) </li> <li> Slide 28 </li> <li> IOStream library details formatted i/o formatted i/o methods for internationalization, different character sets, etc. methods for internationalization, different character sets, etc. described in detail in the library reference ;-) described in detail in the library reference ;-) </li> <li> Slide 29 </li> <li> IOStream library details unformatted i/o unformatted i/o method get method get int get();// read one character, or EOF int get();// read one character, or EOF // equivalent to getchar()/getc() // equivalent to getchar()/getc() istream&amp; get(char &amp;c);// no EOF, instead test the stream state istream&amp; get(char &amp;c);// no EOF, instead test the stream state istream&amp; get(char *pc, streamsize cnt) istream&amp; get(char *pc, streamsize cnt) istream&amp; get(char *pc, streamsize cnt, char delim) istream&amp; get(char *pc, streamsize cnt, char delim) // read to pc buffer, stop after having read // read to pc buffer, stop after having read // cnt-1 characters, or after (just before) the delim character // cnt-1 characters, or after (just before) the delim character // do not input delim, append \0 // do not input delim, append \0 istream&amp; ignore(streamsize cnt, char delim) istream&amp; ignore(streamsize cnt, char delim) // couple variants, read, but do not store // couple variants, read, but do not store </li> <li> Slide 30 </li> <li> IOStream library details unformatted i/o unformatted i/o method getline method getline istream&amp; getline(char *pc, streamsize cnt) istream&amp; getline(char *pc, streamsize cnt) istream&amp; getline(char *pc, streamsize cnt, char delim) istream&amp; getline(char *pc, streamsize cnt, char delim) // read to pc buffer, stop after having read // cnt-1 characters, or after (just before) the delim // cnt-1 characters, or after (just before) the delim // or after reading end of line // delim is being read from the stream // if number of read characters is less than cnt // then set failbit state </li> <li> Slide 31 </li> <li> IOStream library details unformatted i/o unformatted i/o methods read and readsome some aspects methods read and readsome some aspects istream&amp; read(char *pc, streamsize cnt) istrea...</li></ul>