namespaces code example

Example 1: whar is namespace

We can think of namespace as you made a private space for your variables or 
functions so that ther cann't be any condition where you can find name 
collision of variable or function a program.
I found out a great article on tutorialspoint and would definitely suggest you 
all to go there to have a better understanding of namespaces.
https://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm

Example 2: namespace

#include <iostream>

using namespace std;

namespace {
int x;
void display();
}

namespace{
void display(){
cout << "x is "<<x<<endl;
}
}

int main()
{
    x = 25;
    display();
    return 0;
}

Example 3: namespace

// namespaces
#include <iostream>
using namespace std;

namespace foo
{
  int value() { return 5; }
}

namespace bar
{
  const double pi = 3.1416;
  double value() { return 2*pi; }
}

int main () {
  cout << foo::value() << '\n';
  cout << bar::value() << '\n';
  cout << bar::pi << '\n';
  return 0;
}

Tags:

Cpp Example