Namespace in C++ | Set 3 (Accessing, creating header, nesting and aliasing) - GeeksforGeeks

29 May, 2017
Namespace in C++ | Set 1 (Introduction)Namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)
Normal way// C++ program to demonstrate accessing of variables// in normal way, i.e., using "::"#include <iostream>using namespace std;  namespace geek{    int rel = 300; }  int main(){    // variable ‘rel’ accessed     // using scope resolution operator    cout << geek::rel << "\n";  // prints 300      return 0;}Output :300
“using” directive// C++ program to demonstrate accessing of variables// in normal way, i.e., using "using" directive#include <iostream>using namespace std;  namespace geek{    int rel = 300; }  // use of ‘using’ directiveusing namespace geek;  int main(){   // variable ‘rel’ accessed    // without using scope resolution variable   cout << rel << "\n";        //prints 300       return 0;}Output:300

Normal way// C++ program to demonstrate accessing of variables// in normal way, i.e., using "::"#include <iostream>using namespace std;  namespace geek{    int rel = 300; }  int main(){    // variable ‘rel’ accessed     // using scope resolution operator    cout << geek::rel << "\n";  // prints 300      return 0;}Output :300

// C++ program to demonstrate accessing of variables// in normal way, i.e., using "::"#include <iostream>using namespace std;  namespace geek{    int rel = 300; }  int main(){    // variable ‘rel’ accessed     // using scope resolution operator    cout << geek::rel << "\n";  // prints 300      return 0;}
Output :
300

“using” directive// C++ program to demonstrate accessing of variables// in normal way, i.e., using "using" directive#include <iostream>using namespace std;  namespace geek{    int rel = 300; }  // use of ‘using’ directiveusing namespace geek;  int main(){   // variable ‘rel’ accessed    // without using scope resolution variable   cout << rel << "\n";        //prints 300       return 0;}Output:300

// C++ program to demonstrate accessing of variables// in normal way, i.e., using "using" directive#include <iostream>using namespace std;  namespace geek{    int rel = 300; }  // use of ‘using’ directiveusing namespace geek;  int main(){   // variable ‘rel’ accessed    // without using scope resolution variable   cout << rel << "\n";        //prints 300       return 0;}
Output:
300

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 file1using 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.
// C++ program to demonstrate nesting of namespaces#include <iostream>using namespace std;  // Nested namespacenamespace out{  int val = 5;   namespace in  {      int val2 = val;      }}  // Driver codeint main(){  cout << out::in::val2;   // prints 5  return 0;}
OUTPUT :
5

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.
cpp-namespaces
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Left Shift and Right Shift Operators in C/C++
fork() in C
Core Dump (Segmentation fault) in C/C++
Function Pointer in C
Substring in C++
Inheritance in C++
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
Virtual Function in C++
C++ Classes and Objects