5b005636bdd2b04ffe6c0b8ca6fd8f28091fed39
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 float 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
)
118 str
descRect(Shape community
)
125 str
descCirc(Shape community
)
129 return self
->comment
;
132 float calcAreaTri(Shape community
)
139 float calcAreaRect(Shape community
)
143 return (self
->x2
- self
->x1
)*(self
->y2
- self
->y1
);
146 float calcAreaCirc(Shape community
)
150 return PI
* self
->r
* self
->r
;
153 void setTri(Shape community
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
165 void setRect(Shape community
, int x1
, int y1
, int x2
, int y2
, int dummy1
, int dummy2
)
175 void setCirc(Shape community
, int x1
, int y1
, int r
, int dummy1
, int dummy2
, int dummy3
)
184 void nameRect(Shape community
, str name
)
191 /* Example of using object interface methods from inside constructor methods */
193 construct(Tri
,Interface
) (Tri self
, int x1
, int y1
, int x2
, int y2
, int x3
, int y3
)
195 self
->name
= "TRIANGLE";
196 interface_of(self
)->count
++;
197 _(reset
, self
, x1
, y1
, x2
, y2
, x3
, y3
);
202 construct(Rect
,Interface
) (Rect self
, int x1
, int y1
, int x2
, int y2
)
204 self
->desc
= "rectangle";
205 interface_of(self
)->count
++;
206 _(reset
, self
, x1
, y1
, x2
, y2
, 0, 0);
211 construct(Circ
,Interface
) (Circ self
, int x1
, int y1
, int r
)
213 self
->comment
= "Circle";
214 interface_of(self
)->count
++;
215 _(reset
, self
, x1
, y1
, r
, 0, 0, 0);
220 /* registration of implemented methods to three interfaces of the same type */
222 Interface
triInterface(void)
224 Get_mem(this, Interface
);
226 this->draw
= drawTri
;
227 this->move
= moveTri
;
228 this->desc
= descTri
;
230 this->reset
= setTri
;
231 this->area
= calcAreaTri
;
237 Interface
rectInterface(void)
239 Get_mem(this, Interface
);
241 this->draw
= drawRect
;
242 this->move
= moveRect
;
243 this->desc
= descRect
;
244 this->rename
= nameRect
;
245 this->reset
= setRect
;
246 this->area
= calcAreaRect
;
252 Interface
circInterface(void)
254 Get_mem(this, Interface
);
256 this->draw
= drawCirc
;
257 this->move
= moveCirc
;
258 this->desc
= descCirc
;
260 this->reset
= setCirc
;
261 this->area
= calcAreaCirc
;
267 /* usage of objects inside C<<1 program */
271 Interface triangles
= triInterface();
272 Interface rectangles
= rectInterface();
273 Interface circles
= circInterface();
274 Object_list all
= NULL
, one
;
276 one
= get_mem(Object_list
);
277 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 0, 10, 1, 11);
280 one
= get_mem(Object_list
);
281 one
->object
= get_obj_as(Shape
, Tri
, triangles
, 0, 0, 0, 4, 3, 0);
284 one
= get_mem(Object_list
);
285 one
->object
= get_obj_as(Shape
, Rect
, rectangles
, 10, 0, 11, 1);
288 one
= get_mem(Object_list
);
289 one
->object
= get_obj_as(Shape
, Circ
, circles
, 0, 10, 1);
292 printf("We have created %d triangles, %d rectangles and %d circles:\n",
293 triangles
->count
, rectangles
->count
, circles
->count
);
297 _(draw
, one
->object
);
298 printf("Area of this %s is %f (square pixels).\n", _(desc
, one
->object
), _(area
, one
->object
));
299 printf("We have created %d instances of this type of object.\n", interface_of(one
->object
)->count
);
301 if(interface_of(one
->object
) == rectangles
)
303 _(reset
, one
->object
, 0, 0, 1, 1, 0, 0);
304 _(rename
, one
->object
, "Renamed rectangle");
308 _(move
, one
->object
, 10, 10);
312 print("Rectangles were reset to new size and renamed, other objects were moved:");
315 _(draw
, one
->object
);
This page took 0.550834 seconds and 4 git commands to generate.