#include "rect.h" double rect_area(void *self, void *parent) { double area = 0; struct _rect *pthis = (struct _rect *)parent; area = pthis->shape->vtb.area(self, NULL); return area; } void rect_draw(void *self, void *parent) { struct _rect *pthis = (struct _rect *)parent; pthis->shape->vtb.draw(self, NULL); } double _rect_area(void *self, void *parent) { struct _rect *pthis = (struct _rect *)self; double area = pthis->width * pthis->height; printf("rect area = %0.2f\n", area); return area; } void _rect_draw(void *self, void *parent) { printf("rect draw\n"); } struct _rect *rect_new(double width, double height) { // super static struct _virtual_table vtb = { .area = (double (*)(void *self, void *parent))_rect_area, .draw = (void (*)(void *self, void *parent))_rect_draw, }; struct _shape *shape = shape_new(); if (shape == NULL) { return NULL; } shape->vtb = vtb; // self struct _rect *rect = calloc(1, sizeof(struct _rect)); if (rect == NULL) { return NULL; } rect->self = rect; rect->width = width; rect->height = height; // set parent rect->shape = shape; return rect; }