Creating C++ Classes for Use in ILE - IBM-ILE

You can access existing C++ classes from other languages such as ILE C,but you need to write your own functions to display and manipulate the data members of these classes.

A shared C/C++ header for class MyClass might look like the following:

Mapping a C++ Class to a C Structure

A C++ class without virtual functions can be mapped to a corresponding C structure, but there are fundamental differences between both data types. The C++ class contains data members and member functions to access and manipulate these data members.The corresponding C structure contains only the data members, but not the member functions contained in the C++ class.

The class Class1 in C++ is:

To access a C++ class from a C program you need to write your own functions to inspect and manipulate the class data members directly.

Note: While data members in the C++ class can be public, protected, or private, the variables in the corresponding C structure are always publicly accessible. Be careful, you may eliminate the safeguards built into the C++ language.

You can use C++ operators on this class if you supply your own definitions of these operators in the form of member functions.

When you write your own C++ classes that you want to access from other languages:

  • Do not use static data members in your class, because they are not part of the C++ object that is passed to the other language.
  • Do not use virtual functions in your class, because you cannot access the data members since the alignment of the data members between the class and the C structure is different.
  • By making all data members of a class publicly accessible to programs written in other languages, you may be breaking data encapsulation.

Using C++ Objects in a C Program

This program shows how you can access the data members in C++ classes from source code written in C.

Program Structure

The program consists of these files:

  • A C++ source file hourclas.cpp which contains:
    • Definitions of one base class, HourMin, and two derived classes, HourMinSec1 and HourMinSec2
    • Three function prototypes with extern "C" linkage:
      • extern "C" void CSetHour(HourMin *)
      • extern "C" void CSetSec(HourMin *)
      • extern "C" void CSafeSetHour(HourMin *)
    • The definition of a function with extern "C" linkage, extern "C" void CXXSet Hour (HourMin * x)
    • A main() function containing the program logic
  • A C source file hour.c which contains:
    • A structure CHourMin that maps to the C++ class HourMin in file hourclas.cpp
    • Definitions of the three functions with extern "C" linkage declared in hourclas.cpp
    • The definition of a function CSafeSetHour()

Program Description

In its main() function the program:

  1. Instantiates an object hm of the base class HourMin
  2. Assigns a value to the h variable (hour) in the base class
  3. Passes the address of the base class to the function CSetHour() defined in the C source file hour.c, which assigns a new value to h in the base class
  4. Displays the value of h in the base class
  5. Instantiates an object hms1 of the derived class HourMinSec1
  6. Passes the address of this object class to the function CSetSec() defined in the C source file hour.c, which assigns a value to s in the object
  7. Displays the value of s in the object
  8. Instantiates an object hms2 of the class HourMinSec2 which contains the class HourMin
  9. Passes the address of this new object to the function CSetSec() defined in the C source file hour.c, which assigns a value to s in the object
  10. Displays the value of s in the object
  11. Passes the address of the base class object to function SafeSetHour() defined in the C source file hour() which passes the address back to a function CXXSetHour() defined in the C++ source file hourclas.cpp

The C++ source file hourclas.cpp is:

Program Output

The program output is:

Program Output


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM-ILE Topics