Add parameters to constructors.
This commit is contained in:
parent
782cdd4cf1
commit
9821908cb0
|
@ -1,13 +1,18 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#include "ColoredSquare.h"
|
#include "ColoredSquare.h"
|
||||||
|
|
||||||
void ColoredSquare_constructor (void *this)
|
void ColoredSquare_constructor (void *this,
|
||||||
|
double a,
|
||||||
|
int color)
|
||||||
{
|
{
|
||||||
printf ("ColoredSquare_constructor (%lu) called\n",
|
printf ("ColoredSquare_constructor (%lu, %f, %d) called\n",
|
||||||
(unsigned long) this);
|
(unsigned long) this,
|
||||||
Square_constructor (this);
|
a,
|
||||||
((ColoredSquare *) this)->color = 0;
|
color);
|
||||||
|
Square_constructor (this, a);
|
||||||
|
((ColoredSquare *) this)->color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ColoredSquare_copy (void *to, void *from)
|
void ColoredSquare_copy (void *to, void *from)
|
||||||
|
@ -21,7 +26,7 @@ void ColoredSquare_copy (void *to, void *from)
|
||||||
|
|
||||||
void* ColoredSquare_clone (void *this)
|
void* ColoredSquare_clone (void *this)
|
||||||
{
|
{
|
||||||
ColoredSquare *csquare = ColoredSquare_new ();
|
ColoredSquare *csquare = ColoredSquare_new (0.0, 0);
|
||||||
printf ("ColoredSquare_clone (%lu) called\n",
|
printf ("ColoredSquare_clone (%lu) called\n",
|
||||||
(unsigned long) this);
|
(unsigned long) this);
|
||||||
ColoredSquare_copy (csquare, this);
|
ColoredSquare_copy (csquare, this);
|
||||||
|
@ -62,7 +67,7 @@ void ColoredSquare_set_color (void *this, int color)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* public */
|
/* public */
|
||||||
void* ColoredSquare_new ()
|
void* ColoredSquare_new (double a, int color)
|
||||||
{
|
{
|
||||||
static ColoredSquare_interface vtable =
|
static ColoredSquare_interface vtable =
|
||||||
{
|
{
|
||||||
|
@ -77,7 +82,11 @@ void* ColoredSquare_new ()
|
||||||
};
|
};
|
||||||
ColoredSquare *square = malloc (sizeof (*square));
|
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);
|
(unsigned long) square);
|
||||||
|
|
||||||
square->vtable = (void *) &vtable;
|
square->vtable = (void *) &vtable;
|
||||||
|
|
|
@ -19,7 +19,9 @@ typedef struct ColoredSquare_interface
|
||||||
|
|
||||||
} 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_copy (void *to, void *from);
|
||||||
void* ColoredSquare_clone (void *this);
|
void* ColoredSquare_clone (void *this);
|
||||||
const char* ColoredSquare_type (void *this);
|
const char* ColoredSquare_type (void *this);
|
||||||
|
@ -35,6 +37,6 @@ typedef struct ColoredSquare
|
||||||
|
|
||||||
} ColoredSquare;
|
} ColoredSquare;
|
||||||
|
|
||||||
void* ColoredSquare_new ();
|
void* ColoredSquare_new (double a, int color);
|
||||||
|
|
||||||
#endif // __COLORED_SQUARE_H__
|
#endif // __COLORED_SQUARE_H__
|
||||||
|
|
18
Square.c
18
Square.c
|
@ -2,11 +2,12 @@
|
||||||
|
|
||||||
#include "Square.h"
|
#include "Square.h"
|
||||||
|
|
||||||
void Square_constructor (void *this)
|
void Square_constructor (void *this, double a)
|
||||||
{
|
{
|
||||||
printf ("Square_constructor (%lu) called\n",
|
printf ("Square_constructor (%lu, %f) called\n",
|
||||||
(unsigned long) this);
|
(unsigned long) this,
|
||||||
((Square *) this)->a = 0.0;
|
a);
|
||||||
|
((Square *) this)->a = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Square_destructor (void *this)
|
void Square_destructor (void *this)
|
||||||
|
@ -25,7 +26,7 @@ void Square_copy (void *to, void *from)
|
||||||
|
|
||||||
void* Square_clone (void *this)
|
void* Square_clone (void *this)
|
||||||
{
|
{
|
||||||
Square *square = Square_new ();
|
Square *square = Square_new (0.0);
|
||||||
printf ("Square_clone (%lu) called\n",
|
printf ("Square_clone (%lu) called\n",
|
||||||
(unsigned long) this);
|
(unsigned long) this);
|
||||||
Square_copy (square, this);
|
Square_copy (square, this);
|
||||||
|
@ -97,7 +98,7 @@ double Square_diag_length (void *this)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* public */
|
/* public */
|
||||||
void* Square_new ()
|
void* Square_new (double a)
|
||||||
{
|
{
|
||||||
static Square_interface vtable =
|
static Square_interface vtable =
|
||||||
{
|
{
|
||||||
|
@ -111,7 +112,10 @@ void* Square_new ()
|
||||||
};
|
};
|
||||||
Square *square = malloc (sizeof (*square));
|
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);
|
(unsigned long) square);
|
||||||
|
|
||||||
square->vtable = (void *) &vtable;
|
square->vtable = (void *) &vtable;
|
||||||
|
|
4
Square.h
4
Square.h
|
@ -18,7 +18,7 @@ typedef struct Square_interface
|
||||||
|
|
||||||
} Square_interface;
|
} Square_interface;
|
||||||
|
|
||||||
void Square_constructor (void *this);
|
void Square_constructor (void *this, double a);
|
||||||
void Square_destructor (void *this);
|
void Square_destructor (void *this);
|
||||||
void Square_copy (void *to, void *from);
|
void Square_copy (void *to, void *from);
|
||||||
void* Square_clone (void *this);
|
void* Square_clone (void *this);
|
||||||
|
@ -38,6 +38,6 @@ typedef struct Square
|
||||||
|
|
||||||
} Square;
|
} Square;
|
||||||
|
|
||||||
void* Square_new ();
|
void* Square_new (double a);
|
||||||
|
|
||||||
#endif // __SQUARE_H__
|
#endif // __SQUARE_H__
|
||||||
|
|
4
main.c
4
main.c
|
@ -8,8 +8,8 @@ int main (int argc, char *argv[])
|
||||||
int i = 0;
|
int i = 0;
|
||||||
Figure *fig[3];
|
Figure *fig[3];
|
||||||
|
|
||||||
fig[0] = Square_new ();
|
fig[0] = Square_new (1);
|
||||||
fig[1] = ColoredSquare_new ();
|
fig[1] = ColoredSquare_new (2, 2);
|
||||||
|
|
||||||
((ColoredSquare *) fig[1])->vtable->set_color (fig[1], 5);
|
((ColoredSquare *) fig[1])->vtable->set_color (fig[1], 5);
|
||||||
((Square *) fig[0])->vtable->resize (fig[0], 2);
|
((Square *) fig[0])->vtable->resize (fig[0], 2);
|
||||||
|
|
Loading…
Reference in New Issue