c++ 头文件
C++ 头文件
2023-09-27 14:26:23 时间
可以将程序分为二部分:
头文件:包含结构声明和使用这些结构的函数的原型
源代码文件: 包含与结构有关的函数的代码
不要将函数的定义或变量的声明放在头文件里,
一般头文件可以包含以下内容
>函数原型
>使用#define或const定义的符号常量
>结构声明
>类声明
>模板声明
>内联函数
在包含头文件时我们使用 #include "head.h"而不是#include <head.h>
如果使用<> c++编译器将在存储标准头文件的主机系统的文件系统中查找;
如果使用"" 编译器会首先查找当前的工作目录或源代码目录,如果没有找到头文件,再去标准位置查找
注意:在IDE中,不要将头文件加入到项目列表中,也不要在源代码文件中使用#include 来包含其它源代码文件
#ifndef COORDIN_H #define COORDIN_H struct polar { double distance; //distance from origin double angle; //direction form origin }; struct rect { double x; //horizontal distance form origin double y; //vertical distance from origin }; polar rect_to_polar(rect xypos); void show_polar(polar dapos); #endif
在同一个文件中只能将一个头文件包含一次,但有可能会因为错误操作包含多次,有一种标准的c/c++技术可以避免
多次包含一个头文件。它是基于预处理编译指令#ifndef(if not defined)的。上边的代码意味着仅当以前没有
使用预处理编译器指令#define定义名称COORDIN_H时,才处理#ifndef和#endif之间的语句
源代码文件
#include<iostream> #include<cmath> #include "coordin.h" using namespace std; polar rect_to_polar(rect xypos) { polar answer; answer.distance=sqrt(xypos.x*xypos.x+xypos.y*xypos.y); answer.angle=atan2(xypos.y,xypos.x); return answer; } void show_polar(polar dapos) { cout<<dapos.angle<<" "<<dapos.distance<<endl; }
调用
#include<iostream> #include "coordin.h" using namespace std; int main() { rect replace; polar pplace; cout<<"Enter the x and y values:\n"; while(cin>>replace.x>>replace.y) { pplace=rect_to_polar(replace); show_polar(pplace); } cin.get(); }
相关文章
- C++开发人脸性别识别教程(6)——通过SVM实现性别识别
- C/C++各头文件作用总结
- C/C++的“文件包含”处理时头文件被重复包含的问题探究及解决方法(用最简单的例子进行说明)
- c++中的关键字new是什么东西?
- C/C++不同文件夹下包含头文件的方法及#include的使用
- C++ 构造函数的分类机调用
- C++ default 和delete的新用法
- 《C++入门经典(第6版)》——2.2 程序的组成部分
- 《C和C++程序员面试秘笈》——1.10 标准头文件的结构
- C++中的头文件和源文件
- Linux环境下配置vscode的C/C++ 的make编译环境(编写makefile方式)代码Demo版
- 代码中的输入输出(重定向、文件流C/C++)
- 打印并输出 log/日志到文件(C++)
- 【华为OD机试真题java、python、c++、jsNode】匿名信(100%通过+复盘思路)
- Visual studio C++程序内使用Sqlite3
- 156、【动态规划】AcWing ——3. 完全背包问题:二维数组+一维滚动数组(C++版本)
- 128、【贪心算法】leetcode ——376. 摆动序列(C++版本)
- 109、【树与二叉树】leetcode ——701. 二叉搜索树中的插入操作:递归法+双指针迭代法(C++版本)
- C++实操 - 使用C标准库的头文件