Add parameters to constructors.

This commit is contained in:
Kolan Sh 2012-04-30 23:50:36 +04:00
parent 782cdd4cf1
commit 9821908cb0
5 changed files with 36 additions and 21 deletions

View File

@ -1,13 +1,18 @@
#include <stdlib.h>
#include <assert.h>
#include "ColoredSquare.h"
void ColoredSquare_constructor (void *this)
void ColoredSquare_constructor (void *this,
double a,
int color)
{
printf ("ColoredSquare_constructor (%lu) called\n",
(unsigned long) this);
Square_constructor (this);
((ColoredSquare *) this)->color = 0;
printf ("ColoredSquare_constructor (%lu, %f, %d) called\n",
(unsigned long) this,
a,
color);
Square_constructor (this, a);
((ColoredSquare *) this)->color = color;
}
void ColoredSquare_copy (void *to, void *from)
@ -21,7 +26,7 @@ void ColoredSquare_copy (void *to, void *from)
void* ColoredSquare_clone (void *this)
{
ColoredSquare *csquare = ColoredSquare_new ();
ColoredSquare *csquare = ColoredSquare_new (0.0, 0);
printf ("ColoredSquare_clone (%lu) called\n",
(unsigned long) this);
ColoredSquare_copy (csquare, this);
@ -62,7 +67,7 @@ void ColoredSquare_set_color (void *this, int color)
}
/* public */
void* ColoredSquare_new ()
void* ColoredSquare_new (double a, int color)
{
static ColoredSquare_interface vtable =
{
@ -77,7 +82,11 @@ void* ColoredSquare_new ()
};
ColoredSquare *square = malloc (sizeof (*square));
printf ("ColoredSquare_new () returns %lu\n",
ColoredSquare_constructor (square, a, color);
printf ("ColoredSquare_new (%f, %d) returns %lu\n",
a,
color,
(unsigned long) square);
square->vtable = (void *) &vtable;

View File

@ -19,7 +19,9 @@ typedef struct ColoredSquare_interface
} ColoredSquare_interface;
void ColoredSquare_constructor (void *this);
void ColoredSquare_constructor (void *this,
double a,
int color);
void ColoredSquare_copy (void *to, void *from);
void* ColoredSquare_clone (void *this);
const char* ColoredSquare_type (void *this);
@ -35,6 +37,6 @@ typedef struct ColoredSquare
} ColoredSquare;
void* ColoredSquare_new ();
void* ColoredSquare_new (double a, int color);
#endif // __COLORED_SQUARE_H__

View File

@ -2,11 +2,12 @@
#include "Square.h"
void Square_constructor (void *this)
void Square_constructor (void *this, double a)
{
printf ("Square_constructor (%lu) called\n",
(unsigned long) this);
((Square *) this)->a = 0.0;
printf ("Square_constructor (%lu, %f) called\n",
(unsigned long) this,
a);
((Square *) this)->a = a;
}
void Square_destructor (void *this)
@ -25,7 +26,7 @@ void Square_copy (void *to, void *from)
void* Square_clone (void *this)
{
Square *square = Square_new ();
Square *square = Square_new (0.0);
printf ("Square_clone (%lu) called\n",
(unsigned long) this);
Square_copy (square, this);
@ -97,7 +98,7 @@ double Square_diag_length (void *this)
}
/* public */
void* Square_new ()
void* Square_new (double a)
{
static Square_interface vtable =
{
@ -111,7 +112,10 @@ void* Square_new ()
};
Square *square = malloc (sizeof (*square));
printf ("Square_new () returns %lu\n",
Square_constructor (square, a);
printf ("Square_new (%f) returns %lu\n",
a,
(unsigned long) square);
square->vtable = (void *) &vtable;

View File

@ -18,7 +18,7 @@ typedef struct Square_interface
} Square_interface;
void Square_constructor (void *this);
void Square_constructor (void *this, double a);
void Square_destructor (void *this);
void Square_copy (void *to, void *from);
void* Square_clone (void *this);
@ -38,6 +38,6 @@ typedef struct Square
} Square;
void* Square_new ();
void* Square_new (double a);
#endif // __SQUARE_H__

4
main.c
View File

@ -8,8 +8,8 @@ int main (int argc, char *argv[])
int i = 0;
Figure *fig[3];
fig[0] = Square_new ();
fig[1] = ColoredSquare_new ();
fig[0] = Square_new (1);
fig[1] = ColoredSquare_new (2, 2);
((ColoredSquare *) fig[1])->vtable->set_color (fig[1], 5);
((Square *) fig[0])->vtable->resize (fig[0], 2);