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
-
The namespace definition must be done at global scope, or nested inside another namespace.
-
Namespace definition doesn't terminates with a semicolon.
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
- You cannot create instance of namespace.
How to Use Namespace ?
There are three ways to use a namespace in C++ program,- Scope Resolution
- The using directive
- 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
Online Live Training
We provide online live training on a wide range of technologies for working professionals from Corporate. We also provide training for students from all streams such as Computer Science, Information Technology, Electrical and Mechanical Engineering, MCA, BCA.
Courses Offered :
- C Programming
- C++ Programming
- Data Structure
- Core Java
- Python
- Java Script
- Advance Java (J2EE)
- Hibernate
- Spring
- Spring Boot
- Data Science
- JUnit
- TestNg
- Git
- Maven
- Automation Testing - Selenium
- API Testing
NOTE: The training is delivered in full during weekends and during the evenings during the week, depending on the schedule.
If you have any requirements, please send them to prowessapps.in@gmail.com or 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