Following is a C program that uses the class "Hello" (as defined in the Tutorial in Chapter 2). The "Hello" class provides one attribute, "msg", of type string, and one method, "sayHello". The "sayHello" method simply displays the value of the "msg" attribute of the object on which the method is invoked.
#include <hello.h> /* include the header file for Hello */ int main(int argc, char *argv[]) { /* declare a variable (obj) that is a * pointer to an instance of the Hello class: */ Hello obj; /* create an instance of the Hello class * and store a pointer to it in obj: */ obj = HelloNew(); /* invoke method _set_msg on obj with the argument * "Hello World Again". This method sets the value of * obj's 'msg' attribute to the specified string. */ __set_msg(obj, somGetGlobalEnvironment(), "Hello World Again"); /* invoke method sayHello on obj. This method prints * the value of obj's 'msg' attribute. */ _sayHello(obj, somGetGlobalEnvironment()); _somFree(obj); return(0); }
The C++ version of the foregoing client program is shown below:
#include <hello.xh> /* include the header file for Hello */ int main(int argc, char *argv[]) { /* declare a variable (obj) that is a * pointer to an instance of the Hello class: */ Hello *obj; /* create an instance of the Hello class * and store a pointer to it in obj: */ obj = new Hello; /* invoke method _set_msg on obj with the argument * "Hello World Again". This method sets the value of * obj's 'msg' attribute to the specified string. */ obj->_set_msg(somGetGlobalEnvironment(), "Hello World Again"); /* invoke method sayHello on obj. This method prints * the value of obj's 'msg' attribute. */ obj->sayHello(somGetGlobalEnvironment()); obj->somFree(); return(0); }
These client programs both produce the output:
Hello World Again