Experiment no.
10
Date :
Title : Write a C++ program which to demonstrate use of namespace in the program .
Objective : The objective of this assignment is to understand the usage of namespace in
C++ programs.
Software : Dev C++ IDE
Theory :
➢ namespace
If any 2 entities like variables, classes or functions have the same name, it creates ambiguity.
The compiler cannot distinguish which version of that particular entity is expected. This
creates ambiguity in the program. To resolve this, C++ supports the use of namespaces.
A namespace is a declarative region that provides a scope to the identifiers (the names of
types, functions, variables, etc) inside it.
• Namespace provide the space where we can define or declare identifier i.e.
variable, method, classes.
• Using namespace, you can define the space or context in which identifiers are
defined i.e. variable, method, classes. In essence, a namespace defines a scope.
Advantage of Namespace to avoid name collision.
• Example, you might be writing some code that has a function called xyz() and there is
another library available which is also having same function xyz(). Now the compiler
has no way of knowing which version of xyz() function you are referring to within
your code.
• A namespace is designed to overcome this difficulty and is used as additional
information to differentiate similar functions, classes, variables etc. with the same
name available in different libraries.
• The best example of namespace scope is the C++ standard library (std) where all the
classes, methods and templates are declared. Hence while writing a C++ program we
usually include the directive using namespace std;
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;
// first name space
namespace first_space
{
void func()
{
cout << "Inside first_space" << endl;
}
}
// second name space
namespace second_space
{
void func()
{
cout << "Inside second_space" << endl;
}
}
using namespace first_space;
int main ()
{
// This calls function from first name space.
func();
return 0;
}
Output
Inside first_space
➢ 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;
Conclusion : Hence successfully implemented C++ program which to demonstrate use of
namespace in the program .