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

What are generic functions and generic classes?

5 Answers  


What is the point of oop?

0 Answers  


What is object-oriented programming? Webopedia definition

0 Answers  


In multiple inheritance , to create sub class object , is there need to create objects for its superclasses??? in java and c++ both. Actually i have some information that is , all available members from its superclasses , memory created in subclass obj , so no need to create object for its superclasses...??? Thanks in Advance

1 Answers  


what is the use of classes in c++;

2 Answers   HCL,






What is abstraction with example?

0 Answers  


Please send ford technologies placement paper 2 my mail id

0 Answers  


Which language is not a true object oriented programming language?

0 Answers  


write a program to find 2^n+1 ?

0 Answers   CTS,


#include <iostream> using namespace std; struct wow { int x; }; int main() { wow a; a.x = 22; int c = a.x; int *b = new int; cout << c; return 0; } option: No output 0 22 -(11) Will not compile

1 Answers   CTS, Wipro,


Program to open a file with First argument

1 Answers   TCS,


In the following declaration of main, "int main(int argc, char *argv[])", to what does argv[0] usually correspond? 1) The first argument passed into the program 2) The program name 3) You can't define main like that

6 Answers  


Categories