So far, the examples have shown how to define and implement SOM classes but have only mentioned SOM client programs. SOM client programs are applications that create and manipulate SOM objects. A SOM client program can be a Presentation Manager program or a simple C or C++ program.

A SOM client of the new Dog class can, for simplicity, create an instance of the Dog class, define its breed, and display its characteristics. This simple SOM client is shown in the following sample code:

/*
 *  DogMain.C - A client C program to manipulate a Dog object
 */

#define INCL_NOPMAPI     /* Makes compile go faster */
                            /* for non-PM programs     */
#define INCL_DOSERRORS
#define INCL_DOS

#include <os2.h>
/* Include declarations for the Dog class */
#include "dog.h"

int main(int argc, char *argv[])
{
  /* Define a dog object called "Zack" */
  Dog  *Zack;

  /* Create an instance of the object */
  Zack = DogNew();

  /* Set the breed of the dog */
  __set_breed(Zack, "Yorkshire Terrier");

  /* Display information about it */
  _display(Zack);

  /* Delete the object */
  _somFree(Zack);

   return NO_ERROR;
}

To create and manipulate SOM objects, a client program must have access to the object's public methods. In the same way that class data, methods, and functions are available to class implementers through the .IH file associated with the class implementation, class data methods, and functions are available to client programs through the .H file associated with the class implementation. In the above example, DOG.H is included to resolve references to the dog object's public methods.

The variable Zack is defined as a pointer to an instance (object) of the Dog class. In general, a pointer to an instance of a class is declared, as shown in the following figure:

<classname> *object;

The DogNew Dog class macro then is used to create an instance of the Dog class and return the pointer in the variable Zack. DogNew is defined in DOG.H and is a method inherited from the parent (SOMObject) of the Dog class and tailored for the Dog class. The DogNew macro expands to invoke somNew. somNew invokes DogNewClass, which creates the Dog class object, if it has not yet been created. The Dog class object must be created before its instances can be created. If instances of the Dog class are created through some mechanism other than DogNew, DogNewClass must be invoked in the client program.

Because the _set_breed and display methods are public and are defined in DOG.H, the client program can invoke them in the same manner as the class implementation by prefixing the method name with the underscore ( _ ) character. In the client program, the pointer to the dog object (Zack) is the first parameter for these methods. The _set_breed method is called to set Zack's breed as "Yorkshire Terrier". The display method invokes the _get_breed and bark methods and prints the dog's breed and bark.

Finally, somFree releases resources allocated when an object is created by somNew. As previously mentioned, somNew is invoked by the DogNew macro. somFree, like somNew, is a method inherited from the parent, SOMObject, of the Dog class. somFree must be called if somNew is used to create an object.


[Back] [Next]