MainframeMaster

COBOL Tutorial

COBOL CLASS-ID - Quick Reference

Progress0 of 0 lessons

Overview

CLASS-ID is a COBOL keyword used in object-oriented programming to define a class. It marks the beginning of a class definition and is followed by the class name, enabling the creation of classes with methods, properties, and other object-oriented features in COBOL.

Purpose and Usage

  • Object-oriented programming in COBOL
  • Class definition and organization
  • Method and property encapsulation
  • Inheritance and polymorphism
  • Code reusability and modularity

Syntax

CLASS-ID follows specific syntax patterns for class definitions:

Basic CLASS-ID Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
* Basic CLASS-ID syntax CLASS-ID. ClassName. OBJECT SECTION. CLASS ClassName. * Class data declarations 01 class-data PIC X(50). * Method definitions METHOD-ID. methodName. DATA DIVISION. LINKAGE SECTION. 01 parameter PIC X(20). PROCEDURE DIVISION USING parameter. * Method implementation DISPLAY "Method called with: " parameter. EXIT METHOD. END METHOD methodName. END CLASS ClassName. * CLASS-ID with inheritance CLASS-ID. SubClass INHERITS FROM SuperClass. OBJECT SECTION. CLASS SubClass. * Subclass implementation METHOD-ID. newMethod. PROCEDURE DIVISION. * Method implementation EXIT METHOD. END METHOD newMethod. END CLASS SubClass.

CLASS-ID defines classes with methods and properties in object-oriented COBOL.

Class Structure Components

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
* Complete class structure CLASS-ID. CustomerClass. OBJECT SECTION. CLASS CustomerClass. * Class properties (data) 01 customer-id PIC X(10). 01 customer-name PIC X(50). 01 customer-email PIC X(100). * Constructor method METHOD-ID. NEW. DATA DIVISION. LINKAGE SECTION. 01 id PIC X(10). 01 name PIC X(50). 01 email PIC X(100). PROCEDURE DIVISION USING id, name, email. MOVE id TO customer-id. MOVE name TO customer-name. MOVE email TO customer-email. EXIT METHOD. END METHOD NEW. * Getter methods METHOD-ID. getCustomerId. DATA DIVISION. LINKAGE SECTION. 01 return-id PIC X(10). PROCEDURE DIVISION RETURNING return-id. MOVE customer-id TO return-id. EXIT METHOD. END METHOD getCustomerId. * Setter methods METHOD-ID. setCustomerName. DATA DIVISION. LINKAGE SECTION. 01 new-name PIC X(50). PROCEDURE DIVISION USING new-name. MOVE new-name TO customer-name. EXIT METHOD. END METHOD setCustomerName. END CLASS CustomerClass.

Practical Examples

Here are some practical uses of CLASS-ID in COBOL:

Customer Management Class

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
* Customer management class CLASS-ID. CustomerManager. OBJECT SECTION. CLASS CustomerManager. * Customer data 01 customer-list OCCURS 100 TIMES. 05 customer-id PIC X(10). 05 customer-name PIC X(50). 05 customer-email PIC X(100). 01 customer-count PIC 9(3) VALUE 0. * Add customer method METHOD-ID. addCustomer. DATA DIVISION. LINKAGE SECTION. 01 id PIC X(10). 01 name PIC X(50). 01 email PIC X(100). PROCEDURE DIVISION USING id, name, email. IF customer-count < 100 ADD 1 TO customer-count MOVE id TO customer-id(customer-count) MOVE name TO customer-name(customer-count) MOVE email TO customer-email(customer-count) DISPLAY "Customer added successfully" ELSE DISPLAY "Customer list is full" END-IF. EXIT METHOD. END METHOD addCustomer. * Find customer method METHOD-ID. findCustomer. DATA DIVISION. LINKAGE SECTION. 01 search-id PIC X(10). 01 found-name PIC X(50). 01 found-email PIC X(100). 01 found-flag PIC X VALUE "N". PROCEDURE DIVISION USING search-id, found-name, found-email, found-flag. PERFORM VARYING customer-index FROM 1 BY 1 UNTIL customer-index > customer-count IF customer-id(customer-index) = search-id MOVE customer-name(customer-index) TO found-name MOVE customer-email(customer-index) TO found-email MOVE "Y" TO found-flag EXIT PERFORM END-IF END-PERFORM. EXIT METHOD. END METHOD findCustomer. * Get customer count method METHOD-ID. getCustomerCount. DATA DIVISION. LINKAGE SECTION. 01 count PIC 9(3). PROCEDURE DIVISION RETURNING count. MOVE customer-count TO count. EXIT METHOD. END METHOD getCustomerCount. END CLASS CustomerManager.

