Inclusion declaration is a relation among the classes of a component.
This example demonstrates the use of inclusion declarations.
It is made from four files.
The Queue component and I/O functions are discussed already. Therefore only the other components are discussed.
Geometry
Geometry component define some geometric shapes. It has a class to denote a geometric shapes (Shape). In addition, it defines two specific shapes: Rectangle and Triangle. It then defines an inclusion relation to show that they are shapes.
Shape = Rectangle|Triangle;
The Shape class has a name, a constructor and two methods. One of them (print) has body while the other one (perimeter) does not have.
int Shape.perimeter ();
This is an abstract method. Therefore, every subclass of Shape should implement it. In the code, both of Rectangle and Triangle have a perimeter method to calculate their perimeter.
The component has an interface class (Shapes) . It has a Queue whose root is stored in Shapes to store its shapes.
Queue[Shapes, Shape];
App component
App is the main component of the application. It creates one object of type Shapes, stores a triangle and a rectangle in it and finally prints the perimeters of the objects stored in the Shapes object.
Geometry[Shapes]
foreign void cprint (char[] msg, int v);
Shape[];
Rectangle[];
Triangle[];
Shape = Rectangle | Triangle;
Queue[Shapes, Shape];
Shape
{
char[] name;
}
Shape (char[] _name)
{
name = _name;
}
int Shape.perimeter ();
void Shape.print ()
{
cprint (name, perimeter ());
}
Rectangle
{
int length, width;
}
Rectangle (int _length, int _width)
: Shape (“rectangle”)
{
length = _length;
width = _width;
}
int Rectangle.perimeter ()
{
return 2 * (length + width);
}
Triangle
{
int[3] edge;
}
Triangle (int _edge1, int _edge2, int _edge3)
: Shape (“triangle”)
{
edge[0] = _edge1;
edge[1] = _edge2;
edge[2] = _edge3;
}
int Triangle.perimeter ()
{
return edge[0] + edge[1] + edge[2];
}
public void Shapes.print ()
{
Shape p;
while (p = deque ())
p.print ();
}
public void Shapes.addRectangle (int length, int width)
{
enqueue (new Rectangle (length, width));
}
public void Shapes.addTriangle
(int edge1, int edge2, int edge3)
{
enqueue (new Triangle (edge1, edge2, edge3));
}
Geometry component
App
Shapes[];
Geometry[Shapes];
foreign int main ()
{
Shapes p;
p = new Shapes ();
p.addTriangle (23, 12, 7);
p.addRectangle (7, 13);
p.print ();
}