oop_in_c/ColoredSquare.c

107 lines
2.6 KiB
C
Raw Normal View History

2012-04-30 18:59:30 +04:00
#include <stdlib.h>
2012-05-01 01:36:16 +04:00
#include <stdio.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-05-01 10:34:02 +04:00
this->color = color;
2012-04-30 18:59:30 +04:00
}
2012-05-01 19:56:48 +04:00
void ColoredSquare_copy (ColoredSquare *dest, const ColoredSquare *src)
2012-04-30 18:59:30 +04:00
{
printf ("ColoredSquare_copy (%lu, %lu) called\n",
2012-05-01 19:56:48 +04:00
(unsigned long) dest,
(unsigned long) src);
Square_copy ((Square *) dest, (Square *) src);
dest->color = src->color;
2012-04-30 18:59:30 +04:00
}
2012-05-01 11:30:29 +04:00
ColoredSquare* ColoredSquare_clone (const 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 11:30:29 +04:00
const char* ColoredSquare_type (const 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 11:30:29 +04:00
void ColoredSquare_draw (const 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",
2012-05-01 10:34:02 +04:00
this->a,
this->color);
2012-04-30 18:59:30 +04:00
}
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);
2012-05-01 10:34:02 +04:00
this->color = color;
2012-04-30 18:59:30 +04:00
}
/* 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 =
{
2012-05-01 01:26:14 +04:00
{
{
{
2012-05-01 11:30:29 +04:00
(void* (*) (const void *)) ColoredSquare_clone,
2012-05-01 01:26:14 +04:00
(void (*) (void *)) Square_destroy
},
2012-05-01 11:30:29 +04:00
(const char* (*) (const Figure *)) ColoredSquare_type,
(void (*) (const Figure *)) ColoredSquare_draw,
(double (*) (const Figure *)) Square_area
2012-05-01 01:26:14 +04:00
},
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 10:34:02 +04:00
square->vtable = &vtable;
2012-04-30 18:59:30 +04:00
/*goto end;
err:
end:*/
return square;
}