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?

  • View
    217

  • Download
    1

Embed Size (px)

Text of Namespaces, I/O streams. Namespaces Namespace – what’s this?...

  • Slide 1
  • Namespaces, I/O streams
  • Slide 2
  • Namespaces Namespace whats this? Namespace whats this?namespace When do we need the namespace? When do we need the namespace?
  • Slide 3
  • Namespaces using namespace mine { void printf(int); class file; void fprintf(const file &, 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)
  • Slide 4
  • 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.
  • Slide 5
  • 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
  • Slide 6
  • 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&T developed by AT&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
  • Slide 7
  • 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 )
  • Slide 8
  • 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
  • Slide 9
  • IOStream library basics operators > operators > 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 >i; os >d; os >i>>d; cout >i>>d; cout
  • IOStream library details ios_base basic_streambuf basic_streambufbasic_ios virtual virtual basic_istream basic_ostream basic_iostream basic_iostream
  • Slide 12
  • 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
  • Slide 13
  • 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)
  • Slide 14
  • 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
  • Slide 15
  • 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
  • Slide 16
  • 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
  • Slide 17
  • 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)
  • Slide 18
  • 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>>x) // or: while(cin>>x) { // x read without an error } operator ! () // equivalent of fail() operator ! () // equivalent of fail() if ( !!(cin>>x)) // or: while( !!(cin>>x)) { // x read without an error }
  • Slide 19
  • 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
  • Slide 20
  • IOStream library details i/o formatted and unformatted i/o formatted and unformatted operators > by default do formatted i/o (specific precision, skipping white spaces etc.) operators > 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
  • Slide 21
  • 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
  • Slide 22
  • 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
  • Slide 23