#3 refining of object macros
[svn/Cll1h/.git] / demos / objects / objects-wiki.c
... / ...
CommitLineData
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
9def_type(Animal);
10
11def_mem(Actions)
12{
13 str method(talk) (Animal self);
14};
15
16def_obj(Animal)
17{
18 interface(Actions);
19 char *name;
20};
21
22construct(Animal,Actions) (Animal self, str name)
23{
24 self->name = name;
25 return self;
26}
27
28/* implementation of methods */
29
30str catTalk(Animal self)
31{
32 return "Meow!";
33}
34
35str dogTalk(Animal self)
36{
37 return "Arf! Arf!";
38}
39
40/* registration of methods to object interface */
41
42Actions catActions(void)
43{
44 Get_mem(this, Actions);
45
46 this->talk = catTalk;
47 return this;
48}
49
50Actions dogActions(void)
51{
52 Get_mem(this, Actions);
53
54 this->talk = dogTalk;
55 return this;
56}
57
58/* usage of objects inside C<<1 program */
59
60program
61{
62 int i;
63 Animal animal[3];
64 Actions cat = catActions();
65 Actions dog = dogActions();
66
67 animal[0] = get_obj(Animal, cat, "Missy");
68 animal[1] = get_obj(Animal, cat, "Mr. Bojangles");
69 animal[2] = get_obj(Animal, dog, "Lassie");
70
71 for_range(i, 0, 2)
72 {
73 print(animal[i]->name, ":", _(talk,animal[i]));
74 }
75}
This page took 0.135532 seconds and 4 git commands to generate.