C++ header file concept and basic authoring method

  • 2020-05-07 20:05:06
  • OfStack

Header files in the 1 standard library
The 1 cut of the
C++ standard library is placed in the namespace std (the contents of the namespace are not visible to the public), but a new problem is that countless existing C++ code relies on functionality in the pseudo-standard library that has been used for many years, such as declarations in < iostream.h > For compatibility, the standards committee created a new header file for the part of the standard library that wrapped std. The new header file has the same file name as the old one, but without the suffix h, such as < iostream.h > Becomes a < iostream > . For C header files, the same method is used, but the character c is also added before each header file name, such as < string.h > Becomes a < cstring > . < stdio.h > Turned out to be < cstdio > . It is better to use the new header, C++ program with the new header, need to use using namespace std or using namespace std:: specified class name, etc., to make the required class visible to our code.

2 custom header file
To prevent duplicate references to header files, it is best to use a preprocessing definition, as shown below:


#ifndef MYHEAD_H
#define MYHEAD_H
 ... // Content in the header file 
#endif

 

(1) # ifndef:
The     indicator #ifndef checks to see if the contents of the header file have been defined previously, and if so, the statement between #ifndef and #endif will not be executed.
For example, MYHEAD.H header file


#ifndef MYHEAD_H
#define MYHEAD_H

#include "myhead.h"

  ......

#endif

