Tuesday, September 3, 2013

C++ Using Namespace

Here is sample code to show the use namespace in C++.



#include<iostream>
 namespace ns1

{
 void print(){
 std::cout<<"namespace ns1"<<std::endl;
 }
}

 namespace ns2
 {
   void print(){
     std::cout<<"namespace ns2"<<std::endl;
   }
 }


 int main(){
   using namespace ns2;
   ns1::print();
   ns2::print();
   print();

   return 0;
 }



output :

namespace ns1
namespace ns2
namespace ns2

Similarly, using namespace std is defined as :


// iostream header file
namespace std
{
   ostream cout;
   istream  cin;
   // 
};