Tải bản đầy đủ (.pdf) (238 trang)

Tài liệu Object Oriented Programming With Cobol pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.71 MB, 238 trang )

&2%2/
Issue 2b, March 2004
REMHFWRULHQWHG
SURJUDPPLQJZLWKFRERO
2EMHFWRULHQ WHG
oppubb.book Page 1 Tuesday, March 16, 2004 12:23 PM
Copyright © 2004 Micro Focus International Limited.
All rights reserved.
Micro Focus International Limited has made every effort to ensure that this book is
correct and accurate, but reserves the right to make changes without notice at its sole
discretion at any time. The software described in this document is supplied under a
license and may be used or copied only in accordance with the terms of such license,
and in particular any warranty of fitness of Micro Focus software products for any
particular purpose is expressly excluded and in no event will Micro Focus be liable for
any consequential loss.
Animator®, COBOL Workbench®, EnterpriseLink®, Mainframe Express®,
Micro Focus®, Net Express®, REQL® and Revolve® are registered trademarks, and
AAI™, Analyzer™, Application to Application Interface™, AddPack™, AppTrack™,
AssetMiner™, CCI™, DataConnect™, Dialog System™, EuroSmart™, FixPack™,
LEVEL II COBOL™, License Management Facility™, License Server™,
Mainframe Access™, Mainframe Manager™, Micro Focus COBOL™, Object COBOL™,
OpenESQL™, Personal COBOL™, Professional COBOL™, Server Express™,
SmartFind™, SmartFind Plus™, SmartFix™, SourceConnect™, Toolbox™, WebSync™,
and Xilerator™ are trademarks of Micro Focus International Limited. All other
trademarks are the property of their respective owners.
No part of this publication, with the exception of the software product user
documentation contained on a CD-ROM, may be copied, photocopied, reproduced,
transmitted, transcribed, or reduced to any electronic medium or machine-readable
form without prior written consent of Micro Focus International Limited.
Licensees may duplicate the software product user documentation contained on a CD-
ROM, but only to the extent necessary to support the users authorized access to the


software under the license agreement. Any reproduction of the documentation,
regardless of whether the documentation is reproduced in whole or in part, must be
accompanied by this copyright statement in its entirety, without modification.
U.S. GOVERNMENT RESTRICTED RIGHTS. It is acknowledged that the Software and the
Documentation were developed at private expense, that no part is in the public
domain, and that the Software and Documentation are Commercial Computer
Software provided with RESTRICTED RIGHTS under Federal Acquisition Regulations
and agency supplements to them. Use, duplication or disclosure by the U.S.
Government is subject to restrictions as set forth in subparagraph (c)(1)(ii) of The
Rights in Technical Data and Computer Software clause at DFAR 252.227-7013 et. seq.
or subparagraphs (c)(1) and (2) of the Commercial Computer Software Restricted
Rights at FAR 52.227-19, as applicable. Contractor is Micro Focus, 9420 Key West
Avenue, Rockville, Maryland 20850. Rights are reserved under copyright laws of the
United States with respect to unpublished portions of the Software.
20040316122346
oppubb.book Page 2 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
3
Table of Contents
About this Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Command Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Side Headings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Part 1: Overview
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Object-oriented COBOL syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Developing OO Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
What to Read Next. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Object-oriented Analysis and Design . . . . . . . . . . . . . . . . . . . . . . . . 19

2 OO Programming Concepts . . . . . . . . . . . . . . . . . . . 21
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
oppubb.book Page 3 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
4
Part 2: Object-oriented COBOL Programming
3 Using Objects in Programs . . . . . . . . . . . . . . . . . . . . 33
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Declaring Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Using Object References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Declaring Object References . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Manipulating Object References . . . . . . . . . . . . . . . . . . . . . . . . . 36
Object Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Sending Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Using the INVOKE Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Inline Method Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Invocation Using Object Properties . . . . . . . . . . . . . . . . . . . . . . . 40
Conformance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Creating a New Instance Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Destroying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Object Destruction Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Finalized Object References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