(2) # ifdef
The   indicator #ifdef is often used to determine whether a preprocessor constant has been defined to conditionally include program code.
Such as:


 int main()
 {
  #ifdef DEBUG
  cout<<"Beginning execution of main()\n";
  #endif
  string word;
  vector<string> text;
  while(cin>>word)
  {
 #ifdef DEBUG
 cout<<"word read:"<<word<<"\n";
 #endif
 text.push_back(word); 
  }
 //..... }

In this program, if DEBUG is defined, both of the statements contained in it will be executed, and if not, two of the output statements will not be executed.

3. Pre-processing knowledge
(1) #ifdef: determine whether a preprocessor constant is defined, such as #infef DEGUG
(2) #ifndef: determine whether a preprocessor constant is not defined
(3) #define: define 1 preprocessing constant, such as #define DEBUG
(4) # include
(5) # endif
(6) preprocessing constants can also be defined at compile time, such as CC, D, DEBUG, >.c
(7) when compiling C++ program, the compiler automatically defines a pre-processor name of s/s with s/s (note the two underscores), so you can use this to determine if the program is C++ program, so as to conditionally include 1 code, such as:


#ifndef MYHEAD_H
#define MYHEAD_H
#ifdef __cplusplus
extern "C" {
#endif
int DMpostprocessing();
#ifdef __cplusplus
}
#endif
#endif

(8) when compiling the C program, the compiler will automatically define the pre-processing constant of s 106en__. Of course, s 107en and s 108en__ will not be defined simultaneously;
(9) the other two more useful predefined constants are arbitration 110en__ (recording the number of lines the file has been compiled) and arbitration 111en__ (containing the name of the file being compiled). Use as follows:


if(element_count==0)
 cerr<<"Error:"<<__FILE__
   <<":line"<<__LINE__
   <<"element_count must be non-zero.\n";

(10) s 116en__ : compile date, the current compile date of the compiled file
(11) s 118en__ : compile time, the current compile time of the compiled file
Format: hh:mm:ss

  08:17:05     Oct 31 2006
(12) the C++ name of C library header always begins with the letter C and is removed after.

assert() is a universal preprocessor macro provided in the C language standard library. It is often used to determine a necessary prerequisite for the program to execute correctly. The header file associated with it is #include < assert.h >
Such as:


 assert(filename!=0);

If the following program executes correctly, it needs filename not to be 0. If the condition is false, that is, it equals 0, and the assertion fails, the program will output a diagnostic message and then terminate.

Its c++ name is: cassert
The C++ name of the C library header always begins with the letter C
Note: when C++ USES the header file in C standard library, 1 must use using namespace std; Place it in one namespace to use correctly

(13) header file suffixes vary in C++, so the standard C++ header file does not specify suffixes

4 C++ file input and output

Header file: #include < fstream >

Using file I/o instance:


 #include <fstream>
// In order to open the 1 Output file, declare first 1 a ofstream Type of object: 
 ofstream outfile("name-of-file");
// To test whether it has been successfully opened 1 File, as follows: 
 // If the file cannot be opened, the value is false
 if(!outfile)
   cerr<<"Sorry! We were unable to open the file!\n";

// In order to open the 1 Input file, declare first 1 a ifstream Type of object: 
  ifstream infile("name of file");
  if(!infile)
   cerr<<"Sorry! We were unable to open the file!\n";

1 Simple program: 
  #include <iostream>
  #include <fstream>
  #include <string>

  int main()
  {
   ofstream outfile("out_file");
 ifstream infile("in_file");
 if(!infile){
   cerr<<"error:unable to open input file!\n";
   return -1;
 }
 if(!outfile) {
   cerr<<"error:unable to open output file!\n";
   return -2;
 }
 string word;
 while (infile>>word)
  outfile<<word<<' ';
 return 0;
  }

What's in the header file?
The use of
header files is mainly reflected in two aspects, one is heavy (pronounced chong) use (that is, multiple use), and the other is Shared.

Header files that provide standard library functions are intended for reuse. Many programs or projects may use these standard library functions, write them in a header file, and only include the completed header file each time you use them.

The sharing of header files is mainly reflected in the multi-file structure of C++. Because of the small size of the current program, there is no need to use a multi-file structure, so the sharing of header files is not expanded. Interested readers may refer to relevant books.
So, if we were to write a reusable header file ourselves, what would it say?

Similar to the standard library functions, we should give some functions or functions in the header file modularly. You should also include declarations of constants, variables, and types that implement these functions or functions independently.

Let's take a look at an example of a header file application:


//shape.h
#include "math.h"// In the calculation 3 I'm going to use the sine function for the angular area 
const double pi=3.14159265358;// Constants defined 
struct circle// Type declaration 
{
  double r;
};
struct square
{
  double a;
};
struct rectangle
{
  double a,b;
};
struct triangle
{
  double a,b,c,alpha,beta,gamma;
};
double perimeter_of_circle(double r)// The function definitions 
{
  return 2*pi*r;
}
double area_of_circle(double r)
{
  return pi*r*r;
}
double perimeter_of_square(double a)
{
  return 4*a;
}
double area_of_square(double a)
{
  return a*a;
}
double perimeter_of_rectangle(double a,double b)
{
  return 2*(a+b);
}
double area_of_rectangle(double a,double b)
{
  return a*b;
}
double perimeter_of_triangle(double a,double b,double c)
{
  return a+b+c;
}
double area_of_triangle(double a,double b,double gamma)
{
  return sin(gamma/180*pi)*a*b/2;
}
//main.cpp
#include "iostream.h"
#include "shape.h"// Including what we wrote shape.h
int main()
{
  circle c={2};
  square s={1};
  rectangle r={2,3};
  triangle t={3,4,5,36.86989,53.13011,90};
  cout <<"Perimeter of circle " <<perimeter_of_circle(c.r) <<endl;
  cout <<"Area of square " <<area_of_square(s.a) <<endl;
  cout <<"Perimeter of rectangle " <<perimeter_of_rectangle(r.a,r.b) <<endl;
  cout <<"Area of triangle " <<area_of_triangle(t.b,t.c,t.alpha) <<endl;
  return 0;
}

Operation results:


Perimeter of circle 12.5664
Area of square 1
Perimeter of rectangle 10
Area of triangle 6

We have written the shape.h header file, so when we use it to calculate the circumference or area of the graph, we don't need to rewrite the function, we just need to include this header file.


Related articles: