home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_12 / 9n12117a < prev    next >
Text File  |  1991-10-21  |  2KB  |  70 lines

  1. /* LISTING 8 - OBJ.C */
  2.  
  3. /* OBJ.C  -  implement private data storage
  4.  *              for each object */
  5.  
  6. /* note we no longer need to include obj.h */
  7.  
  8. #include <stdio.h>
  9. #define GETCOLOR 0
  10. #define SETCOLOR 1
  11.  
  12. #define YELLOW 14
  13. #define BLUE   9
  14. #define RED    12
  15.  
  16. /* duplication of typedef struct circle */
  17. typedef struct circle  
  18.    {
  19.    void *pprivate;
  20.  
  21.    /* action pack is now accessed with
  22.     *  a pointer to a pointer to a function
  23.     *  returning int
  24.     */
  25.    int (**pcact)(); 
  26.    } CIRCLE;
  27.  
  28. main()
  29.    {
  30.    CIRCLE c1, c2;  /* declare two circles */
  31.    int color;
  32.  
  33.    /* call the constructor for each circle */
  34.    constructor(&c1, YELLOW);
  35.    constructor(&c2, RED);
  36.  
  37.    /* use the act pack to get color */
  38.    color = (*c1.pcact[GETCOLOR])(&c1);
  39.    printf("Color of c1 is %d\n", color);
  40.  
  41.    color = (*c2.pcact[GETCOLOR])(&c2);
  42.    printf("Color of c2 is %d\n", color);
  43.  
  44.    (*c1.pcact[SETCOLOR])(&c1, BLUE); 
  45.    printf("Setting color of c1 to BLUE\n");
  46.  
  47.    color = (*c1.pcact[GETCOLOR])(&c1);
  48.    printf("Color of c1 is now %d\n", color);
  49.  
  50.    (*c2.pcact[SETCOLOR])(&c2, YELLOW);
  51.    printf("Setting color of c2 to YELLOW\n");
  52.  
  53.    color = (*c2.pcact[GETCOLOR])(&c2);
  54.    printf("Color of c2 is now %d\n", color);
  55.  
  56.    destructor(&c1);  /* free circle storage */
  57.    destructor(&c2);
  58.    }
  59.  
  60.  
  61.  
  62. /* SAMPLE OUTPUT FROM LISTING 9 */
  63. Color of c1 is 14
  64. Color of c2 is 12
  65. Setting color of c1 to BLUE
  66. Color of c1 is now 9
  67. Setting color of c2 to YELLOW
  68. Color of c2 is 14
  69. */
  70.