some work
[svn/Cll1h/.git] / demos / objects / objects-wiki.c
1 #include <cll1.h>
2
3 /* This is C<<1 rewrite of object polymorphism from
4 http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming */
5
6 struct Animal
7 {
8 use_interface(AnimalActions);
9 char *name;
10 };
11
12 struct AnimalActions
13 {
14 const char *(method(talk));
15 }
16
17 struct *Animal get_animal(char *name, struct AnimalActions *interface)
18 {
19 struct *Animal animal=get_object(Animal,interface);
20 animal->name=name;
21 return animal;
22 }
23
24 const char *cat_talk(void *_self)
25 {
26 return "Meow!";
27 }
28
29 const char *dog_talk(void *_self)
30 {
31 return "Arf! Arf!";
32 }
33
34 interface_implementation(CatActions,AnimalActions)
35 {
36 bind_method(talk,cat_talk);
37 }
38
39 interface_implementation(DogActions,AnimalActions)
40 {
41 bind_method(talk,dog_talk);
42 }
43
44 program
45 {
46 int i;
47 struct Animals *animal[3];
48 struct AnimalActions *cat = get_interface(CatActions);
49 struct AnimalActions *dog = get_interface(DogActions);
50
51 animal[0]=get_animal("Missy",cat);
52 animal[0]=get_animal("Mr. Bojangles",cat);
53 animal[0]=get_animal("Lassie",dog);
54
55 for_range(i,0,2)
56 printf("%s: %s\n",animal[i].name,(_(animal[i],talk)));
57
58 }
This page took 0.660894 seconds and 4 git commands to generate.