1. In C++,seperated .h and .cpp file are used to define on class. C++用.h 和 .cpp文件来分别定义类
2. Class declaration and prototypes in that class are in the header file.(.h) 类的声明和原型在.h文件内
3. All the bodies of these functions are in the source file(.cpp). 方法的实现在.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 fike,you must include the header file everywhere the class is used and where class member function 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. 编译器强制执行这个约定,使用方法之前必须声明。
头文件的包含指令以#开头,是编译预处理指令,编译器,会将整个.h文件,插入到include的位置
#include is to insert the included file into the .cpp file at where t he #include statement is.
#include "xx.h" :first search in the current directory, then the directories declared somewhere. 先在当前目录找,再在指定目录找
#include <xx.h>:search in the specified directories. 在指定目录查找: unix/linux 是/usr/include. windows 则是在安装的编译器指定的位置,没有系统统一的位置
#include<xx> same as #include<xx.h>
g++ a.cpp --save-temps vi a.ii 可以看到包含的文件已经插入到include的位置。
Declarations vs. Definitions. 声明 vs. 定义
A .cpp file is a compile unit. 一个cpp文件是一个编译单元
Only declarations are allowed to be in .h .h文件中只能包含声明
extern variables
function prototypes
class/struct declaration
Standard header file structure 标准头文件结构,用于解决a.h包含b.h,反过来b.h又包含a.h的问题。
#ifndef HEADER_FLAG
#define HEADER_FLAG
//Type declaration here..
#endif