3 /* This is example of advanced C<<1 object oriented features... */
9 void method(draw
) (Shape self
);
10 void method(move
) (Shape self
, int x
, int y
);
11 str
method(desc
) (Shape self
);
12 void method(rename
) (Shape self
, str name
);
13 void method(reset
) (Shape self
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
);
14 int method(area
) (Shape self
);
18 /* We intentionaly define heterogenous objects, which share only the interface
19 binding. Not only we can use different order of variables, and different
20 names - but we can even store these data on remote systems, etc. */
25 int x1
, y1
, x2
, y2
, x3
, y3
;
44 def_community(Shape
, Interface
);
46 /* Note: interface(Interface); has to be at first position in all objects asociated in community */
55 /* implementation of methods */
57 void drawTri(Shape community
)
61 printf("Drawing %s: %d,%d - %d,%d - %d,%d .\n",
62 self
->name
, self
->x1
, self
->y1
, self
->x2
, self
->y2
, self
->x3
, self
->y3
);
65 void drawRect(Shape community
)
69 printf("Drawing %s: %d,%d - %d,%d .\n",
70 self
->desc
, self
->x1
, self
->y1
, self
->x2
, self
->y2
);
73 void drawCirc(Shape community
)
77 printf("Drawing %s: %d,%d - r=%d .\n",
78 self
->comment
, self
->x1
, self
->y1
, self
->r
);
81 void moveTri(Shape community
, int x
, int y
)
93 void moveRect(Shape community
, int x
, int y
)
103 void moveCirc(Shape community
, int x
, int y
)
111 str
descTri(Shape community
)
116 str
descRect(Shape community
)
121 str
descCirc(Shape community
)
126 int calcAreaTri(Shape community
)
131 int calcAreaRect(Shape community
)
136 int calcAreaCirc(Shape community
)
141 void setTri(Shape community
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
153 void setRect(Shape community
, int x1
, int y1
, int x2
, int y2
, int dummy1
, int dummy2
)
163 void setCirc(Shape community
, int x1
, int y1
, int r
, int dummy1
, int dummy2
, int dummy3
)
172 void nameRect(Shape community
, str name
)
179 /* Example of using object interface methods from inside constructor methods */
181 construct(Tri
) (Tri self
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
183 self
->name
= "TRIANGLE";
184 interface_of(self
)->count
++;
185 _(reset
, self
, x1
, y1
, x2
, y2
, x3
, y3
);
190 construct(Rect
) (Rect self
, int x1
, int y1
, int x2
, int y2
)
192 self
->desc
= "rectangle";
193 interface_of(self
)->count
++;
194 _(reset
, self
, x1
, y1
, x2
, y2
, 0, 0);
199 construct(Circ
) (Circ self
, int x1
, int y1
, int r
)
201 self
->comment
= "Circle";
202 interface_of(self
)->count
++;
203 _(reset
, self
, x1
, y1
, r
, 0, 0, 0);
208 /* registration of implemented methods to three interfaces of the same type */
210 Interface
triInterface(void)
212 Get_mem(this, Interface
);
214 this->draw
= drawTri
;
215 this->move
= moveTri
;
216 this->desc
= descTri
;
218 this->reset
= setTri
;
219 this->area
= calcAreaTri
;
225 Interface
rectInterface(void)
227 Get_mem(this, Interface
);
229 this->draw
= drawRect
;
230 this->move
= moveRect
;
231 this->desc
= descRect
;
232 this->rename
= nameRect
;
233 this->reset
= setRect
;
234 this->area
= calcAreaRect
;
240 Interface
circInterface(void)
242 Get_mem(this, Interface
);
244 this->draw
= drawCirc
;
245 this->move
= moveCirc
;
246 this->desc
= descCirc
;
248 this->reset
= setCirc
;
249 this->area
= calcAreaCirc
;
255 /* usage of objects inside C<<1 program */
259 Interface triangles
= triInterface();
260 Interface rectangles
= rectInterface();
261 Interface circles
= circInterface();
262 Object_list all
= NULL
, one
;
264 one
= get_mem(Object_list
);
265 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 0, 10, 1, 11);
268 one
= get_mem(Object_list
);
269 one
->object
= get_obj_as(Shape
, Tri
, triangles
, 0, 0, 0, 4, 3, 0);
272 one
= get_mem(Object_list
);
273 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 10, 0, 11, 1);
276 one
= get_mem(Object_list
);
277 one
->object
= get_obj_as(Shape
, Circ
, circles
, 10, 0, 5);
280 printf("We have created %d triangles, %d rectangles and %d circles:\n",
281 triangles
->count
, rectangles
->count
, circles
->count
);
285 _(draw
, one
->object
);
286 printf("Area of this %s is %d (square pixels).\n", _(desc
, one
->object
), _(area
, one
->object
));
287 printf("We have created %d instances of this type of object.\n", interface_of(one
->object
)->count
);
289 if(interface_of(one
->object
) == rectangles
)
291 _(reset
, one
->object
, 0, 0, 1, 1, 0, 0);
292 _(rename
, one
->object
, "Renamed rectangle");
296 _(move
, one
->object
, 10, 10);
300 print("Rectangles were reset to new size and renamed, other objects were moved:");
303 _(draw
, one
->object
);
This page took 0.430216 seconds and 4 git commands to generate.