Namespace in C++
1. Namespace is a drum or container for identifiers.
2. Namespace puts the names of its members in a distinct space in such a way that they don't conflict with the names in other namespace or global namespace.
3. Namespace definition does not terminates with a semicolon like in class definition.
4. Namespaces allow you to group entities under a name. (i.e., you can group classes, objects and functions under a namespace.)
5. The definition of namespace must be done at global scope, or nested inside another namespace.
6. You can also use an alias name for your namespace name in order to use it easily. for example: namespace MS = MySpace;
7. Namespaces can be extended over multiple files.
Syntax of creating Namespace:
namespace NameOfNameSpace { //declaration of namespace }
How to create a namespace in C++?
Namespace can be simply created by using "namespace" keyword and the name of namespace. If you want to create a namespace named NewNameSpace. You can create it in this way:
namespace NewNameSpace {
//declaration of namespace
}
Here, one thing you should remember that namespace is not a class so you can't create instance (or object) of namespace. That is why you can't add semicolon (;) at the end of its declaration unlike class.
How to access members of namespaces?
Any name (or identifier) declared in a namespace can be explicitly specified using the namespace's name and the scope resolution (::) operator with the identifier.
For Example:
#include <iostream>
using namespace std;
// declaring namespace
namespace first
{
int value1 = 51;
}
int main()
{
//accessing identifier of namespace using scope resolution (::) operator
cout << first::value1 << "\n";
return 0;
}
/* Output
51
*/
What is unnamed namespaces?
There can also be an unnamed namespace. Unnamed namespaces are those namespaces that exists and do not have any name. for example:
namespace { //declaration of namespace }
1. The unnamed namespace will not have any names.
2. They are directly used in same program.
3. These are used for declaring unique identifiers.
4. The name of namespaces is uniquely generated by the compiler itself.
For Example:
#include <iostream> using namespace std; //declaring unnamed namespace namespace { int new_variable = 25; } main() { cout << "The value of new_variable: " << new_variable << endl; }
Output:
The value of new_variable: 25
Generally, the namespace are created in two ways:
1. namespaces with different names.
2. namespaces with same name twice.
Namespaces with same name
#include <iostream> using namespace std; //declaring first name space namespace firstNameSpace { int func1() { return 51; } } //declaring second name space namespace secondNameSpace { int func2() { return 11; } } int main() { // member function of namespaces // accessed by scope resolution operator cout << firstNameSpace::func1() << "\n"; cout << secondNameSpace::func2() << "\n"; return 0; }
Output:
51 11
Extended Namespace (namespaces with same name twice)
1. Namespaces can be extended.
2. A namespace definition can be continued and extended over multiple files.
3. They are not redefined or overridden.
Example 1: Extended namespace
#include <iostream> using namespace std; // first name space namespace first { int value1 = 51; } // rest part of the first namespace namespace first { int value2 = 101; } int main() { cout << first::value1 << "\n"; cout << first::value2 << "\n"; return 0; }
Output:
51 101
Example 2: Extended namespace
namespace MySpace { int num1; int func1(); } #include <iostream> using namespace std; namespace MySpace { class Dummy { public: void func2(); }; } int MySpace :: func1() { cout << "HELLO! Welcome to Learning Mania." << endl; } void MySpace :: Dummy :: func2() { cout << "HELLO! I am Dummy Class Function func2." << endl; } using namespace MySpace; int main() { num1 = 11; func1(); Dummy OBJ; OBJ.func2(); return 0; }
Output:
HELLO! Welcome to Learning Mania. HELLO! I am Dummy Class Function func2.
Points To Remember:
1. You need to use scope resolution (::) when you are defining functions that are declared inside any namespace just like func1 and func2 functions defined in the above example.
2. You need not to use scope resolution (::) when calling functions that are declared inside any namespace. Make sure you have used using namespace name_of_namespace; before calling namespace's functions or identifiers. using namespace MySpace; is used in the above example.
3. You need to add an extra scope resolution (::) when you define the functions of class which are inside any namespace. for example: void MySpace :: Dummy :: func2() is used in the above example to define func2 function of Dummy class. Dummy class was declared inside MySpace namespace. So, 2 scope resolution (::) operators are used here.
The using directive
1. using keyword allows you to import an entire namespace into your program with a global scope.
2. using directive can be used to import a namespace into another namespace or into any program.
For Example:
// File saved in "File1.h" namespace MySpace { int num1, num2; class DummyClass { //... some code ... }; }
//Use of using directive
//include File1.h to use MySpace namespace
#include "File1.h"
namespace MyNewSpace
{
//importing MySpace namespace with the help of using directive
using namespace MySpace;
int num3, num4;
DummyClass D1;
}