c++ 头文件

    技术2022-05-20  41

    The purpose of a header file is to hold declarations for other files to use. These files allow programmers to separate certain elements of a program's source code into reusable files. Keep in mind that header files typically only contain declarations. They do not define how something is implemented. That is to say, all the functions(classes, types,etc.) you use in your programm, must have their declarations(usually using the header files to do so). Intuitively, the header file just do the job as "insert the content of header file into the current program when compiling ".

     

     

    Example:

    see the code below:

    1 #include <iostream> 2 int main() 3 { 4      using namespace std; 5      cout << "Hello, world!" << endl; 6      return 0; 7 }

     

    The running process:

     

    Some basic topics about the header file:

     

    1. Difference between “” and <>

        #include "XXX" , first search in the source code(current or user's) dir, if not find then search in the system dir.

        #include <XXX>, only search in the system dir.

     

         Usage : when users have their own header files(in the users' dir), use " " to include.

                     when users want to include the standard header files(in the system dir or with compiler), use <> to include.

     

         Note: the #include is in the compiling process, that is to say, the procedure do not change the running    performance.

     

     

    2. header files and namespace

        We may note that some header file have ".h" extension while some have no-extension names. What is the difference? The issue is because the C++ has been standardized, the functions in the runtime library were moved into the std namespace, but some function do not have non-extension, so we have to be careful when we choose the header files. 

      Also when we use the non-extension header files, a line"using namespace std;"must be presented after the "#include" lines.

     

     

    3. String head file

    When we want to use the string type in C++, usually the format is :

     

    (1) c-style string: char szString[] = "string" ;

    (2)std::string: (string class in std)

        #include <string> // for std::string

           using namespace std

           string strString; )

     

     

    4. #include "stdafx.h"

     

    stdafx.h is a file, generated by Microsoft Visual Studio IDE wizards, that describes both standard system and project specific include files that are used frequently but hardly ever change.

    Compatible compilers (for example, Visual C++ 6.0 and newer) will pre-compile this file to reduce overall compile times.

     

     

    Good website: http://www.learncpp.com/cpp-tutorial/19-header-files/


    最新回复(0)