Professional Documents
Culture Documents
Namespace Scope
Namespace Scope
Namespace Scope
Learn C++
Dr. Neenu Garg (Assistant Professor) Thapar Institute of Engineering and Technology
Namespace scope
• ANSI C++ standard has added a new keyword namespace to define a scope
that could hold global identifiers.
• The best example of namespace scope is the C++ standard library. All
classes functions and templates are declared within the namespace named
std.
• The using namespace statement specifies that the members defined in std
namespace will be used frequently throughout the program.
Defining a namespace
• The syntax for defining a namespace is similar to the //sample namespace declaration
syntax for defining a class.
namespace Testspace
{
Namespace namespace_name
int m;
{
//Declara0on of
void display(int n)
//Variables , func0ons, classes etc. {
} cout << n;
• There is one difference between a class defini4on and a }
namespace defini4on . The namespace is concluded with a }
closing brace but no termina4ng semicolon.
Defining a namespace
namespace NS1
{
……….
namespace NS2
{
int m=100;
}
………..
}
To access the variable m , we must qualify the variable with both the enclosing namespace names. The Statement