C++ Standard Library

- C++ began a standardization process in 1989 to define exact content of the language
- Included the development of a library which became known as the C++ standard library

- Extends the core language to provide powerful new features including

-       Templates

-       Namespaces

-       New bool variable type

-       Standard Template Library (STL)

-       Header file conventions

-       Error handling and allocators

-       string and C++ stream classes

 

 

 

Namespaces

- More and more software is written as libraries, modules, or components
- Combining user code with such software could result in a problem
- What if you named your class the same as a class contained in a library?
- What if you named a function the same as a function contained in a library?
- The names of your classes or functions would clash with names in the library

- Included in the standard library is a feature known as namespaces to solve this problem
- Namespaces solves the problem by grouping names (identifiers) in a named scope
- Names in a namespace scope will not conflict with names in other scopes

- In the example below, a class is designed with the same name as a library class

- It is not clear which string class to use (the one in the program or the library)

 

#include <iostream>

using namespace std;

 

class string

{

public:

  string() { std::cout << "My string" << std::endl; }

};

 

main()

{

  string s;

}

 

Output

ns4.C: In function 'int main()':

ns4.C:13: error: reference to 'string' is ambiguous

ns4.C:6: error: candidates are: class string

 

    - Knowing that string is used in the library, we should have never designed a class with that name

    - Although this might be true, what about the class names in the library we don’t know about?

    - What if, in addition, we were linking with third-party libraries containing all kinds of other names?

    - Must a programmer be forced to be aware of all available names in use so as not to clash?

    - By using namespaces, a programmer is relieved from having to keep track of all names in use

    - Let’s examine how we can create a namespace to avoid clashing in the example above

    - Consider, for example, if we wanted this program to use the string class from the library?

    - We need a way to make a distinction between the two different string classes

    - We create this distinction by creating a namespace and grouping names within its scope

 

#include <iostream>

using namespace std;

 

namespace MySpace

{

  class string;

}

 

class MySpace::string

{

public:

  string() { std::cout << "My string" << std::endl; }

};

 

    - Names in the program that might clash with others are grouped in a namespace

    - In this example, we’ve named our scope MySpace although any name will do

 

namespace MySpace

{

  class string;

}

 

    - The elements are identified with the namespace using the scoping operator in the program

 

class MySpace::string

 

    - Elements from specific namespaces are distinguished in a program in one of 3 different ways

    - The first method involves specifying a using directive to inform the compiler which names to use

    - For example, consider the following directive

 

using namespace std;

 

    - This informs the compiler to use all names in the namespace that is named std

    - The program will now use the string class from the standard library (std) as in the example below

 

namespace MySpace

{

  class string;

}

 

#include <iostream>

using namespace std;

 

class MySpace::string

{

public:

  string() { std::cout << "My string" << std::endl; }

};

 

main()

{

  string s;

}

   

    - To use the string from our string class, we simply change the using directive in our example above

 

using namespace MySpace;

 

    - A second method of distinction is to specify only certain names in a namespace

    - Using the scoping operator we can instruct the compiler which name to use in a namespace

 

namespace MySpace

{

  class string;

}

 

#include <iostream>

using MySpace::string;

 

class MySpace::string

{

public:

  string() { std::cout << "My string" << std::endl; }

};

 

main()

{

  string s;

}

 

    - In this example, only the string name from the MySpace namespace is to be used

 

using MySpace::string;

 

    - Switching out the namespace, we could use the string class from the std

 

namespace MySpace

{

  class string;

}

 

#include <iostream>

using std::string;

 

class MySpace::string

{

public:

  string() { std::cout << "My string" << std::endl; }

};

 

main()

{

  string s;

}

 

    - A third method of distinction is to associate a namespace upon declaration in a program

    - This method uses the scoping operator to associate namespaces with the actual names

 

namespace MySpace

{

  class string;

}

 

#include <iostream>

 

class MySpace::string

{

public:

  string() { std::cout << "My string" << std::endl; }

};

 

main()

{

  MySpace::string s1;

  std::string s2;

}

 

    - This provides a way to use the same name from different namespaces in a program

    - s1 is an object from the string class in the program and s2 is a std string

    - Namespaces can include multiple names as in the following example

    - Names can include variable, function, or class names

 

#include <iostream>

 

// Use std names for I/O

using namespace std;

 

// Identifiers under named space

namespace MySpace
{

  class Test;

  void print();

}

 