4 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Class Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Method Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Data Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Files in OO Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Programming Factory Object Behavior . . . . . . . . . . . . . . . . . . . . . . . 56
Factory Object Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Class Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Factory Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Programming Instance Object Behavior . . . . . . . . . . . . . . . . . . . . . . 58
Instance Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Instance Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
oppubb.book Page 4 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
5
Parameterized Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Method Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Predefined Object Reference Names . . . . . . . . . . . . . . . . . . . . . . . . 67
Instance Creation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Get and Set Property Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
PROPERTY Clause in a Data Item Definition . . . . . . . . . . . . . . . 72
PROPERTY Clause in a Method Definition . . . . . . . . . . . . . . . . . 72
Coding the Object Property Syntax . . . . . . . . . . . . . . . . . . . . . . 73
6 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

Interface Source Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Interface Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Parameterized Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
7 Compiling and Debugging OO COBOL Applications 81
Compiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Compiler Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Directly Inherited Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Preventing Reallocation of Object Handles . . . . . . . . . . . . . . . . 84
Finding Memory Leaks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Object Data Guard Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Message Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Troubleshooting Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Memory Exceptions and Protection Violations on Method
Invocations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Symbol Redefined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Program Not Found . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
oppubb.book Page 5 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
6
Part 3: Tutorials
8 Objects and Messages Tutorial. . . . . . . . . . . . . . . . . 95
The Stopwatch Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Sending Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
9 Simple Class Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . 101
Structure of a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Identifying a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Identifying Classes Used by a Program . . . . . . . . . . . . . . . . . . . . 102
The Factory Object Source Element . . . . . . . . . . . . . . . . . . . . . . . 103

Factory Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
The Instance Object Source Element . . . . . . . . . . . . . . . . . . . . . . 104
Instance Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Animating the Stopwatch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
10 More Complex Class Tutorial . . . . . . . . . . . . . . . . . . 111
The Personnel Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Exploring the Personnel Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Animating the Personnel Application . . . . . . . . . . . . . . . . . . . . . . . . 114
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
11 Interfaces and Parameterized Class Tutorial . . . . . . 119
The PClass Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Exploring the Pclass Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Animating the Pclass Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
oppubb.book Page 6 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
7
Part 4: Micro Focus OO COBOL Alternatives and
Extensions
12 Micro Focus OO COBOL Alternative Syntax . . . . . . 127
Summary of Syntax Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Shared Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Working-Storage Section in a Method . . . . . . . . . . . . . . . . . . . . . . . 129
Working Storage and Object Storage. . . . . . . . . . . . . . . . . . . . . . . . 129
Data Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Direct Data Inheritance in Source Code . . . . . . . . . . . . . . . . . . . 130
Direct Data Inheritance at Run Time . . . . . . . . . . . . . . . . . . . . . 132
Extending a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
13 Requirements-based Vocabulary . . . . . . . . . . . . . . . 137

Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Defining a Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
External Class Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Method Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
User-defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Using Vocabulary-defined Verbs and Functions. . . . . . . . . . . . . . . . 141
Part 5: Micro Focus Class Libraries
14 Introduction to the Class Libraries . . . . . . . . . . . . . . 145
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Public and Private Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Class Library Method Parameter Types . . . . . . . . . . . . . . . . . . . . . . . 147
Class Library Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Animating the Class Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
oppubb.book Page 7 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
8
15 Collection Frameworks . . . . . . . . . . . . . . . . . . . . . . . 151
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Different Categories of Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Creating Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Creating Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Working with CharacterArray Objects. . . . . . . . . . . . . . . . . . . . . 156
Creating a CharacterArray Object . . . . . . . . . . . . . . . . . . . . . . . . 156
Querying a CharacterArray Object. . . . . . . . . . . . . . . . . . . . . . . . 157
Comparison Between Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Relative Value of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Hashing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Display Mechanisms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

