X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=demos%2Fobjects%2Fobjects-wiki.c;h=50e1288f2d82cd9fd58a158991751649d7a12beb;hb=47e4883a12f36914d419e9e5fd1a0ff97227c093;hp=127f959d096b9e74cc32483389f5767cb4af539a;hpb=1a8d78d9e9f1a4a2e8b7c3b740c5516195fbc04b;p=svn%2FCll1h%2F.git diff --git a/demos/objects/objects-wiki.c b/demos/objects/objects-wiki.c index 127f959..50e1288 100644 --- a/demos/objects/objects-wiki.c +++ b/demos/objects/objects-wiki.c @@ -1,48 +1,50 @@ #include "cll1.h" /* This is C<<1 rewrite of object polymorphism from - http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming */ - -def_type(Animal); + http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming + + Of course there are easier ways to do the same thing... :-) + (G)2008 xChas */ def_mem(Actions) { - void method(init) (Animal self, str name); - str method(talk) (Animal self); + str method(talk) (anonymous self); }; -def_obj(Animal) +def_mem(Animal) { interface(Actions); char *name; }; -Animal construct(Animal) (Animal self, str name) +construct(Animal) (Animal self, str name) { self->name = name; return self; } -str cat_talk(Animal self) +str cat_talk(anonymous self) { return "Meow!"; } -str dog_talk(Animal self) +str dog_talk(anonymous self) { return "Arf! Arf!"; } Actions cat_actions(void) { - Actions this=get_mem(Actions); + Get_mem(this,Actions); + this->talk = cat_talk; return this; } Actions dog_actions(void) { - Actions this=get_mem(Actions); + Get_mem(this,Actions); + this->talk = dog_talk; return this; } @@ -54,12 +56,12 @@ program Actions cat = cat_actions(); Actions dog = dog_actions(); - animal[0] = get_obj(Animal, cat, "Missy"); - animal[1] = get_obj(Animal, cat, "Mr. Bojangles"); - animal[2] = get_obj(Animal, dog, "Lassie"); + animal[0] = get_obj(Animal,cat,"Missy"); + animal[1] = get_obj(Animal,cat,"Mr. Bojangles"); + animal[2] = get_obj(Animal,dog,"Lassie"); for_range(i, 0, 2) { - printf("%s: %s\n", animal[i]->name, _(talk, animal[i])); + print(animal[i]->name,":",_(talk,animal[i])); } }