Você está na página 1de 38

Structured COBOL Programming

Nancy Stern Hofstra University Robert A. Stern

Copyright @ 2000 John Wiley & Sons, In. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express permission of the copyright owner is unlawful. Request for further information should be addressed to the permissions Department , John Wil y & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resal e. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein.

Nassau Community College


PowerPoint Presentation: Richard H. Baum, Ph.D.
DeVry Institute of Technology

9th Edition

CHAPTER 18 AN INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING

Structured COBOL Programming, Stern & Stern, 9th Edition

TABLE OF CONTENTS
The Advantages of Object-Oriented Programming Overview of Object-Oriented Programming (OOP) Tutorials for Object-Oriented COBOL
Lessons 1 through 3 What Youve Learned

Structured COBOL Programming, Stern & Stern, 9th Edition

Advantages of ObjectOriented Programming

Object-oriented programming (OOP) is a concept that has the potential for significantly enhancing the quality of all programs. This concept is considered by many to to be the key to improved productivity in future programs.
ANSI formed an OO COBOL Task Group to define the OO extensions to the COBOL language as a part of the COBOL 2000+ standard.
Structured COBOL Programming, Stern & Stern, 9th Edition

Advantages of ObjectOriented Programming


SMALLTALK C++

Some programming languages that already implement the OO approach are:

Keep in mind that the concept of objectoriented programming will be added to the COBOL standard as an extension.
Thus any new compiler incorporating object oriented techniques will be compatible with previous standards.
Structured COBOL Programming, Stern & Stern, 9th Edition

Overview of Object-Oriented Programming


Data and procedures are combined and stored in a program as units referred to as objects. Action occurs when an object receives a message from the user. Services or actions are performed by the program as responses to user messages.

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview of Object-Oriented Programming


Objects can be written so that they share attributes--data and methods--with other objects.
Objects can have any number of other attributes unique to them. Data and procedure components need only be written once and copied to all objects with same attributes.

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
A class defines a template for a series of similar objects.
The class can be used to define and create objects that are the same except for the value of their data.

By defining classes and objects within classes the following takes place: 1. Complex applications will be easier to develop. 2. Programs will become more Structured COBOL Programming, Stern & Stern, 9th Edition standardized.

Overview
3. Reusable code stored in libraries will reduce duplication of effort, programming and maintenance costs, and errors. 4. Improve programmer productivity 5. Objects can be acted on in any program by responding to the messages provided by the user.
Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
In summary:
Data and procedures are combined into a class definition. The class definition is specified in COBOL with procedures and global data that looks like standard COBOL. Multiple instances of a class, each with its own local data, can be created at any point. These instances of a class are the objects that can send and receive messages from other objects.
Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
OOP is a different way of writing programs. With traditional code, actions are accomplished by procedures that act on data. With OOP, objects that include data and procedures are prewritten and acted upon in a user program by messages sent to the objects.
Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
Objects are ENCAPSULATED:
This means their data and procedures are hidden behind an INTERFACE.

One goal of OOP is to develop libraries of objects that can be shared and called into user programs as needed
CALL and COPY are verbs that can achieve some of the objectives of OOP.

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
A CLASS is a group of objects that share attributes which are methods for operating on the data.
A METHOD is an action achieved by issuing a message to an object.
A method is really an objects way of responding to a message.

Defining classes means placing reusable code in a central location or library.


Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
An object is called an INSTANCE of a class.
Objects INHERIT attributes--data and procedures- from their class.

A class can include a group of objects and may be included in another object called a MEMBER object.

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
Example-1: A Bank-Account may be defined as a class. It may have checking-account and savings-account as subclasses, each of which shares data attributes (e.g., account number and balance) and procedures (e.g., calculating interest) defined as part of Bank-Account.
Structured COBOL Programming, Stern & Stern, 9th Edition

Overview: Example-2
A Method that can be applied to these classes include deposit and withdrawal. All objects within bank-account can share deposit and withdrawal services.
That means that the mechanisms used for withdrawing or depositing money will be the same for all objects within the class.

Each object can have additional methods not inherited from the class but unique to that object.
That is, process-check-fee can be a method applied to checking-account but not to savings-account.
Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
Example-3 Data such as account-number can be shared by objects within the class as well. This may be made available to users for processing but may also be protected so that users can enter and retrieve them, but not be able to change them.
Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
Classes and their objects consist of data and procedures. There are two types of data in a class: INSTANCE VARIABLE-- object data and procedures that are unique for each object in the class. FACTORY--data and procedures that are data shared by all objects in the class. Class is a COBOL reserved word so we use FACTORY.
Factory data must be set to its initial value Structured COBOL Programming, Stern & Stern, 9th Edition using the INITIALIZE verb or a VALUE clause

Overview
METHODS can be two types:
Object methods unique to each object in the class. Factory methods shared by all objects in the class.

