home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX0808.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  1.3 KB  |  50 lines

  1. // \EXAMPLES\EX0808.CPP
  2. // program that access members of nested classes
  3.  
  4. //--------------------------------------------------------------
  5. #include <iostream.h>
  6.  
  7. //--------------------------------------------------------------
  8. // global declaration
  9. int a;
  10.  
  11. //--------------------------------------------------------------
  12. //declartion of nested classes  Tower and Bell
  13.  
  14. struct Tower
  15. { int m;
  16.   static int r;
  17.   class Bell
  18.   {  int k;
  19.   public:
  20.      void ring(Tower* t )
  21.      {  //m = 85;           error - m not accessible
  22.         t->m = 85;           // OK - access through pointer
  23.         r = 50;              // OK - r is static
  24.         a = 100; }           // OK - a is global
  25.   };
  26.   void pull()
  27.   { //k = 75;                error - k is private
  28.     cout << "function pull()" << endl;
  29.   }
  30. };
  31.  
  32. //--------------------------------------------------------------
  33. // define and initialize static member
  34. int Tower::r = 0;
  35.  
  36. //--------------------------------------------------------------
  37. //main to exercise Tower and Bell
  38.  
  39. void main()
  40. {  //Bell chime;              error - Bell not in scope
  41.    Tower::Bell chime;          // OK
  42.    Tower test;
  43.    test.pull();
  44.    chime.ring(&test);
  45.    cout << a << '\t' << test.m << '\t' << Tower::r << '\t'
  46.         << endl;
  47. }
  48.  
  49. //--------------------------------------------------------------
  50.