– The class definition is specified in COBOL with procedures and global data that looks like standard COBOL.. • A CLASS is a group of objects that share attributes which are methods f
Trang 1Request for further information should be addressed
to the permissions Department , John Wily & Sons, Inc The purchaser may make back-up copies for his/her own use only and not for distribution or resale 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.”
Trang 2CHAPTER 18
AN INTRODUCTION TO OBJECT-ORIENTED
PROGRAMMING
Trang 4Advantages of Object-Oriented 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.
Trang 5Advantages of Object-Oriented Programming
• Some programming languages that
already implement the OO approach are:
– SMALLTALK
– C++
• Keep in mind that the concept of oriented programming will be added to the COBOL standard as an extension
object-– Thus any new compiler incorporating object
oriented techniques will be compatible with
Trang 6Overview 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.
Trang 7Overview 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.
Trang 8• 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 standardized.
Trang 93 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.
Trang 10• 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.
Trang 11• OOP is a different way of writing
programs.
• With traditional code, actions are
accomplished by procedures that act on
Trang 12• 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
Trang 13• 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 object’s way of responding to a message.
– Defining classes means placing reusable code
in a central location or library.
Trang 14• 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
Trang 15Example-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)
Trang 16Overview: 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.
Trang 17Example-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.
Trang 18• 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 using the INITIALIZE verb or a VALUE clause (e.g., interest rate).
Trang 19• 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
Trang 20• POLYMORPHISM means a method can be implemented differently depending on the object.
– Withdrawal , for example, may be
Trang 21• 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.
Trang 22• 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.
Trang 23• 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:
Trang 24Terms and Concept Analogies
Object-Oriented Traditional
Terms Programming Terms
1 Methods 1 Procedures,
functions, subroutines
2 Instance variable 2 Data
3 Message 3 Procedure call or
function call
4 Class 4 Abstract data type
5 Inheritance 5 Copy
Trang 25• 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
Trang 27• 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
Trang 28• Often you begin establishing a new
instance of an object from a class of
• This may be accomplished by code such as:
INVOKE ASAVINGSACCOUNT 'New' RETURNING MYSTAVINGSACCOUNT.
Trang 29• 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.
Trang 32OBJECT-ORIENTED COBOL
PRODUCTS
• The following companies and products
have incorporated OO options into their COBOL compilers:
SOFTWARE DEVELOPER PRODUCT
Micro Focus Object COBOL
IBM Visual Age for COBOL Computer Associates Visual Realia COBOL Netron, Inc Netron/Fusion
TechBridge Technology TechBridge Builder
Trang 33TUTORIAL 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
Trang 34TUTORIAL 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
Object and Returning a Value.
Trang 35WHAT YOU’VE LEARNED
• Once you've completed the tutorial
lessons outlined in your textbook you will have seen Personal COBOL’s basic
structures and had some amount of
interaction between program entities
• These are the basic components of all OO programming.
Trang 36WHAT YOU’VE 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.
Trang 37WHAT YOU’VE 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.
Trang 38WHAT YOU’VE 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