C + + multi file structure and compilation preprocessing commands (Chapter 5)

catalogue

Multi file structure and compilation preprocessing commands

General organization of C + + programs

Example 5-10 multi file project

External variable

External function

Restrict variables and functions to compilation units

Standard C + + Library

Compilation preprocessing

l #include include instruction

l #define macro definition instruction

l #undef

Conditional compilation instructions - #if and #endif

Conditional compilation instruction - #else

Conditional compilation instruction - #elif

Conditional compilation instruction

Multi file structure and compilation preprocessing commands

General organization of C + + programs

l) a project can be divided into multiple source files:

n# class declaration file (. h file)

n# class implementation file (. cpp file)

Where the usage file (main() of the n} class is located cpp file)

l) use engineering to combine various documents.

Example 5-10 multi file project

//File 1, class definition, point h

class Point { //Class definition

public:          //External interface

       Point(int x = 0, int y = 0) : x(x), y(y) { }

       Point(const Point &p);

       ~Point() { count--; }

       int getX() const { return x; }

       int getY() const { return y; }

       static void showCount();          //Static function member

private:         //Private data member

       int x, y;

       static int count; //Static data member

};


//File 2, class implementation, point cpp

#include "Point.h"

#include <iostream>

using namespace std;


int Point::count = 0;            //Initializing static data members with class names


Point::Point(const Point &p) : x(p.x), y(p.y) {

       count++;

}


void Point::showCount() {

       cout << "  Object count = " << count << endl;

}


//File 3, main function, 5_10.cpp

#include "Point.h"

#include <iostream>

using namespace std;


int main() {

       Point a(4, 5);      //Define object a, whose constructor increases count by 1

       cout <<"Point A: "<<a.getX()<<", "<<a.getY();

       Point::showCount();      //Number of output objects

       Point b(a);         //Define object b and its constructor will increase count by 1

       cout <<"Point B: "<<b.getX()<<", "<<b.getY();

       Point::showCount();      //Number of output objects

       return 0;

}

External variable

l) if a variable can be used by other files in addition to the source file that defines it, it is called an external variable.

l) variables defined in the file scope are external variables by default, but if this variable needs to be used in other files, it needs to be declared with the extern keyword.

External function

l) functions declared outside all classes (that is, non member functions) have file scope.

l) such functions can be called in different compilation units, as long as referential declaration (i.e. declaration of function prototype) is made before calling. You can also modify a function prototype or define a function with extern, which has the same effect as the default state without modification.

Restrict variables and functions to compilation units

l) use anonymous namespace: variables and functions defined in anonymous namespace will not be exposed to other compilation units.

   namespace {         //Anonymous namespace

         int n;

         void f() {

                     n++;

         }

   }

l Here by“ namespace { ...... }"The enclosed areas belong to anonymous namespaces.

Standard C + + Library

l) the standard C + + class library is a collection of extremely flexible and extensible reusable software modules. Standard C + + classes and components are logically divided into six types:

n) input / output class

n. container classes and abstract data types

n) storage management class

n} algorithm

n) error handling

n. operating environment support

Compilation preprocessing

l #include include instruction

n) embed a source file at this point in the current source file.

n #include < file name >

– search in the standard way, and the files are located in the include subdirectory of the C + + system directory

n #include "file name"

– first search in the current directory, if not, then search in the standard way.

l #define macro definition instruction

n defines symbolic constants, which in many cases have been replaced by const definition statements.

n# define macro with parameters, which has been replaced by inline function.

l #undef

n # delete the macro defined by #define so that it no longer works.

Conditional compilation instructions - #if and #endif

#if constant expression

 //Compile when constant expression is non-zero

     Program body 

#endif

......

Conditional compilation instruction - #else

  #if constant expression

            Program body 1   //Compile when constant expression is non-zero

#else

     Program body 2   //Compile when constant expression is zero

#endif

Conditional compilation instruction - #elif

#if constant expression 1

    Program body 1  //Compile when constant expression 1 is non-zero

#elif constant expression 2

    Program body 2  //Compile when constant expression 2 is non-zero

#else

    Program body 3  //Compile in other cases

#endif

Conditional compilation instruction

#ifdef identifier

    Segment 1

#else

    Segment 2

#endif

l If the "identifier" is#If defined is defined and not deleted by undef, compile program segment 1;

l Otherwise, compile segment 2.


#ifndef identifier

   Segment 1

#else

   Segment 2

#endif

l If the "identifier" has not been defined, compile program segment 1;

l Otherwise, compile segment 2.

Keywords: C++ p2p

Added by cavolks on Fri, 14 Jan 2022 00:32:53 +0200