Multi file structure of C + + program
I have shown you many examples of relatively complete C + + programs. You may find that their structure can be basically divided into three parts: class declaration, class member function implementation and main function. Because there are few codes, we can write them in one file, but when we actually develop software, the program will be more complex and the amount of code will be relatively large.
A program can be divided into at least three files according to its structure: class declaration file (*. h file), class implementation file (*. cpp file) and main function file (file using class). If the program is more complex, we will create a declaration file and an implementation file for each class separately. In this way, when we want to modify a class, we can directly find its file and modify it without other file changes.
There is an example of clock class. Now let's see that the program is divided into three files according to the above structure:
// File 1: the declaration of the clock class, which can be named clock h #include <iostream> using namespace std; class Clock //Clock class declaration { public: //External interface Clock(); void SetTime(int NewH, int NewM, int NewS); //All three parameters have function prototype scope void ShowTime(); ~Clock(){} private: //Private data member int Hour,Minute,Second; }; // File 2: implementation of Clock class, which can be named Clock cpp #include "Clock.h" //Implementation of clock class member function Clock::Clock() //Constructor { Hour=0; Minute=0; Second=0; } void Clock::SetTime(int NewH,int NewM,int NewS) { Hour=NewH; Minute=NewM; Second=NewS; } void Clock::ShowTime() { cout<<Hour<<":"<<Minute<<":"<<Second<<endl; } // File 3: main function, which can be named main cpp #include "Clock.h" //Declare global object g_Clock, with file scope and static lifetime Clock g_Clock; int main() //Main function { cout<<"Clock class object for file scope:"<<endl; //Reference objects with file scope: g_Clock.ShowTime(); g_Clock.SetTime(10,20,30); Clock myClock(g_Clock); //Declare the object myClock with block scope and use G through the default copy constructor_ Clock initialize mycock cout<<"Block scoped clock class object:"<<endl; myClock.ShowTime(); //Reference objects with block scope return 0; }
Clock.cpp and main CPP uses #include "Clock.h" to set the header file of the class clock H included.
#The function of the include instruction is to embed the #include file into the current source file. The embedded file can be h files can also be cpp file.
If you do not include clock h,Clock.cpp and main CPP doesn't know the declaration form of clock class, so it can't use this class, so all files using this class should include the header file that declares it.
The above program is compiled by clock CPP and clock H compile to generate clock Obj, by main CPP and clock H compile to generate main Obj, and then the link process, clock Obj and main Obj link generates main Exe executable.
If we only modify the implementation file of the class, we just need to recompile clock CPP and link can be used, and other files need not be managed, which improves the efficiency. C + + programs in Windows system use engineering to manage multi file structure, while Unix system is generally managed with make tool. If you are engaged in Unix system software development, you need to write make files yourself.
Compile preprocessor
Before the compiler compiles the source program, the preprocessor should preprocess the source program file.
The preprocessor provides some compilation preprocessing instructions and preprocessing operators. Preprocessing instructions must start with "#", and each preprocessing instruction must occupy a separate line and cannot end with a semicolon. It can appear anywhere in the program file.
#include instruction
#The include instruction, also known as the file include instruction, is used to embed the contents of another source file into the current source file. In fact, we usually use this instruction to include header files.
#The include instruction can be written in two ways:
#Include < file name >
When using this writing method, you will find the files indicated in < > under the include subdirectory of the C + + installation directory, which is usually called searching by standard method.
#include "file name"
When using this writing method, the files indicated in "" will be searched in the current directory, that is, the directory of the current project. If they are not found, they will be searched in a standard way.
#define and #undef directives
If you have studied C language, you will know that #define can be used to define symbolic constants. For example, #define PI 3.14 defines a symbolic constant PI whose value is 3.14.
C + + can also define symbolic constants in this way, but it is more commonly used to modify them with const keyword when declaring them.
C language also uses #define to define parameter macros to realize simple function operations. For example, #define add(x,y) (x+y) instruction shows that if we use add(1,2), it will be replaced by (1 + 2) after preprocessing. In C + +, it is generally realized by inline functions.
#undef is used to delete macros defined by #define so that they no longer work.
Conditional compilation instruction
Conditional compilation instructions can be used to realize that some codes will participate in compilation only when certain conditions are met. In this way, we can use conditional compilation instructions to generate different object codes for the same program under different compilation conditions.
For example, we can add some debugging statements when debugging the program, and use conditional compilation instructions to control that these debugging statements participate in compilation only in debug mode, but not in release mode.
Conditional compilation instructions have five forms:
a. The first form:
#if constant expression Program body //When the "constant expression" is non-zero, this program segment participates in compilation #endif
b. The second form:
#if constant expression Procedure body 1 //When the "constant expression" is non-zero, this program segment participates in compilation #else Program body 2 //When the "constant expression" is zero, this program segment participates in compilation #endif
c. The third form:
#if constant expression 1 Procedure body 1 //When "constant expression 1" is non-zero, this program segment participates in compilation elif Constant expression 2 Program body 2 //When "constant expression 1" is zero and "constant expression 2" is non-zero, this program segment participates in compilation ... elif Constant expression n Program body n //When "constant expression 1" When "constant expression n-1" is zero and "constant expression n" is non-zero, this program segment participates in compilation #else Program body n+1 //In other cases, this program segment participates in compilation #endif
d. The fourth form:
#ifdef identifier Segment 1 #else Segment 2 #endif
If the "identifier" has been #defined and has not been deleted by undef, compile segment 1, otherwise compile segment 2.
e. The fifth form:
#ifndef identifier Segment 1 #else Segment 2 #endif
If the "identifier" is not defined, compile segment 1, otherwise compile segment 2.
define operator
Define is a preprocessing operator, not an instruction, so it cannot start with #. The use form is: define (identifier). If the identifier in brackets has been defined with #define and has not been deleted with #undef, then define (identifier) is non-0, otherwise it is 0. It can be used as follows:
#if !define(HEAD_H) #define HEAD_H
When we include header files, sometimes we repeatedly include the same header file for many times, such as the following:
// main.cpp file #include "file1.h" #include "file2.h" int main() { ... } // file1.h file #include "head.h" ... // file2.h file #include "head.h" ... // head.h file ... class A { ... } ...
main.cpp contains file1 H file, file1 H contains head H file, main CPP also includes File2 H file, File2 H also contains head H file, then main CPP contains head twice H file, an error will be reported during compilation, saying head Class A in H is repeatedly defined.
At this time, we can create a header in the repeatedly included file H uses conditional compilation instructions and uses a unique identifier to identify head Whether the H file has been compiled. If it has been compiled, it will not be compiled again.