Below is the implementation for a class Animal that introduces an attribute sound of type string and overrides somInit and somUninit, along with a main program that creates and then frees an instance of class Animal.
#define Animal_Class_Source
#include <animal.ih>
#include <string.h>
SOM_Scope void SOMLINK somInit (Animal somSelf)
{
AnimalData *somThis = AnimalGetData (somSelf);
Environment *ev = somGetGlobalEnvironment();
Animal_parents_somInit (somSelf);
if (!__get_sound(somSelf, ev)) {
__set_sound(somSelf, ev, SOMMalloc(100));
strcpy (__get_sound(somSelf, ev), "Unknown Noise");
somPrintf ("New Animal Initialized\n");
}
}
SOM_Scope void SOMLINK somUninit (Animal somSelf)
{
AnimalData *somThis = AnimalGetData (somSelf);
Environment *ev = somGetGlobalEnvironment();
if (__get_sound(somSelf, ev)) {
SOMFree(__get_sound(somSelf, ev);
__set_sound(somSelf, ev, (char*)0);
somPrintf ("Animal Uninitialized\n");
Animal_parents_somUninit (somSelf);
}
}
/* main program */
#include <animal.h>
void main()
{
Animal myAnimal;
myAnimal = AnimalNew ();
_somFree (myAnimal);
}
/*
Program output:
New Animal Initialized
Animal Uninitialized
*/