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.
CLASS-ID follows specific syntax patterns for class definitions:
1234567891011121314151617181920212223242526272829303132* 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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344* 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.
Here are some practical uses of CLASS-ID in COBOL:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263* 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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778* 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.
1. What is CLASS-ID in COBOL?
2. What does CLASS-ID define?
3. Where is CLASS-ID typically used?
4. What follows CLASS-ID in a class definition?
5. What is the primary benefit of using CLASS-ID?