#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; }