Pre-Knowledge:
The difference between our CPP file and the. h file in C + + is that CPP files are files that need to be compiled, become a standalone compilation unit, and H files never need to be compiled, just for preprocessing.
Usually we complete the implementation of the function in the CPP file, then in H is the declaration of the function, because by default, global variables and global function storage types are extern type, so we do not need to display the use of extern
In this way, our other CPP files, as long as the. h file is available, the functions implemented in CPP can be used in other CPP because we have completed the operation of the extern function declaration with the. h file.
The definition of a C + + class is actually defined as a type.
Class a{
Public
void Fun (int n);
int fun1 (void);
Public
int num;
};
is actually defining a type. is different from what we usually call definitions.
The definition of a class cannot be defined repeatedly, and in the same compilation unit, the class can be defined only once. If the definition is repeated, an error occurs. both class declarations and class definitions are internal links. is used only for the current compilation unit.
Therefore, the definition of the class is placed in the. h file, and the implementation of the class is placed in a dedicated CPP. This includes the other CPP in. h, which you can use to implement functions in CPP.
Also note: The implementation of the CPP file for the class is compiled and must depend on the class definition file. h, so we must #include< in the class implementation file CPP ... h>, for compiling, otherwise an error occurs. This is different from the normal function.
//Student.h (This is the header file, the class declaration is made in this file)classStudent//class declaration{ Public:voiddisplay (); Private:intnum;Charname[ -]; Charsex; };//Student.cpp//define the function in this file#include <iostream>#include ″student.h″voidStudent∷display () {cout<<″num:″<<num<<Endl; cout<<″name:″<<name<<Endl; cout<<″sex:″<<sex<<Endl; }//main.cpp main Function Module#include <iostream>#include ″student.h″intMain () {Student stud; Stud.display (); return 0;}
There are only static member variables in the class. has external link attributes. So this requires that the initialization of static member variables must be in the. CPP file. If it is in the H file. Then multiple CPP files are # include, and multiple duplicate-defined errors occur.
Benefits of separating class definitions from class implementations
:
1/Fast compile speed
Of course you can. Conversely, if you have all the code for the class inline defined in the header file, then all CPP files that need to use this class actually contain more code, which is compiled by the compiler when compiling each of these CPP files. Separately defined, the code is compiled only once, that is, when compiling the CPP file for that class.
Yes, in special cases.
If I had a class A that was contained by hundreds of CPP, if the definition and declaration were put together, the hundreds of files would have to be recompiled as long as I made any changes to a. And if the header file contains only declarations, modifying the implementation of a does not cause those files to be recompiled, sometimes greatly increasing the speed
C + + class definitions and class implementations