diff --git a/main.c b/main.c index 8c5f1d0..6e4ddac 100644 --- a/main.c +++ b/main.c @@ -1,13 +1,126 @@ #include #include +#include #include +#define VIRTUAL +struct _shape +{ + VIRTUAL void (*area)(struct _shape *self); + VIRTUAL void (*draw)(struct _shape *self); +}; +void shape_area(struct _shape *self) +{ + printf("shape area\n"); +} + +void shape_draw(struct _shape *self) +{ + printf("shape draw\n"); +} + +struct _shape *shape_new(void) +{ + struct _shape *shape = calloc(1, sizeof(struct _shape)); + return shape; +} + +#if 0 +void shape_free(struct _shape * self) +{ + if (self != NULL) + { + free(self); + } +} +#endif + +struct _rect +{ + // super + // struct _shape shape; + + // attribute + double width, height; + + // method + void (*area)(struct _rect *self); + void (*draw)(struct _rect *self); +}; + +void rect_area(struct _rect *self) +{ + printf("rect area = %0.2f\n", self->width * self->height); +} + +void rect_draw(struct _rect *self) +{ + printf("rect draw\n"); +} + +struct _rect *rect_new(double width, double height) +{ + struct _rect *rect = calloc(1, sizeof(struct _rect)); + if (rect == NULL) + { + return NULL; + } + rect->width = width; + rect->height = height; + + rect->area = rect_area; + rect->draw = rect_draw; + return rect; +} + +struct _circle +{ + // super + // struct _shape shape; + + // attribute + double radius; + + // method + void (*area)(struct _circle *self); + void (*draw)(struct _circle *self); +}; + +void circle_area(struct _circle *self) +{ + printf("circle area = %0.2f\n", 3.14 * self->radius * self->radius); +} +void circle_draw(struct _circle *self) +{ + printf("circle draw\n"); +} + +struct _circle *circle_new(double radius) +{ + struct _circle *circle = calloc(1, sizeof(struct _circle)); + if (circle == NULL) + { + return NULL; + } + circle->radius = radius; + + circle->area = circle_area; + circle->draw = circle_draw; + return circle; +} int main(int argc, char *argv[]) { + struct _rect *rect = rect_new(4, 5); + rect->area(rect); + rect->draw(rect); + + struct _circle *circle = circle_new(3); + circle->area(circle); + circle->draw(circle); return 0; } diff --git a/mk.bat b/mk.bat index 6c46bd1..aedfdac 100644 --- a/mk.bat +++ b/mk.bat @@ -9,3 +9,5 @@ make -C build test @REM cpack -C ./build/CPackConfig.cmake @REM cpack -C ./build/CPackSourceConfig.cmake + +"build/release/bin/demo.exe"