Site icon i2tutorials

CPP- Namespace scope

Namespace scope

 

A namespace is a declaratory region that provides a scope to the identifiers (names of types, functions, variables, etc.) within it.Namespaces are used to arrange code into logical groups and to avoid name collisions that can occur especially when your code base includes multiple libraries. All identifiers within the name space are visible to one another without qualification.

 

Example Program:

#include <iostream>
using namespace std;
// Variable created inside namespace
namespace first
{
int val = 500;
}
int main()
{
// Local variable
int val = 200;
// These variables can be accessed from
// outside the namespace using the scope
// operator ::
cout << first::val << '\n';
cout << val << '\n';
return 0;
}

 

Output:

Exit mobile version