C++: namespace

Author: Ronald S. Holland
at
Total Application Works

ronholland@sumtotalz.com





To contact us



HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript ]

© 2002 - 2016 All Rights Reserved Total Application Works


  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
    <html>
    <head>
    <title>Example of a link </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/CPP/CPP_Namespaces.html">
    Programming Tutorials III </a>
    </table>
    </body>
    </html>


Introduction

In this tutorial, we will explore the concept of a namespace. What is a namespace? When you think of an array, you think of a block of contiguous memory that can be accessed by an identifier and an index. With a namespace, you use the scope resolution (::) operator to access those items declared within the namespace's scope. Instead of an identifier/name and an index that is used with an array, you use a namespace identifier and the name of an entity declared within the namespace's scope.. Consider the following example:

Definition and Creation Variable int Array float Array double Array
  
    int a = 5 ;

    int x[] ;
    x = new int[5] ;

    float y[] ;
    y = new float[5] ;

    double xx[] ;
    xx = new double[5] ;
  
   
| 5 |
+---+
  
| 5 |   <-- index 0
+---+
| 5 |   <-- index 1
+---+
| 5 |   <-- index 2
+---+
| 5 |   <-- index 3
+---+
| 5 |   <-- index 4
+---+
   
   
| 5 |   <-- index 0
+---+
| 5 |   <-- index 1
+---+
| 5 |   <-- index 2
+---+
| 5 |   <-- index 3
+---+
| 5 |   <-- index 4
+---+
   
   
| 5 |   <-- index 0
+---+
| 5 |   <-- index 1
+---+
| 5 |   <-- index 2
+---+
| 5 |   <-- index 3
+---+
| 5 |   <-- index 4
+---+
   
Figure 1: Array Definition and Creation


There are a few points that are being made by the depictions in Figure 1. Each array shown above:

  1. Can only contain one data type;
  2. Are of a fixed length as defined;
  3. Can be referenced by a variable name;
  4. Depict data that is stored in contiguous memory locations;
  5. Depict data that is stored and referenced in sequential order.
For those who have programming experience, Figure 1 does not provide any new news. Why am I using arrays in this tutorial on namespaces? Arrays allow the use of a single name and index to access multiple elements. Likewise, when you use a namespace identifier, you can access multiple entities. For example, the general form of a namespace is:

 
namespace indentifier { 
   // namespace body 
   entity_1
   entity_2
   entity_3
   ...
   entity_n
}
 
Figure 2: General form of a namespace


In the example depicted in Figure 2, you access entity_3 by using the following format:


indentifier::entity_3.

So what is the point? The point is that by using namespaces, the risk of name collisions is reduced. In the real world, name collisions is a common problem. Consider the following composition example. Composition allows you to build more complex objects from simple object components. A car is built from a collection/composition of objects, such as wheels, engines, bumper, etc. You design your program as an application object and construct it from other objects, using composition.

 
public class MyCar {

   SteeringWheel stwheel ;
   SteelBeltedTire sbTire ;
   Bumper bumper ;
   Windshield wShield ;
   FuelInjection fuelInj ;
   Brake brake ;

   ...

   public MyCar() {
      addComponents() ;
   }

   /** ******************************************
    *  On an assembley line, this is where the 
    *  car will be built.
    *  
    ********************************************/
   public addComponents() {
      ...
   }

   public static void main(String args[]) {
      ...
   }
   
}
Figure 3: MyCar built through composition


If MyCar is a Toyota Highlander or an Acura MDX, I can't just order brakes. When I order brakes, I order


 
2001::Toyota::Highlander::engine size::brakes 
 
Figure 4: Brakes for MyCar


This ensures that I get the brakes that are designated for the model MyCar. To see this qualifying in practice, try ordering brakes form online Advance Auto Parts. You will see an image similar to the following:

Figure 5: Brakes for MyCar at Advance Auto Parts


When you go to an auto parts store, this is how you find the correct wiper blades in the wiper blades catalog. Wiper blades without proper qualifying may lead to the incorrect wiper blades being purchased. In the latter case, we have an example of name collision.

When you consider Figure 3: MyCar built through composition, specific components are gathered, identified, using a process similar to that in Figure 4, and assembled to build a 2010 or 2011 automobile. When you build you program, you are building with user defined, class libraries and/or 3rd party supplied components. What components do we use from a c++ class libraries?

Entities we use from class libraries

Namespaces are a useful way of maintaining logical groupings in class libraries and encapsulating their components/functionality. All the files in the C++ standard library declare all of its entities within the std namespace. Some examples of using namespaces are:

 
using namespace std;

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
 
Figure 6: Using namespaces


You can then refer to functions that are part of a namespace by prefixing the function with the namespace name followed by the scope operator (:: ). You will find some of the headers in the stdlib listed in the next section.

Entities we use from stdlib class libraries

You will find other stdlib headers in the following Figure 7: table.

<algorithm> - Defines Standard Template Library (STL) container template
functions that perform algorithms.
<bitset> <cassert>
<cctype> <cerrno> <cfloat>
<ciso646> <climits> <clocale>
<cmath> <complex> <csetjmp>
<csignal> <cstdarg> <cstddef>
<cstdio> <cstdlib> <cstring>
<ctime> <cwchar> <cwctype>
<deque> <exception> <fstream>
<functional> <hash_map> <hash_set>
<iomanip> <ios> <iosfwd>
<iostream> <iso646.h> <istream>
<iterator> <limits> <list>
<locale> <map> <memory>
<new> <numeric> <ostream>
<queue> <set> <sstream>
<stack> <stdexcept> <streambuf>
<string> <strstream> <utility>
<valarray> <vector>
Figure 7: Other stdlib headers


For a complete description of these headers, refer to MS Visual Studio Documentation (help index) --> cstdlib --> Standard C++ Library Overview.






HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript ]


To contact us

© 2002 - 2016 All Rights Reserved Total Application Works