A namespace is used as an additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries.
Namespaces are used to identify and differentiate the same entities that we have used in our program.
A namespace can be defined with the keyword namespace and with its name.
namespace namespace_name {
// code declarations
}
#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;
}
}
int main ()
{
// Calls function from first name space.
first_space::func();
// Calls function from second name space.
second_space::func();
return 0;
}
0 Comment(s)