binary B+ tree - first attempt, compiles and runs
[svn/Cll1h/.git] / demos / objects / objects-wiki.c
index e55cb7439a3a2ebcfb6612ee3cac2bd811ad8f10..08403cb91af186a2a8830537b0a509af8217d5c2 100644 (file)
@@ -1,64 +1,74 @@
 #include "cll1.h"
 
 /* This is C<<1 rewrite of object polymorphism from
-   http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming */
+   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_type(Animal);
-
-def_mem(Actions)
+def_interface(Animal,Actions)
 {
- Animal method(init) (Animal self, str name);
  str method(talk) (Animal self);
 };
 
 def_obj(Animal)
 {
  interface(Actions);
char *name;
str name;
 };
 
-str cat_talk(Animal self)
+construct(Animal,Actions) (Animal self, str name)
 {
- return "Meow!";
+ self->name = name;
+ return self;
 }
 
-str dog_talk(Animal self)
+/* implementation of methods */
+
+str catTalk(Animal self)
 {
- return "Arf! Arf!";
+ return "Meow!";
 }
 
-Animal animal_init(Animal self, str name)
+str dogTalk(Animal self)
 {
- self->name = name;
- return self;
+ return "Arf! Arf!";
 }
 
-Actions cat_actions(Actions this)
+/* registration of methods to object interface */
+
+Actions catActions(void)
 {
- this->init = animal_init;
- this->talk = cat_talk;
+ Get_mem(this, Actions);
+
+ this->talk = catTalk;
+ return this;
 }
 
-Actions dog_actions(Actions this)
+Actions dogActions(void)
 {
- this->init = animal_init;
- this->talk = dog_talk;
+ Get_mem(this, Actions);
+
+ this->talk = dogTalk;
+ return this;
 }
 
+/* usage of objects inside C<<1 program */
+
 program
 {
  int i;
  Animal animal[3];
- Actions cat = cat_actions();
- Actions dog = dog_actions();
+ Actions cat = catActions();
+ Actions dog = dogActions();
 
- animal[0] = init_obj(Animal, Actions, cat)
-  "Missy");
- animal[1] = init_obj(Animal, cat, "Mr. Bojangles");
- animal[2] = init_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]));
  }
+
 }
This page took 0.110086 seconds and 4 git commands to generate.