what is namespace? what are the uses of namespace?

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is constructor overloading in oop?

607


What is overloading and its types?

615


assume the program must insert 4 elements from the key board and then do the following programs.sequential search(search one of the elements),using insertion sort(sort the element) and using selection sort(sort the element).

1662


#include #include #include #include void insert(struct btreenode **, int); void inorder(struct btreenode *); struct btreenode { struct btreenode *leftchild; struct btreenode *rightchild; int data; }; main() { struct btreenode *bt; bt=(struct btreenode *)NULL; int req,i=1,num; clrscr(); printf("Enter number of nodes"); scanf("%d",&req); while(i<=req) { printf("Enter element"); scanf("%d",&num); insert(&bt,num); i++; } inorder(bt); } void insert(struct btreenode **sr, int num) { if(*sr==NULL) { *sr=(struct btreenode *)malloc (sizeof(struct btreenode)); (*sr)->leftchild=(struct btreenode *)NULL; (*sr)->rightchild=(struct btreenode *)NULL; (*sr)->data=num; return; } else { if(num < (*sr)->data) insert(&(*sr)->leftchild,num); else insert(&(*sr)->rightchild,num); } return; } void inorder(struct btreenode *sr) { if(sr!=(struct btreenode *)NULL) { inorder(sr->leftchild); printf("\n %d",sr->data); inorder(sr->rightchild); } else return; } please Modify the given program and add two methods for post order and pre order traversals.

3248


What is interface in oop?

662






What do you mean by overloading?

580


c++ program to swap the objects of two different classes

1761


Why do while loop is used?

576


What is the main purpose of inheritance law?

667


What does and I oop and sksksk mean?

650


What is abstraction with example?

607


INSTANCE FIELDS DECLARED private ARE ACCESSIBLE BY THE METHODS ONLY.CAN WE CHANGE THE private FIELD OF AN OBJECT IN A METHOD OF SOME OTHER OBJECT OF THE SAME CLASS?

1634


What makes a language oop?

597


Are polymorphisms mutations?

702


What is the diamond problem in inheritance?

578