We have introduced namespaces in below set 1.
Namespace in C++ | Set 1 (Introduction)
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;
}
|
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;
}
|
It is also possible to create more than one namespaces in the global space. This can be done in two ways.
- namespaces having different names
CPP
#include <iostream>
using namespace std;
namespace first
{
int func() { return 5; }
}
namespace second
{
int func() { return 10; }
}
int main()
{
cout << first::func() <<"\n";
cout << second::func() <<"\n";
return 0;
}
|
5
10
- Extending namespaces (Using same name twice)
It is also possible to create two namespace blocks having the same name. The second namespace block is nothing but actually the continuation of the first namespace. In simpler words, we can say that both the namespaces are not different but actually the same, which are being defined in parts.
CPP
#include <iostream>
using namespace std;
namespace first
{
int val1 = 500;
}
namespace first
{
int val2 = 501;
}
int main()
{
cout << first::val1 <<"\n";
cout << first::val2 <<"\n";
return 0;
}
|
500
501
Unnamed Namespaces
- They are directly usable in the same program and are used for declaring unique identifiers.
- In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace.
- The name of the namespace is uniquely generated by the compiler.
- The unnamed namespaces you have created will only be accessible within the file you created it in.
- Unnamed namespaces are the replacement for the static declaration of variables.
CPP
#include <iostream>
using namespace std;
namespace
{
int rel = 300;
}
int main()
{
cout << rel << "\n";
return 0;
}
|
Output:
300
This article is contributed by Abhinav Tiwari .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or 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.