Although inclusion declaration can be used only among the classes of a component combining it with instance declaration provides the same service among the classes of different components.

In this lesson, the Geometry component of the previous lesson is rewritten to show this capability.

This is the Geometry example of the previous lesson. It has been revised to extend an inclusion relation among the classes of different components.

For this purpose, the Geometry component only contains general code of handling geometry shapes. The code that defines Triangle and Rectangle are moved to the App component.

The example is made from the following files.

  • Geometry component
  • App component
  • Queue component
  • I/O functions in C
Geometry component

Geometry component has two interface classes: Shapes and Shape. Shapes has a queue of geometric shapes.

Shape is a geometric shape. It has become an interface class so that instantiating components can extend it.

The body of the component is straightforward. It defines the behavior of Shapes and Shape classes which is highly similar to the previous lesson.

An important difference between this example and the previous one is that Shape.perimeter is defined extern in this lesson.

extern int Shape.perimeter ();

It means that every component that instantiates Geometry should provide an implementation for its Shape.perimeter method.

App component

App is the main component of the application. Compared with the previous example it has the declaration of Rectangle and Triangle now.

In addition, it has the inclusion relation. Note the way that it is used.

For this purpose, App has an internal class named Shape itself.

It then has an instance of Geometry component which add Geometry.Shape to App.Shape.

It then defines the inclusion relation. To clarify they are gathered together here.


Shapes[];

Shape[];

Geometry[Shapes, Shape];

Shape = Triangle | Rectangle;


In this manner App.Shape inherits from Geometry.Shape and Triangle inherits from App.Shape. Therefore, Triangle inherits from Geometry.Shape.

Geometry[Shapes, Shape]


extern int Shape.perimeter ();


foreign void cprint (char[] msg, int v);


Queue[Shapes, Shape];


Shape

{

    char[] name;

}


Shape (char[] _name)

{

    name = _name;

}



void Shape.print ()

{

    cprint (name, perimeter ());

}


public void Shapes.print ()

{

    Shape p;


    while (p = deque ())

        p.print ();

}


public void Shapes.addShape (Shape s)

{

    enqueue (s);

}


Geometry component

App


Shapes[];

Shape[];

Rectangle[];

Triangle[];


Geometry[Shapes, Shape];

Shape = Triangle | Rectangle;


Shape (char[] _name)

    : Geometry.Shape (_name)

{

}


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.addRectangle (int length, int width)

{

    addShape (new Rectangle (length, width));

}

public void Shapes.addTriangle

    (int edge1, int edge2, int edge3)

{

    addShape (new Triangle (edge1, edge2, edge3));

}


foreign int main ()

{

    Shapes p;


    p = new Shapes ();

    p.addTriangle (23, 12, 7);

    p.addRectangle (7, 13);

    p.print ();

}


App component