Hello, I’m Liang Tang.
This is part 43 of the EasyC++ series on writing header files.
If you want a better reading experience, you can visit github repository: EasyLeetCode.
Write headers
The left and right examples we’ve done before were done in a single CPP file, and when we’re working on a relatively complex or large project, we obviously can’t put all of our code in a SINGLE CPP. This requires us to split the code, but the code is logically divided and written into different CPP files.
As we compile, we can compile these CPP files separately and then link them together. The advantage of this is that if you modify only one file, you can only compile that file without affecting the compilation results of other files. Most large projects will use an automated compilation tool, such as make, that will not execute the compilation process manually, but it is important to understand some of the details.
Let’s look at an example provided in C++ primer.
Now we need to implement a function to convert direct coordinates to polar coordinates, we need to define two structures representing cartesian coordinates and polar coordinates respectively, and also need to implement the transformation from direct coordinates to polar coordinates.
Obviously, this part of the code is separate from the main program, so we can put them in a separate CPP file. The first thing to be clear about is that the main() function and the other functions use the same structure, so both CPP files need to include a declaration of that structure. Copying code is obviously a bad option. It is better to write the structure declaration in a header file and introduce it with a #include statement.
So the overall code is divided into three parts:
- Header file: contains structure declaration, function declaration
- Coordin.cpp: contains code related to coordinate system transformation
- Main. CPP: the main program
We’ll use this code structure a lot in future object-oriented sections.
There are strict restrictions on the contents of header files. Since header files may be imported by multiple CPP files, we cannot put function implementations or parameter definitions in header files. This is because multiple definitions of the same function in the same program can cause an error, as can arguments.
Only the following can be written to a header file:
- Function prototype (function declaration)
#define
orconst
Symbolic constants defined- Structure declaration
- Class declaration
- The template declaration
- Inline functions (inline)
A header file can only be imported once in the same file, but it can sometimes be imported twice due to reference dependencies. For example, if A and B headers are introduced, B headers introduce A, causing A to be introduced twice.
To solve this problem, we can add the precompiled instruction #ifndef, which means if not defined, to determine whether a definition exists. The statement between #ifndef and #endif is only direct if the definition does not exist:
#ifndef COORDIN_H_
// statements
#endif
Copy the code
In general we create symbolic constants using #define:
#define MAXI 4096
Copy the code
But since we are only using it to distinguish between imports, we only need the name:
#ifndef COORDIN_H
#define COORDIN_H
// todo
#endif
Copy the code
In this way, when the COORDIN_H is introduced once, it is defined so that the code will not be executed the next time. Finally, we write out the complete header code:
#ifndef COORDIN_H__
#define COORDIN_H__
struct polar {
double distance, angle;
};
struct rect {
double x, y;
};
polar rect_to_polar(rect xpros);
void show_polar(polar dapos);
#endif
Copy the code