Home >>C++ Tutorial >C++ Namespaces

Namespaces in C++

C++ Namespaces

In order to organize too many classes so that the application handling can be made easy, the namespaces in C++ are used.

namespacename::classname is generally used by the programmers to access the class of a namespace. In case the programmers don't want to use the complete name at all times then they can use the using keyword instead.

The root namespace is generally known to be the global namespace in the C++ programming. By default the global:std always refer to the namespace "std" of the C++ framework.

C++ namespace Example

Here are the examples of the namespace in c++ that will help you understand the topic from a better point of view:

 	
#include <iostream>  
using namespace std;  
namespace namespace1 
	{    
    void print() 
	{   
        cout<<"This is Our First Namespace"<<endl;          
    }    
}    
namespace namespace2  
{    
       void print() 
	   {   
           cout<<"This is Our Second Namespace"<<endl;   
       }    
}   
int main()  
{  
namespace1::print();  
namespace2::print();  
return 0;  
}  
Output
This is Our First Namespace
This is Our First Namespace

using Keyword C++ namespace example

#include <iostream>  
using namespace std;  
namespace namespaceFirst
{  
   void print()
   {  
      cout << "This is our  First Namespace Example" << endl;  
   }  
}  
namespace namespaceSecond
{  
   void print1()
   {  
      cout << "This is our Second Namespace Example" << endl;  
   }  
}  
using namespace namespaceSecond;
using namespace namespaceFirst;  
int main () 
{  
print();
print1();	
return 0;  
}  
Output
This is our First Namespace Example
This is our Second Namespace Example