-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimal.c
61 lines (47 loc) · 1.21 KB
/
animal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "animal.h"
typedef struct {
char* type;
} animal_p;
int animal_new(animal_t* self, const char* type, const char* name) {
if (type == NULL || name == NULL) {
return ERROR_VAL;
}
self->vtable.speak = NULL;
self->vtable.delete = NULL;
self->name = malloc(strlen(name)+1);
strcpy(self->name, name);
self->private = malloc(sizeof(animal_p));
animal_p* private = self->private;
private->type = malloc(strlen(type) + 1);
strcpy(private->type, type);
return SUCCESS_VAL;
}
int animal_delete(animal_t* self) {
if (self->vtable.delete != NULL) {
return self->vtable.delete(self);
}
if (self->name != NULL) {
free(self->name);
self->name = NULL;
}
if (self->private != NULL) {
animal_p* private = self->private;
if (private->type != NULL) {
free(private->type);
private->type = NULL;
}
free(private);
self->private = NULL;
}
return SUCCESS_VAL;
}
void animal_speak(animal_t* self) {
if (self->vtable.speak) {
return self->vtable.speak(self);
}
printf("%s cannot speak. Poor %s!\n", self->name, Animal.getType(self));
}
const char* animal_getType(animal_t* self) {
animal_p* private = self->private;
return private->type;
}