#define _GNU_SOURCE #include #include #include #include "student_v2.h" Vtable_entry Student_vtable[STUDENT_SET_ID+1]; Student *student_init(Student *s, const char *name, unsigned int id) { if (person_init((Person*)s, name) == NULL ) return NULL; s->id = id; return s; } Student *student_new(const char *name, unsigned int id) { Student *s = malloc(sizeof(Student)); if (!s) { return NULL; } s->vtable = Student_vtable; if (student_init(s, name, id) == NULL ) { free(s); return NULL; } return s; } int student_delete(Student *s) { if (s==NULL) return -1; if ( person_clean((Person*)s) != 0 ) return -1; free(s); return 0; // Assume it always works :) } char *student_toString(const Student *s) { char *str = NULL; if (s==NULL) return NULL; if (asprintf(&str, "Name: %s\n", s->name) <= 0) return NULL; return str; } const char *student_get_name(const Student *s) { if (s==NULL) return NULL; return s->name; } unsigned int student_get_id(Student *s) { return s->id; } int student_set_id(Student *s, unsigned int new_id) { if (s == NULL) return -1; s->id = new_id; return 0; } Vtable_entry Student_vtable[STUDENT_SET_ID+1] = { student_delete, student_toString, person_get_name, student_get_id, student_set_id };