- Namespace provide the space where we can define or declare identifier i.e. variable, method, classes.
- Using namespace, you can define the space or context in which identifiers are defined i.e. variable, method, classes. In essence, a namespace defines a scope.
Advantage of Namespace to avoid name collision.
- Example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
- A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries.
- The best example of namespace scope is the C++ standard library (std) where all the classes, methods and templates are declared. Hence while writing a C++ program we usually include the directive using namespace std;
Defining a Namespace:
- A namespace definition begins with the keyword namespace followed by the namespace name as follows:
namespace namespace_name
{
// code declarations i.e. variable (int a;)
method (void add();)
classes ( class student{};)
}
- It is to be noted that, there is no semicolon (;) after the closing brace.
- To call the namespace-enabled version of either function or variable, prepend the namespace name as follows:
- namespace_name: :code; // code could be variable , function or class.
The using directive:
- You can also avoid prepending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace.
- The namespace is thus implied for the following code:
C++
#include <iostream>
using namespace std;
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
}
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main ()
{
func();
return 0;
}
|
Output
Inside first_space
- Names introduced in a using directive obey normal scope rules. The name is visible from the point of the using directive to the end of the scope in which the directive is found. Entities with the same name defined in an outer scope are hidden.
Nested Namespaces:
- Namespaces can be nested where you can define one namespace inside another name space as follows:
SYNTAX:
namespace namespace_name1
{
// code declarations
namespace namespace_name2
{
// code declarations
}
}
You can access members of nested namespace by using resolution operators as follows:
// to access members of namespace_name2
using namespace namespace_name1::namespace_name2;
// to access members of namespace_name1
using namespace namespace_name1;
In the above statements if you are using namespace_name1, then it will make elements of namespace_name2 available in the scope as follows:
C++
#include <iostream>
using namespace std;
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
}
}
using namespace first_space::second_space;
int main ()
{
func();
return 0;
}
|
Output
Inside second_space
Let us see how namespace scope the entities including variable and functions:
C++
#include <iostream>
using namespace std;
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
}
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
}
int main ()
{
first_space :: func();
second_space :: func();
return 0;
}
|
Output
Inside first_space
Inside second_space
Consider the following C++ program:
CPP
int main()
{
int value;
value = 0;
double value;
value = 0.0;
}
|
Output :
Compiler Error:
'value' has a previous declaration as 'int value'
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.
CPP
#include <iostream>
using namespace std;
namespace first {
int val = 500;
}
int val = 100;
int main()
{
int val = 200;
cout << first::val << '\n' ;
return 0;
}
|
Definition and Creation: 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. Namespaces provide the space where we can define or declare identifiers i.e. names of variables, methods, classes, etc.
- A namespace is a feature added in C++ and is not present in C.
- A namespace is a declarative region that provides a scope to the identifiers (names of functions, variables or other user-defined data types) inside it.
- 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
}
- Namespace declarations appear only at global scope.
- Namespace declarations can be nested within another namespace.
- Namespace declarations don’t have access specifiers (Public or Private).
- No need to give a semicolon after the closing brace of the definition of namespace.
- We can split the definition of namespace over several units.
Defining a Namespace:
A namespace definition begins with the keyword namespace followed by the namespace name as follows:
C++
namespace namespace_name{
method ( void add();)
classes ( class student{};)
}
|
It is to be noted that there is no semicolon (;) after the closing brace.
To call the namespace-enabled version of either a function or a variable, prepend the namespace name as follows:
namespace_name: :code; // code could be a variable, function or class.
C++
#include <iostream>
using namespace std;
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
}
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
}
int main ()
{
first_space::func();
second_space::func();
return 0;
}
|
Output
Inside first_space
Inside second_space
The using directive:
You can avoid prepending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace. The namespace is thus implied for the following code:
C++
#include <iostream>
using namespace std;
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
}
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main ()
{
func();
return 0;
}
|
Output
Inside first_space
Instead of accessing the whole namespace, another option (known as using declaration) is to access a particular item within a namespace. For example, if the only part of the std namespace that you intend to use is cout, you can refer to it as follows:
using std::cout;
Subsequent code can refer to cout without prepending the namespace, but other items in the std namespace will still need to be explicit as follows:
C++
#include <iostream>
using std::cout;
int main ()
{
cout << "std::endl is used with std!" << std::endl;
return 0;
}
|
Output
std::endl is used with std!
Names introduced in a using directive obey normal scope rules, i.e., they are visible from the point the using directive occurs to the end of the scope in which the directive is found. Entities with the same name defined in an outer scope are hidden.
C++
#include <iostream>
using namespace std;
namespace ns1 {
int value() { return 5; }
}
namespace ns2 {
const double x = 100;
double value() { return 2 * x; }
}
int main()
{
cout << ns1::value() << '\n' ;
cout << ns2::value() << '\n' ;
cout << ns2::x << '\n' ;
return 0;
}
|
Output:
5
200
100
Classes and Namespace: The following is a simple way to create classes in a namespace:
C++
#include<iostream>
using namespace std;
namespace ns
{
class geek
{
public :
void display()
{
cout<< "ns::geek::display()" <<endl;;
}
};
}
int main()
{
ns::geek obj;
obj.display();
return 0;
}
|
Output
ns::geek::display()
A class can also be declared inside namespace and defined outside namespace using the following syntax:
CPP
#include <iostream>
using namespace std;
namespace ns {
class geek;
}
class ns::geek {
public :
void display() { cout << "ns::geek::display()\n" ; }
};
int main()
{
ns::geek obj;
obj.display();
return 0;
}
|
Output
ns::geek::display()
We can define methods as well outside the namespace. The following is an example code:
C++
#include <iostream>
using namespace std;
namespace ns {
void display();
class geek {
public :
void display();
};
}
void ns::geek::display()
{
cout << "ns::geek::display()\n" ;
}
void ns::display() { cout << "ns::display()\n" ; }
int main()
{
ns::geek obj;
ns::display();
obj.display();
return 0;
}
|
Output:
ns::display()
ns::geek::display():
Nested Namespaces:
Namespaces can be nested, i.e., you can define one namespace inside another namespace as follows:
C++
namespace namespace_name1 {
namespace namespace_name2 {
}
}
|
You can access the members of a nested namespace by using the resolution operator (::) as follows:
C++
using namespace namespace_name1::namespace_name2;
using namespace namespace_name1;
|
In the above statements, if you are using namespace_name1, then it will make the elements of namespace_name2 available in the scope as follows:
C++
#include <iostream>
using namespace std;
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
}
}
using namespace first_space::second_space;
int main ()
{
func();
return 0;
}
|
Output
Inside second_space
Namespace provides the advantage of avoiding name collision:-
For example, you might be writing some code that has a function called xyz() and there is another library available in your code which also has the same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables, etc. with the same name available in different libraries.
The best example of namespace scope is the C++ standard library (std), where all the classes, methods and templates are declared. Hence, while writing a C++ program, we usually include the directive
using namespace std;
namespace in C++ | Set 2 (Extending namespace and Unnamed namespace) Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing) Can namespaces be nested in C++? Reference: http://www.cplusplus.com/doc/tutorial/namespaces/ If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Jan, 2023
Like Article
Save Article