hi
i want to know what is the difference between using namespace and include.
in both of them , we call librery to use class in program.
So can any one explane the diffrence between them ?
#include is a directive of preprocessor It simply includes one source code into another source code.
using directive is a construction of C++. It places names declared in one namespace into another namespace.
ok
So when we use #include , it is one file ; for namespace , it is many files (library).
is it like that?
but when i read a source code , i can't distinguish between class used from namesapce and class used from #include.
every time i should google it :(
no! #include includes the header you specify and all codes in the global namespace in it. usingnamespace includes all the codes inside that namespace you specify in all the headers you included. Get it?
if you check the headers in the "include" folder of your IDE folder, you'l see that most of them have this:namespace std {...}. The #include directive only includes the coude OUTSIDE of that! by saying usingnamespace std;, you include everything INSIDE that namespace! Is it clearer now?
When you #include someFile , the entire contents of that file is copied into place before the compiler sees it.
#include is NEVER seen by the compiler. Your source code is changed before being presented to the compiler. Imagine that you copied all of a file, such as <iostream>, and pasted it in at the top of your code. It's doing that. It's got nothing to do with namespace. Nothing at all. It's a quick and easy way to copy and paste a file, which could contain anything, into another file.
namespace is something the compiler does get to see. Imagine we had two functions with the same name. Oh no. Disaster. How can we possibly tell them apart. By putting them in different namespaces. That's all it does. Simply gives us a way to tell them apart. Here's a simple example.
Imagine this is in namespace one int add(int a, int b);
and this is in namespace two int add(int a, int b);
1 2
usingnamespace one;
int x = add(a,b); //Which will be used? the one in namespace one
1 2
usingnamespace two;
int x = add(a,b); //Which will be used? the one in namespace two
int x = one::add(a,b); //Which will be used? the one in namespace one
int x = two::add(a,b); //Which will be used? the one in namespace two
1 2
using one::add;
int x = add(a,b); //Which will be used? the one in namespace one
1 2
using two::add;
int x = add(a,b); //Which will be used? the one in namespace two
ok
If i want to modify in a source code , how can i distanguish between class called from namespace and other from include, or i coudn't know, i should every time google it?
If someone has stuck usingnamespace xxx; at the top of the code, you've got no way of knowing if a class being used is in that namespace or in the global namespace. Look it up.