Skip to main content

5. Namespaces and introduction to standard class library

16/02/23

Namespaces and scoping

  • Namespaces are used to avoid name conflicts
  • To avoid needing to keep saying <namespace>:: you can use using namespace >namespace
  • The standard class library is in the std namespace
namespace <NamespaceName>
{
//Insert code
}

Scoping Operator

  • Can use the scoping operator to call global functions or access global variables.
    • Use :: with nothing before it
  • Also used to denote that a function is a class member in a definition
  • Left of scoping operator is
    • blank - to access a global variable/function
    • class name - to access member of that class
    • namespace name - to use that namespace

Standard class library classes

  • String class is in the std namespace
  • Can be accessed as std::string
  • string() - Default empty string
  • string(const char* str); - From a char* type string
  • string(const string& str); - From another string - copy the constructor

Streams

  • C++ input/output classes use streams
  • istream cin; = stdin
  • ostream cout; = stdout
  • ostream cerr; = stderr
  • endl sent to a stream will output \n and flush
  • >> and << operators overloaded for input/output

File access

ifstream object - open the file for input ofstream object - open the file for output fstream object - specify what to open file for Use the << and >> to read/write

Standard Template Library

  • These are template classes.
    • vector
    • string
    • map
    • list
    • set
    • stack
    • queue
    • deque
    • multimap
    • multiset