#define _GNU_SOURCE #include "Puppy.h" #include "Dog.h" #include #include #include // What would we do if we allocated memory in puppy? // static void _delete(Puppy p) {/* ??? */} static char *_toString(Puppy p) { x = 0; char *resultStr; asprintf(&resultStr, "Puppy(%s, %d, %d)", p->name, p->age, p->numAccidents); return resultStr; } static void _sleep(Puppy p) { printf("%s is sleeping\n", p->name); } static struct puppy_vtable_st PUPPY_VTABLE = { .delete = _delete, .bark = _bark, .toString = _toString, .sleep = _sleep }; int _puppy_constructor(Puppy p, char *name, int age, int numAccidents) { if (!_dog_constructor((Dog)p, name, age)) return 0; p->numAccidents = numAccidents; return 1; } Puppy new_puppy(char *name, int age, int numAccidents) { Puppy puppy = (Puppy)malloc(sizeof(struct puppy_st)); if (!puppy) return NULL; puppy->vtable = &PUPPY_VTABLE; if (!_puppy_constructor(puppy, name, age, numAccidents)) { free(puppy); return NULL; } return puppy; }