What is the difference between header file and namespace?

拈花ヽ惹草 提交于 2019-11-30 06:46:48

Header files are actual files - stored in the file system, referenced by file name, and #include'd in other files (at least, in C/C++ or other languages using the M4 macro preprocessor). Header files typically group pieces of code that are all interdependent parts of the same specific item together. For instance, a game might have a header file for all of its graphics rendering.

Namespaces, on the other hand, are an element of the programming language - they don't exist as a file system object, but rather as a designation within code telling the compiler that certain things are within that namespace. Namespaces typically group interfaces (functions, classes/structs, types) of similar (but not necessarily interdependent) items. For instance, the std namespace in C++ contains all of the Standard Library functions and classes.

Adrian Shum

To know what is Header file, you need to know the meaning of "declaration".

To put it in simple words, in C/C++, compilation happens in per-source manner. If I have a A.cpp and inside I make use of a function foo(), which will be defined somewhere else, I need to tell the compiler that: "Hey, I am using foo(), although you cannot see it defined anywhere in my source, don't worry, it is defined in another source". They way to tell compiler about this is by "declaring" foo() in A.cpp.

If I am the author of foo(), everyone that use foo() need to write down the declaration void foo(); in their source file. It will be a lot of duplicated and meaningless work. And it is so difficult for me to tell the guy that "use" foo() to have a correct declaration. Therefore as the author of foo(), I write down a file, containing the declaration for using foo(), and distribute it so that people can just "import" the file content to their source. The file I am distributing is Header file. The action of import is #include in C/C++. Yes, #include is nothing but inserting the content of the included file to the spot of #include.


Namespace is another story. To make it short, you can think of it is "real" name of function/class etc. for example, if I make

namespace FOO {
  class Bar { }
}

The class is not really named Bar, it's "real" name is in fact FOO::Bar.

C++ provides some way to save you typing the long real name, by "using".

A header file is a file that is intended to be included by source files. They typically contain declarations of certain classes and functions.

A namespace enables code to categorize identifiers. That is, classes, functions, etc. can be placed inside a namespace, keeping those separate from other classes that are unrelated. For example, in C++ everything from the standard library is in the std namespace.

In common man langauge, Header file would be unique file on file system and namespace would be covering one or more files.

i.e. HeaderFile is physical thing and namespace is logical thing.

learn more about them here http://en.wikipedia.org/wiki/Namespace and http://en.wikipedia.org/wiki/Header_file

Namespace is a new concept of 'ansi c++' to identify global identifiers which are to be frequently used in your programme.

Header file is a source file that supports your program by reusing reliable and tested code hence saving time and effort!!!!

Header file is basically the file system, it is physical thing, it contains the classes and functions if you didn't include header file and try to perform some operation like converting uppercase string to lower case, calculating string length you need to include header file string.h by which compile understand you are performing operation on string And while executing code it will link with corresponding library in which such functions reside On other hand namespace is a part of program, it is logical thing, it is designed for providing a way to keep one set of name separate from other. The class name declared in one namespace does not conflict with the same class name declared in another . All namespaces reside in .net framework class library which is huge collection of hierarchy of namespaces. Each namespaces contains classes, structures, interfaces, enumeration, delegates ...so on...

Namespace is new word given to headerfile. To make new versions avaiable.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!