zoukankan      html  css  js  c++  java
  • VC DLL 动态链接库(四)

      DLL 导出类

      DLL 中定义的类可以在应用工程中使用。

      接下来,我将演示在 DLL 中定义 point 和 circle 两个类, 并在应用工程中引用了他们。

    // point.h point 类的声明
    #ifnedf POINT_H
    #define POINT_H
    #ifdef DLL_FILE
    class _declspec(dllexport) point // 导出类 point
    #else
    class _declspec(dllimport) point // 导入类 point
    #endif
    {
    public:
        float x, y;
        point();
        point(folat x_coordinate, folat y_coordinate);
    };
    #endif
    // point.cpp point 类的实现
    #ifnedf DLL_FILE
    #define DLL_FILE
    #endif
    
    #include "point.h"
    // 类 point 的缺省构造函数
    point::point(){
        x = 0.0, y = 0.0;
    }
    
    // 类 point 的构造函数
    point::point(float x_coordinate, float y_coordinate){
        x = x_coordinate;
        y = y_coordinate;
    }
    // circle.h circle 类的声明
    #ifndef CIRCLE_H
    #define CIRCLE_H
    #include "point.h"
    #idnef DLL_FILE
    class _declspec(dllexport)circle // 导出类 circle
    #else
    class _declspec(dllimport)circle // 导入类 circle
    #endif
    {
    public:
        void SetCentre(const point centrePoint);
        void SetRadius(float r);
        float GetGirth();
        float GetArea();
        circle();
    private:
        float radius;
        point centre;
    };
    #endif;
    // circle.cpp, circle类的实现
    #ifndef DLL_FILE
    #define DLL_FILE
    #endif
    #include "circle.h"
    #define PI 3.1415926
    // circle 类的构造函数
    circle::circle()
    {
        centre = point(0,0);
        radius = 0;
    }
    // 得到圆的面积
    float circle::GetArea()
    {
        return PI*radius*radius;
    }
    // 得到圆的周长
    float circle::GetGirth()
    {
        return 2*PI*radius;
    }
    // 设置圆心坐标
    void circle::SetCentre(const point centrePoint)
    {
        centre = centrePoint;
    }
    // 设置圆的半径
    void circle::SetRadius(float r)
    {
        radius = r;
    }
    // test.cpp 类的引用
    #include "..\circle.h"
    #pragma comment(lib, "dllTest.lib");
    
    int main()
    {
        circle c;
        point p(2.0, 2.0);
        c.SetCentre(p);
        c.SetRadius(1.0);
        cout << "area:" << c.GetArea() << "girth" << c.GetGirth() << endl;
        return 0;
    }

      

    通过 DLL 中的

    class _declspec(dllexport) point // 导出类 point

    class _declspec(dllimport) point // 导入类 point

    来完成类的导入和导出。

    转载请注明出处:http://www.cnblogs.com/ygdblogs
  • 相关阅读:
    夏令营讲课内容整理 Day 6 Part 3.
    夏令营讲课内容整理 Day 6 Part 2.
    计算几何:模板
    字符串:SAM
    字符串:回文自动机
    字符串:马拉车
    数学&模拟:随机化-矩阵随机化
    模拟:随机增量法
    模拟:爬山算法与模拟退火算法
    模拟:压位高精度
  • 原文地址:https://www.cnblogs.com/ygdblogs/p/5384362.html
Copyright © 2011-2022 走看看