Advertisement
Felanpro

Namespace

Apr 27th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. /*I just did this because I was bored*/
  2.  
  3. //A namespace is basically a class but you can store var, func and classes in it!
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. namespace PROGRAM_1
  9. {
  10.     int x = 18;
  11.     int y = 34;
  12. }
  13.  
  14. namespace PROGRAM_2
  15. {
  16.     void Hello()
  17.     {
  18.         cout << "Hello world!" << endl;
  19.     }
  20. }
  21.  
  22. int main()
  23. {
  24.     using namespace PROGRAM_2; //This means that you can use var and funcs without needing to type in the scope ::
  25.  
  26.     cout << PROGRAM_1::x + PROGRAM_1::y << endl; //Here we need to type :: because we didn't do: using namespace PROGRAM_1;
  27.  
  28.     Hello(); //If we didn't include using namespace PROGRAM_2; We would have to do: PROGRAM_2::Hello();
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement