zoukankan      html  css  js  c++  java
  • 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
      
      
  • 相关阅读:
    echarts基础使用
    将数据表中的热词统计结果用echarts热词云展示
    LInux下bash: wget: command not found解决方法
    利用Jieba对txt进行分词操作并保存在数据库中
    idea运行Guns示例demo
    浅谈一下mshta在CVE201711882里的命令构造
    CVE201711882 POC 全版本通杀
    本地复现Flash 0day漏洞(CVE20184878)
    Oracle安装错误
    oracle远程连接服务器出现 ORA12170 TNS:连接超时 解决办法
  • 原文地址:https://www.cnblogs.com/sonictl/p/14488951.html
Copyright © 2011-2022 走看看