3 /* This is example of advanced C<<1 object oriented features... */
10 void method(draw
) (Shape self
);
11 void method(move
) (Shape self
, int x
, int y
);
12 str
method(desc
) (Shape self
);
13 void method(rename
) (Shape self
, str name
);
14 void method(reset
) (Shape self
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
);
15 float method(area
) (Shape self
);
19 /* This is example of hierarchical object architecture in C<<1 */
49 def_community(Shape
, Interface
);
51 /* Note: interface(Interface); has to be at first position in all objects asociated in community */
59 /* implementation of methods */
61 void drawTri(Shape community
)
65 printf("Drawing %s: %d,%d - %d,%d - %d,%d .\n",
66 self
->point
->desc
, self
->point
->x
, self
->point
->y
, self
->x2
, self
->y2
, self
->x3
, self
->y3
);
69 void drawRect(Shape community
)
73 printf("Drawing %s: %d,%d - %d,%d .\n",
74 self
->point
->desc
, self
->x1
, self
->y1
, self
->x2
, self
->y2
);
77 void drawCirc(Shape community
)
81 printf("Drawing %s: %d,%d - r=%d .\n",
82 self
->comment
, self
->x1
, self
->y1
, self
->r
);
85 void moveTri(Shape community
, int x
, int y
)
97 void moveRect(Shape community
, int x
, int y
)
107 void moveCirc(Shape community
, int x
, int y
)
115 str
descTri(Shape community
)
119 return _(desc
, self
->point
);
122 str
descRect(Shape community
)
126 return _(desc
, self
->point
);
129 str
descCirc(Shape community
)
133 return _(desc
, self
->point
);
136 float calcAreaTri(Shape community
)
143 float calcAreaRect(Shape community
)
147 return (self
->x2
- self
->x1
)*(self
->y2
- self
->y1
);
150 float calcAreaCirc(Shape community
)
154 return PI
* self
->r
* self
->r
;
157 void setTri(Shape community
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
169 void setRect(Shape community
, int x1
, int y1
, int x2
, int y2
, int dummy1
, int dummy2
)
179 void setCirc(Shape community
, int x1
, int y1
, int r
, int dummy1
, int dummy2
, int dummy3
)
188 void nameRect(Shape community
, str newname
)
192 _(name
, self
->point
, newname
);
195 /* Example of using object interface methods from inside constructor methods */
197 construct(Tri
,Interface
) (Tri self
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
199 self
->name
= "TRIANGLE";
200 interface_of(self
)->count
++;
201 _(reset
, self
, x1
, y1
, x2
, y2
, x3
, y3
);
206 construct(Rect
,Interface
) (Rect self
, int x1
, int y1
, int x2
, int y2
)
208 self
->desc
= "rectangle";
209 interface_of(self
)->count
++;
210 _(reset
, self
, x1
, y1
, x2
, y2
, 0, 0);
215 construct(Circ
,Interface
) (Circ self
, int x1
, int y1
, int r
)
217 self
->comment
= "Circle";
218 interface_of(self
)->count
++;
219 _(reset
, self
, x1
, y1
, r
, 0, 0, 0);
224 /* registration of implemented methods to three interfaces of the same type */
226 Interface
pointInterface(void)
228 Get_mem(this, Interface
);
231 this->move
= movePoint
;
232 this->desc
= descPoint
;
234 this->reset
= setTri
;
235 this->area
= calcAreaTri
;
241 Interface
triInterface(void)
243 Get_mem(this, Interface
);
245 this->draw
= drawTri
;
246 this->move
= moveTri
;
247 this->desc
= descTri
;
249 this->reset
= setTri
;
250 this->area
= calcAreaTri
;
256 Interface
rectInterface(void)
258 Get_mem(this, Interface
);
260 this->draw
= drawRect
;
261 this->move
= moveRect
;
262 this->desc
= descRect
;
263 this->rename
= nameRect
;
264 this->reset
= setRect
;
265 this->area
= calcAreaRect
;
271 Interface
circInterface(void)
273 Get_mem(this, Interface
);
275 this->draw
= drawCirc
;
276 this->move
= moveCirc
;
277 this->desc
= descCirc
;
279 this->reset
= setCirc
;
280 this->area
= calcAreaCirc
;
286 /* usage of objects inside C<<1 program */
290 Interface triangles
= triInterface();
291 Interface rectangles
= rectInterface();
292 Interface circles
= circInterface();
293 Object_list all
= NULL
, one
;
295 one
= get_mem(Object_list
);
296 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 0, 10, 1, 11);
299 one
= get_mem(Object_list
);
300 one
->object
= get_obj_as(Shape
, Tri
, triangles
, 0, 0, 0, 4, 3, 0);
303 one
= get_mem(Object_list
);
304 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 10, 0, 11, 1);
307 one
= get_mem(Object_list
);
308 one
->object
= get_obj_as(Shape
, Circ
, circles
, 0, 10, 1);
311 printf("We have created %d triangles, %d rectangles and %d circles:\n",
312 triangles
->count
, rectangles
->count
, circles
->count
);
316 _(draw
, one
->object
);
317 printf("Area of this %s is %f (square pixels).\n", _(desc
, one
->object
), _(area
, one
->object
));
318 printf("We have created %d instances of this type of object.\n", interface_of(one
->object
)->count
);
320 if(interface_of(one
->object
) == rectangles
)
322 _(reset
, one
->object
, 0, 0, 1, 1, 0, 0);
323 _(rename
, one
->object
, "Renamed rectangle");
327 _(move
, one
->object
, 10, 10);
331 print("Rectangles were reset to new size and renamed, other objects were moved:");
334 _(draw
, one
->object
);
This page took 0.385349 seconds and 4 git commands to generate.