Namespace in C++ | Set 1 (Introduction)
Namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)
In C++, there are two ways of accessing namespace variables and functions.
- Normal way
// C++ program to demonstrate accessing of variables// in normal way, i.e., using "::"#include <iostream>usingnamespacestd;namespacegeek{intrel = 300;}intmain(){// variable ‘rel’ accessed// using scope resolution operatorcout << geek::rel <<"\n";// prints 300return0;}chevron_rightfilter_noneOutput :
300
- “using” directive
// C++ program to demonstrate accessing of variables// in normal way, i.e., using "using" directive#include <iostream>usingnamespacestd;namespacegeek{intrel = 300;}// use of ‘using’ directiveusingnamespacegeek;intmain(){// variable ‘rel’ accessed// without using scope resolution variablecout << rel <<"\n";//prints 300return0;}chevron_rightfilter_noneOutput:
300
We can create namespace in one file and access contents using another program. This is done in the following manner.
- We need to create two files. One containing the namespace and all the data members and member functions we want to use later.
- And the other program can directly call the first program to use all the data members and member functions in it.
File 1
// file1.h namespace foo { int value() { return 5; } } |
File 2
// file2.cpp - Not to be executed online #include <iostream> #include “file1.h” // Including file1 using namespace std; int main () { cout << foo::value(); return 0; } |
Here we can see that the namespace is created in file1.h and the value() of that namespace is getting called in file2.cpp.
In C++, namespaces can also be nested i.e., one namespace inside another. The resolution of namespace variables is hierarchical.
// C++ program to demonstrate nesting of namespaces #include <iostream> using namespace std; // Nested namespace namespace out { int val = 5; namespace in { int val2 = val; } } // Driver code int main() { cout << out::in::val2; // prints 5 return 0; } |
OUTPUT :
5
In C++, you can use an alias name for your namespace name, for ease of use. Existing namespaces can be aliased with new names, with the following syntax:
namespace new_name = current_name;
#include <iostream> namespace name1 { namespace name2 { namespace name3 { int var = 42; } } } // Aliasing namespace alias = name1::name2::name3; int main() { std::cout << alias::var << '\n'; } |
Output :
42
This article is contributed by Abhinav Tiwari .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)
- Why strict aliasing is required in C ?
- Strict Aliasing Rule in C with Examples
- Namespace in C++ | Set 1 (Introduction)
- Difference between namespace and class
- Cons of using the whole namespace in C++
- Why "using namespace std" is considered bad practice
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- random header in C++ | Set 1(Generators)
- random header | Set 2 (Distributions)
- random header in C++ | Set 3 (Distributions)
- Output of C programs | Set 66 (Accessing Memory Locations)
- accumulate() and partial_sum() in C++ STL : numeric header
- What’s difference between header files "stdio.h" and "stdlib.h" ?
- Difference between Header file and Library
- Last element of vector in C++ (Accessing and updating)
- Print "Hello World" in C/C++ without using any header file
- Comment in header file name?
- How to write your own header file in C?
- clocale header file in C++

