home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ANSICPP.ZIP / EX08012.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-24  |  858 b   |  36 lines

  1. // ex08012.cpp
  2. // Overloaded -> operator
  3. #include <iostream.h>
  4.  
  5. class Date {
  6.     int mo, da, yr;
  7. public:
  8.     Date(int m, int d, int y) { mo = m; da = d; yr = y; }
  9.     void display() 
  10.         { cout << '\n' << mo << '/' << da << '/' << yr; }
  11. };
  12. // ----------- "smart" Date pointer
  13. class DatePtr {
  14.     static Date *dp;
  15. public:
  16.     DatePtr() {}
  17.     DatePtr(Date *d) { dp = d; }
  18.     Date *operator->();
  19. };
  20. Date *DatePtr::operator->()
  21. {
  22.     static Date nulldate(0,0,0);
  23.     if (dp == NULL)            // if the pointer is NULL
  24.         return &nulldate;    // return the dummy address
  25.     return dp;                // otherwise return the pointer
  26. }
  27.  
  28. main()
  29. {
  30.     DatePtr dp;            // a date pointer with nothing in it
  31.     dp->display();        // use it to call display function
  32.     Date dt(3,17,90);    // a Date
  33.     dp = &dt;            // put address of date in pointer
  34.     dp->display();        // display date through the pointer
  35. }
  36.