The Header File of C++
1.Definition of a class- in c++ , separated .h and .cpp files are used to define one class.
- Class declaration and prototypes in that class are in the header file(.h).
- All the bodies of these functions are in the source file(.cpp)
2.The header files
- if a function is declared in a header file , you must include the header file everywhere the function is used and where the function is defined.
- if a class is declared in a header file , you must include the header file everywhere the class is used and where class member functions are defined.
3.Header = interface
- The header is a contract between you and the user of your code.
- The compile enforces the contract by requiring you to declare all structures and functions before they are used.
4.Declarations vs definitions
- A .cpp file is a compile unit
- only declarations are allowed to be in .h
- extern variables
- function prototypes
- class/struct declaration
5.#include
- #include is to insert the included file into the .cpp file at where the #include statement is .
- #include "xx.h" : first search in the current directory , then directories declared somewhere
- #include <xx.h> :search in the specified directories
- #include <xx>:same as #include <xx.h>
评论