Using the display Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Display on a Listbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Collection Sort Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Iterator Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
16 Intrinsic Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Using Intrinsic Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Cloning an Intrinsic Data Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Sending a Message to an Intrinsic Data Type . . . . . . . . . . . . . . . 167
Writing New Intrinsic Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Code for an Intrinsic Class Object. . . . . . . . . . . . . . . . . . . . . . . . . 168
Code for an Intrinsic Instance Object. . . . . . . . . . . . . . . . . . . . . . 169
17 Callback Frameworks . . . . . . . . . . . . . . . . . . . . . . . . 173
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Using Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Creating a Callback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Invoking a Callback. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
oppubb.book Page 8 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
9
18 Exception Handling Frameworks . . . . . . . . . . . . . . . 177
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Creating an Error Message File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Registering an Exception Message File. . . . . . . . . . . . . . . . . . . . . . . 179
Raising an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Providing Your Own Exception Handlers . . . . . . . . . . . . . . . . . . . . . 182
Writing Exception Handler Methods . . . . . . . . . . . . . . . . . . . . . 183
Registering an Object with the Exception Handler . . . . . . . . . . 185
Canceling an Exception Registration . . . . . . . . . . . . . . . . . . . . . 185
Replacing the System Exception Method . . . . . . . . . . . . . . . . . . . . . 185

19 Component Frameworks. . . . . . . . . . . . . . . . . . . . . . 187
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Defining Output Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Defining Input Sockets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Connecting Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Sending Signals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Part 6: Micro Focus OO COBOL Tutorials
20 Inheritance Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . 197
The Account Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Simple Account Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
21 Collections, Intrinsics and Dictionaries Tutorial. . . . 203
Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Using Intrinsics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Dictionaries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Iterator Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
oppubb.book Page 9 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
10
22 Exception Handling Tutorial . . . . . . . . . . . . . . . . . . . 215
Raising an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Registering an Exception Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Writing an Exception Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
23 Requirements-based Vocabulary Tutorial . . . . . . . . 223
Introducing the Vocabulary Example . . . . . . . . . . . . . . . . . . . . . . . . . 223
Using Requirements-based Vocabulary . . . . . . . . . . . . . . . . . . . . . . . 225
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Part 7: Appendices

A Descriptions of OO Run-time Switches . . . . . . . . . . 229
List of Switches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
oppubb.book Page 10 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
11
About this Book
This book explains how to do object-oriented programming in COBOL,
using ISO 2002 COBOL, Micro Focus OO extensions or a mixture of
both. It includes explanations of concepts, programming tutorials,
programming documentation and information about the Micro Focus
class libraries.
Audience
You should be familiar with the COBOL language and with your
operating system. You should read the Getting Started book and be
familiar with the COBOL development cycle described in your
documentation.
Notation
The notation used in the books is as follows:
• Enter refers to the carriage-return or Enter key. Where commands
to be typed are shown, the Enter key is not shown. It is treated as
implicit that the Enter key must be pressed at the end of the line.
• Hexadecimal numbers are enclosed in quotation marks and
preceded by a lower-case "x" or "h"; for example, x"9D", h"03FF".
The "x" is used when the hexadecimal number represents a
character string; the "h" when it represents a numerical value.
• With COMP-X and COMP-5, PIC X is used rather than PIC 99. Unlike
PIC 99, PIC X shows the length of the data item directly and so
demonstrates more clearly the use of COMP-X, which is to define a
binary item of the specified number of bytes.
oppubb.book Page 11 Tuesday, March 16, 2004 12:23 PM

Object-oriented Programming with COBOL
12 About this Book
Command Lines
The notation used to describe the format of command lines is as
follows:
• Words printed in italics are generic terms representing names to be
devised by you.
• Words printed in nonitalic characters are the actual words you must
enter.
Windows: On Windows you can type them in upper, lower, or mixed case
except where otherwise stated.
UNIX: On UNIX you must type them in upper or lower case as shown.
• Square brackets [ ] mean the material inside them is optional.
• Braces { } mean you must choose from the options inside them. If
there is only one option in the braces, they mean repetition.
• An ellipsis ( ) following { } or [ ] means you can repeat the material
inside them. The number of repetitions allowed is unlimited unless
otherwise stated. Square brackets [ ] with an ellipsis mean you can
omit the material altogether.
• If a command line does not fit across the page, it is continued on the
next line; the continuation line is indented.
Server Express:
• On UNIX, all command line formats and examples are for the
standard UNIX shell, the Bourne shell. If you are using another shell,
see your UNIX documentation for the appropriate formats.
Server Express:
• Where examples showing environment variables do not specifically
show them being exported to the shell, it is treated as implicit that
they are exported.
Server Express:

