cmake_demo/library/copper.c

70 lines
1.7 KiB
C

#include "copper.h"
double copper_cash_area(struct _copper_cash *self, void *parent)
{
// 如果用的继承,那么就不能直接调用父类的虚接口,实现自己。
// 因为父类虚接口,是自己实现的。这样相当于自己调用自己。导致卡死。
// double circle_area = self->circle->shape->vtb.area(self->circle, self->circle);
// double rect_area = self->rect->shape->vtb.area(self->rect, self->rect);
double circle_area = 3.14 * self->circle->radius * self->circle->radius;
double rect_area = self->rect->width * self->rect->height;
double area = circle_area - rect_area;
printf("copper cash area = %0.2f\n", circle_area - rect_area);
return area;
}
void copper_cash_draw(struct _copper_cash *self, void *parent)
{
printf("copper cash draw\n");
}
struct _copper_cash *copper_cash_new(double width, double height, double radius)
{
// super
static struct _virtual_table vtb = {
.area = (double (*)(void *self))copper_cash_area,
.draw = (void (*)(void *self))copper_cash_draw,
};
struct _shape *shape = shape_new();
if (shape == NULL)
{
return NULL;
}
shape->vtb = vtb;
struct _rect *rect = rect_new(width, height);
if(rect == NULL)
{
return NULL;
}
rect->shape = shape;
struct _circle *circle = circle_new(radius);
if (circle == NULL)
{
return NULL;
}
circle->shape = shape;
// self
struct _copper_cash *cc = calloc(1, sizeof(struct _copper_cash));
if (cc == NULL)
{
return NULL;
}
cc->me = cc;
cc->price = 100;
// set parent
cc->shape = shape;
cc->rect = rect;
cc->circle = circle;
return cc;
}