[ACCEPTED]-C++: using namespace and #include-c++

Accepted answer
Score: 39

In C++, #include is used to add files to your project 20 while namespace is used to keep your objects in logical 19 modules (namespace does not apply to C)

For 18 example, you might have a vector class in 17 file "vector.h" so you include it in your project.

vector 16 is a part of a large library (the standard 15 library) STD, so you can access it with


However 14 since programmers are lazy and don't want 13 to write std:: all over the place (the standard 12 library has many many very useful parts), you 11 can write

using namespace std

on the top of your file. This 10 will tell the compiler that everytime it 9 sees a type (such as vector), also check 8 in the namespace std because the definition 7 might be there. This way, the following 6 statements become equivalent.


In vector.h, you 5 should see something like

namespace std
   class vector { /* Implementation */ }

So #include is to add files, while 4 using namespace is to keep your code cleaner and packaged 3 in "meaningful" libraries. You can omit 2 using namespace when programming but you absolutely need 1 the #include

Score: 32

In order to answer your question, I will 54 go a little back and take some basics of 53 C and C++.

When compiling C/C++, the compiling 52 the source files into an actual executable 51 is actually two steps, compiling and linking. The 50 compilation step takes one .cpp file at 49 a time and compiles this. The contents of 48 other .cpp files are not visible to the 47 compiler. This generates an "object file" (I 46 don't know why it's called such). All the 45 object files are then linked by the linker 44 to produce the final executable.

This brings 43 in two important concepts in C++, declarations 42 and definitions. A declaration specifies 41 that something (a variable or a function) will 40 exists somewhere. The following is the declaration 39 of function Foo()

void Foo();

This means that we have 38 told the compiler that somewhere there will 37 be a function Foo() that takes no arguments 36 and return no value.

A definition specified 35 what function actually does. Here the function 34 is defined

void Foo() { cout << "Foo!!"; }

Lets define another function, Bar()

void Bar() {
    cout << "bar";

This 33 function calls function Foo(). This function 32 cannot be compiled if function foo has not 31 already been either declared or defined 30 previously in the same file. So the declaration 29 does not in itself produce any compiled 28 code. They just have to be there.

If the 27 function Foo() is not defined in this file, but 26 a different .cpp file, then it is the job 25 of the linker to make the connection between 24 these two functions. If the function Foo(), is 23 not defined anywhere you will get a linker 22 error, not a compiler error.

This comes to 21 the concept of header files. Header files 20 are the place where you store your declarations. When 19 using #include to include the contents of 18 the header file, then what actually happens 17 is that the preprocessor (a step that is 16 executed before the actual compiler) will 15 load the included file and "paste" the contents 14 into the original source file. So the compiler 13 will see the file as if the entire header 12 file actually was pasted into the c++ file.

So 11 when you program in C++, you will normally 10 place your definitions in .cpp files, and 9 you will place your declarations in .h files

Namespaces 8 on the other hand is simply a way to logically 7 group your code.

So no, namespaces are not 6 stored in separate files, and they do not 5 have a specific file extension. If I had 4 a project with multiple namespaces I might 3 create a separate directory for each namespace 2 (and then again, I might not, it would depend 1 on the situation).

More Related questions