关于头文件的使用
这里写一下这个东西,毕竟我在使用的时候还是有不少的疑问
一、头文件
头文件就是在写 C++ 代码的时候,在最开头几行引用的文件,这里比如说:
# include
我们就是引用了一个名称为 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// . /** @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 #include #include namespace std _GLIBCXX_VISIBILITY(default){_GLIBCXX_BEGIN_NAMESPACE_VERSION /** * @name Standard Stream Objects * * The <iostream> header declares the eight standard stream * objects. 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 // 引用你需要的头文件,可以是自定义的 using namespace std; inline void Afunction () ; class A { public : inline void init () ; } ;
这就是头文件,而具体函数的定义,我们放在源文件里
二、源文件
源文件的后缀名是 .cpp
,用于写 C++
代码,这里我们把一组头文件和源文件配对,名称的话最好是一样的
然后我们在源文件里引用头文件,在源文件里面定义头文件中声明而没有定义的函数即可
具体格式见下:
# include # include "..." // 你的头文件的地址 using namespace std; void Afuncion () { return ; } void A :: init () { if (1 + 1 == 2) return ; }
三、实战
为了让大家更透彻的理解,我们写一个有关秦子涵的文件
首先,我们这样拜访我们的文件:
然后我们在 Qinzihan.h
里面这样写:
# ifndef _QINZIHAN_H_ # define _QINZIHAN_H_ # include 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 # 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 # include "Qinzihan.h" using namespace std; signed main () { Qinzihan qinzh = ReadQinzihan (); while (! qinzh.Dead) qinzh.Eat (100000); return 0; }
四、结语
这里提前祝大家龙年大吉啦
__EOF__