C++程序设计(c++头文件)
Definition of a class
- In C++,separated .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 function are in the source file(.cpp).
include叫做编译预处理
示例1
a.h的代码
#ifndef A //如果没有定义宏,则定义这个宏
#define A
using namespace std;
void show(){
cout<<"hello world"<<endl;
}
#endif // A
main.cpp的代码
#include<iostream>
#include"a.h"
using namespace std;
int main(){
show();
}
结果
hello world
Declarations vs. Definition
- A.cpp file is a compile unit
- only declarations are allowed to be in .h
1.extern
2.functionprototypes
3.class/struct declaration
Tips for header
1.One class declaration per header file
2.Associated with one source file in the same prefix of name.
3.The contents of a header file is surrounded with
#ifndef
#define
#endif