def_interface() in object demo
[svn/Cll1h/.git] / demos / objects / objects-wiki.c
index 50e1288f2d82cd9fd58a158991751649d7a12beb..871488b2686ecdb89dc969b607ea63e6042cb0ef 100644 (file)
@@ -6,62 +6,69 @@
    Of course there are easier ways to do the same thing... :-) 
    (G)2008 xChas */
 
-def_mem(Actions)
+def_interface(Animal,Actions)
 {
- str method(talk) (anonymous self);
+ str method(talk) (Animal self);
 };
 
-def_mem(Animal)
+def_obj(Animal)
 {
  interface(Actions);
  char *name;
 };
 
-construct(Animal) (Animal self, str name)
+construct(Animal,Actions) (Animal self, str name)
 {
  self->name = name;
  return self;
 }
 
-str cat_talk(anonymous self)
+/* implementation of methods */
+
+str catTalk(Animal self)
 {
  return "Meow!";
 }
 
-str dog_talk(anonymous self)
+str dogTalk(Animal self)
 {
  return "Arf! Arf!";
 }
 
-Actions cat_actions(void)
+/* registration of methods to object interface */
+
+Actions catActions(void)
 {
- Get_mem(this,Actions);
+ Get_mem(this, Actions);
 
- this->talk = cat_talk;
+ this->talk = catTalk;
  return this;
 }
 
-Actions dog_actions(void)
+Actions dogActions(void)
 {
- Get_mem(this,Actions);
+ Get_mem(this, Actions);
 
- this->talk = dog_talk;
+ 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] = 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)
  {
-  print(animal[i]->name,":",_(talk,animal[i]));
+  print(animal[i]->name, ":", _(talk, animal[i]));
  }
+
 }
This page took 0.113979 seconds and 4 git commands to generate.