oop_in_c/Square.c

135 lines
2.8 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 "Square.h"
2012-05-01 00:55:56 +04:00
void Square_constructor (Square *this, double a)
2012-04-30 18:59:30 +04:00
{
2012-04-30 23:50:36 +04:00
printf ("Square_constructor (%lu, %f) called\n",
(unsigned long) this,
a);
2012-05-01 10:34:02 +04:00
this->a = a;
2012-04-30 18:59:30 +04:00
}
2012-05-01 00:55:56 +04:00
void Square_destructor (Square *this)
2012-04-30 18:59:30 +04:00
{
printf ("Square_destructor (%lu) called\n",
(unsigned long) this);
}
2012-05-01 19:56:48 +04:00
void Square_copy (Square *dest, const Square *src)
2012-04-30 18:59:30 +04:00
{
printf ("Square_copy (%lu, %lu) called\n",
2012-05-01 19:56:48 +04:00
(unsigned long) dest,
(unsigned long) src);
dest->a = src->a;
2012-04-30 18:59:30 +04:00
}
2012-05-01 11:30:29 +04:00
Square* Square_clone (const Square *this)
2012-04-30 18:59:30 +04:00
{
2012-04-30 23:50:36 +04:00
Square *square = Square_new (0.0);
2012-04-30 18:59:30 +04:00
printf ("Square_clone (%lu) called\n",
(unsigned long) this);
Square_copy (square, this);
printf ("Square_clone (%lu) returns %lu\n",
(unsigned long) this,
(unsigned long) square);
return square;
}
2012-05-01 00:55:56 +04:00
void Square_destroy (Square *this)
2012-04-30 18:59:30 +04:00
{
printf ("Square_destroy (%lu) called\n",
(unsigned long) this);
free (this);
}
2012-05-01 11:30:29 +04:00
const char* Square_type (const Square *this)
2012-04-30 18:59:30 +04:00
{
static const char *square_type_str = "Square";
printf ("Square_type (%lu) called\n",
(unsigned long) this);
printf ("Square_type (%lu) returns \"%s\"\n",
(unsigned long) this,
square_type_str);
return square_type_str;
}
2012-05-01 11:30:29 +04:00
void Square_draw (const Square *this)
2012-04-30 18:59:30 +04:00
{
printf ("Square_draw (%lu) called\n",
(unsigned long) this);
printf ("Drawing Square with %f side\n",
2012-05-01 10:34:02 +04:00
this->a);
2012-04-30 18:59:30 +04:00
}
2012-05-01 11:30:29 +04:00
double Square_area (const Square *this)
2012-04-30 18:59:30 +04:00
{
2012-05-01 10:34:02 +04:00
double area = this->a * this->a;
2012-04-30 18:59:30 +04:00
printf ("Square_area (%lu) called\n",
(unsigned long) this);
printf ("Square_area (%lu) returns %f\n",
(unsigned long) this,
area);
return area;
}
2012-05-01 00:55:56 +04:00
void Square_resize (Square *this, double a)
2012-04-30 18:59:30 +04:00
{
printf ("Square_resize (%lu, %f) called\n",
(unsigned long) this,
a);
2012-05-01 10:34:02 +04:00
this->a = a;
2012-04-30 18:59:30 +04:00
}
2012-05-01 11:30:29 +04:00
double Square_diag_length (const Square *this)
2012-04-30 18:59:30 +04:00
{
double diag_length = ((Square *)this)->a * 1.41421356;
printf ("Square_diag_length (%lu) called\n",
(unsigned long) this);
printf ("Square_diag_length (%lu) returns %f\n",
(unsigned long) this,
diag_length);
return diag_length;
}
/* public */
2012-05-01 00:55:56 +04:00
Square* Square_new (double a)
2012-04-30 18:59:30 +04:00
{
static Square_interface vtable =
{
2012-05-01 01:26:14 +04:00
{
{
2012-05-01 11:30:29 +04:00
(void* (*) (const void *)) Square_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 *)) Square_type,
(void (*) (const Figure *)) Square_draw,
(double (*) (const Figure *)) Square_area
2012-05-01 01:26:14 +04:00
},
Square_resize,
Square_diag_length
2012-04-30 18:59:30 +04:00
};
Square *square = malloc (sizeof (*square));
2012-04-30 23:50:36 +04:00
Square_constructor (square, a);
printf ("Square_new (%f) returns %lu\n",
a,
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;
}