OO COBOL: classes and objects
CLASS-ID
COBOL 2002 added objects. A class starts with CLASS-ID, keeps its instance data in the OBJECT section of DATA DIVISION, and ends with END CLASS. This is the standard OO COBOL shape; GnuCOBOL 3.2 does not implement it, so the lesson is a reading lesson (IBM Enterprise COBOL and Micro Focus do):
IDENTIFICATION DIVISION.
CLASS-ID. Employee.
DATA DIVISION.
OBJECT SECTION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(30).
METHOD-ID
Methods are declared in the PROCEDURE DIVISION with METHOD-ID and end with END METHOD. SELF is the object itself; the parameter arrives through LINKAGE SECTION:
PROCEDURE DIVISION.
METHOD-ID. SetName.
DATA DIVISION.
LINKAGE SECTION.
01 LS-NAME PIC X(30).
PROCEDURE DIVISION USING LS-NAME.
MOVE LS-NAME TO WS-NAME.
END METHOD.
END OBJECT.
END CLASS Employee.
Objects
A calling program creates an object with INVOKE and releases it when done. Most shop code stays procedural because the bulk of legacy systems is procedural.
01 WS-EMP USAGE OBJECT REFERENCE Employee.
INVOKE Employee "NEW" RETURNING WS-EMP.
INVOKE WS-EMP "SetName" USING "Ada".
The code on this page was not run: GnuCOBOL 3.2.0 does not implement OO COBOL.
Article author: Arthur Isaev