Object-Oriented CIS 3100

Cobol 2002 examples first we will look at two classes

This preview shows page 6 - 9 out of 11 pages.

COBOL 2002 Examples First we will look at two classes called driver and student. These are written to demonstrate simple inheritance and object creation and definition using the new standards. The driver class is the main calling class, it creates an object of type student and calls students methods CalcGPA and GiveRating. CalcGPA accepts an input parameter and calculates a simple GPA based upon this and returns this value. GiveRating accepts an input GPA and returns a rating based upon the value. IDENTIFICATION DIVISION. PROGRAM-ID. DRIVER. *> ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT GRADE-FILE *> ASSIGN TO DISK "J:\CS423\ASSIGN4\HW4.DAT" ASSIGN TO UT-S-INPUT ORGANIZATION IS LINE SEQUENTIAL. *> CLASS-CONTROL. STUDENT IS CLASS "STUDENT". *> DATA DIVISION. Figure 1: Class Driver(Partial) The most important thing to notice in this code piece is the Class-Control Section. Notice that it creates the reference to a class student with the dataname “student.” The next code snippet will show the usage of the Usage Object Reference clause. 01 STUDENTHANDLE OBJECT REFERENCE. Figure 2: Handle Example
Image of page 6

Subscribe to view the full document.

This single statement creates a variable with the name “StudentHandle” which is an Object Reference. Now let’s look at the code in the Procedure Division used to create a Student Object and call the methods of that object. INVOKE STUDENT "NEW" RETURNING STUDENTHANDLE. Figure 3: Invoke “New” Example The above statement creates an instance of the Student object and assigns it to the StudentHandle Object Reference dataname. Note the Invoke keyword, this keyword is used to reference any methods of an Object. The “New” method is the default constructor that is created for you. INVOKE STUDENTHANDLE "CALCGPA" USING WS-PASS-VARS RETURNING WF-GPA *> INVOKE STUDENTHANDLE "GIVERATING" USING WF-GPA RETURNING WF-RATING Figure 4: Invoke Method Examples The above two Invoke statements are used to call the CalcGPA and GiveRating methods of Student. You can see that their general form is the same and it is easy to note which values are passed into the methods and which are returned. The important thing to note is the use of the StudentHandle dataname in the Invoke statement. This indicates we are calling a specific object instance and NOT the class itself, this is an important distinction to make! Now let’s look at some of the code for the Student class. IDENTIFICATION DIVISION. CLASS-ID. STUDENT INHERITS FROM BASE. *> ENVIRONMENT DIVISION. OBJECT SECTION. CLASS-CONTROL. STUDENT IS CLASS "STUDENT". *> OBJECT. *> DATA DIVISION. *> PROCEDURE DIVISION.
Image of page 7
*> METHOD-ID. "CALCGPA". *> WORKING-STORAGE SECTION. 01 WS-DATA. *> LINKAGE SECTION. 01 LS-DATA. 01 LS-GPA PIC 9V99. *> PROCEDURE DIVISION USING LS-DATA RETURNING LS-GPA. Figure 5: Student Class(Partial) There are several important points to note in this code. First notice the Inherits keyword, this class is inheriting from the base class for all Objects in COBOL 2002 which is aptly named Base. Next notice the Object Section clause, this tells us we are now defining the elements of an Object. Then the Object keyword indicating we are defining a specific Object. Notice also that we then have a Data Division where we can define datanames to be members of the object.
Image of page 8

Subscribe to view the full document.

Image of page 9

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern