首页 > 其他分享 >关于头文件的使用

关于头文件的使用

时间:2024-02-05 11:36:04浏览次数:25  
标签:Qinzihan void long standard 关于 使用 头文件 include

关于头文件的使用

这里写一下这个东西,毕竟我在使用的时候还是有不少的疑问

一、头文件

头文件就是在写 C++ 代码的时候,在最开头几行引用的文件,这里比如说:

# include <iostream>

我们就是引用了一个名称为 iostream 的头文件

这里这个文件为什么没有后缀名呢,这我就不是很清楚了,据说是取巧,或者是为了统一 C++ 头文件的格式

这里我把 iostream 的文件放在这里:

// Standard iostream objects -*- C++ -*-

// Copyright (C) 1997-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file include/iostream
 *  This is a Standard C++ Library header.
 */

//
// ISO C++ 14882: 27.3  Standard iostream objects
//

#ifndef _GLIBCXX_IOSTREAM
#define _GLIBCXX_IOSTREAM 1

#pragma GCC system_header

#include <bits/c++config.h>
#include <ostream>
#include <istream>

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  /**
   *  @name Standard Stream Objects
   *
   *  The &lt;iostream&gt; header declares the eight <em>standard stream
   *  objects</em>.  For other declarations, see
   *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/io.html
   *  and the @link iosfwd I/O forward declarations @endlink
   *
   *  They are required by default to cooperate with the global C
   *  library's @c FILE streams, and to be available during program
   *  startup and termination. For more information, see the section of the
   *  manual linked to above.
  */
  //@{
  extern istream cin;		/// Linked to standard input
  extern ostream cout;		/// Linked to standard output
  extern ostream cerr;		/// Linked to standard error (unbuffered)
  extern ostream clog;		/// Linked to standard error (buffered)

#ifdef _GLIBCXX_USE_WCHAR_T
  extern wistream wcin;		/// Linked to standard input
  extern wostream wcout;	/// Linked to standard output
  extern wostream wcerr;	/// Linked to standard error (unbuffered)
  extern wostream wclog;	/// Linked to standard error (buffered)
#endif
  //@}

  // For construction of filebuffers for cout, cin, cerr, clog et. al.
  static ios_base::Init __ioinit;

_GLIBCXX_END_NAMESPACE_VERSION
} // namespace

#endif /* _GLIBCXX_IOSTREAM */

你会发现,我们在这个头文件中定义了一些东西,所以我们在引用这个头文件的时候就可以用这些定义过的东西

但是说的再准确一点,这又不叫定义,而是叫做声明

具体一点说,我们先看一下头文件的格式:

# ifndef _NAME_H_

# define _NAME_H_

// 在这里定义一些东西

# endif

这里我们的第一行的 _NAME_H_ 是你自己起的名字,但是最好和头文件的名字一样

保存,后缀名 .h

然后中间定义的格式是这样的:

# include <bits/stdc++.h> // 引用你需要的头文件,可以是自定义的

using namespace std;

inline void Afunction () ;

class A {

    public :

        inline void init () ;

} ;

这就是头文件,而具体函数的定义,我们放在源文件里

二、源文件

源文件的后缀名是 .cpp,用于写 C++ 代码,这里我们把一组头文件和源文件配对,名称的话最好是一样的

然后我们在源文件里引用头文件,在源文件里面定义头文件中声明而没有定义的函数即可

具体格式见下:

# include <bits/stdc++.h>

# include "..." // 你的头文件的地址

using namespace std;

void Afuncion () {

    return ;

}

void A :: init () {

    if (1 + 1 == 2) return ;

}

三、实战

为了让大家更透彻的理解,我们写一个有关秦子涵的文件

首先,我们这样拜访我们的文件:

image

然后我们在 Qinzihan.h 里面这样写:

# ifndef _QINZIHAN_H_

# define _QINZIHAN_H_

# include <bits/stdc++.h>

using namespace std;

class Qinzihan {

    public :

        long long Weight = 2147483647; // 体重

        bool Dead = false; // 解脱了没

        void Init (long long w) ; // 初始化/创造一个秦子涵

        void Eat (long long food) ; // 本能:吃东西

} ;

Qinzihan ReadQinzihan () ; // 读入一个秦子涵

# endif

然后我们在 Qinzihan.cpp 里写这些:

# include <bits/stdc++.h>

# include "Qinzihan.h" // 引用我们的头文件

using namespace std;

// 把声明的函数定义一下

void Qinzihan :: Init (long long w) {

    this -> Weight = w * 10; // 初始天赋,10 倍体重

}