• Some keystrokes using function keys or the Alt or Ctrl keys are not
available on all UNIX platforms. The User’s Guide contains a UNIX
Key Usage Chart, listing how the keystrokes shown in the books
map onto actual keystrokes.
oppubb.book Page 12 Tuesday, March 16, 2004 12:23 PM
Notation 13
Object-oriented Programming with COBOL
Side Headings
In a generic book or chapter, text that does not apply to all supported
environments and COBOL systems is marked by a side heading in the
left margin. A side heading applies to the paragraph it is next to, unless
it is next to the first paragraph in a section, in which case it applies to
that whole section.
The following examples of side headings show what they mean:
Server Express: This text applies to Server Express on UNIX.
Net Express: This text applies to Net Express on Windows.
oppubb.book Page 13 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
14 About this Book
oppubb.book Page 14 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
15
Part 1: Overview
This part contains the following chapters:
• Chapter 1, “Introduction”
• Chapter 2, “OO Programming Concepts”
oppubb.book Page 15 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
16 Part 1: Overview
oppubb.book Page 16 Tuesday, March 16, 2004 12:23 PM

Object-oriented Programming with COBOL
17
1 Introduction
This chapter introduces the facilities for object-oriented programming
provided in your COBOL system, and explains how to use this book to
begin OO programming with COBOL.
Object-oriented COBOL syntax
Your COBOL system enables you to do object-oriented (OO)
programming in COBOL, while still retaining all the syntax and features
previously available. Your COBOL system supports:
• Syntax that conforms to the ISO 2002 COBOL standard
• Micro Focus alternatives and extensions to the ISO 2002 COBOL
standard
You can choose to just use ISO 2002 syntax, or you can use ISO 2002
and Micro Focus syntax, mixing them in the same program. However,
we recommend that you use the ISO 2002 syntax if you want your
programs to be portable to platforms that do not support Micro Focus
COBOL.
The chapters in Parts 1, 2 and 3 of this book use the ISO 2002 syntax
and related terminology. Micro Focus alternatives and extensions are
generally mentioned when a new piece of syntax is introduced.
The chapters in Parts 4, 5 and 6 of this book use the Micro Focus syntax
and related terminology. The main differences between ISO 2002 and
Micro Focus OO COBOL are listed in the chapter Micro Focus OO COBOL
Alternative Syntax, in Part 4.
oppubb.book Page 17 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
18 Chapter 1 Introduction
Developing OO Programs
Development starts with the analysis of the problem and the design of a

program or programs that solve the problem. Object-oriented design
involves identifying the objects that you want to work with and what
they need to do. Object-oriented analysis and design lie outide the
scope of this book; for a reading list, see the section Object-Oriented
Analysis and Design.
When you reach the design phase, you can make use of the Micro Focus
class libraries, as long as you do not want to port your programs to
platforms that do not support Micro Focus COBOL. A class library is a
collection of ready-made objects that you can use in your programs. (For
definitions of the terms class and object see the chapter OO
Programming Concepts.) More information on the Micro Focus class
libraries is available in the chapter Introduction to the Class Libraries.
ISO 2002 COBOL also provides a class library. However, it includes far
fewer classes than the Micro Focus class libraries.
When you start coding your program you will be on familiar territory,
using your usual development environment tools such as the Editor and
Animator.
Net Express: Net Express provides features that help you specifically with OO
development:
• Wizards that you can use to create new programs
• The ability to search for OO programming constructs in the Browse
tool (click Search > Browse).
• An OO COBOL tool bar that helps you to develop and maintain OO
COBOL code quickly and easily
If you use both Net Express and Server Express, you can develop and
debug your application on Net Express, and transfer the code to a
Server Express platform for production. OO COBOL code is portable
between Net Express and Server Express, except for code developed for:
• GUI programming
• COM components

