Learn C++ Namespace with Syntax and Example in just 4 Mins!

Every C++ program contains namespace. If it is not used, an alternative is available which proves to be more complex than namespaces. It is a relatively new concept introduced by the latest C++ standards. It is evident that namespaces in C++ are of immense significance as it is an integral part of each and every C++ program.

Introduction to C++ Namespace

Before we start our article, it is important to have strong command on C basic Syntax

C++ Namespace with Example

Let us consider the situation. There are 30 students in your classroom and out of 30 students, suppose there are 3 students with the same first name. If we simply use their first name to refer them, it is pretty obvious that some confusion would be created. To avoid this problem, we refer them by their unique feature, like their personal id. Similarly, we use namespaces to uniquely identify different functions that go by the same name.

For instance, the keyword “class” connotes a different meaning when implemented as classes and objects and mean an entirely different thing when used while defining a template. This key difference is highlighted by namespaces.

In programming terminology, namespaces act as a declarative region that provides scope to the identifiers used in the program such as functions and variables.

We use namespaces to organize the code into a logical collection in order to prevent name collisions. These collisions may occur when our base code contains multiple libraries.

Explore the concept of  C++ Class and Object

Syntax of C++ Namespace

The basic statement of using namespaces is:

using namespace std;

So far, we have seen the need to include namespaces in our program.

But, the question arises- Is there any alternative way of achieving the same purpose without the use of the namespace statement?

Surprisingly, the answer is – yes!

But this alternative method proves to be a bit complicated as compared to simply using namespaces in C++.

This is how your code segment would look without the inclusion of namespaces:

int x = 10; 
namespace test
{
int x = 20;
void function() 
{ 
std::cout << x << std::endl; // Prints20
std::cout << ::x << std::endl; // Prints 10
} 
}

Here, “::” refers to the global namespace.

Here is a C++ program that illustrates the use of namespaces in C++:

#include <iostream> 
using namespace std; 

namespace data 
{ 
int val = 30; 
} 

int val = 10; // Global variable initialization

int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

int val = 20; // Local variable initialization
cout << data::val << endl; 

return 0; 
}

Code-

How to use Namespaces in C++

Output-

Output of Namespaces in C++

Rules for C++ Namespace

  1.  It is mandatory to either provide a global scope for the namespace definition or nest it within another namespace.
  2.  Be sure not to terminate the namespace definition with a semicolon.
  3. C++ gives you the provision to choose an alias name for the namespace you want to create in order to simplify things for you. If you do not wish to provide any name to the namespace you’re creating, you may leave it unnamed.
  4. C++ prohibits you to create an instance of the namespace.
  5. We can extend the namespace definition over multiple files without having to worry about the files being overridden or redefined.

To run the above code in C++, install g++ compiler on Linux

C++ Discontiguous Namespace

C++ gives you the provision to define namespaces in various parts of the program that can be spread over multiple files. This is called as a discontiguous namespace. After all, the entire namespace is considered as the sum of its separately defined parts.

The “using” Directive

The “using” directive instructs the compiler that the subsequent code uses names in the specified namespace.

Here is a C++ program that illustrates the use of the “using” directive:

Join DataFlair on Telegram
#include <iostream>
using namespace std;

namespace n1 
{
void function() 
{
cout << "Namespace n1" << endl;
}
}

namespace n2 
{
void function() 
{
cout << "Namespace n2" << endl;
}
}

using namespace n1;
int main () 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

function();

return 0;
}

Code-

Example of using directive

Output-

Using directives in C++

We can use the ‘using’ keyword to refer to a specific item within a namespace.

For instance, if you want to use the “cout” function, you can use the std global namespace.

using std::cout;

Get a clear understanding of C++ Functions with examples

Nested Namespace in C++

Just like a nesting of functions, loops, classes, and structures exist, C++ gives us the provision to nest namespaces as well.

The basic syntax of nesting a namespace n2 in n1 is as follows:

namespace n1
{
// BODY OF THE NAMESPACE n1
namespace n2
{
// BODY OF THE NAMESPACE n2
}
}

In order to access these namespaces, we may write the following statements:

// Accessing the members of n2

using namespace n1 :: n2;

// Accessing the members of n1

using namespace n1

Here is a C++ program that illustrates the use of nested namespaces:

#include <iostream>
using namespace std;

namespace n1 
{
void function() 
{
cout << "Namespace n1" << endl;
}

namespace n2 
{
void function() 
{
cout << "Namespace n2" << endl;
}
}
}

using namespace n1::n2;
int main () 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

function();

return 0;
}

Code-

Example of Nested Namespace in C++

Output-

Output of Example of Nested Namespace in C++

Summary

Namespaces in C++ is basically a small concept but it has a significance in coding. Now, you know why we work with “using” directory in C++. All the topics we covered today are important for a successful programmer.

Get a Complete guide to learn Tokens in C++ (Keywords, Identifiers, Constants, Strings, Operators, Special Symbols)

If you have any queries, feel free to leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.