void Qinzihan :: Eat (long long food) {

    if (this -> Dead) return ;

    this -> Weight += food;

    if (this -> Weight < 0) Dead = true;

}

Qinzihan ReadQinzihan () {

    long long w = scanf ("%lld", &w);

    Qinzihan nw = init (w);

    return Qinzihan;

}

// 若是不运行这个程序,就不用写 main 函数

接下来我们在新文件 test.cpp 里试试:

# include <bits/stdc++.h>

# include "Qinzihan.h"

using namespace std;

signed main () {

    Qinzihan qinzh = ReadQinzihan ();

    while (! qinzh.Dead) qinzh.Eat (100000);

    return 0;

}

四、结语

这里提前祝大家龙年大吉啦

标签:Qinzihan,void,long,standard,关于,使用,头文件,include
From: https://www.cnblogs.com/Tzf-tzf/p/18007559

相关文章

  • 使用C语言构建一个独立栈协程和共享栈协程的任务调度系统
    使用了标准库头文件<setjmp.h>中的setjmp和longjmp两个函数,构建了一个简单的查询式协作多任务系统,支持独立栈和共享栈两种任务。其中涉及到获取和设置栈的地址操作,因此还需要根据不同平台提供获取和设置栈的地址操作(一般是汇编语言,因为涉及到寄存器)该调度系统仅运行在一个......
  • 理解日志基础:使用Python进行有效的日志记录
    源码分享https://docs.qq.com/sheet/DUHNQdlRUVUp5Vll2?tab=BB08J2日志记录是任何软件开发过程中的一个基本组成部分,尤其是在爬虫开发中。有效的日志记录策略可以帮助开发者监控爬虫的行为,诊断问题,以及追踪爬虫的性能。Python的logging模块提供了一套强大的日志记录工具,它可以帮助......
  • service命令使用笔记
    一、简介#service--helpUsage:service[-h|-?]servicelistservicecheckSERVICEservicecallSERVICECODE[i32N|i64N|fN|dN|s16STR|null|fdf|nfdn|afdf]...Options:i32:Writethe32-bitintegerNintothes......
  • 4.WPF样式使用规范
    在Web开发的时候,编写css样式的时候通常是统一写在.css样式文件中。在WPF中也可以使用这样的思想。样式引用:1.新建一个项目用于统一存放样式WPF.UI添加一个资源字典Button.xaml或者CheckBox.xaml等等....<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/......
  • 关于POSIX定义的宏S_ISLINK(),S_ISREG()的使用
    摘自:https://forum.ubuntu.org.cn/viewtopic.php?t=380854我在学习linuxC系统编程,书上有个源代码可以实现自己的ls命令,不过在查错的过程中这个问题卡了我很久#include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>#include<sys/stat.h>#include<......
  • 关于考勤统计写的工具
    1'自动考勤计算2'byCaptainAmazing3'2020/7/64'2022/7/4更新迟到写入功能5'2023/7/3做大的更改6SubAutomaticAttendanceCounting()78Dims,partStrAsString......
  • SpringBoot中使用Spring自带线程池ThreadPoolTaskExecutor与Java8CompletableFuture实
    场景关于线程池的使用:Java中ExecutorService线程池的使用(Runnable和Callable多线程实现):https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/126242904Java中创建线程的方式以及线程池创建的方式、推荐使用ThreadPoolExecutor以及示例:https://blog.csdn.net/BADAO_......
  • SpringCloud工程添加openfeign使用服务之间调用
    SpringCloud服务之间的调用可以采用openfeign,今天这里就简单记录下需要做的步骤。前置条件就是微服务都建好了,并且两个服务都注册到nacos上,这里用两个微服务模块。简单描述:请求A模块,然后去调用B模块数据,最后从A模块接口返回。需要在A模块添加openfeign的依赖和service写好接口,B......
  • [转]gdb源码安装,指定使用的python版本
    转自:https://www.cnblogs.com/shengulong/p/8053370.html gdb调试python的时候,需要根据不同的python版本2.6、2.7、3.x安装相应的gdb;如何指定关联的python版本?下面gdb源码,解压后,进入目录:./configure-h并没有发现--with-python的选项。没有也没有问题,没有也可以自己加:whi......
  • 【踩坑指南】线程池使用不当的五个坑
    线程池是Java多线程编程中的一个重要概念,它可以有效地管理和复用线程资源,提高系统的性能和稳定性。但是线程池的使用也有一些注意事项和常见的错误,如果不小心,就可能会导致一些严重的问题,比如内存泄漏、死锁、性能下降等。本文将介绍线程池使用不当的五个坑,以及如何避免和解决它......