Namespace in C++

Namespace in C++ programming is a collection of related name or identifier i.e. functions, class, and variables which helps to separate these identifiers from similar.

Program Using Name Space and Calling his Functions

Program

#include<iostream.h>
#include<conio.h>
namespace first_space //'first_space' is name of Name Space.
   {
   void func()
   {
      cout<<"Inside first_space"<<endl;
   }
}
namespace second_space //'second_space' is name of Name Space.
   {
   void func()
   {
      cout<<"Inside second_space"<<endl;
   }
}
void main()
{
   first_space::func(); //Calls function'func()' from first name space.
   second_space::func(); //Calls function'func()' from second name space.
  getch();
}
Output
Inside first_space
Inside second_space

Using Name Space

Program

#include<iostream.h>
#include<conio.h>
namespace first_space
{
   void func()
   {
      cout<<"Inside first_space"<<endl;
   }
}
namespace second_space
{
   void func()
   {
      cout<<"Inside second_space"<<endl;
   }
}
using namespace first_space; //using 'first_space' Name Space here.
void main()
{
  func(); //This calls function from first name space.
  getch();
}
Output
Inside first_space

Program Calling Name Space from Another Name Space

Program

#include<iostream.h>
#include<conio.h>
namespace first_space
{
   void func()
   {
      cout<<"Inside first_space"<<endl;
   }
   namespace second_space
   {
      void func()
      {
         cout<<"Inside second_space"<<endl;
      }
   }
}
using namespace first_space::second_space;
void main()
{
   func(); //This calls function from second name space.
  getch();
}
Output
Inside second_space