A closer look into Namespaces

Why we need Namespaces?
Namespaces were introduced to avoid name collisions. For example if you created a function called abs( ) in your program, then it may over-ride the standard library function abs ( ), because both have the same name.
Similarly, sometimes you might make use of libraries made by other programmers. You may include two such libraries in your program. It is possible that the name of a function in one of those libraries will collide with a name in the other library. For example, both libraries may have the function draw( ). But both may have a different meaning to the function. So when you use the function draw ( ), the compiler would have a problem in deciding which library function it should use. Namespaces were created to solve these problems.

What namespace does?
Namespace localizes the visibility of names declared within it (the names are only visible within that particular namespace). So when you refer to some name, you can specify the namespace to which it belongs to avoid conflicts.

Syntax :

General form of a namespace is :
namespace name
{
.....// declarations
}
Anything defined within a namespace is only within the scope of the namespace and not available outside.

Example:

#include <iostream>
using namespace std;
namespace counterspace    
                                     // Creating a namespace 
{
    int increment; 
                                                        // increment is visible within entire namespace
                class counter                                             // class counter within namespace
                    {
                        private :
                                    int count;
                        public :
                                    counter (int a) : count(a)      
       // single argument constructor
                                    { }
                                    void display ( )
                                        {
                                            cout<<endl<<"The new count is "<<count + increment;
                                         }
                     };
}                                                         
        // end of namespace
int main ( )
{

counterspace :: increment = 5;                         // referring to increment variable of namespace
counterspace :: counter ob1(20);                     // creating object belonging to class counter which belongs to namespace
ob1.display( );
return 0;
}

I think the above example is clear. I've created a namespace called counterspace. It contains a variable called increment and also a class called counter.
Within the main function, when we want to refer to the variable increment remember the following:

increment is only visible within the namespace counterspace. So when referring to it, you have to use the scope resolution operator to access it.
Similarly when creating an object belonging to the class counter, remember :

The class counter is present within the namespace. So you have to specify the namespace and then the scope resolution operator.
But once the object has been created, you needn't keep specifying the namespace.

Next Section is on : Making use of the using keyword

or Go back to the Contents 2.