oppubb.book Page 18 Tuesday, March 16, 2004 12:23 PM
What to Read Next 19
Object-oriented Programming with COBOL
What to Read Next
We advise you to start by reading the chapter OO Programming
Concepts which provides an introduction to object-oriented
programming concepts and terminology.
When you are ready to find out more about coding in object-oriented
COBOL, move onto Parts 2 and Part 3. Part 2 contains more detailed
information about object-oriented programming, specifically in COBOL,
while Part 3 contains some tutorials. If you want to go straight to the
tutorials, we recommend that you try only the tutorials described in the
chapters Objects and Messages Tutorial and Simple Class Tutorial,
before returning to Part 2, starting with the chapter Using Objects in
Programs. All the chapters in Parts 2 and 3 use the ISO 2002 OO COBOL
syntax and terminology.
The chapters in Parts 4, 5 and 6 all relate to features and facilities of
Micro Focus COBOL and use the Micro Focus OO COBOL syntax and
terminology. Part 4 describes Micro Focus syntax that provides
alternatives and extensions to the ISO 2002 syntax. Part 5 is devoted to
the Micro Focus class libraries; start with the chapter Introduction to the
Class Libraries. Part 6 contains tutorials that use Micro Focus alternatives
and extensions to the ISO 2002 syntax.
Object-oriented Analysis and Design
The documentation supplied with your COBOL system will help you to
start programming in OO COBOL. However, if you are new to OO,
learning about the principles of Object-Oriented Design and Analysis
(OOD and OOA) will enable you to make the best use of this new
technology.
The reading list below suggests some books dealing with object-

oriented methodologies and technologies.
Booch, Grady. Object-Oriented Design. Benjamin/Cummings, 1994.
ISBN: 0-8053-0091-0.
Jacobson , Ivor. Object-Oriented Software Engineering.
Addison-Wesley, 1992. ISBN: 0-201-54435-0.
oppubb.book Page 19 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
20 Chapter 1 Introduction
Rumbaugh. James. Object-Oriented Modeling and Design.
Prentice Hall, 1991. ISBN: 0-13-629841-9.
Shlaer, Sally and Mellor, Steve. Object-Oriented Systems Analysis:
Modeling the World in Data. Prentice Hall, 1988. ISBN: 0-13-629023-1
and Object Lifecycles: Modeling the World in States. Prentice Hall, 1992.
ISBN: 0-13-629940-7
Taylor, David. Object Oriented Information Systems: Planning and
Implementation. John Wiley, ISBN: 0-471-54364-0.
Wirfs-Brock, Rebecca. Designing Object-oriented Software .
Prentice Hall, 1990. ISBN: 0-13-629825-7.
There are also several training organizations that run language-
independent courses on OOD and OOA.
oppubb.book Page 20 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
21
2 OO Programming Concepts
This chapter describes the key concepts supported by an object-
oriented programming language, and how they are implemented by
Micro Focus COBOL. Micro Focus COBOL supports the ISO 2002
standard for Object-Oriented COBOL (OO COBOL) and supplies some
additional OO facilities.
Overview

OO COBOL provides the following elements, which are typical of any
OO programming language:
• Objects
• Classes
• Methods
• Interfaces
• Messages
These together provide the three characteristics that define an OO
language:
• Encapsulation
• Inheritance
• Polymorphism
The following sections describe these elements and characteristics in
turn, and how they are implemented in OO COBOL.
oppubb.book Page 21 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
22 Chapter 2 OO Programming Concepts
Objects
An object is a combination of data and the procedures to operate on
that data. The data is known as the object’s attributes, and the
procedures are known as its methods. Every object in an object-oriented
application has a unique object identifier, allocated to it at creation and
fixed for its lifetime.
Objects hold data, but they do not replace files and databases. The data
in an object exists only during the lifetime of the object. Objects are
created and destroyed by object-oriented applications.
Many of the objects in an OO application represent objects in the real
world. For example, a banking system would include objects to
represent customers, accounts and ledgers. The attributes of an account
would include the balance, and its methods would include Debit, Credit,

