Example of polymorphic behavior
The following sample can be found in the samples directory as file ICC$RES2.
It is presented here without the terminal IO requests.
See
C++ sample programs.
#include "icceh.hpp"
#include "iccmain.hpp"
char* dataItems[] =
{
"Hello World - item 1",
"Hello World - item 2",
"Hello World - item 3"
};
void IccUserControl::run()
{
Here we include Foundation Class headers and the
main
function.
dataItems
contains
some sample data items. We write our application code in the
run
method
of
IccUserControl
class.
IccBuf buffer( 50 );
IccResource* pObj[2];
We create an
IccBuf
object (50 bytes initially)
to hold our data items. An array of two pointers to
IccResource
objects
is declared.
pObj[0] = new IccDataQueue("ICCQ");
pObj[1] = new IccTempStore("ICCTEMPS");
We create two objects whose classes are derived from
IccResource
–
IccDataQueue
and
IccTempStore.
for ( short index=0; index <= 1 ; index++ )
{
pObj[index]->clear();
}
For both objects we invoke the
clear
method. This
is handled differently by each object in a way that is transparent
to the application program; this is polymorphic behavior.
for ( index=0; index <= 1 ; index++ )
{
for (short j=1 ; j <= 3 ; j++)
{
buffer = dataItems[j-1];
pObj[index]->put( buffer );
}
}
Now we
put
three data items in each of our resource
objects. Again the
put
method responds to the request in a
way that is appropriate to the object type.
for ( index=0; index <= 1 ; index++ )
{
buffer = pObj[index]->get();
while (pObj[index]->condition() == IccCondition::NORMAL)
{
buffer = pObj[index]->get();
}
delete pObj[index];
}
return;
}
The data items are read back in from each of our resource objects using the get method. We delete the resource objects and return control to CICS®.