Namespace
In C++ two variables with the same name are not possible in the same scope i.e a name given to specific type or function represent one entity in a particluar scope but with namespaces, there is a possibility to create two variables or member functions having the same name. A namespace can be defined as a declarative region that is providing a specific scope to the identifiers such as names of the types, function, variables etc which are declared inside it.
Example of Namespace:
#include <iostream>
using namespace std;
namespace first // Variable created inside namespace
{
int value = 600;
}
int value = 400; //A Global variable with same name as variable inside namespace first
int main()
{
int value = 900; // A Local variable with same name as variable inside namespace first
cout << first::value << '\n'; //Namespace variables can be accessed from outside the namespace using the scope ::
cout << value << '\n'; //Local variable with same name as namespace variable will be accessed
return 0;
}
Output:
600
900
0 Comment(s)