Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: using namespace and #include

Tags:

c++

In C++ what is the difference between the #include directive and using namespace? Also do you store your namespaces as separate files and what is the file extension for such files?

like image 281
JacKeown Avatar asked Feb 24 '11 23:02

JacKeown


People also ask

Can you use namespaces in C?

No, C does not have a namespace mechanism whereby you can provide “module-like data hiding”.

How many namespaces are there in C?

In addition, C also partitions a program's identifiers into four namespaces. Identifiers in one namespace, are also considered different from identifiers in another.

Can you use multiple namespaces C++?

You can have the same name defined in two different namespaces, but if that is true, then you can only use one of those namespaces at a time. However, this does not mean you cannot use the two namespace in the same program. You can use them each at different times in the same program.


2 Answers

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

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

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

std::vector 

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

using namespace std 

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

std::vector vector 

In vector.h, you should see something like

namespace std {    class vector { /* Implementation */ } } 

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

like image 152
Eric Avatar answered Sep 24 '22 02:09

Eric


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

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

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

void Foo(); 

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

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

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

Lets define another function, Bar()

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

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

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

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

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

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

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

like image 24
Pete Avatar answered Sep 21 '22 02:09

Pete