Namespace in C++





  • Namespace is a container for identifiers and functions.

  • It puts the names of its members in a distinct space so that they don't conflict with the names in other namespaces or global namespace.

  • C++ provides namespace keyword to declare namespace.

    Syntax:

    namespace identifier
     {
          //declarations
     }
                                

Rules to Create Namespace

  1. The namespace definition must be done at global scope, or nested inside another namespace.

  2. Namespace definition doesn't terminates with a semicolon.

  3. You can use an alias name for your namespace name, for ease of use.

    Example :

    namespace MySpace{
    int a,b;
    void display();
    class MyClass{
        };
    }
    namespace ms = MySpace;
    // ms is alias for MySpace
  4. You cannot create instance of namespace.


How to Use Namespace ?

There are three ways to use a namespace in C++ program,
  1. Scope Resolution
  2. The using directive
  3. The using declaration

1. Using Scope Resolution ( : : )

Any member declared in a namespace can be explicitly specified using the namespace's name using scope resolution :: operator .

for e.g. [std::cout, std::cin]

Example:

#include<iostream>
namespace MySpace{
 void show(){
 std::cout<<"Hello!"<< std::endl;
 std::cout<<"Access Namespace";
 std::cout<<" with SRO.";
 cout<< std::endl;	
 }
}
int main() {
MySpace::show();
return 0;
}
OUTPUT:
Hello!
Access namespacce with SRO.

2. Using Directive

  • The using directive allows all the names in a namespace to be used without the namespace-name as an explicit qualifier.

  • using keyword allows you to import an entire namespace into your program with a global scope.

  • A using directive can be placed at the top of a .cpp file (at file scope), or inside a class or function definition.


for e.g. [using namespace std;]

Example:

#include<iostream>
using namespace std;
namespace MySpace{
 void show(){
 cout<<"Hello!"<< endl;
 cout<<"Access Namespace";
 cout<<" using directive.";
 cout<< endl;	
 }
}
int main() {
using namespace MySpace;
show();
return 0;
}
OUTPUT:
Hello!
Access namespacce using directive.

3. Using Declaration

  • The using directive imports all the names in namespace.

  • if you are just using one or two identifiers, then consider a using declaration to only bring those identifiers into scope and not all the identifiers in the namespace.

  • A using directive can be placed at the top of a .cpp file (at file scope), or inside a class or function definition.


for e.g. [using std::cout;]

Example:

#include<iostream>
using std::cout;
namespace MySpace{
 void show(){
/*here cout is using direct,
but to use endl, usin SRO
*/
 cout<<"Hello!"<< std::endl;
 cout<<"Access Namespace";
 cout<<" using declaration.";
 cout<< std::endl;	
 }
}
int main() {
using namespace MySpace;
show();
return 0;
}
OUTPUT:
Hello!
Access namespacce using declaration.


Nested Namespace:

Namespace can be nested where you can define one namespace inside another namespace.

namespace outer_namespace{
  //members declaration
    namespace nested_namespace{
     //member declaration
    }
}

You can access member of nested namespace by using scope resolution operator as follows:


using namespace outer_namespace::nested_namespace;



Anonymous Namespace:

You can create an explicit namespace without providing any name to it.

namespace
 {
   int MyFunc(){}    
 }

This is called an unnamed or anonymous namespace.

Next topic is Aliasing or reference variable





 





Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved