How to declare random namespace with example


1. How to declare namespace?

To declare a random namespace you will use the keyword namespace like this:

Namespace example
{
            // Code that I want to have in the namespace example…
}

The previous code will define the namespace with the name example. All name declaration in the code between the brace will be define within the example namespace, so to access any such name from a point outside the namespace, the name must be qualified by the namespace name, example, or have a using declaration that identifies that the name is from the example namespace.
You can’t declare the namespace inside a function. The namespace declaration isn’t intended to be used the other way around; you use a namespace to contain function, global variables, and other entities such as classes in your program. You must not put the definition main ( ) in namespace. The function main ( ) is where execution starts, and it must be at global namespace scope otherwise the compiler won’t recognize it.

1.1.  Example of declaring the namespace

Start the new project, select the Win32 Console Application. Under the project name type “DeclaringtheNamespace1.” After you type the name of the project, click Ok, and the Win32 Console Application Wizard window will appear. In this window just click Next and then Finish.
Type the following code:

// DeclaringtheNamesapce1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
namespace example
{
            int value = 5;
}
int _tmain(int argc, _TCHAR* argv[])
{
            cout << "Enter an integer (number):";
            cin >>  example::value;
            cout << "\nYou entered " << example::value  << endl;
            return 0;
}

In this program the example namespace defines a scope, and everything within the namespace scope is qualified with the namespace name. To refer to a name declared within a namespace from outside, you must qualify it with the namespace name. Inside the namespace scope, any of the names declared within it can be referred to without qualification. Now, you must qualify the name value with example, the name of your namespace. If not, the program will not compile. The function main ( ) now refers to names in two different namespaces, and in general, you can have as many namespaces in your program as you need. You could remove the need to qualify value by adding a using directive:

// DeclaringtheNamesapce1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
namespace example
{
            int value = 5;
}
using namespace example;
int _tmain(int argc, _TCHAR* argv[])
{
            cout << "Enter an integer (number):";
            cin >>  value;
            cout << "\nYou entered " << value << endl;
            return 0;
}

You could also have a using directive for std as well, so you wouldn’t need to qualify standard library names either, but as I said, this defeats the whole purpose of namespaces. Generally, if you use namespaces in your program, you should not add using directives all over your program; otherwise, you might as well not bother with namespaces in the first place. Having said that, I will add a using directive for std in some of our examples to keep the code less cluttered and easier for you to read. When you are starting out with a new programming language, you can do without clutter, no matter how useful it is in practice.
Share on Google Plus