OO COBOL: CLASS-ID and objects
COBOL 2002 added classes, methods and objects, and a compiler may implement them or not. This one does not: the reserved words are in its grammar and behind them there is nothing. The first half of this lesson is that statement with the build that proves it, and the second half is the account object written the way COBOL programs have always written one.
CLASS-ID: the real error
A class in OO COBOL is a source file that starts with CLASS-ID instead of PROGRAM-ID, and holds two bodies: a FACTORY that makes instances and an OBJECT that holds the methods, each an METHOD-ID with its own data division. Here is an account class with a method that takes a deposit:
IDENTIFICATION DIVISION.
CLASS-ID. ACCOUNT INHERITS FROM BASE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS BASE.
IDENTIFICATION DIVISION.
FACTORY.
DATA DIVISION.
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.
METHOD-ID. NEW.
PROCEDURE DIVISION.
DISPLAY "factory new".
END METHOD NEW.
END FACTORY.
OBJECT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-BALANCE PIC 9(7) VALUE 0.
PROCEDURE DIVISION.
METHOD-ID. DEPOSIT.
DATA DIVISION.
LINKAGE SECTION.
01 L-AMOUNT PIC 9(7).
PROCEDURE DIVISION USING L-AMOUNT.
ADD L-AMOUNT TO WS-BALANCE.
DISPLAY "balance " WS-BALANCE.
END METHOD DEPOSIT.
END OBJECT.
END CLASS ACCOUNT.
cobc -free -Wall -c account.cob
account.cob:2: error: 'CLASS-ID' is a reserved word, but isn't supported
1 | IDENTIFICATION DIVISION.
2 > CLASS-ID. ACCOUNT INHERITS FROM BASE.
3 | ENVIRONMENT DIVISION.
4 | CONFIGURATION SECTION.
account.cob:2: error: syntax error, unexpected Identifier, expecting FUNCTION-ID or PROGRAM-ID
The compiler knows the word and refuses it, and the second line says what it does accept in that place: a PROGRAM-ID or a FUNCTION-ID. Nothing below the second line was read.
The words of the class are worth knowing anyway, because a COBOL shop that moved to an OO compiler uses them: the FACTORY is the code that runs when a class makes an instance, the OBJECT body holds the data of one instance, and a METHOD-ID is a paragraph with a parameter list and its own divisions. An object variable is declared USAGE OBJECT REFERENCE and a call is INVOKE ... "METHOD" USING .... None of that compiles here; the next two lessons show each piece and its message.
A class without classes
The same account is a subprogram whose working storage holds the balance between calls and whose first parameter is the operation. State across calls is the subject of the INITIAL and CANCEL lesson, and the operation code is the interface of the copybook lesson:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-OP PIC X(8).
01 WS-AMOUNT PIC 9(7).
01 WS-RESULT PIC S9(9).
PROCEDURE DIVISION.
MOVE "DEPOSIT" TO WS-OP.
MOVE 1250 TO WS-AMOUNT.
CALL "ACCOUNT" USING WS-OP WS-AMOUNT WS-RESULT
DISPLAY "deposit " WS-RESULT.
MOVE 250 TO WS-AMOUNT.
CALL "ACCOUNT" USING WS-OP WS-AMOUNT WS-RESULT
DISPLAY "deposit " WS-RESULT.
MOVE "WITHDRAW" TO WS-OP.
MOVE 400 TO WS-AMOUNT.
CALL "ACCOUNT" USING WS-OP WS-AMOUNT WS-RESULT
DISPLAY "withdraw " WS-RESULT.
MOVE "BALANCE" TO WS-OP.
MOVE 0 TO WS-AMOUNT.
CALL "ACCOUNT" USING WS-OP WS-AMOUNT WS-RESULT
DISPLAY "balance " WS-RESULT.
STOP RUN.
END PROGRAM MAINP.
IDENTIFICATION DIVISION.
PROGRAM-ID. ACCOUNT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-BALANCE PIC S9(9) VALUE 0.
LINKAGE SECTION.
01 L-OP PIC X(8).
01 L-AMOUNT PIC 9(7).
01 L-RESULT PIC S9(9).
PROCEDURE DIVISION USING L-OP L-AMOUNT L-RESULT.
EVALUATE L-OP
WHEN "DEPOSIT"
ADD L-AMOUNT TO WS-BALANCE
WHEN "WITHDRAW"
SUBTRACT L-AMOUNT FROM WS-BALANCE
WHEN "BALANCE"
CONTINUE
WHEN OTHER
DISPLAY "unknown operation " L-OP
END-EVALUATE.
MOVE WS-BALANCE TO L-RESULT.
GOBACK.
END PROGRAM ACCOUNT.
cobc -x -free -Wall -o acct acct.cob
./acct
deposit +000001250
deposit +000001500
withdraw +000001100
balance +000001100
The balance lives in the working storage of the subprogram and survives every call, because a subprogram of this compiler is not INITIAL. Four calls, four operations and one account: that is an object in everything but the syntax, and every COBOL program of the last forty years that models a thing with a balance does it this way.
FUNCTION-ID, which does build
The error message of the first section named one other header the compiler accepts, and it is not a consolation prize: a user-defined function is a subprogram with a return value, usable where an intrinsic function goes. It needs a REPOSITORY entry to be visible to the caller:
IDENTIFICATION DIVISION.
FUNCTION-ID. DOUBLE-IT.
DATA DIVISION.
LINKAGE SECTION.
01 L-IN PIC 9(5).
01 L-OUT PIC 9(10).
PROCEDURE DIVISION USING L-IN RETURNING L-OUT.
COMPUTE L-OUT = L-IN * 2
GOBACK.
END FUNCTION DOUBLE-IT.
IDENTIFICATION DIVISION.
PROGRAM-ID. USE1.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION DOUBLE-IT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9(5) VALUE 1250.
01 WS-R PIC 9(10).
PROCEDURE DIVISION.
COMPUTE WS-R = FUNCTION DOUBLE-IT(WS-A)
DISPLAY "result " WS-R.
STOP RUN.
END PROGRAM USE1.
cobc -x -free -Wall -o both both.cob
./both
result 0000002500
One call, one value, no factory and no object. Without the REPOSITORY entry the build stops with FUNCTION 'DOUBLE-IT' unknown, which is the compiler saying that a function is known by declaration and not by file name.
Exercises
- Add an "INTEREST" operation to the account subprogram that adds ten percent of the balance.
- Make the account subprogram IS INITIAL and run the caller again. Which line of the output changes?
- Give the function of the third section a second parameter and call it with two arguments.
- Remove the REPOSITORY entry and read the message of the build.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this compiler say about CLASS-ID?
Answer
That it is a reserved word which is not supported
The first error line of the build says reserved word, but is not supported.
-
Which headers does the compiler accept in place of CLASS-ID?
Answer
PROGRAM-ID and FUNCTION-ID
The second error line asks for FUNCTION-ID or PROGRAM-ID.
-
What holds the balance in the account subprogram?
Answer
Its working storage, which survives the calls
The four calls saw one balance of 1100 because the storage is kept.
-
What does FUNCTION-ID declare?
Answer
A user-defined function with a return value
DOUBLE-IT returned a value that the caller used inside a COMPUTE.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-B PIC S9(7) VALUE 0. PROCEDURE DIVISION. ADD 1250 TO W-B SUBTRACT 400 FROM W-B DISPLAY W-B. STOP RUN.Answer
+0000850
The deposit and the withdrawal leave 850 in the signed field.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-OP PIC X(8). 01 W-N PIC 9 VALUE 0. PROCEDURE DIVISION. MOVE "DEPOSIT" TO W-OP EVALUATE W-OP WHEN "DEPOSIT" ADD 1 TO W-N WHEN "WITHDRAW" ADD 2 TO W-N WHEN OTHER ADD 9 TO W-N END-EVALUATE. DISPLAY W-N. STOP RUN.Answer
1
The first branch matches the operation code.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(5) VALUE 1250. 01 W-R PIC 9(10). PROCEDURE DIVISION. COMPUTE W-R = W-A * 2 DISPLAY W-R. STOP RUN.Answer
0000002500
Twice 1250 is 2500 in a ten-digit field, with eight leading zeros.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(3) VALUE 0. PROCEDURE DIVISION. DISPLAY W-A. MOVE 1250 TO W-A. DISPLAY W-A. STOP RUN.Answer
000 then 250
The three-digit field keeps the three low-order digits of the move.
-
Why does an account subprogram keep state across calls?
Answer
Working storage of a subprogram is initialised once and then kept, unless IS INITIAL says otherwise
The four calls of the page shared one balance.
-
What is the honest way to learn OO COBOL on this compiler?
Answer
Read the error, model the object as a subprogram with an operation code, and use FUNCTION-ID where a value is needed
The class file does not build at all in this version.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev