#3 FINALY !!! objects-wiki.c compiles (with -Wall) and runs !
[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 Of course there are easier ways to do the same thing... :-)
7 (G)2008 xChas */
8
9 def_mem(Actions)
10 {
11 str method(talk) (anonymous self);
12 };
13
14 def_mem(Animal)
15 {
16 interface(Actions);
17 char *name;
18 };
19
20 construct(Animal) (Animal self, str name)
21 {
22 self->name = name;
23 return self;
24 }
25
26 str cat_talk(anonymous self)
27 {
28 return "Meow!";
29 }
30
31 str dog_talk(anonymous self)
32 {
33 return "Arf! Arf!";
34 }
35
36 Actions cat_actions(void)
37 {
38 Get_mem(this,Actions);
39
40 this->talk = cat_talk;
41 return this;
42 }
43
44 Actions dog_actions(void)
45 {
46 Get_mem(this,Actions);
47
48 this->talk = dog_talk;
49 return this;
50 }
51
52 program
53 {
54 int i;
55 Animal animal[3];
56 Actions cat = cat_actions();
57 Actions dog = dog_actions();
58
59 animal[0] = get_obj(Animal,cat,"Missy");
60 animal[1] = get_obj(Animal,cat,"Mr. Bojangles");
61 animal[2] = get_obj(Animal,dog,"Lassie");
62
63 for_range(i, 0, 2)
64 {
65 print(animal[i]->name,":",_(talk,animal[i]));
66 }
67 }
This page took 0.256616 seconds and 4 git commands to generate.