Thread: Just a general question;

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    8

    Just a general question;

    Could somebody please explain to me the differences between "#include"-ing header files, and the "using namespace" code and what each does? Thanks :]

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    "#include" takes the whole text of a file and places it in this file.
    "using namespace" is a completely different thing;
    For C++ to avoid name collisions between same function names, there is an option that we declare a function not global, but part of a namespace.
    Example:
    Code:
    #include <iostream>
    
    int main()
    {
         std::cout << "Hello World!";
         std::cin.get();
          
         return 0;
    }
    "using namespace" just makes all functions under this namespace global

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
         cout << "Hello World!";
         cin.get();
          
         return 0;
    }
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    8
    So in your second example, int main and anything below it would be considered a global function?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by fthmdg View Post
    So in your second example, int main and anything below it would be considered a global function?
    No. It puts the namespace "std" into the global namespace (so that std::cout is now in the global namespace and can now be called just cout, e.g.).

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Exactly
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    8
    so in a weird way namespaces are like groups of functions?
    and using a namespace just allows you to call any of those functions from within that cpp file?

    and header files are basically just a list of variables, prototypes, and other stuff like that

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    My bad, when i say "under the namespace" i mean "declared and/or defined inside the namespace". I should've though that you'd take it literally...
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    8
    Well yeah, I figured that much. Thanks for the help :]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General Programming Question!!
    By chottachatri in forum C# Programming
    Replies: 7
    Last Post: 07-29-2009, 10:51 AM
  2. A general question aout programming?
    By bradt93 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-26-2008, 11:00 AM
  3. winsock - general question
    By keira in forum Windows Programming
    Replies: 1
    Last Post: 09-28-2007, 01:56 PM
  4. general programming compatability question
    By Metarectilinear in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-25-2002, 11:51 PM
  5. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM