您的位置:首页 > 编程语言 > C语言/C++

c++头文件

2015-11-12 07:57 309 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 头文件