如何在c ++中创建自己的头文件?

use*_*770 15 c c++ header

是否可以创建自己的新头文件?任何人都可以帮助我如何使用一个例子在c ++中创建自己的头文件?

Jef*_*jit 29

是的,当然可以,但在此之前,您需要了解哪些头文件以及如何正确使用它们.

file:yourname.h

#ifndef YOUR_NAME_INCLUDE
#define YOUR_NAME_INCLUDE

/* Your function statement here */
#endif
Run Code Online (Sandbox Code Playgroud)

yourname.cpp

#include <iostream.h>
#include "yourname.h"

/* Your function definition here */
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <iostream.h>
#include "yourname.h"

/* Your function calling here */
Run Code Online (Sandbox Code Playgroud)

要了解有关头文件和包含语句的更多信息,请单击下面的链接.

标题教程


Ash*_*osh 5

是的,您可以创建自己的头文件。

  1. 请通过 bruce eckel 第 1 卷在 C++ 中思考。
  2. 首先,头文件的扩展名为“.h”/“.hpp”
  3. 这些文件具有用户定义的数据结构和接口的声明,例如类声明、函数原型等。
  4. 声明并存储到项目文件夹后。您需要将此文件包含在 .cpp/.c 中,例如:

文件:myheader.h

#ifndef MYHEADER 
#define MYHEADER
    ......
#endif
Run Code Online (Sandbox Code Playgroud)

文件:myclass.cpp

#include <iostream.h>
#include "myheader.h"
Run Code Online (Sandbox Code Playgroud)