DLL学习(5)

7 导出类
代码例子:
//文件名:point.h,point类的声明
#ifndef POINT_H
#define POINT_H
#ifdef DLL_FILE
 class _declspec(dllexport) point //导出类point
#else
 class _declspec(dllimport) point //导入类point
#endif
{
 public:
  float y;
  float x;
  point();
  point(float x_coordinate, float y_coordinate);
};

#endif

//文件名:point.cpp,point类的实现
#ifndef 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"
#ifdef 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;
}

使用:
#include "..\circle.h"  //包含类声明头文件
#pragma comment(lib,"dllTest.lib");

int main(int argc, char *argv[])
{
 circle c;
 point p(2.0, 2.0);
 c.SetCentre(p);
 c.SetRadius(1.0);
 printf("area:%f girth:%f", c.GetArea(), c.GetGirth());
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值