Inside COBOL #15
by
Shawn M. Gordon
President S.M.Gordon & Associates

You are in for a daunting treat this month. I have made it a point to try to keep up with the emerging COOL (Cobol Object Oriented Language) technology as best I can, and guess what, Microfocus has leap frogged the ANSI committee. They have recently release their Object Workbench add-on for MicroFocus COBOL. I don’t know how useful a real world tool it is yet, or if it is still a novelty, but I got my hands on some sample OO COBOL code, and I thought you might be interested in what is in store for you. I am going to reprint this stuff as I found it, and I make no claims to understanding everything that is going on, but I will reprint some of the doc that I found with it.

$set mfoo
*————————————————————
* Account is the base class of the various types of accounts.
* It is created by calling method “openAccount”.
*
* Copyright (c) Micro Focus Ltd. 1991 – 1994
*————————————————————

class-id. Account data is protected
inherits from Base.

object section.
class-control.
Account is class “ACCOUNT”
Base is class “BASE”
.
working-storage section.
copy “accinfo.cpy”.

object-storage section.

01 nextAccountNumber pic 9(8).

*————————————————————
* Open a new account for lsName with initialValue
*————————————————————

method-id. “openAccount”.
linkage section.
01 lsName pic x(accNameLength).
01 lsAccount usage object reference.
01 lsInitialValue pic s9(10)v99.
procedure division using lsName lsinitialValue
returning lsAccount.
*—-A001. Create a new instance of account.
invoke super “new” returning lsAccount
*—-A002. Initialize the account instance data.
invoke lsAccount “initAccount” using lsName lsInitialValue
nextAccountNumber
add 1 to nextAccountNumber
exit method.
end method “openAccount”.

Object.

working-storage section.

object-storage section.
*—-Instance data common to all accounts.
01 balance pic S9(10)v99.
01 aName pic x(accNameLength).
01 accountNumber pic 9(accNoLength).
*—-The charge period variable ensures that interest and bank
* charges are only debited once for each period. In a real
* application this would be linked to the date in some way.
01 chargePeriod pic x comp-5.
88 newPeriod value 1.
88 chargesApplied value 0.

*————————————————————
* Withdraw lsAmount from this account
*————————————————————
method-id. “withdraw”.
working-storage section.
01 errorCode pic x(4) comp-5 value
debitOverdrawsAccount.

linkage section.
01 lsAmount pic 9(10)v99.

