oop_in_c/ColoredSquare.c

101 lines
2.6 KiB
C
Raw Normal View History

2012-04-30 18:59:30 +04:00
#include <stdlib.h>
2012-04-30 23:50:36 +04:00
#include <assert.h>
2012-04-30 18:59:30 +04:00
#include "ColoredSquare.h"
2012-05-01 00:55:56 +04:00
void ColoredSquare_constructor (ColoredSquare *this,
2012-04-30 23:50:36 +04:00
double a,
int color)
2012-04-30 18:59:30 +04:00
{
2012-04-30 23:50:36 +04:00
printf ("ColoredSquare_constructor (%lu, %f, %d) called\n",
(unsigned long) this,
a,
color);
2012-05-01 00:55:56 +04:00
Square_constructor ((Square *) this, a);
2012-04-30 23:50:36 +04:00
((ColoredSquare *) this)->color = color;
2012-04-30 18:59:30 +04:00
}
2012-05-01 00:55:56 +04:00
void ColoredSquare_copy (ColoredSquare *to, ColoredSquare *from)
2012-04-30 18:59:30 +04:00
{
printf ("ColoredSquare_copy (%lu, %lu) called\n",
(unsigned long) to,
(unsigned long) from);
2012-05-01 00:55:56 +04:00
Square_copy ((Square *) to, (Square *) from);
2012-04-30 18:59:30 +04:00
((ColoredSquare *) to)->color = ((ColoredSquare *) from)->color;
}
2012-05-01 00:55:56 +04:00
ColoredSquare* ColoredSquare_clone (ColoredSquare *this)
2012-04-30 18:59:30 +04:00
{
2012-04-30 23:50:36 +04:00
ColoredSquare *csquare = ColoredSquare_new (0.0, 0);
2012-04-30 18:59:30 +04:00
printf ("ColoredSquare_clone (%lu) called\n",
(unsigned long) this);
ColoredSquare_copy (csquare, this);
printf ("ColoredSquare_clone (%lu) returns %lu\n",
(unsigned long) this,
(unsigned long) csquare);
return csquare;
}
2012-05-01 00:55:56 +04:00
const char* ColoredSquare_type (ColoredSquare *this)
2012-04-30 18:59:30 +04:00
{
static const char *csquare_type_str = "ColoredSquare";
printf ("ColoredSquare_type (%lu) called\n",
(unsigned long) this);
printf ("ColoredSquare_type (%lu) returns \"%s\"\n",
(unsigned long) this,
csquare_type_str);
return csquare_type_str;
}
2012-05-01 00:55:56 +04:00
void ColoredSquare_draw (ColoredSquare *this)
2012-04-30 18:59:30 +04:00
{
printf ("ColoredSquare_draw (%lu) called\n",
(unsigned long) this);
printf ("Drawing ColoredSquare with %f side and %d color\n",
((ColoredSquare *) this)->a,
((ColoredSquare *) this)->color);
}
2012-05-01 00:55:56 +04:00
void ColoredSquare_set_color (ColoredSquare *this, int color)
2012-04-30 18:59:30 +04:00
{
printf ("ColoredSquare_draw (%lu, %d) called\n",
(unsigned long) this,
color);
((ColoredSquare *) this)->color = color;
}
/* public */
2012-05-01 00:55:56 +04:00
ColoredSquare* ColoredSquare_new (double a, int color)
2012-04-30 18:59:30 +04:00
{
static ColoredSquare_interface vtable =
{
(void* (*) (void *)) ColoredSquare_clone,
(void (*) (void *)) Square_destroy,
(const char* (*) (Figure *)) ColoredSquare_type,
(void (*) (Figure *)) ColoredSquare_draw,
(double (*) (Figure *)) Square_area,
Square_resize,
Square_diag_length,
ColoredSquare_set_color
2012-04-30 18:59:30 +04:00
};
ColoredSquare *square = malloc (sizeof (*square));
2012-04-30 23:50:36 +04:00
ColoredSquare_constructor (square, a, color);
printf ("ColoredSquare_new (%f, %d) returns %lu\n",
a,
color,
2012-04-30 18:59:30 +04:00
(unsigned long) square);
2012-05-01 00:55:56 +04:00
square->vtable = (ColoredSquare_interface *) &vtable;
2012-04-30 18:59:30 +04:00
/*goto end;
err:
end:*/
return square;
}