The header file for C + + provides a centralized location for related claims. The header file generally contains the definition of the class, the declaration of the extern variable, and the declaration of the function.
Because the header file is contained in more than one source file, you should not include the definition of a variable or function. Otherwise, there will be redefinition errors when linking.
But there are three exceptions: the header file can define the class, the const object and the inline function that the value is known at compile time. These entities can be defined in multiple source files, as long as the definitions in each source file are the same.
declarations and definitions in C + +
1. Variables in C + + must be and can be defined only once, and variables must be defined or declared before they are used .
2. int i; Defines a variable i. You can declare a variable name without defining it by using the extern keyword, extern int i, declaring the variable i. The extern declaration is not defined, nor does it allocate storage space, in fact, it simply illustrates that the variable is defined elsewhere in the program. However, if the declaration has an initialization, it is treated as defined, even if the declaration is marked extern.
3. In summary, variables used in multiple files can be declared in a header file, defined in a source file, and other files that use the variable contain the header file that declares the variable.
C + + header file definition rules