procedure division using lsAmount.
*—-A010. Ensure that there are sufficient funds to cover the
* withdrawal.
if lsAmount <= balance *----A011. Make the withdrawal. subtract lsAmount from balance else *----A012. Raise an exception if there are insufficient funds * If there is no exception handler in place to trap * this error, the default exception handler stops the * application. invoke self "raiseException" using errorCode end-if exit method. end method "withdraw". *------------------------------------------------------------ * Deposit lsAmount to this account *------------------------------------------------------------ method-id. deposit. linkage section. 01 lsAmount pic 9(10)v99. procedure division using lsAmount. *----A020. Deposit money to the account. add lsAmount to balance exit method. end method deposit. *------------------------------------------------------------ * Returns account number *------------------------------------------------------------ method-id. "getAccountNumber". linkage section. 01 lsAccountNumber pic 9(accNoLength). procedure division returning lsAccountNumber. *----A040. Return the account number. move AccountNumber to lsAccountNumber exit method. end method "getAccountNumber". *------------------------------------------------------------ * PRIVATE. Initialize this account with the name lsName and the * amount lsInitialValue. *------------------------------------------------------------ method-id. "initAccount". linkage section. 01 lsInitialValue pic s9(10)v99. 01 lsName pic x(80). 01 lsAccountNumber pic 9(8). procedure division using lsName lsInitialValue lsAccountNumber. *----A050. Initialize the account instance data. move lsName to aName move lsInitialValue to balance move lsAccountNumber to accountNumber *----A051. Ensures that charges/interest are only debited/credited * once for each charging period. set newPeriod to true exit method end method "initAccount". *------------------------------------------------------------ * Starts a new charge/interest period. * The charge period variable ensures that interest and bank * charges are only debited once for each period. In a real * application this would be linked to the date in some way. *------------------------------------------------------------ method-id. "newPeriod". procedure division. *----A060. Set the flag. set newPeriod to True exit method. end method "newPeriod". *----------------------------------------------------------------- * Return the contents of this account as a string. * Provided to enable display of accounts on listboxes. *----------------------------------------------------------------- method-id. "getValue". local-storage section. 01 accountType pic x(accTypeLength). 01 i pic x comp-5. linkage section. 01 displayString pic x(accDisplayLength). procedure division returning displayString. *----A0070. Return a string showing the account type, number * and name. invoke self "getType" returning accountType move spaces to displayString move accountNumber to displayString(1:accnoLength) add 2 to accNoLength giving i move accountType to displayString (i:accTypeLength) add accTypeLength to i move aName to displayString (i:accNameLength) exit method. end method "getValue". *----------------------------------------------------------------- * Return the size of the string returned by getValue. * Provided to enable display of accounts on listboxes. *----------------------------------------------------------------- method-id. "size". linkage section. 01 aSize pic x(4) comp-5. procedure division returning aSize. *----C080. Return the size of the display string. move accDisplayLength to aSize exit method. end method "size". end object. end class Account. What I don't notice immediatly in this sample code is any use of operator overloading, which is what I always found useful in C++. This is where you can redefine something like the addition symbol '+' so that it will now how to do date math if you pass two variables that are in a date format. This is of course a very limited explanation of operator overloading, but you get the idea. While I dislike doing a specific product push, I am going to also include a document here that describes the implementation of OO COBOL by MicroFocus, hopefully this will explain everything for you that is going on here. Due to space considerations I had to cut out a fair amount of text, and some of the sample code, hopefully this is still informative. Next month I am going to include some information that I got back in my state of the union column. Thanks to everyone for their continued input. The Object COBOL Option to Micro Focus Workbench (Introductory Product) Description The 1.5 Release of the Object COBOL Option to Micro Focus Workbench contains significant new Object Oriented technology. It is intended to: a) introduce Object Orientation to the commercial data processing world, and b) provide an environment for developing Object COBOL applications. Object COBOL The popular features of Object Orientation are presented in the context of the COBOL programming language. These features include the ability to combine data with operations on the data ("encapsulation"), the ability to create new data types by specializing existing data types ("inheritance"), the ability to create an arbitrarily large number of instances of these data types dynamically ("instantiation"), to activate instances of these data types at will ("messaging") and to have the appropriate routines ("methods") respond to these messages ("dynamic binding"). Finally, these features include a way that makes these data types respond to a common message with behavior that depends on the data type itself ("polymorphism"). As you can see, the features are quite straight-forward and easy to understand, but the terminology in parentheses seems strange and a bit intimidating. In fact, you may be thinking, "is this all there is to Object Orientation?" -- and the answer is, yes! You know far more about OO than you realize -- particularly if you have been programming in COBOL! Object COBOL Programming Language and Run-time Environment A key feature of Object Orientation is the ability to define a class of objects, create instances of the class, and to have the instances collaborate with one another to implement a model of desired behavior. The Object COBOL language enables the programmer in creating these classes. The Object COBOL Run-time Environment supports the modelling of behaviors, including the instantiation, messaging and dynamic binding required to have a true dynamic object model. Class Libraries As you have probably heard, one of the key attributes that makes OO popular is reuse -- the ability to produce new applications by reusing what has been already written. This is made possible through libraries of extended data type descriptions ("classes"). The Object COBOL Option comes with a rich Class Library that contains class definitions for Lists, Sets, Dictionaries, and Collections that ease the creation of new applications or the migration of existing ones. In addition, there are a group of classes that support Graphic User Interface development. GUI Classes Elements of the GUI classes include TextEntry, Subpane, ListBox for producing views of a data model. Also included are classes for defining events (such as keyboard and mouse) and for associating these events with actions. Finally, the GUI Class Library eases the building of application objects that oversee the views, the data model, and the events. Using this Library, you can create new instances of these classes or can write your own class descriptions that are based on ones in the Class Library. Object COBOL Browser To make it easy to work with the Class Library, the Object COBOL Option also contains a "Browser". The Browser is actually an integrated development environment for Object Orientation that includes an editor, a compiler, and a de-bugger in addition to a way to examine existing class definitions and to write new sub-classes based on them. Requirements-based Vocabulary The Object COBOL Option contains a feature that separates it from other Object Oriented languages. This feature, invented by Micro Focus, is designed to make programs easier to read, as well as easier to write. Because this feature allows the statements in the requirements statement to be used directly in a program, it is called Requirements-based Vocabulary. Requirement-based Vocabulary allows the writer of a new class to specify how the operations on this class are to be written. For example, the programmer writing an Account class can say, "Here is how programs specify that money is to be added to the account: 'Deposit an amount into an account.'" As a result, the code written by applications have statements that are similar to those in a requirements statement, and can be understood by a non-programmer. Requirements-based Vocabulary can be expressed either in the form of imperative statements (like "Deposit an amount") or in a functional notation. The functional notation is similar to the intrinsic functions that have recently been added to the COBOL language. The main distinction is that these functions can be invented at will by the programmer who writes the class definitions. For example, one could . use the functional notation to support the use of a function like BALANCE, as in: COMPUTE NewBalance = BALANCE(in account) + amount. With the spectrum of features described above, the COBOL programming language is modernized and re-vitalized, allowing corporations with large amounts of tried-and-true production applications to re-vamp these applications with a minimum of effort and cost to exploit the advantages of Object Orientation.