Here is source code of the C++ program which demonstrates the use of namespaces. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Demonstrate the use of Namespaces
*/
#include <iostream>
namespace One
{
int val;
}
namespace Two
{
char val;
}
void functionOne(void)
{
using namespace One;
/* Variable val is defined in context of namespace One */
std::cout << "Enter the value of val : ";
std::cin >> val;
std::cout << "functionOne()" << endl
<< "Value of val : "
<< val << endl;
}
void functionTwo(void)
{
using namespace Two;
/* Variable val is defined in context of namespace Two */
std::cout << "Enter the value of val : ";
std::cin >> val;
std::cout << "functionTwo()" << endl
<< "Value of val : "
<< val << endl;
}
int main()
{
functionOne();
functionTwo();
return 0;
}
$ g++ main.cpp $ ./a.out Enter the value of val : 5 functionOne() Value of val : 5 Enter the value of val : 5 functionTwo() Value of val : ♣