c++入门 (头文件)<三>

Definition of a class      类的定义
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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值