Objects are identified by unique names called OBJECT HANDLES

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
POLYMORPHISM means a method can be implemented differently depending on the object.
Withdrawal, for example, may be implemented:
one way for objects like checking-account within the bank-account class. a different way for objects in a credit-card class.

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
Although the term "polymorphism" itself may be new to you, it has relevance to the current COBOL language. The READ statement, for example is polymorphic in that it results in different actions depending on whether we are reading from a sequential, indexed, or relative file.
Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
The Interface that links the Method to the object may be different but the service provided will be similar.
An INTERFACE is the entire set of messages to which an object can respond along with the parameters required by each message.

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
The concept of DATA ABSTRACTION in OOP encourages programmers to think of data and methods in abstract terms, as fixed objects;
Common objects--data and procedures-should be factored out and located in ancestor classes, sometimes referred to as abstract classes.

Consider the following analogies:


Structured COBOL Programming, Stern & Stern, 9th Edition

Terms and Concept Analogies


Object-Oriented Terms
1. Methods

Traditional Programming Terms


1. Procedures, functions, subroutines 2. Data 3. Procedure call or function call 4. Abstract data type 5. Copy

2. 3. 4. 5.

Instance variable Message Class Inheritance

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
ENCAPSULATION--the ability to hide internal details of data and procedures while providing a public interface through a user-defined message BASE CLASS--a new class from an existing class through INHERITANCE.

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
Conceptually the implementation of OO in COBOL begins using standard COBOL:
The COPY statement enables the copying of class definitions and objects into a program. The CALL statement enables the sending of message to objects to get results. The INVOKE statement may be used also.

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
A COBOL Example: INVOKE ASAVINGSACCOUNT Withdraw USING CUSTOMER-ACCT TRANS-AMT RETURNING ACCOUNT
ASAVINGSACCOUNT may be an instance of some class object such as ACCOUNT. Withdraw is a method. USING indicate the parameters to pass to the object. RETURNING indicate the parameters passed back to the user program.
Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
Often you begin establishing a new instance of an object from a class of objects.
MYSAVINGSACCOUNT may be an instance of ASAVINGSACCOUNT.
Defining an instance of an object is called INSTANTIATION. This may be accomplished by code such as:

INVOKE ASAVINGSACCOUNT 'New' RETURNING MYSTAVINGSACCOUNT.


Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
PERSISTENCE is the ability for changes to be retained after the program is terminated and when the program executes it begins just as it ended.
This is analogous to a "Save on EXIT" menu item in some programs.

Structured COBOL Programming, Stern & Stern, 9th Edition

Overview
For objects within a class the following is done in COBOL: IDENTIFICATION DIVISION. FACTORY. ENVIRONMENT DIVISION. DATA DIVISION. <define Factory Data here> PROCEDURE DIVISION.
Structured COBOL Programming, Stern & Stern, 9th Edition

<define Factory Methods here>

Overview
To define methods and data unique to a specific object the following COBOL is written: IDENTIFICATION DIVISION. OBJECT. <Any number of objects can be define ENVIRONMENT DIVISION. DATA DIVISION. <Object Data> PROCEDURE DIVISION. <Object Methods> END OBJECT.
Structured COBOL Programming, Stern & Stern, 9th Edition

OBJECT-ORIENTED COBOL PRODUCTS


The following companies and products have incorporated OO options into their COBOL compilers:
SOFTWARE DEVELOPER PRODUCT

Micro Focus COBOL IBM COBOL Computer Associates COBOL

Object Visual Age for Visual Realia

Structured COBOL Programming, Stern & Stern, 9th Edition

TUTORIAL FOR OO COBOL BY MICRO FOCUS


Lesson One: "Hello World"
Introduces the Class Browser, along with some of the fundamental concepts of OO COBOL programming. In going through the tutorial, you'll learn:
The basics parts of a class. The key elements needed to use an object. Passing a parameter to an object method. Passing a parameter to an object method and getting back a return value.
Structured COBOL Programming, Stern & Stern, 9th Edition

TUTORIAL FOR OO COBOL BY MICRO FOCUS


Lesson Two: "Hello World" and OO COBOL
Permits working with an Object and a Class
Throughout the rest of these lessons, you will find programs presented in pairs. Each pair consists of a drive program and a class program. Each pair is packaged as a project.

Lesson Three: OOP and Parameter Passing


Permits the Passing of a Parameter to an Structured COBOL Programming, Stern & Stern, 9th Edition Object and Returning a Value.

WHAT YOUVE LEARNED


Once you've completed the tutorial lessons outlined in your textbook you will have seen Personal COBOLs basic structures and had some amount of interaction between program entities. These are the basic components of all OO programming.

Structured COBOL Programming, Stern & Stern, 9th Edition

WHAT YOUVE LEARNED


You will have learned the following:
How to open a project in the Browser and view various program sections. How to compile and run a program. How important menu items work. How to work with a procedural COBOL program in the Browser.

Structured COBOL Programming, Stern & Stern, 9th Edition

WHAT YOUVE LEARNED


The object-oriented syntax items in Personal COBOL. The basic structure of classes and their objects and how to access them with driver programs. Passing a parameter to an object method and returning a value. How one object method can access a peer method.
Structured COBOL Programming, Stern & Stern, 9th Edition

WHAT YOUVE LEARNED


What you didn't experience are the concepts of inheritance and polymorphism. The Help/Online Tutorials and Reference Beginning Tutorials treats both of these in a series of examples.

Structured COBOL Programming, Stern & Stern, 9th Edition

Você também pode gostar