IDL Programming > Objects > Object-Oriented Programming > Example: Overloading the HELP and PRINT/PRINTF Procedures

Example: Overloading the HELP and PRINT/PRINTF Procedures

In this example, we create the spaceCraftObject_doc class, which stores a spacecraft’s name and mass. We overload the class’s _overloadHelp and _overloadPrint methods, so that calling the HELP or PRINT/PRINTF procedures with this class of object results in the output we desire.

Example Code: The spaceCraftObject_doc object definition and overloaded function method code listed in this section is contained in the procedure file spacecraftobject_doc__define.pro, and is located in the examples/doc/objects subdirectory of the IDL distribution. To view the file in an IDL editor window, enter .EDIT spacecraftobject_doc__define.pro at the IDL command line.

For more information, refer to Overloading the HELP and PRINT/PRINTF Procedures.

FUNCTION spaceCraftObject_doc::Init, type, mass

self.shipType = type

self.shipMass = mass

RETURN, 1

END

 

FUNCTION spaceCraftObject_doc::_overloadHelp, varname

tempString = varname + ' = ' + '{spaceCraftObject_doc: ' $

+ self.shipType + STRING(self.shipMass) + '}'

RETURN, tempString

END

 

FUNCTION spaceCraftObject_doc::_overloadPrint

tempString = self.shipType + ' ' + STRING(self.shipMass) $

+ ' kg'

RETURN, tempString

END

 

PRO spaceCraftObject_doc__define

struct = {spaceCraftObject_doc, INHERITS IDL_Object, $

shipType: '', $

shipMass: 0 }

END

 

a=SpaceCraftObject_doc('Soyuz', 6650)
HELP, a

IDL prints:

A = {spaceCraftObject_doc: Soyuz 6650}

 

PRINT, a

IDL prints:

Soyuz 6650 kg