ILE C/C++ Programmer's Guide

Example: An ILE C Program that Uses C++ Objects

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:

Figure 199. C++ Source File hourclas.cpp Definitions Used by C Source File hour.c


#include <iostream.h>
 
class HourMin {        // base class
  protected:
    int h;
    int m;
 
  public:
    void set_hour(int hour) { h = hour % 24; }  // keep it in range
    int  get_hour()         { return h; }
    void set_min(int min)   { m =  min % 60; } // keep it in range
    int  get_min()          { return m; }
    HourMin(): h(0), m(0) {}
    void display() { cout << h << ':' << m << endl; }
};
 
// derived from class HourMin
class HourMinSec1 : public HourMin {
  public:
    int s;
    void set_sec(int sec) { s = sec % 60; }  // keep it in range
    int  get_sec()        { return s; }
    HourMinSec1() { s = 0; }
    void display() { cout << h << ':' << m << ':' << s << endl; }
};
 
// has an HourMin contained inside
class HourMinSec2 {
  private:
    HourMin a;
    int s;
 
  public:
    void set_sec(int sec) { s = sec % 60; }  // keep it in range
    int  get_sec()        { return s; }
    HourMinSec2()         { s = 0; }
    void display() {
       cout << a.get_hour() << ':' << a.get_min() << ':' << s << endl; }
};
 
extern "C" void CSetHour(HourMin *);     // defined in C
extern "C" void CSetSec(HourMin *);      // defined in C
extern "C" void CSafeSetHour(HourMin *); // defined in C
 
// wrapper function to be called from C code */
extern "C" void CXXSetHour(HourMin * x) {
  x->set_hour(99);  // much like the C version but the C++
                    // member functions provide some protection
                    // expect 99 % 24, or 3 to be the result
}
 
 
// other wrappers may be written to access other member functions
// or operators ...
 
main() {
 
  HourMin hm;
  hm.set_hour(18);  // supper time;
  CSetHour(&hm);    // pass address of object to C function
  hm.display();     // hour is out of range
 
  HourMinSec1 hms1;
  CSetSec((HourMin *) &hms1)
  hms1.display();
 
  HourMinSec2 hms2;
  CSetSec(&hms2);
  hms2.display();
 
  CSafeSetHour(&hm); // pass address to a safer C function
  hm.display();     // hour is not out of range
}

Figure 200. C Source file hour.c that Uses Definitions from C++ Source File hourclas.cpp



/* C code hour.c */

struct CHourMin {
int hour;
int min;
};

void CSetHour(void * v) {
struct CHourMin * p;
p = (struct CHourMin *) v; // force it to the type we want
p->hour = 99; // with power comes responsibility (oops!)
}

struct CHourMinSec {
struct CHourMin hourMin;
int sec;
};

// handles both HourMinSec1, and HourMinSec2 classes

void CSetSec(void *v) {
struct CHourMinSec * p;
p = (struct CHourMinSec *) v; // force it to the type we want
p->sec = 45;
}

void CSafeSetHour(void *v) {
struct CHourMin * p;
p = (struct CHourMin *) v; // force it to the type we want

// ... do things with p, but be careful
// ...
// use a C++ wrapper function to access C++ function members

CXXSetHour(p); // almost the same as p->hour = 99
}

Program Flow

As shown in Figure 199, the main() C++ function:

  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 (see Figure 200), 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 (see Figure 200), 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 (see Figure 200), 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 (see Figure 199)

Program Output

The program output is:

+--------------------------------------------------------------------------------+
|99:0                                                                            |
|0 -:0 :45                                                                       |
|0 -:0 :45                                                                       |
|3 -:0                                                                           |
|Press ENTER to end terminal session.                                            |
+--------------------------------------------------------------------------------+


[ Top of Page | Previous Page | Next Page | Table of Contents ]