// Associate class with namespace

class MySpace::Test

{

public:

  Test() { cout << "Test object" << endl; }

};

 

// Associate function with namespace

void MySpace::print()

{

  cout << "Hello world" << endl;

}

 

main()

{

  // Explicit use of namespace identifiers

  MySpace::Test t;

  MySpace::print();
}

 

Output

Test object

Hello world

 

 

    - We could have also employed the using directive to accomplish the same result

 

#include <iostream>

 

// Use std names for I/O

using namespace std;

 

// Identifiers under named space

namespace MySpace
{

  class Test;

  void print();

}

 

// Associate class with namespace

class MySpace::Test

{

public:

  Test() { cout << "Test object" << endl; }

};

 

// Associate function with namespace

void MySpace::print()

{

  cout << "Hello world" << endl;

}

 

// Using directive

using MySpace::Test;

using MySpace::print;

 

main()

{

  Test t;

  print();

}

 

Output

Test object
Hello world

 

 

    - Finally, all names are distinguished with the using namespace directive

 

#include <iostream>

 

// Use std names for I/O

using namespace std;

 

// Identifiers under named space

namespace MySpace
{

  class Test;

  void print();

}

 

// Associate class with namespace

class MySpace::Test

{

public:

  Test() { cout << "Test object" << endl; }

};

 

// Associate function with namespace

void MySpace::print()

{

  cout << "Hello world" << endl;

}

 

// Using namespace

using namespace MySpace;

 

main()

{

  Test t;

  print();

}

 

Output

Test object
Hello world

 

 

    - A third example demonstrating the using directive with multiple namespaces

    - Which print method from which namespace will be called in the example below?

 

#include <iostream>

 

using namespace std;

 

namespace ns1

{

  void print();

}

 

namespace ns2

{

  void print();

}

 

void ns1::print()

{

  cout << "Print from namespace ns1" << endl;

}

 

void ns2::print()

{

  cout << "Print from namespace ns2" << endl;

}

 

using ns2::print;

 

main()

{

  print();

}

 

Output
Print from namespace ns2

 

 

 

    bool Type

    - Oftentimes a program variable is needed to act as a simple switch or flag

     - The value needs to be either on or off, 1 or 0, true or false

    - The standard library introduced a new Boolean type, bool, to support this behavior

    - Boolean variables store one of two values, true or false

    - bool variables also provide automatic type conversion

- 0 is equivalent to false

- any other value is equivalent to true

 

#include <iostream>

 

using namespace std;

 

main()

{

  bool flag1 = true;

  bool flag2 = false;

  bool flag3 = 77;

  bool flag4 = 0;

 

  cout << "flag1: "  << flag1 << endl;

  cout << "flag2: "  << flag2 << endl;

  cout << "flag3: "  << flag3 << endl;

  cout << "flag4: "  << flag4 << endl;

}

Output

flag1: 1

flag2: 0

flag3: 1

flag4: 0

 

 

 

 

    Header File Convention
     
- Several extensions were being used before the standard (.h, .hpp, .hxx, .H)
     
- Standard defined headers as no longer requiring extensions in include statements 
     
- Note this requirement for no extension applies only to standard header files
     
- Still required for user-designed class header files

 

#include <iostream>
#include <string>

 

 

 

 

    General features

     Error and exception handling
        - Organized method of handling errors or unexpected situations
        - Dealing with errors outside of where they actually occur
        - Makes easier to read code ("unpolluted")
        - Library provides specialized classes for handling exceptions

     Allocators
        - Special objects to handle the allocation and deallocation of memory
        - Different memory models (shared memory, garbage collection, ...)
        - Interface to different memory models remains the same
 

 


  
Standard Template Library (STL)
   
- Subcomponent of the C++ standard library
   
- Influenced the overall architecture of the standard library
   
- Provides solutions to managing collections of data with modern/efficient algorithms
   
- Programmers benefit from innovations in area of data structures and algorithms
   
- Programmers don't necessarily have to know how they work, just how to use them
   
- All components of the STL are templates, can be used for arbitrary types
   
- New level of abstraction for the C++ programmer
   
- STL components include...

Containers
    - different methods of managing collections of objects

Iterators
    - used to step through elements of collections of objects

Algorithms
    - used to process the elements of collections
    - search, sort, modify

      - These components are extremely useful for employing a variety of data structure methods
      - As we’ll see, the STL provides the C++ developer with a rich set of data processing tools