Declaration of a class
In C++, seperated .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 sourse file(.cpp).
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.
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.
Structure of C++ program
declarations(.h)+definitions(.cpp)→ after pre-compiler
Declarations vs. Definitions
A .cpp file is a compile unit.
Only declarations are allowed to be in .h
extern viriables
function prototypes
class/struct declaration(class/struct is declaration ONLY!!!)
#include
#include is to insert the included file into the file(couls be .cpp or .h)at where the #include statement is.
- #include"xx.h": first search in the current direction, then the directories declared somewhere.
- #include<xx.h>: search in the specified directories
- #include<xx>: same as #include<xx.h>
Standard header file structure
#ifndefine HEADER_FLAG
#define HEADER_FLAG
//type declaration here
#end if//HEADER_FLAG
Tips for header
1.Only one class declaration per header file.
2.Associated with one source file in the same prefix of file name.
3.The contents of a header file is surrounded with #ifndef #define #endif