C ++ - multiple files and preprocessor command structure

A multi-file structure

1, a project can be divided into multiple source files

  • Class declaration file (.h file)
  • Class implementation file (.cpp file)
  • Use file (function where the main .cpp file) class

2, the use of engineering to combine individual files

//Point.h ,类的定义
#ifndef POINT_H
#define POINT_H
 
class Point {
public:
    Point(int x = 0, int y = 0) : x(x), y(y) { count++; }
    Point(const Point &p);
    ~Point() { count--; }
    int getX() const { return x; }
    int getY() const { return y; }
    static void showCount();
private:
    int x, y;
    static int count;
//不能在类体中声明时初始化类的数据成员
//建议在类的构造函数中初始化数据成员
//只有整型或enum类型的 static const 数据成员可以在类体中初始化
};
 
#endif
//用预处理指令#ifndef、#define、#endif形成一个预处理器包装,可以防止将头文件多次包含在一个程序中
//Point.cpp ,类的实现
#include"Point.h"
#include<iostream>
using namespace std;
 
int Point::count = 0;    //静态数据成员在类体外定义并初始化
 
Point::Point(const Point &p) : x(p.x), y(p.y) {    //复制构造
    count++;
}
 
void Point::showCount() {
    cout << "  Object count = " << count << endl;
}
//5_10.cpp,主函数
#include"Point.h"
#include<iostream>
using namespace std;
 
int main() {
    Point a(4, 5);
    cout <<"Point A: "<<a.getX()<<", "<<a.getY();
    Point::showCount();
    Point b(a);
    cout <<"Point B: "<<b.getX()<<", "<<b.getY();
    Point::showCount();
    return 0;
}

1) external variables
if a variable is defined it can be used in addition to the source file, but also can be used by other files, then call this variable is an external variable
defined file scope variables, by default all external variables, in other documents if desired to use this variable need be declared extern keyword
2) external function
of all classes are declared outside the function (i.e., non-member function), the file has a scope
that can be compiled in separate units calls, as long as the statement in the function prototype to call. Can also be used extern modification function declaration prototype or defined functions, its effect without modification default state is the same as
3) The variables and functions limited within a compilation unit
using anonymous namespace: defining the anonymous namespace variables and functions are not exposed to other coding unit
is "namespace {......}" enclosed area belonging anonymous namespace

namespace { //匿名的命名空间
    int n;
    void f() { n++; }
}

4) Standard C ++ library
set a standard C ++ class library is extremely flexible and scalable reusable software modules
Standard C ++ classes and components into six types logically:

  • Input / Output Class
  • Containers with abstract data types
  • Storage Management category
  • algorithm
  • Error Handling
  • Operating Environment Support

Second, the preprocessor

Traditionally, the symbolic constant preprocessing directive is usually uppercase name of the header, and underlined alternate periods
1, # include contains instructions
to embed a source file to the current source file that point
#include<文件名>
- Search for a standard manner, the file is located in C ++ include subdirectory under the system directory
#include"文件名"
- searches in the current directory, if not, then standard search

2, # define macro definition instruction
defined symbolic constant, has been replaced in many cases const definition statement
defines the macro parameters, has been replaced inline functions
#undef: remove the #define macro definitions to no longer function

3, conditional compilation directives
1) #if and #endif

#if(常量表达式)
    程序正文    //当"常量表达式"非零时编译
#endif

2)#else

#if(常量表达式)
    程序正文1    //当"常量表达式"非零时编译
#else
    程序正文2    //当"常量表达式"为零时编译
#endif

3) #elif

#if(常量表达式1 
    程序正文1   //当"常量表达式1"非零时编译
#elif(常量表达式2) 
    程序正文2   //当"常量表达式2"非零时编译
#else
    程序正文3   //其他情况下编译
#endif

4)#ifdef & #ifndef

#ifdef 标识符
    程序段1
#else
    程序段2
#endif
//如果"标识符"经#defined定义过,且未经undef删除,则编译程序段1;否则编译程序段2
 
 
#ifndef 标识符
    程序段1
#else
    程序段2
#endif
//如果"标识符"未被定义过,则编译程序段1;否则编译程序段2

Guess you like

Origin www.cnblogs.com/xxwang1018/p/11546666.html