×

Search anything:

Namespace in C++

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

Reading time: 30 minutes

Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope. This allows organizing the elements of programs into different logical scopes referred to by names.

1)Namespace is a feature added in C++ and not present in C.

2)A namespace is a declarative region that provides a scope to the identifiers (names of the types, function, variables etc) inside it.

3)Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.

A namespace definition begins with the keyword namespace followed by the namespace name as follows:

namespace namespace_name 
{
   int x, y; // code declarations where 
             // x and y are declared in 
             // namespace_name's scope
}

Consider following C++ program :

int main() 
{ 
    int v; 
    v = 0; 
    double v; // Error here 
    v = 0.0; 
}

Output :

Compiler Error:
'v' has a previous declaration as 'int v' already.

In each scope, a name can only represent one entity. So, there cannot be two variables with the same name in the same scope. Using namespaces, we can create two variables or member functions having the same name.

We can convert the above program into namespace :

#include <iostream> 
using namespace std; 
namespace ns1 
{ 
    int v()    
    { 
    
         return 5; 
    
    } 
} 
namespace ns2  
{ 
    const double x = 100; 
    double v() 
    {  
    
          return 2*x; 
    
    } 
} 
  
int main() 
{ 
    // Access value function within ns1 
       cout << ns1::v()'\n';  
  
    // Access value function within ns2 
    cout << ns2::v() << '\n';  
  
    // Access variable x directly 
    cout << ns2::x << '\n';        
  
    return 0; 
} 

Output :

5
200
100

Why do we need namespace?

1)A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it.

  1. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

  2. All identifiers at namespace scope are visible to one another without qualification.

Points to remeber

1)Namespace declarations appear only at global scope.
2)Namespace declarations can be nested within another namespace.
3)Namespace declarations donā€™t have access specifiers. (Public or private)
4)No need to give semicolon after the closing brace of definition of namespace.
5)We can split the definition of namespace over several units.

Why do we mention 'using namespace std'

Is ā€˜using namespace stdā€™ is necessary to mention?
The answer is NO.

The std namespace is special, the built in C++ library routines are kept in the standard namespace. This includes cout, cin, string, vector, map, etc. Because these tools are used so commonly, itā€™s popular to add ā€œusing namespace stdā€ at the top of your source code so that you wonā€™t have to type the std:: prefix constantly. It only make our task easy, It is not necessary.

cout << ā€œHello world.ā€ << endl; // Here it is necessary to put ā€˜using namespace stdā€™ on the top of code.
std::cout << ā€œHello world.ā€ << std::endl; // Here there is no need to put ā€˜using namespace stdā€™ on the top of code.

Drawbacks of ā€˜using namespaceā€™

Professional programmers considers that it is bad manner to put ā€œusing namespaceā€ declarations in header files. It forces all includers of that header file to use that namespace, which might result in naming ambiguities that are hard to fix. This practice is called namespace pollution. Instead, always use the fully prefixed names in header files (std::string not string) and save the using declarations for the source files.

Unnamed Namespaces

1)They are directly usable in the same program and are used for declaring unique identifiers.
2)In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace.
3)The name of the namespace is uniquely generated by the compiler.
4)The unnamed namespaces you have created will only be accessible within the file you created it in.
5)Unnamed namespaces are the replacement for the static declaration of variables.

#include <iostream> 
using namespace std; 
  
// unnamed namespace declaration 
namespace 
{ 
   int rel = 300;  
} 
  
int main() 
{ 
   cout << rel << "\n"; // prints 300 
   return 0; 
} 

Output :

300

Question

Identify the correct statement

Namespace is used to group class, objects and functions
Namespace is used to mark the beginning of the program
Namespace is used to seperate the class, objects
None of the above
Namespace allow you to group class, objects and functions. It is used to divide the global scope into the sub-scopes.
Harshita Sahai

Harshita Sahai

Maintainer at OpenGenus | Previously Software Developer, Intern at OpenGenus (June to August 2019) | B.Tech in Information Technology from Guru Gobind Singh Indraprastha University (2017 to 2021)

Read More

Improved & Reviewed by:


OpenGenus Tech Review Team OpenGenus Tech Review Team
Namespace in C++
Share this