码迷,mamicode.com
首页 > 编程语言 > 详细

C++之 头文件

时间:2014-11-02 11:58:33      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   sp   文件   div   on   cti   bs   

bash:
    g++ a.cpp b.cpp --save-temps -Wall
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 functions are in the source file(.cpp).
The header files
  • If a function is declared in a header file,you must include the header file everywhere the function is used and where the function is defined.
  • If a class is declared in a header file,you must include the header file everywhere the class is used and where class member functions are defined.
Header = interface
  • The header is  a contract between you and the user of your code.
  • The compile enforces the contract by requirig you to declare all structures and functions before they are used.
bubuko.com,布布扣
Declarations vs. Definitions
  • A .cpp file is a compile unit
  • Only declaratons are allowed to be in .h
    • extern variables
    • function prototypes  //函数原型
    • class/struct declaration
#include
  • #include is to insert the included file into the .cpp file at where the #include statement is.
    • #include "xx.h":first search in the current directory,then the directories declared somewhere
    • #include <xx.h>:search in the specified directories
    • #include <xx>:same as #include <xx.h>
Standard header file struct
#ifndef HEADER_FLAG
#define HEADER_FLAG 
//Type declaration here...
#endif   //  HEADER_FLAG
 
Tips for header
  1. One class declaration per header file
  2. Associated with one source file in the same prefix of file name.
  3. The contents of a header file is surrounded with #ifndef #define #endif

C++之 头文件

标签:io   ar   for   sp   文件   div   on   cti   bs   

原文地址:http://www.cnblogs.com/sunchangjun/p/4068763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!