Skip to content

Commit 8f0058f

Browse files
author
Christian Howe
committed
Added private variables and a second example object
1 parent 03cb5d6 commit 8f0058f

File tree

6 files changed

+86
-34
lines changed

6 files changed

+86
-34
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
build:
2-
gcc main.c animal.c dog.c -o test
2+
gcc -g main.c animal.c dog.c -o test
33

44
memcheck:
55
valgrind ./test

animal.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
#include "animal.h"
22

3-
int animal_new(animal_t* self, const char* type) {
4-
if (type == NULL) {
3+
typedef struct {
4+
char* type;
5+
} animal_p;
6+
7+
int animal_new(animal_t* self, const char* type, const char* name) {
8+
if (type == NULL || name == NULL) {
59
return ERROR_VAL;
610
}
711

8-
self->type = malloc(strlen(type)+1);
9-
strcpy(self->type, type);
12+
self->vtable.speak = NULL;
13+
14+
self->name = malloc(strlen(name)+1);
15+
strcpy(self->name, name);
16+
17+
self->private = malloc(sizeof(animal_p));
18+
animal_p* private = self->private;
19+
20+
private->type = malloc(strlen(type) + 1);
21+
strcpy(private->type, type);
1022

1123
return SUCCESS_VAL;
1224
}
1325

1426
int animal_delete(animal_t* self) {
15-
if (self->type != NULL) {
16-
free(self->type);
17-
self->type = NULL;
27+
if (self->name != NULL) {
28+
free(self->name);
29+
self->name = NULL;
30+
}
31+
32+
if (self->private != NULL) {
33+
animal_p* private = self->private;
34+
if (private->type != NULL) {
35+
free(private->type);
36+
private->type = NULL;
37+
}
38+
free(private);
39+
self->private = NULL;
1840
}
1941

2042
return SUCCESS_VAL;
@@ -25,5 +47,10 @@ void animal_speak(animal_t* self) {
2547
return self->vtable.speak(self);
2648
}
2749

28-
printf("This %s cannot speak!", self->type);
50+
printf("%s cannot speak. Poor %s!\n", self->name, Animal.getType(self));
51+
}
52+
53+
const char* animal_getType(animal_t* self) {
54+
animal_p* private = self->private;
55+
return private->type;
2956
}

animal.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,30 @@ typedef struct {
1212
void (*speak)(void* self);
1313
} vtable;
1414

15-
char* type;
15+
void* private;
16+
char* name;
1617
} animal_t;
1718

18-
int animal_new(animal_t* self, const char* type);
19+
int animal_new(animal_t* self, const char* type, const char* name);
1920

2021
int animal_delete(animal_t* self);
2122

2223
void animal_speak(animal_t* self);
2324

25+
const char* animal_getType(animal_t* self);
26+
2427
// Create a nice structure to expose them in :D
2528
static const struct {
26-
int (*new)(animal_t*, const char*);
29+
int (*new)(animal_t*, const char*, const char*);
2730
int (*delete)(animal_t*);
2831

2932
void (*speak)(animal_t*);
33+
const char* (*getType)(animal_t*);
3034
} Animal = {
3135
.new = &animal_new,
3236
.delete = &animal_delete,
3337
.speak = &animal_speak,
38+
.getType = &animal_getType
3439
};
3540

3641
#endif // ANIMAL_H

dog.c

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,62 @@
11
#include "dog.h"
22

3+
typedef struct {
4+
char* furColor;
5+
char* size;
6+
} dog_p;
7+
38
void dog_speak (void* _self) {
49
dog_t* self = _self;
5-
printf("'woof!' says the %s %s dog!\n", self->size, self->furColor);
10+
printf("'woof!' says the %s %s dog!\n", Dog.getSize(self), Dog.getFurColor(self));
611
}
712

8-
int dog_new (dog_t* self, const char* furColor, const char* size) {
13+
int dog_new (dog_t* self, const char* name, const char* furColor, const char* size) {
914
if (furColor == NULL || size == NULL) {
1015
return ERROR_VAL;
1116
}
1217

13-
Animal.new(&(self->super), "dog");
18+
Animal.new(&(self->super), "dog", name);
1419

1520
self->super.vtable.speak = &dog_speak;
1621

17-
self->furColor = malloc(strlen(furColor) + 1);
18-
strcpy(self->furColor, furColor);
22+
self->private = malloc(sizeof(dog_p));
23+
dog_p* private = self->private;
24+
25+
private->furColor = malloc(strlen(furColor) + 1);
26+
strcpy(private->furColor, furColor);
1927

20-
self->size = malloc(strlen(size) + 1);
21-
strcpy(self->size, size);
28+
private->size = malloc(strlen(size) + 1);
29+
strcpy(private->size, size);
2230

2331
return SUCCESS_VAL;
2432
}
2533

2634
int dog_delete (dog_t* self) {
2735
Animal.delete(&self->super);
2836

29-
if (self->furColor != NULL) {
30-
free(self->furColor);
31-
self->furColor = NULL;
32-
}
33-
if (self->size != NULL) {
34-
free(self->size);
35-
self->size = NULL;
37+
if (self->private != NULL) {
38+
dog_p* private = self->private;
39+
if (private->furColor != NULL) {
40+
free(private->furColor);
41+
private->furColor = NULL;
42+
}
43+
if (private->size != NULL) {
44+
free(private->size);
45+
private->size = NULL;
46+
}
47+
free(private);
48+
self->private = NULL;
3649
}
3750

3851
return SUCCESS_VAL;
3952
}
4053

4154
const char* dog_getFurColor (dog_t* self) {
42-
return self->furColor;
55+
dog_p* private = self->private;
56+
return private->furColor;
4357
}
4458

4559
const char* dog_getSize (dog_t* self) {
46-
return self->size;
60+
dog_p* private = self->private;
61+
return private->size;
4762
}

dog.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111

1212
typedef struct {
1313
animal_t super;
14-
char* furColor;
15-
char* size;
14+
void* private;
1615
} dog_t;
1716

1817
// Creates a dog, takes fur color and size. Returns pointer to dog. This copies
1918
// the strings.
20-
int dog_new (dog_t* self, const char* furColor, const char* size);
19+
int dog_new (dog_t* self, const char* name, const char* furColor, const char* size);
2120

2221
// Deletes a dog. Takes pointer to dog created with dog_ctor D:
2322
int dog_delete (dog_t* self);
@@ -32,7 +31,7 @@ const char* dog_getSize (dog_t* self);
3231

3332
// Create a nice structure to expose them in :D
3433
static const struct {
35-
int (*new)(dog_t*, const char* furColor, const char* size);
34+
int (*new)(dog_t*, const char*, const char*, const char*);
3635
int (*delete)(dog_t*);
3736

3837
const char* (*getFurColor)(dog_t*);

main.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55

66
int main (int argc, char* argv[]) {
77
dog_t* dog = malloc(sizeof(dog_t));
8-
Dog.new(dog, "white", "small");
8+
Dog.new(dog, "Scruffy", "white", "small");
99

10-
printf("The %s dog has %s fur!\n", Dog.getSize(dog), Dog.getFurColor(dog));
10+
animal_t cat;
11+
Animal.new(&cat, "cat", "Garfield");
12+
13+
printf("%s is a %s %s %s!\n", ((animal_t*)dog)->name, Dog.getSize(dog), Dog.getFurColor(dog), Animal.getType((animal_t*)dog));
1114

1215
Animal.speak((animal_t*)dog);
16+
Animal.speak(&cat);
17+
18+
Animal.delete(&cat);
1319

1420
Dog.delete(dog);
1521
free(dog);

0 commit comments

Comments
 (0)