C++ Program to Demonstrate the use of Namespaces

bookmark

#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;    
}

Output:
$ 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 : &#x2663;