mirror of
https://gitee.com/apaki/cmake_demo.git
synced 2025-05-18 04:11:37 +08:00
58 lines
1.2 KiB
C
58 lines
1.2 KiB
C
|
|
#include "circle.h"
|
|
|
|
double circle_area(void *self, void *parent)
|
|
{
|
|
double area = 0;
|
|
struct _circle *pthis = (struct _circle *)parent;
|
|
area = pthis->shape->vtb.area(self, NULL);
|
|
return area;
|
|
}
|
|
|
|
void circle_draw(void *self, void *parent)
|
|
{
|
|
struct _circle *pthis = (struct _circle *)parent;
|
|
pthis->shape->vtb.draw(self, NULL);
|
|
}
|
|
|
|
double _circle_area(void *self, void *parent)
|
|
{
|
|
struct _circle *pthis = (struct _circle *)self;
|
|
double area = 3.14 * pthis->radius * pthis->radius;
|
|
printf("circle area = %0.2f\n", area);
|
|
return area;
|
|
}
|
|
|
|
void _circle_draw(void *self, void *parent)
|
|
{
|
|
printf("circle draw\n");
|
|
}
|
|
|
|
struct _circle *circle_new(double radius)
|
|
{
|
|
// super
|
|
static struct _virtual_table vtb = {
|
|
.area = (double (*)(void *self, void *parent))_circle_area,
|
|
.draw = (void (*)(void *self, void *parent))_circle_draw,
|
|
};
|
|
|
|
struct _shape *shape = shape_new();
|
|
if (shape == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
shape->vtb = vtb;
|
|
|
|
// self
|
|
struct _circle *circle = calloc(1, sizeof(struct _circle));
|
|
if (circle == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
circle->radius = radius;
|
|
// set parent
|
|
circle->shape = shape;
|
|
|
|
return circle;
|
|
}
|