GetBalance. Figure 2-1 shows two ways of representing such an object.
Figure 2-1. An Object
The user of an object can only find out about or change its attributes by
making requests to the object. These requests are known as messages,
and each message invokes a method supported by the object. The
object interface is a description of all the messages to which the object
responds. For example, to find out the balance of an account, you
would send an account object the message GetBalance.
The actual representation of the data is known only to the object. As
long as the object interface remains the same, a programmer can
oppubb.book Page 22 Tuesday, March 16, 2004 12:23 PM
Classes 23
Object-oriented Programming with COBOL
change the internals of how the object represents and operates on
data, without affecting the rest of the system.
Classes
A class is a definition of an object; it embodies all the information you
need to create and manipulate objects of a particular type. An account
class defines account objects and a ledger class defines ledger objects.
An account object is said to be an instance of the account class, or an
instance object, or simply an instance.
A class is a programming language construct, while an object is an
entity that exists in memory at run time.
A class not only defines an instance object; it also defines a factory
object. The factory object defines the class’s own data and behavior.
The class does not have the same behavior as the instances it creates. A
class is like a printer’s plate, printing identical forms. A plate enables
you to print a form, but is not a form itself.
The main function of the factory object is to create new instance
objects, but it can also contain data and methods that are shared by all

instance objects. For example, the factory object of an account class
could include a data item for keeping a count of the number of account
objects created.
In OO COBOL, a class is a COBOL source element, which consists of a set
of nested source elements, including source elements for the factory
object and the instance object. The factory source element contains all
the attributes and methods specific to the factory object. The object
source element contains the attributes and methods specific to all
instance objects. Figure 2-2 shows a class and instances being created
from it.
oppubb.book Page 23 Tuesday, March 16, 2004 12:23 PM
Object-oriented Programming with COBOL
24 Chapter 2 OO Programming Concepts
Figure 2-2. A Class and Instances Created From It
Note: Micro Focus COBOL provides alternative terminology and
features:
• The equivalent of a factory object is the class object.
• The top-level source element for the class can contain data that is
common to all instance objects and the class object

Methods
Methods are the pieces of code that implement the behavior of an
object. In OO COBOL, each method is a separate source element nested
within the factory or object source element. An object method can
access its own data, the instance data and the factory data declared in
oppubb.book Page 24 Tuesday, March 16, 2004 12:23 PM
Interfaces 25
Object-oriented Programming with COBOL
the factory object source element. A factory method can access its own
data and the factory data.

Methods can be incomplete; these are known as method prototypes. A
method prototype does not contain any code, just a heading and an
end-marker. A method prototype is always fully implemented
elsewhere in the application; for an explanation of the benefits of using
method prototypes see the section Interfaces.
Interfaces
Interfaces are collections of method prototypes. The set of method
prototypes defines a common behavior that a variety of objects might
share. For example, you might have an interface Rentable, that defines
methods appropriate for objects that people can rent, such as cars and
video tapes. The Car class is a subclass of the Vehicle class, while the
VideoTape class is a subclass of the VideoRecording class, but both Car
and VideoTape implement the Rentable interface. The interface
methods might include pickUp and dropOff. These are defined as
prototypes in the interface. Each class that implements the interface
must provide full method definitions for the method prototypes in the
interface.
Interfaces give you additional flexibility in designing your OO
applications; they are one of the elements in an OO language that
provide polymorphism (see the section Polymorphism).
Messages
A message is the way you request an object to perform a service. A
message always consists of the following:
• Object reference
• Method selector
oppubb.book Page 25 Tuesday, March 16, 2004 12:23 PM

×