what is namespace? what are the uses of namespace?



what is namespace? what are the uses of namespace?..

Answer / rdl

Namespaces allow to group entities like classes, objects
and functions under a name. This way the global scope can
be divided in "sub-scopes", each one with its own name.
The format of namespaces is:
namespace identifier
{
entities
}
Where identifier is any valid identifier and entities is
the set of classes, objects and functions that are included
within the namespace. For example:
namespace myNamespace
{
int a, b;
}
In this case, the variables a and b are normal variables
declared within a namespace called myNamespace. In order
to access these variables from outside the myNamespace
namespace we have to use the scope operator ::. For
example, to access the previous variables from outside
myNamespace we can write:
myNamespace::a
myNamespace::b

The functionality of namespaces is especially useful in the
case that there is a possibility that a global object or
function uses the same identifier as another one, causing
redefinition errors. For example:
// namespaces
#include <iostream>
using namespace std;
namespace first
{
int var = 5;
}
namespace second
{
double var = 3.1416;
}
int main () {
cout << first::var << endl;
cout << second::var << endl;
return 0;
}
5
3.1416
In this case, there are two global variables with the same
name: var. One is defined within the namespace first
and the other one in second. No redefinition errors happen
thanks to namespaces.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More OOPS Interview Questions

Write a macro for swapping integers

5 Answers  


Why is abstraction needed?

0 Answers  


What are the 3 principles of oop?

0 Answers  


What is a scope operator and tell me its functionality?

3 Answers   emc2,


Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the maximum number of concurrent threads that the InnoDB plug-in can create.

1 Answers  






What is the advantage of oop over procedural language?

0 Answers  


What is the output of the following code: int v() { int m=0; return m++; } int main() { cout<<v(); } 1) 1 2) 0 3) Code cannot compile

4 Answers  


Why is oop useful?

0 Answers  


What does and I oop and sksksk mean?

0 Answers  


what is the definition of incapsulation

2 Answers  


What is Inheritance, Multiple Inheritance, Shared and Repeatable Inheritance?

4 Answers   Accenture, L&T,


what is data hiding.

3 Answers   Wipro,


Categories