0% found this document useful (0 votes)
237 views8 pages

Namespaces in C++

Namespaces in C++ allow for differentiation of functions, classes, and variables with the same name from different libraries. A namespace defines a scope by wrapping code declarations within a namespace block defined with the keyword "namespace" followed by the namespace name. Functions or variables within a namespace must be called by prepending the namespace name with "::". The "using" directive avoids prepending namespaces and tells the compiler subsequent code is using names from that namespace.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
237 views8 pages

Namespaces in C++

Namespaces in C++ allow for differentiation of functions, classes, and variables with the same name from different libraries. A namespace defines a scope by wrapping code declarations within a namespace block defined with the keyword "namespace" followed by the namespace name. Functions or variables within a namespace must be called by prepending the namespace name with "::". The "using" directive avoids prepending namespaces and tells the compiler subsequent code is using names from that namespace.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

NAMESPACES IN C++

NAMESPACES IN C++

Consider a situation, when we have two persons with the same name,
Zara, in the same class.
Whenever we need to differentiate them definitely we would have to
use some additional information along with their name, like either
the area, if they live in different area or their mother’s or father’s
name, etc.
Same situation can arise in your C++ applications.
For 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.
NAMESPACES IN C++

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.
Using namespace, you can define the context in which names are
defined. In essence, a namespace defines a scope.
Defining a Namespace

A namespace definition begins with the keyword namespace followed by the


namespace name as follows −
namespace namespace_name
{
// code declarations
}
To call the namespace-enabled version of either function or variable, prepend
(::) the namespace name as follows −
name::code; // code could be variable or function.
NAMESPACES IN C++ EXAMPLE
#include <iostream>
int main ()
using namespace std; {
// first name space // Calls function from first name space.
first_space::func();
namespace first_space
{ // Calls function from second name space.
second_space::func();
void func() { cout << "Inside first_space" << endl;}
} return 0;
}
// second name space
namespace second_space
{
void func() { cout << "Inside second_space" << endl;}
}
OUTPUT

If we compile and run above code, this would produce the following
result −

Inside first_space
Inside second_space
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 −
// second name space
#include <iostream>
namespace second_space {
using namespace std;
void func() {
cout << "Inside second_space" << endl;}
// first name space
}
namespace first_space
using namespace first_space;
{
int main ()
void func() {
{
cout << "Inside first_space" << endl;
// This calls function from first name space.
}
func();
}
return 0;
}
THE USING DIRECTIVE
The ‘using’ directive can also be used to refer to a particular item within a namespace. For
example, if the only part of the std namespace that you intend to use is cout, you can refer to it
as follows −
using std::cout;
Subsequent code can refer to cout without prepending the namespace, but other items in
the std namespace will still need to be explicit as follows −
#include <iostream>
using std::cout;
int main ()
{
cout << "std::endl is used with std!" << std::endl;
return 0;
}

You might also like