• split your cpp code into multiple files


    split your cpp code into multiple files

    reference: http://cse230.artifice.cc/lecture/splitting-code.html

    understande the #include

    The compiler will substitute #include with the actual contents of the file xyz.

    the header files

    the .h file is called the header files. People often use the header file as the one included.
    Header files usually have the ending .h and source files usually have the ending .cpp

    A header file contains only class and function declarations.

    Note:
    the declaration is a statement that a class exists and has certain properties and methods, or that a function exists;
    neither the class methods nor the functions will be defined;
    their implementations will not be provided in the header file.

    We also need to prevent repeated-includes because the compiler is not happy when a class or function or variable of the same name is declared twice.

    The frequent #ifndef XXX_H, #define XXX_H and #endif

    Because the compiler is not happy when a class or function or variable of the same name is declared twice. Thus, in every header file (named blah.h in this example), we write the following at the top and bottom:

    ```
    #ifndef BLAH_H
    #define BLAH_H
    
    ...
    
    #endif
    ```
    

    This means “if the name BLAH_H is not already defined, then define it.” If a file is included twice, then BLAH_H will be defined (by the first inclusion) so the entire “if—endif” will be skipped (which is the whole file, because the whole file is between the “if” and “endif”). Of course, BLAH_H can be anything; it could be FOO; we usually write FILE_H in the file named file.h so that we don’t reuse names.

    Example

    files in structure:

    • main.cpp

      • eclipse.h (eclipse.cpp)

        • shape.h
      • rectangle.h (eclipse.cpp)

        • shape.h

    • main.cpp:
    #include <iostream>
    #include "rectangle.h"
    #include "eclipse.h"
    using namespace std;
    
    int main()
    {
        Rectangle r;
        r.width = 10;
        r.height = 15;
        r.x = 3;
        r.y = 2;
    
        cout << r.area() << endl;
    
        Ellipse e;
        e.major_axis = 3;
        e.minor_axis = 5;
        e.x = 14;
        e.y = 68;
    
        cout << e.area() << endl;
    
        return 0;
    }
    
    • rectangle.h
    #ifndef RECTANGLE_H
    #define RECTANGLE_H
    
    #include "shape.h"
    
    class Rectangle : public Shape
    {
        public:
        double width;
        double height;
    
        double area();
    };
    
    #endif
    
    
    • rectangle.cpp:

      #include "rectangle.h"
      double Rectangle::area()
      {
          return width * height;
      }
      
    • eclipse.h

    #ifndef ELLIPSE_H
    #define ELLIPSE_H
    
    #include "shape.h"
    
    class Ellipse : public Shape
    {
        public:
        double major_axis;
        double minor_axis;
     
        double area();
    };
    
    #endif
    
    
    • ellipse.cpp:

      #include "ellipse.h"
      double Ellipse::area()
      {
          return 3.1415926 * major_axis * minor_axis;
      }
      
    • shape.h:

      #ifndef SHAPE_H
      #define SHAPE_H
      
      class Shape
      {
          public:
          double x;
          double y;
      
          virtual double area() = 0;
      };
      
      #endif
      
      
  • 相关阅读:
    构造方法调用另一个构造方法,用this
    排块游戏
    阶乘 大数存储
    Circle
    Git简单入门教程
    Python爬虫第一个成功版
    Python爬虫入门
    js中去除字符串两边的空格
    UUID生成字符串
    连接数据库工具类DBUtil
  • 原文地址:https://www.cnblogs.com/sonictl/p/14488951.html
Copyright © 2020-2023  润新知