Customer management class with methods for adding and finding customers.

Bank Account Class

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
* Bank account class with inheritance CLASS-ID. BankAccount. OBJECT SECTION. CLASS BankAccount. * Account properties 01 account-number PIC X(10). 01 account-holder PIC X(50). 01 balance PIC 9(10)V99 VALUE 0.00. * Constructor METHOD-ID. NEW. DATA DIVISION. LINKAGE SECTION. 01 number PIC X(10). 01 holder PIC X(50). PROCEDURE DIVISION USING number, holder. MOVE number TO account-number. MOVE holder TO account-holder. EXIT METHOD. END METHOD NEW. * Deposit method METHOD-ID. deposit. DATA DIVISION. LINKAGE SECTION. 01 amount PIC 9(10)V99. PROCEDURE DIVISION USING amount. ADD amount TO balance. DISPLAY "Deposited: " amount " New balance: " balance. EXIT METHOD. END METHOD deposit. * Withdraw method METHOD-ID. withdraw. DATA DIVISION. LINKAGE SECTION. 01 amount PIC 9(10)V99. 01 success PIC X VALUE "N". PROCEDURE DIVISION USING amount RETURNING success. IF balance >= amount SUBTRACT amount FROM balance MOVE "Y" TO success DISPLAY "Withdrawn: " amount " New balance: " balance ELSE DISPLAY "Insufficient funds" MOVE "N" TO success END-IF. EXIT METHOD. END METHOD withdraw. * Get balance method METHOD-ID. getBalance. DATA DIVISION. LINKAGE SECTION. 01 current-balance PIC 9(10)V99. PROCEDURE DIVISION RETURNING current-balance. MOVE balance TO current-balance. EXIT METHOD. END METHOD getBalance. END CLASS BankAccount. * Savings account subclass CLASS-ID. SavingsAccount INHERITS FROM BankAccount. OBJECT SECTION. CLASS SavingsAccount. * Interest rate 01 interest-rate PIC 9(3)V99 VALUE 2.50. * Add interest method METHOD-ID. addInterest. PROCEDURE DIVISION. COMPUTE balance = balance + (balance * interest-rate / 100). DISPLAY "Interest added. New balance: " balance. EXIT METHOD. END METHOD addInterest. END CLASS SavingsAccount.

Bank account class with inheritance for savings accounts.

Best Practices

  • Use meaningful class names that describe the purpose of the class.
  • Encapsulate data and provide getter/setter methods for access.
  • Use inheritance to create class hierarchies when appropriate.
  • Implement constructors to initialize class data properly.
  • Document class methods and their parameters clearly.
  • Follow object-oriented design principles.

Common Pitfalls

  • Not properly initializing class data in constructors.
  • Creating overly complex class hierarchies.
  • Not providing appropriate access methods for class data.
  • Mixing procedural and object-oriented programming styles.
  • Not documenting class interfaces and method purposes.

Test Your Knowledge

1. What is CLASS-ID in COBOL?

  • A data type
  • A class identifier for object-oriented programming
  • A file operation
  • A program structure

2. What does CLASS-ID define?

  • A data structure
  • A class with methods and properties
  • A file definition
  • A program section

3. Where is CLASS-ID typically used?

  • In procedural COBOL
  • In object-oriented COBOL
  • In file processing
  • In data declarations

4. What follows CLASS-ID in a class definition?

  • Data declarations
  • Class name and object section
  • File definitions
  • Procedure division

5. What is the primary benefit of using CLASS-ID?

  • Faster execution
  • Object-oriented programming capabilities
  • Better file handling
  • Improved data storage

Frequently Asked Questions