some unfinished work (from Thera island and later)
[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 def_type(Animal);
7
8 def_mem(Actions)
9 {
10 Animal method(init) (Animal self, str name);
11 str method(talk) (Animal self);
12 };
13
14 def_obj(Animal)
15 {
16 interface(Actions);
17 char *name;
18 };
19
20 str cat_talk(Animal self)
21 {
22 return "Meow!";
23 }
24
25 str dog_talk(Animal self)
26 {
27 return "Arf! Arf!";
28 }
29
30 Animal animal_init(Animal self, str name)
31 {
32 self->name = name;
33 return self;
34 }
35
36 Actions cat_actions(Actions this)
37 {
38 this->init = animal_init;
39 this->talk = cat_talk;
40 }
41
42 Actions dog_actions(Actions this)
43 {
44 this->init = animal_init;
45 this->talk = dog_talk;
46 }
47
48 program
49 {
50 int i;
51 Animal animal[3];
52 Actions cat = cat_actions();
53 Actions dog = dog_actions();
54
55 animal[0] = init_obj(Animal, Actions, cat)
56 "Missy");
57 animal[1] = init_obj(Animal, cat, "Mr. Bojangles");
58 animal[2] = init_obj(Animal, dog, "Lassie");
59
60 for_range(i, 0, 2)
61 {
62 printf("%s: %s\n", animal[i].name, _(talk, animal[i]));
63 }
64 }
This page took 0.365559 seconds and 4 git commands to generate.