diff --git a/ColoredSquare.c b/ColoredSquare.c index 52f068b..76903da 100644 --- a/ColoredSquare.c +++ b/ColoredSquare.c @@ -3,7 +3,7 @@ #include "ColoredSquare.h" -void ColoredSquare_constructor (void *this, +void ColoredSquare_constructor (ColoredSquare *this, double a, int color) { @@ -11,20 +11,20 @@ void ColoredSquare_constructor (void *this, (unsigned long) this, a, color); - Square_constructor (this, a); + Square_constructor ((Square *) this, a); ((ColoredSquare *) this)->color = color; } -void ColoredSquare_copy (void *to, void *from) +void ColoredSquare_copy (ColoredSquare *to, ColoredSquare *from) { printf ("ColoredSquare_copy (%lu, %lu) called\n", (unsigned long) to, (unsigned long) from); - Square_copy (to, from); + Square_copy ((Square *) to, (Square *) from); ((ColoredSquare *) to)->color = ((ColoredSquare *) from)->color; } -void* ColoredSquare_clone (void *this) +ColoredSquare* ColoredSquare_clone (ColoredSquare *this) { ColoredSquare *csquare = ColoredSquare_new (0.0, 0); printf ("ColoredSquare_clone (%lu) called\n", @@ -37,7 +37,7 @@ void* ColoredSquare_clone (void *this) return csquare; } -const char* ColoredSquare_type (void *this) +const char* ColoredSquare_type (ColoredSquare *this) { static const char *csquare_type_str = "ColoredSquare"; printf ("ColoredSquare_type (%lu) called\n", @@ -48,7 +48,7 @@ const char* ColoredSquare_type (void *this) return csquare_type_str; } -void ColoredSquare_draw (void *this) +void ColoredSquare_draw (ColoredSquare *this) { printf ("ColoredSquare_draw (%lu) called\n", (unsigned long) this); @@ -57,7 +57,7 @@ void ColoredSquare_draw (void *this) ((ColoredSquare *) this)->color); } -void ColoredSquare_set_color (void *this, int color) +void ColoredSquare_set_color (ColoredSquare *this, int color) { printf ("ColoredSquare_draw (%lu, %d) called\n", (unsigned long) this, @@ -67,7 +67,7 @@ void ColoredSquare_set_color (void *this, int color) } /* public */ -void* ColoredSquare_new (double a, int color) +ColoredSquare* ColoredSquare_new (double a, int color) { static ColoredSquare_interface vtable = { @@ -89,7 +89,7 @@ void* ColoredSquare_new (double a, int color) color, (unsigned long) square); - square->vtable = (void *) &vtable; + square->vtable = (ColoredSquare_interface *) &vtable; /*goto end; err: diff --git a/ColoredSquare.h b/ColoredSquare.h index 8411bb0..41fdce6 100644 --- a/ColoredSquare.h +++ b/ColoredSquare.h @@ -11,22 +11,24 @@ typedef struct ColoredSquare_data } ColoredSquare_data; +struct ColoredSquare; + typedef struct ColoredSquare_interface { Square_interface; - void (*set_color) (void *this, int color); + void (*set_color) (struct ColoredSquare *this, int color); } ColoredSquare_interface; -void ColoredSquare_constructor (void *this, +void ColoredSquare_constructor (struct ColoredSquare *this, double a, int color); -void ColoredSquare_copy (void *to, void *from); -void* ColoredSquare_clone (void *this); -const char* ColoredSquare_type (void *this); -void ColoredSquare_draw (void *this); -void ColoredSquare_set_color (void *this, int color); +void ColoredSquare_copy (struct ColoredSquare *to, struct ColoredSquare *from); +struct ColoredSquare* ColoredSquare_clone (struct ColoredSquare *this); +const char* ColoredSquare_type (struct ColoredSquare *this); +void ColoredSquare_draw (struct ColoredSquare *this); +void ColoredSquare_set_color (struct ColoredSquare *this, int color); /* public */ typedef struct ColoredSquare @@ -37,6 +39,6 @@ typedef struct ColoredSquare } ColoredSquare; -void* ColoredSquare_new (double a, int color); +ColoredSquare* ColoredSquare_new (double a, int color); #endif // __COLORED_SQUARE_H__ diff --git a/Figure.h b/Figure.h index 3b3a19a..5c2492c 100644 --- a/Figure.h +++ b/Figure.h @@ -5,13 +5,15 @@ #include "Object.h" +struct Figure; + typedef struct Figure_interface { Object_interface; - const char* (*type) (void *this); - void (*draw) (void *this); - double (*area) (void *this); + const char* (*type) (struct Figure *this); + void (*draw) (struct Figure *this); + double (*area) (struct Figure *this); } Figure_interface; diff --git a/Square.c b/Square.c index 577d226..abbdd9e 100644 --- a/Square.c +++ b/Square.c @@ -2,7 +2,7 @@ #include "Square.h" -void Square_constructor (void *this, double a) +void Square_constructor (Square *this, double a) { printf ("Square_constructor (%lu, %f) called\n", (unsigned long) this, @@ -10,13 +10,13 @@ void Square_constructor (void *this, double a) ((Square *) this)->a = a; } -void Square_destructor (void *this) +void Square_destructor (Square *this) { printf ("Square_destructor (%lu) called\n", (unsigned long) this); } -void Square_copy (void *to, void *from) +void Square_copy (Square *to, Square *from) { printf ("Square_copy (%lu, %lu) called\n", (unsigned long) to, @@ -24,7 +24,7 @@ void Square_copy (void *to, void *from) ((Square *) to)->a = ((Square *) from)->a; } -void* Square_clone (void *this) +Square* Square_clone (Square *this) { Square *square = Square_new (0.0); printf ("Square_clone (%lu) called\n", @@ -37,14 +37,14 @@ void* Square_clone (void *this) return square; } -void Square_destroy (void *this) +void Square_destroy (Square *this) { printf ("Square_destroy (%lu) called\n", (unsigned long) this); free (this); } -const char* Square_type (void *this) +const char* Square_type (Square *this) { static const char *square_type_str = "Square"; printf ("Square_type (%lu) called\n", @@ -56,7 +56,7 @@ const char* Square_type (void *this) return square_type_str; } -void Square_draw (void *this) +void Square_draw (Square *this) { printf ("Square_draw (%lu) called\n", (unsigned long) this); @@ -64,7 +64,7 @@ void Square_draw (void *this) ((Square *) this)->a); } -double Square_area (void *this) +double Square_area (Square *this) { double area = ((Square *)this)->a * ((Square *)this)->a; printf ("Square_area (%lu) called\n", @@ -76,7 +76,7 @@ double Square_area (void *this) return area; } -void Square_resize (void *this, double a) +void Square_resize (Square *this, double a) { printf ("Square_resize (%lu, %f) called\n", (unsigned long) this, @@ -84,7 +84,7 @@ void Square_resize (void *this, double a) ((Square *) this)->a = a; } -double Square_diag_length (void *this) +double Square_diag_length (Square *this) { double diag_length = ((Square *)this)->a * 1.41421356; @@ -98,7 +98,7 @@ double Square_diag_length (void *this) } /* public */ -void* Square_new (double a) +Square* Square_new (double a) { static Square_interface vtable = { @@ -118,7 +118,7 @@ void* Square_new (double a) a, (unsigned long) square); - square->vtable = (void *) &vtable; + square->vtable = (Square_interface *) &vtable; /*goto end; err: diff --git a/Square.h b/Square.h index 36252a6..dcf09cf 100644 --- a/Square.h +++ b/Square.h @@ -9,25 +9,27 @@ typedef struct Square_data } Square_data; +struct Square; + typedef struct Square_interface { Figure_interface; - void (*resize) (void *this, double a); - double (*diag_length) (void *this); + void (*resize) (struct Square *this, double a); + double (*diag_length) (struct Square *this); } Square_interface; -void Square_constructor (void *this, double a); -void Square_destructor (void *this); -void Square_copy (void *to, void *from); -void* Square_clone (void *this); -void Square_destroy (void *this); -const char* Square_type (void *this); -void Square_draw (void *this); -double Square_area (void *this); -void Square_resize (void *this, double a); -double Square_diag_length (void *this); +void Square_constructor (struct Square *this, double a); +void Square_destructor (struct Square *this); +void Square_copy (struct Square *to, struct Square *from); +struct Square* Square_clone (struct Square *this); +void Square_destroy (struct Square *this); +const char* Square_type (struct Square *this); +void Square_draw (struct Square *this); +double Square_area (struct Square *this); +void Square_resize (struct Square *this, double a); +double Square_diag_length (struct Square *this); /* public */ typedef struct Square @@ -38,6 +40,6 @@ typedef struct Square } Square; -void* Square_new (double a); +struct Square* Square_new (double a); #endif // __SQUARE_H__ diff --git a/main.c b/main.c index f2a9d8b..cd65bf1 100644 --- a/main.c +++ b/main.c @@ -5,18 +5,18 @@ int main (int argc, char *argv[]) { - int i = 0; + unsigned long i = 0; Figure *fig[3]; - fig[0] = Square_new (1); - fig[1] = ColoredSquare_new (2, 2); + fig[0] = (Figure *) Square_new (1); + fig[1] = (Figure *) ColoredSquare_new (2, 2); - ((ColoredSquare *) fig[1])->vtable->set_color (fig[1], 5); - ((Square *) fig[0])->vtable->resize (fig[0], 2); - ((Square *) fig[1])->vtable->resize (fig[0], 3); + ((ColoredSquare *) fig[1])->vtable->set_color ((ColoredSquare *) fig[1], 5); + ((Square *) fig[0])->vtable->resize ((Square *) fig[0], 2); + ((Square *) fig[1])->vtable->resize ((Square *) fig[0], 3); fig[2] = fig[1]->vtable->clone (fig[1]); - ((ColoredSquare *) fig[2])->vtable->set_color (fig[2], 3); + ((ColoredSquare *) fig[2])->vtable->set_color ((ColoredSquare *) fig[2], 3); for (i = 0; i < sizeof (fig) / sizeof (Figure *); i++) { @@ -32,7 +32,7 @@ int main (int argc, char *argv[]) printf ("diag_length(%lu) = %f\n", (unsigned long) fig[i], - ((Square *) fig[i])->vtable->diag_length (fig[i])); + ((Square *) fig[i])->vtable->diag_length ((Square *) fig[i])); } puts ("---");