                                                            ++   591

                                        IV

                                

     4     , ..  
 C++    ,     , 
 .       , C++ 
      .  
 ,    ,    
,  .     
, -      .
        ,  
 . ,     ,
   ,    
   .
     13     :   , 
  (..      ),
     ,  ,
       .
     14   C++    
 ,        
 - ,  ,   
.        ,
         
  .
     15     ,   
     ,    4. 
,           ,  
   .    15    
,    ,   
 , ,  ,  , ,  
    new  delete.   
 ,   ,     ,
   ,   .     
  -  ,   
  .   
,        
      .  
         ,
-   .
     16   .       , 
     . , vector  
  ,   ,  buffer    
   .    ,    
.    C++       
,    -,     .
        ,    10, 
,       .

                                                               ++   592

13
                                  13. 

      C++     
.        . 
      . ,
, IntArray,    2,   , 
  int.        ,
 Screen ()  Account ( ).  , 
  ,     .
       ,       ;
,      -,  ,  
-,   ,    .
 ,     ,  
   ,     .
,     ,  
    .
               
.   ,       
- ,   ,  
         
 .       
      ;   ,
  , -     .

                          13.1.  

        : ,   
class,     ,  ,    . 
         :

   class Screen { /* ... */ };
   class Screen { /* ... */ } myScreen, yourScreen;

      -  -    
 .  ,      .
        .      
 ,      :

                                                              ++   593

   class First {
      int memi;
     double memd;
   };

   class Second {
      int memi;
      double memd;
   };

   class First obj1;
   Second obj2 = obj1; // : obj1  obj2   

        .    
      .       
    ,      . (
        13.9.)
        ,      :

      class,      .   
     obj1  First    ;
      .    obj2  Second  
    .

         .     C 
     ;     C++ 
 .

                              13.1.1. -

   -    ,  . ,   Screen
   -:

   #include <string>

   class Screen {
      string _screen; // string( _height * _width )
      string::size_type _cursor; //    
      short _height; //  
      short _width; //  
   };

           
 Screen,   _screen   string.  _cursor    
,        .   
  string::size_type. ( size_type    6.8.)
        short  .   
Screen,   :

                                                                ++   594

   class Screen {
   /*
    * _ screen    _height * _width
    * _cursor     
    * _height  _width -     
    */
      string _screen;
     string::size_type _cursor;
      short _height, _width;
   };

        :

   class StackScreen {
      int topStack;
      void (*handler)(); //   
      vector<Screen> stack; //  
   };

    -  .     
 -. (    ,    
 13.5.)
    -       
    .  ,   
,      :

   class First {
      int memi = 0; // 
      double memd = 0.0; // 
   };

   -      . (
     2.3;     
 14.)

                           13.1.2. -

   , -,       
Screen:   ,      
      ,    
  .        -.
   -     .      ,
       . (, 
         .
     8.2,      
8.5.) :

                                                              ++   595

   class Screen {
   public:
      void home();
      void move( int, int );
      char get();
      char get( int, int );
      void checkRange( int, int );
      // ...
   };

 -      :

   class Screen {
   public:
      //   home()  get()
      void home() { _cursor = 0; }
      char get() { return _screen[_cursor]; }
      // ...
   };

   home()       ; get()  ,
    .
   -     :

   -      , ,   
        .  -    
            (.)   (->):

    ptrScreen->home();
    myScreen.home();

    (  13.9      );
   -      ,     
    ,       . , -
      ,  ,     - 
    .

   -    (   
 9).       -  .
   ,       ,
-      , ,  
 . ,  get(int, int)   get()  
  Screen:

                                                               ++   596

   class Screen {
   public:
      //   - get()
      char get() { return _screen[_cursor]; }
      char get( int, int );
      // ...
   };

(    -    13.3.)

                               13.1.3.   

     ,        
 . ,     Screen , 
       80  24.     
      ,  
:

   class Screen {
   public:
      // -
   private:
      //    (. 13.5)
      static const int _height = 24;
      static const int _width = 80;
      string _screen;
      string::size_type _cursor;
   };

     - (,    -
)   ,   .    ,  
   - (    
 ).
     -  Screen       
,        
 ?

    ,     - 
    ,   . ,    
       ;
       ,  ,   
    Screen   -,    .  
     -   ,    
    .

        ,    
      .   
      ,   
public, private  protected   . ,  
 public,  ,     private  protected
   .

                                                              ++   597

         . , 
    ,    -,  ,
            ;
       -   . , 
      ,   - ;
              
           . (  2  
          IntArray.  
       17,    .)

       Screen   public  private:

   class Screen {
   public:
      void home() { _cursor = 0; }
      char get() { return _screen[_cursor]; }
      char get( int, int );
      void move( int, int );
      // ...
   private:
      string _screen;
      string::size_type _cursor;
      short _height, _width;
   };

     ,     .
( ,     C++     
    - , .   [LIPPMAN96a].)   
    public, protected  private.  
     ,     .
    ,  ,   
 ,    private.

                                 13.1.4. 

            .
         .
         friend    
  .       ,   
,     .      
     :

   class Screen {
      friend istream&
         operator>>( istream&, Screen& );
      friend ostream&
         operator<<( ostream&, const Screen& );
   public:
      // ...    Screen
   };

                                                         ++   598

             
 Screen.       :

   #include <iostream>

   ostream& operator<<( ostream& os, const Screen& s )
   {
      // :    _height, _width  _screen
      os << "<" << s._height
           << "," << s._width << ">";

      os << s._screen;

      return os;
   }

         , -   
  .      - 
    ,   . (
 15.2    .)

                       13.1.5.    

     ,   ,    ,  
.       ,  ,   .
     ,   . :

   class Screen; //   Screen

         Screen  ,     
.
    ,        
.     ,      ,
          , 
   .
            ,   
  ,    . ,    
  ,    (*)   , 
          , 
    .
           - 
 ,       .  
  ,       . 
  StackScreen,       
Screen,  ,    :

                                                             ++   599

   class Screen; // 

   class StackScreen {
      int topStack;
      // :    Screen
      Screen *stack;
      void (*handler)();
   };

       ,     ,    
  -   .    ,
    ,     , 
     . :

   class LinkScreen {
      Screen window;
      LinkScreen *next;
      LinkScreen *prev;
   };

                                      13.1

      Person    :

   string _name;
   string _address;

  -:

   Person( const string &n, const string &s )
         : _name( n ), _address( a ) { }
   string name() { return _name; }
   string address() { return _address; }

          public,      private? 
 .

                                       13.2

         .    
  ?  ?

                                    13.2.  

 ,  Screen,     . 
  ,     . ,  
  Screen:

                                                                 ++   600

   class Screen {
   public:
      // -
   private:
      string _screen;
      string:size_type _cursor;
      short _height;
      short _width;
   };

 

   Screen myScreen;

  ,      Screen. 
myScreen    .       
-.   myScreen      
   Screen.
             . 
   ,    :

   class Screen {
      //  
   };

   int main()
   {
      Screen mainScreen;
   }

    Screen     ,    mainScreen  
   main().
        .    ,  ( 
      )   ( 
)  ,        
       .   
        . (   
     8.)
              . 
        .
:

   Screen bufScreen = myScreen;
   // bufScreen._height = myScreen._height;
   // bufScreen._width = myScreen._width;
   // bufScreen._cursor = myScreen._cursor;
   // bufScreen._screen = myScreen._screen;


                                                             ++   601

           .    
          
.    l-    . (
-       
         .)

   int main()
   {
      Screen myScreen, bufScreen[10];
      Screen *ptr = new Screen;
      myScreen = *ptr;
      delete ptr;
      ptr = bufScreen;
     Screen &ref = *ptr;
     Screen &ref2 = bufScreen[6];
   }

         ,     
     .   
           
. (  7.3   ,   
   ,  ,    .   7.4 
      .)
        -    
 .   (.) ,  
      ;   (->)    
  :

   #include "Screen.h"

   bool isEqual( Screen& s1, Screen *s2 )
   { //  false,    ,  true -  
      if (s1.height() != s2->height() ||
          s2.width() != s2->width() )
      return false;

      for ( int ix = 0; ix < s1.height(); ++ix )
         for ( int jy = 0; jy < s2->width(); ++jy )
            if ( s1.get( ix, jy ) != s2->get( ix, jy ) )
               return false;
      return true; //  ? ,  
   }

   isEqual()      ,    
Screen.         Screen,  
     .      -
  .
         isEqual()   -
 height()  width()     .  
:

                                                         ++   602

   class Screen {
   public:
      int height() { return _height; }
      int width() { return _width; }
      // ...
   private:
      short _heigh, _width;
      // ...
   };

           
   :   
(*)  ,    ,   
       . , 

   s2->height()

  :

   (*s2).height()

        .

                           13.3. - 

   -   ,    . ,
 Screen         -:

   class Screen {
   public:
      void home() { _cursor = 0; }
      char get() { return _screen[_cursor]; }
      char get( int, int );
      void move( int, int );
      bool checkRange( int, int );
      int height() { return _height; }
      int width() { return _width; }
      // ...
   };

            -, 
-    :

   Screen myScreen, groupScreen;
   myScreen.home();
   groupScreen.home();

      home()   myScreen     
_cursor.        groupScreen,   

                                                             ++   603

  _cursor   ,    home()    . 
   -   -  ?  
  this,    .

               13.3.1.    -

    ,    home(), get(), height()  width()
    .    . ( 
    7.6.)
   -        ,  
     inline:

   class Screen {
   public:
      //    inline
      //    -
      inline void home() { _cursor = 0; }
      inline char get() { return _screen[_cursor]; }
      // ...
   };

    home()  get()    . 
  inline ,          -
,    .
   -,      ,    . 
        
:        .  
   checkRange(),   Screen:

   #include <iostream>
   #include "screen.h"

   //  -   Screen::
   bool Screen::checkRange( int row, int col )
   { //   
      if ( row < 1 || row > _height ||
         col < 1 || col > _width ) {
      cerr << "Screen coordinates ( "
             << row << ", " << col
             << " ) out of bounds.\n";

         return false;
      }
      return true;
   }

      -   ,    
,   . ,     
checkRange()      Screen.h,    
  .       .   
     .

                                                               ++   604

 -,    ,   . 
    ,     inline 
          ,    
 .    move()    -
  Screen:

   inline void Screen::move( int r, int c )
   { //     
      if ( checkRange( r, c ) ) //     ?
      {
         int row = (r-1) * _width; //   
         _cursor = row + c - 1;
      }
   }

    get(int, int)      inline:

   class Screen {
   public:
      inline char get( int, int );
      //   -  
   };

        .    inline 
:

   char Screen::get( int r, int c )
   {
      move( r, c ); //  _cursor
      return get(); //   - get()
   }

      -       ,
  ,   ,     , 
     ,     . ,
   move()  get()    
 Screen.h    Screen.

                         13.3.2.    

   ,   -    
  ,        .   
:

     -       ,
      ,      ;
    -    ,    
      .

                                                             ++   605

   :

   #include <string>

   void Screen::copy( const Screen &sobj )
   {
      //      sobj -    ,
      //  
      //    this (.  13.4)
      if ( this != &sobj )
      {
         _height = sobj._height;
         _width = sobj._width;
         _cursor = 0;

         //   ;
         //    ,  sobj._screen
         _screen = sobj._screen;
      }
   }

    _screen, _height, _width  _cursor     Screen,
- copy()    .     
  ,  ,       , 
 - .   copy()  :

   #include "Screen.h"

   int main()
   {
      Screen s1;
      //  s1

      Screen s2;
      s2.copy(s1);

      // ...
   }

  sobj   copy()    s1  
main(). - copy()    s2,   
.     _screen, _height, _width  _cursor,  
        ,    
s2.           
-   ,  , ,    
   this.

                      13.3.3.    -

   -       public, private  protected 
.     ?  -  ,
   .   -

                                                            ++   606

  . , - home(), move()  get() 
Screen  ,      
 .
          ,  
 ,      Screen  
 -.       
       .
           . 
       , 
      .
          ,    
   .     set(), 
   Screen.      :

   class Screen {
   public:
      void set( const string &s );
      void set( char ch );
      //   -  

   };

      :

   void Screen::set( const string &s )
   { //   ,     
      int space = remainingSpace();
      int len = s.size();
      if ( space < len ) {
         cerr << "Screen: warning: truncation: "
               << "space: " << space
               << "string length: " << len << endl;
         len = space;
      }

      _screen.replace( _cursor, len, s );
      _cursor += len - 1;
   }

   void Screen::set( char ch )
   {
      if ( ch == '\0' )
         cerr << "Screen: warning: "
                << "null character (ignored).\n";
      else _screen[_cursor] = ch;
   }

      Screen  ,   Screen   
.    set()      .
       -  ,    
  ,       - (

                                                             ++   607

) ,    ,     
  .    - remainingSpace
 Screen(),   set(const string&).

   class Screen {
   public:
      //   -  
   private:
      inline int remainingSpace();
   };

remainingSpace() ,     :

   inline int Screen::remainingSpace()
   {
      int sz = _width * _height;
      return ( sz - _cursor );
   }

(  -     17.)
          
 -:

   #include "Screen.h"
   #include <iostream>

   int main() {
      Screen sobj(3,3); //     13.3.4
      string init("abcdefghi");
      cout << "Screen Object ( "
             << sobj.height() << ", "
             << sobj.width() << " )\n\n";

      //   
      string::size_type initpos = 0;
      for ( int ix = 1; ix <= sobj.width(); ++ix )
         for ( int iy = 1; iy <= sobj.height(); ++iy )
         {
            sobj.move( ix, iy );
            sobj.set( init[ initpos++ ] );
        }
            
        //   
        for ( int ix = 1; ix <= sobj.width(); ++ix )
        {
           for ( int iy = 1; iy <= sobj.height(); ++iy )
              cout << sobj.get( ix, iy );
              cout << "\n";
       }
      return 0;
   }

                                                          ++   608

       ,   :

   Screen Object ( 3, 3 )
   abc
   def
   ghi

                        13.3.4.  -

      -,     
,  , ,  , 
  .    .  
   ,      
 new.         . ,
,    Screen,     
   hi, wid  bkground:

   class Screen {
   public:
      Screen( int hi = 8, int wid = 40, char bkground = '#');
      //   -  
   };

      Screen  :

   Screen::Screen( int hi, int wid, char bk ) :
      _height( hi ), //  _height  hi
      _width( wid ), //  _width  wid
      _cursor ( 0 ), //  _cursor 
      _screen( hi * wid, bk ) //    hi * wid
                                      //   
                                      //  '#'
   {  //       
      //      14.5
   }

       Screen  
:

   Screen s1; // Screen(8,40,'#')
   Screen *ps = new Screen( 20 ); // Screen(20,40,'#')

   int main() {
      Screen s(24,80,'*'); // Screen(24,80,'*')
      // ...

   }

                                                         ++   609

(  14 ,     
 .   15      .)

          13.3.5. -   const  volatile

           
  . :

   const char blank = ' ';
   blank = '\n'; // 

     ,  ,    . 
      -.     
 ,     (,  
 )   (,    ) -:

   const Screen blankScreen;
   blankScreen.display(); //   
   blankScreen.set( '*' ); // :   

      ,  -   ,
      const:

   class Screen {
   public:
      char get() const { return _screen[_cursor]; }
      // ...
   };

    ,   const,      -,
     const.   const 
     -.   -,
   ,       ,  
 :

                                                              ++   610

   class Screen {
   public:
      bool isEqual( char ch ) const;
      // ...
   private:
      string::size_type _cursor;
      string _screen;
      // ...
   };

   bool Screen::isEqual( char ch ) const
   {
      return ch == _screen[_cursor];
   }

      -,    .
,    :

   class Screen {
   public:
      int ok() const { return _cursor; }
      void error( int ival ) const { _cursor = ival; }
      // ...
   private:
      string::size_type _cursor;
      // ...
   };

 - ok() ,       _cursor.
   error()  _cursor ,   -
          :

   error: cannot modify a data member within a const member function
   :    -   -

       ,    -, 
 , .    const 
 -     . 
  ,  -    -, 
   ,       
 ,    .    
  . :

                                                                ++   611

   #include <cstring>

   class Text {
   public:
      void bad( const string &parm ) const;
   private:
      char *_text;
   };

   void Text::bad( const string &parm ) const
   {
      _text = parm.c_str(); // :   _text
      for ( int ix = 0; ix < parm.size(); ++ix )
         _text[ix] = parm[ix]; //  ,   
   }

    _text ,     char*,  ,   
,     -  Text. -
 bad()    .  -
  ,        
,       .
 -       
 :

   class Screen {
   public:
      char get(int x, int y);
      char get(int x, int y) const;
      // ...
   };

        const    ,   
  :

   int main() {
      const Screen cs;
      Screen s;

      char ch = cs.get(0,0); //   -
      ch = s.get(0,0); //   -
   }

          -, 
      .   
 ,    ,    , 
  .  ,    const
          
.

                                                              ++   612

   -      volatile (   
 3.13).     volatile,    
,     (,   
,   /).     
-    ,   :

   class Screen {
   public:
      char poll() volatile;
      // ...
   };

   char Screen::poll() volatile { ... }

                         13.3.6.  mutable

       Screen    .
,     Screen,    
.         . 
    Screen:

   const Screen cs ( 5, 5 );

       ,    (3,4),   
:

   //      (3,4)
   // !   
   cs.move( 3, 4 );
   char ch = cs.get();

       : move()     -,  
  .  move()   :

   inline void Screen::move( int r, int c )
   {
       if ( checkRange( r, c ) )
       {
          int row = (r-1) * _width;
          _cursor = row + c - 1; //  _cursor
      }
   }

    ,  move()   _cursor, ,  
  .
       _cursor     Screen?
 _cursor    .  ,    

                                                              ++   613

,       .  _cursor
     ,    Screen   const.
       ,   ,
   (mutable).      
,      .   ,  
 -   const.   
     mutable:

   class Screen {
   public:
      // -
   private:
      string _screen;
      mutable string::size_type _cursor; //  
      short _height;
      short _width;
   };

         _cursor,  move() 
  .  move()   ,   
 .

   // move() -  -
   inline void Screen::move( int r, int c ) const
   {
      // ...
      // :  -   
      //   mutable
      _cursor = row + c - 1;
     // ...
   }

            
     .
   ,      _cursor,   _screen, _height 
_width    mutable,      
 Screen  .

                                  13.3

   ,     copy()   :

   Screen myScreen;
   myScreen.copy( myScreen );

                                  13.4

                                                              ++   614

            
   .          
 .   forward()  backward().

                                  13.5

              
.           
 ;     ,    
.   up()  down().      
  cout    '007'.

                                  13.6

     -  Screen   ,  
, .   .

                             13.4.   this

          -. :

   int main() {
      Screen myScreen( 3, 3 ), bufScreen;
      myScreen.clear();
      myScreen.move( 2, 2 );
      myScreen.set( '*' );
      myScreen.display();

      bufScreen.resize( 5, 5 );
      bufScreen.display();
   }

     myScreen    _width, _height, _cursor  _screen,   
bufScreen  .   -    
.    myScreen  bufScreen.
       ,  -     
,    . ,   move() 
 :

   inline void Screen::move( int r, int c )
   {
      if ( checkRange( r, c ) ) //     ?
      {
         int row = (r-1) * _width; //  
         _cursor = row + c - 1;
     }
   }

     move()    myScreen,   _width  _height, 
    ,     myScreen.   
   bufScreen,       

                                                             ++   615

.    _cursor,   move(),  
 myScreen,  bufScreen?    this.
    -    ,    , 
this.   -     ,   
     ,      volatile
    . ,  - move() 
Screen  this   Screen*,    - List  
List*.
    this  ,    -,   
move()  myScreen     myScreen,     bufScreen 
  bufScreen.  ,  _cursor,    
move(),      myScreen,     bufScreen.
      ,   ,     this. 
    :

   1.   - ,   :

   // , ,   
   //  -
   //     C++
   inline void Screen::move( Screen *this, int r, int c )
   {
      if ( checkRange( r, c ) )
      {
         int row = (r-1) * this->_width;
        this->_cursor = row + c - 1;
      }
   }

          this     _width 
     _cursor  .
  2.    -     
         ,    :


       myScreen.move( 2, 2 );

     

       move( &myScreen, 2, 2 );

         this  . , 
,   ,  - home()  :

   inline void Screen::home()
   {
      this->_cursor = 0;
   }

                                                             ++   616

     ,      ,    
 - copy()  Screen.     
  .

                 13.4.1.    this

     main()  -  Screen   myScreen 
bufScreen  ,       .   
  - ,     
      . ,    main() 
 :

   int main() {
      // ...
      myScreen.clear().move( 2, 2 ), set( '*' ). display();
      bufScreen.reSize( 5, 5 ).display();
   }

          :
  myScreen,     (2,2),    
 '*'   .
        , ..  
  . ,   myScreen.clear(), 
myScreen.move()  ..  myScreen.move()    
myScreen.clear(),  clear()    myScreen, 
   .   ,      -
     this.   clear():

   //  clear()    
   //       bkground = '#'
   Screen& Screen::clear( char bkground )
   { //         
      _cursor = 0;
      _screen.assign( //   
          _screen.size(), // size() 
          bkground //   bkground
      );

      //  ,     
      return *this;
   }

    ,     -  Screen&   
   .   ,   
 move()  set().      void  Screen&,  
  *this.
    - display()   :

                                                             ++   617

   Screen& Screen::display()
   {
      typedef string::size_type idx_type;

      for ( idx_type ix = 0; ix < _height; ++ix )
      { //   
         idx_type offset = _width * ix; //  

         for ( idx_type iy = 0; iy < _width; ++iy )
            //     
            cout << _screen[ offset + iy ];

         cout << endl;
      }
      return *this;
   }

      reSize():

   //  reSize()    
   //       bkground = '#'
   Screen& Screen::reSize( int h, int w, char bkground )
   { //     h,   -  w
      //   
      string local(_screen);

      //   _screen
      _screen.assign( //   
         h * w, // h * w 
         bkground //   bkground
      );

      typedef string::size_type idx_type;
      idx_type local_pos = 0;

      //      
      for ( idx_type ix = 0; ix < _height; ++ix )
      { //   
         idx_type offset = w * ix; //  
         for ( idx_type iy = 0; iy < _width; ++iy )
           //      
           _screen[ offset + iy ] = local[ local_pos++ ];
      }

      _height = h;
      _width = w;
      // _cursor  

      return *this;
   }

     this    ,    
-.   copy()   13.3      
:

                                                              ++   618

   void Screen::copy( const Screen& sobj )
   {
      //    Screen  sobj -    ,
      //  
      if ( this != sobj )
      {
         //   sobj  this
      }
   }

    this   ,     -. 
,    sobj,    this,  sobj  this
      ,      . ( 
   ,     
   14.7.)

                                   13.7

    this      ,   
      . , - assign() 
classType  .    ,   ?

   classType& classType::assign( const classType &source )
   {
      if ( this != &source )
      {
         this->~classType();
         new (this) classType( source );
      }
      return *this;
   }

   ,  ~classType    .  new  
,         8.4.
         ?    ?
?

                           13.5.   

    ,         
 . ,  ,    ;
          ,
,       .    
    ,    , 
    .     , 
      .
           ,  
   ,   .     ,
        , 
         ,    
 .   ,     .

                                                            ++   619

            
:

           ,
    ,       
     ;
      ,     
     ,     .

      ,         
  static.        ,  
 . ,     Account 
_interestRate       double:

   class Account { //  
      Account( double amount, const string &owner );
      string owner() { return _owner; }
   private:
      static double _interestRate; //  
      double _amount; //   
      string _owner; // 
   };

    _interestRate  ,  _amount  _owner ?    
    ,    . ,
  _interestRate    , 
   Account.
      _interestRate    ,    
 .         const. 
    ,       Account  
 .        ,   
  ,       .
           .   
      .   
 _interestRate:

   //     
   #include "account.h"

   double Account::_interestRate = 0.0589;

           .  , 
        ,  , 
   - .
          .    
, ,    .. :

                                                               ++   620

   #include <string>

   class Account {
      // ...
   private:
      static const string name;
   };

   const string Account::name( "Savings Account" );

           
:   .        
    ,        
    int:

   //  
   class Account {
      //...
   private:
      static const int nameSize = 16;
      static const string name[nameSize];
   };

   //  
   const string Account::nameSize; //   
   const string Account::name[nameSize] = "Savings Account";

   ,      , 
,    .    
 ,        
. ,     nameSize 
 ,       -
   name.
          ,     
  .        ,
     .
  name    (   ),      .
       :

   class Account {
      //...
   private:
      static const int nameSize = 16; // :  
      static const string name[nameSize] = "Savings Account"; // 
   };

    name      .
    ,   nameSize    name  ,
   :

                                                               ++   621


   const string Account::name[nameSize] = "Savings Account";

nameSize     Account.     ,
 name    .    ? 
    - ,  
   .    name   
       ,    
  Account::name. (     
   13.9.)
       -      
 :

   inline double Account::dailyReturn()
   {
      return( _interestRate / 365 * _amount );
   }

      ,    ,     
   . -,   :

   class Account {
      // ...
   private:
      friend int compareRevenue( Account&, Account* );
      //   
   };

   //      ,
   //     
   int compareRevenue( Account &ac1, Account *ac2 );
   {
      double ret1, ret2;
      ret1 = ac1._interestRate * ac1._amount;
      ret2 = ac2->_interestRate * ac2->_amount;
      // ...
   }

    ac1._interestRate,   ac2->_interestRate    
Account::_interestRate.
          ,   
    .     , 
    ,     :

   //        

   if ( Account::_interestRate < 0.05 )

            , 
     ,    
  :

                                                             ++   622

   Account::

    ,       ,  , 
   .   
 compareRevenue   :

   int compareRevenue( Account &ac1, Account *ac2 );
   {
      double ret1, ret2;
      ret1 = Account::_interestRate * ac1._amount;
      ret2 = Account::_interestRate * ac2->_amount;
      // ...
   }

        ,     
 ,      ,  
  .

           ,  
     .         
     :

   class Bar {
   public:
      // ...
   private:
      static Bar mem1; // 
      Bar *mem2; // 
      Bar mem3; // 
   };

            
    - ,     :

   extern int var;

   class Foo {
   private:
      int var;
      static int stcvar;
   public:
       // :   Foo::var,
       //      
       int mem1( int = var );

      // :   static Foo::stcvar,
      //     
      int mem2( int = stcvar );

      // :     var
      int mem3( int = :: var );
   };

                                                         ++   623

                13.5.1.  -

   - raiseInterest()  interest()   
  _interestRate:

   class Account {
   public:
      void raiseInterest( double incr );
      double interest() { return _interestRate; }
   private:
      static double _interestRate;
   };

   inline void Account::raiseInterest( double incr )
   {
      _interestRate += incr;
   }

     ,   -     
    .    
    _interestRate,   , 
   .       
   .
       -  .   
 :

   class Account {
   public:
      static void raiseInterest( double incr );
      static double interest() { return _interestRate; }
   private:
      static double _interestRate;
   };

   inline void Account::raiseInterest( double incr )
   {
      _interestRate += incr;
   }

     -   ,   :  
     static,   const  volatile
.   ,    ,  static  
.
    -  this  ,    
        .  , 
         this ,
, . ,   -
dailyReturn()   ,    
  _amount.

                                                           ++   624

    -     ,   
 .     ,   ,
      .   ,
  :

   #include <iostream>
   #include "account.h"

   bool limitTest( double limit )
   {
      //       Account  
      // :   -
      return limit <= Account::interest() ;
}

   int main() {
      double limit = 0.05;

      if ( limitTest( limit ) )
      {
         //    -
         //    
         void (*psf)(double) = &Account::raiseInterest;
         psf( 0.0025 );
      }

      Account ac1( 5000, "Asterix" );
      Account ac2( 10000, "Obelix" );

      if ( compareRevenue( ac1, &ac2 ) > 0 )
         cout << ac1.owner()
                << " is richer than "
                << ac2.owner() << "\n";
      else
         cout << ac1.owner()
                << " is poorer than "
                << ac2.owner() << "\n";

      return 0;

   }

                                13.8

      Y    -   
-:

                                                            ++   625
   class X {
   public:
      X( int i ) { _val = i; }
      int val() { return _val; }
   private:
      int _val;
   };

   class Y {
   public:
      Y( int i );
      static X xval();
      static int callsXval();
   private:
      static X _xval;
      static int _callsXval;
   };

    _xval  20,  _callsXval  0.

                                    13.9

       13.8,    - 
 Y. callsXval()  ,    xval().

                                    13.10

        ? ?

   // example.h
   class Example {
   public:
      static double rate = 6.5;
      static const int vecSize = 20;
      static vector<double> vec(vecSize);
   };

   // example.c
   #include "example.h"
   double Example::rate;
   vector<double> Example::vec;

                             13.6.    

   ,     Screen    -:
forward(), back(), up()  down(),     ,
,   .        :

                                                        ++   626

   class Screen {
   public:
      inline Screen& forward();
      inline Screen& back();
      inline Screen& end();
      inline Screen& up();
      inline Screen& down();
      //  -  
   private:
      inline int row();
      //  -  
   };

   - forward()  back()     . 
          
 .

   inline Screen& Screen::forward()
   { //  _cursor     

      ++_cursor;

      //    ,    
      if ( _cursor == _screen.size() )
         home();

      return *this;
   }

   inline Screen& Screen::back()
   { //  _cursor     
      //    ,    
      if ( _cursor == 0 )
         end();
      else
         --_cursor;

      return *this;
   }

end()           
  - home():

   inline Screen& Screen::end()
   {
      _cursor = _width * _height - 1;
      return *this;
   }

    up()  down()        .  
           :

                                                          ++   627

   const char BELL = '\007';

   inline Screen& Screen::up()
   { //  _cursor    
      //   ,      
      if ( row() == 1 ) // ?
         cout << BELL << endl;
      else
         _cursor -= _width;

      return *this;
   }

   inline Screen& Screen::down()
   {
      if ( row() == _height ) //?
         cout << BELL << endl;
      else
         _cursor += _width;

      return *this;
   }

row()    -,     up()  down(),
  ,   :

   inline int Screen::row()
   { //   
      return ( _cursor + _width ) / height;
   }

     Screen     repeat(), 
   n .      :

   Screen &repeat( char op, int times )
   {
      switch( op ) {
         case DOWN: // n   Screen::down()
            break;
         case DOWN: // n   Screen::up()
            break;
         // ...
      }
   }

       .  , ,  -
  Screen  ,     
- repeat()  .    
.      -,  
     .
        op     
-  Screen.  repeat()    , 

                                                           ++   628

  ,    switch  .  
        .

                          13.6.1.   

         -,   
      . ,
 pfi       ,  
  int:


   int (*pfi)();

       HeightIs()  WidthIs() :

   int HeightIs();
   int WidthIs();

   pfi     :

   pfi = HeightIs;
   pfi = WidthIs;

     Screen     , height()  width(), 
      int:

   inline int Screen::height() { return _height; }
   inline int Screen::width() { return _width; }

        pfi     
 :

   //  :  
   pfi = &Screen::height;

     ?  -    , 
 ,   ,  .   - 
       ,    : 
    ;   ;  ,
   .
          -   
      .     
  ,      .
(      7.9.)    -

                                                                ++   629

         ,   this,
        -. ( 
  ,    .)    
      -      ,
  .
       -     
.         -.  
_height  Screen.    :   Screen  short.
   ,     _height       Screen
 short:

   short Screen::*

        Screen  short   :

   short Screen::*ps_Screen;

    ps_Screen    _height:

   short Screen::*ps_Screen = &Screen::_height;

    _width:

   short Screen::*ps_Screen = &Screen::_width;

    ps_Screen     _width  _height, 
     Screen  short.
       -      
   .     ,
    .   -  
      ,       
  . (  Inside the C++ Object Model ([LIPPMAN96a]) 
    .)
     -      
,     . ,  ,  
    height()  width(),    
-  Screen  ,     int:


int (Screen::*)()

     -  ,   :

   //    -     0
   int (Screen::*pmf1)() = 0;
   int (Screen::*pmf2)() = &Screen::height;
   pmf1 = pmf2;
   pmf2 = &Screen::width;

                                                              ++   630

    typedef       .
,     -  Screen  , 
    Screen, ..

   Screen& (Screen::*)()

    typedef  Action   :

   typedef Screen& (Screen::*Action)();
   Action default = &Screen::home;
   Action next = &Screen::forward;

      -     
     .      
     :

   Screen& action( Screen&, Action)();

action()     :     Screen 
  - Screen  ,     
.  action()     :

   Screen meScreen;
   typedef Screen& (Screen::*Action)();
   Action default = &Screen::home;
   extern Screen& action( Screen&, Sction = &Screen::display );

   void ff()
   {
      action( myScreen );
      action( myScreen, default );
      action( myScreen, &Screen::end );
   }

        -  .

                13.6.2.      

              
     .       
 (.*         ->*  ). , 
 -    :

                                                               ++   631

     int (Screen::*pmfi)() = &Screen::height;
     Screen& (Screen::*pmfS)( const Screen& ) = &Screen::copy;

   Screen myScreen, *bufScreen;

   //   -
   if ( myScreen.height() == bufScreen->height() )
      bufScreen->copy( myScreen );

   //    
   if ( (myScreen.*pmfi)() == (bufScreen->*pmfi)() )
   (bufScreen->*pmfS)( myScreen );

   

   (myScreen.*pmfi)()
   (bufScreen->*pmfi)();

 ,     () ,   
  -.  

   myScreen.*pmfi()

 

   myScreen.*(pmfi())

       pmfi()       
(.*). ,  pmfi    ,   
   .
     -  :

   typedef short Screen::*ps_Screen;
   Screen myScreen, *tmpScreen = new Screen( 10, 10 );

   ps_Screen pH = &Screen::_height;
   ps_Screen pW = &Screen::_width;

   tmpScreen->*pH = myScreen.*pH;

   tmpScreen->*pW = myScreen.*pW;

     - repeat(),      
.       -:

                                                              ++   632
   typedef Screen& (Screen::Action)();

   Screen& Screen::repeat( Action op, int times )
   {
      for ( int i = 0; i < times; ++i )
         (this->*op)();
      return *this;

  }

    op     -,    times .
           ,   repeat()
   :

   class Screen {
   public:
      Screen &repeat( Action = &Screen::forward, int = 1 );
      // ...
   };

      :

   myScreen.repeat( &Screen::down, 20 );

     .    Menu     
-  Screen,    .
CursorMovements  ,      
Menu.

   Action::Menu() = {
      &Screen::home,
      &Screen::forward,
      &Screen::back,
      &Screen::up,
      &Screen::down,
      &Screen::end
   };

   enum CursorMovements {
      HOME, FORWARD, BACK, UP, DOWN, END
   };

      - move(),   
CursorMovements    Menu    -. 
 :

                                                       ++   633

   Screen& Screen::move( CursorMovements cm )
   {
      ( this->*Menu[ cm ] )();
      return *this;
   }

       ([])  ,     
- (->*).    move()     
 Menu  -,       this
    -. move()    
,         
 .

                13.6.3.     

            .
          
.        ,  .
      . (,   -
   this.)
           ,    
 ,    .     
 .   Account:

   class Account {
   public:
      static void raiseInterest( double incr );
      static double interest() { return _interestRate ; }
      double amount() { return _amount; }
   private:
      static double _interestRate;
      double _amount;
      string _owner;
   };

   inline void Account::raiseInterest( double incr )
   {
      _interestRate += incr;
   }

    &_interestRate   double*:

   //     &_interestRate
   double Account::*

      &_interestRate  :

                                                           ++   634

   // : double*,   double Account::*
   double *pd = &Account::_interestRate;

       ,   ,     
:

   Account unit;

   //    
   double daily = *pd / 365 * unit._amount;

   ,  _interestRate  _amount   ,  
 - interest()   amount().
     interest()      :

   // 
   double (*)()

   -  Account:

   double (Account::*)()

        interest()   ,   
 :

   // : double(*pf)(),   double(Account::*pf)()
   double(*pf)() = &Account::interest;
   double daily = pf() / 365 * unit.amount();

                                    13.11

       _screen  _cursor  Screen?

                                    13.12

           Screen::_screen;
   Screen::_cursor.

                                    13.13

    typedef    -  Screen.

                                   13.14

                                                           ++   635

          - . 
  Screen ,       -
  ,  home()  end().

                                13.15

       Screen (  ) ,
       -  Screen, 
         ,  
home()  end().         
    ,    13.14. 
- Screen,     .

                                 13.16

      repeat(),    
cursorMovements.

               13.7.   ,  

        . -    
,    .   ,     
 .     ,    
   .        
  .
    ,   . 
,    ,   
 . , 

   int i = 0;

     :

  1.   int.
  2.  i.
  3.  =
  4.  0  int.
  5.   .

         , ,
   .  
        
.       , 
  :

   Type ID Assign Constant Semicolon
   (      )

        .     :

                                                           ++   636

   Type <==> int
   ID <==> i
   Constant <==> 0

    Assign  Semicolon    ,     
   :  :=  ;.
    ,          token  value.
token    , ,     Type, ID, Assign,
Constant  Semicolon,  85  ID  72  Semicolon.value 
  . ,   ID    value
   "i",    Type     int.
     value  .    
      ,      
.   ID  value   ,   Constant  
.
   ,        . 
  ,  value    ,   
     .
        value.    
 value        , , 
    ,    ,  
   .        
 ,  .    :

   union TokenValue {
      char _cval;
      int _ival;
      char *_sval;
      double _dval;
   };

          TokenValue  dval,  
TokenValue      double.   
 .       , 
  :

   //   TokenValue
   TokenValue last_token;
   //     TokenValue
   TokenValue *pt = new TokenValue;

      ,     ,   
 :

                                                         ++   637

   last_token._ival = 97;
   char ch = pt->_cval;

       ,   :

   union TokenValue {
   public:
      char _cval;
      // ...
   private:
      int priv;
   }

   int main() {
      TokenValue tp;
      tp._cval = '\n'; // 

      // : main()      
      // TokenValue::priv
      tp.priv = 1024;
   }

          ,  . 
    ,  ,   
 . :

   union illegal_members {
      Screen s; // :  
      Screen *ps; // 
      static int is; // :  
      int &rfi; // : -
   };

       -,   
:

                                                         ++   638

   union TokenValue {
   public:
      TokenValue(int ix) : _ival(ix) { }
      TokenValue(char ch) : _cval(ch) { }
      // ...
      int ival() { return _ival; }
      char cval() { return _cval; }
   private:
      int _ival;
      char _cval;
      // ...
   };

   int main() {
      TokenValue tp(10);
      int ix = tp.ival();
      //...
   }

    TokenValue:

   enum TokenKind ( ID, Constant /*     */ }
   class Token {
   public:
      TokenKind tok;
      TokenValue val;
   };

     Token   :

   int lex() {
      Token curToken;
      char *curString;
      int curIval;

      // ...
     case ID: // 
        curToken.tok = ID;
        curToken.val._sval = curString;
        break;
     case Constant: //  
         curToken.tok = Constant;
         curToken.val._ival = curIval;
         break;
         // ...  ..
   }

   ,    ,   ,  
     ,    . ,  
    _ival,     ,
  _sval. ,   ,     .

                                                           ++   639

        ,    ,
 ,   ,    
  .   Token      tok:

   char *idVal;
   //     ,    sval
   if ( curToken.tok == ID )
      idVal = curToken.val._sval;

      ,   ,    
      :

   #include <cassert>
   //      sval
   string Token::sval() {
   assert( tok==ID );
    return val._sval;
   }

        .     
       ,   .
,    Token  
,    :

   class Token {
   public:
      TokenKind tok;
       //    
       union {
          char _cval;
          int _ival;
          char *_sval;
          double _dval;
      } val;
   };

         ,    
 . , ,   Token,  
:

                                                         ++   640

   class Token {
   public:
      TokenKind tok;
      //  
      union {
         char _cval;
         int _ival;
         char *_sval;
         double _dval;
      };
   };

    -        
,    .   lex(), 
 :

   int lex() {
      Token curToken;
      char *curString;
      int curIval;

      // ... ,    
      // ...   curToken
      case ID:
         curToken.tok = ID;
         curToken._sval = curString;
         break;
      case Constant: //  
         curToken.tok = Constant;
         curToken._ival = curIval;
         break;

      // ...  ..
   }

         ,   
       Token.       
 ,   -.  ,  
  ,      
    static.

                 13.8.    ,  

             ,
  .      ,    
:

   class File {
      // ...
      unsigned int modified : 1; //  
   };

                                                           ++   641

        ,    
,   .  , modified      .
    ,     ,    
    ,     . , 
          
unsigned int,     mode:

   typedef unsigned int Bit;

   class File {
   public:
      Bit mode: 2;
      Bit modified: 1;
      Bit prot_owner: 3;
      Bit prot_group: 3;
      Bit prot_world: 3;
      // ...
   };

         ,     . , 
 ,    ,    
-    :

   void File::write()
   {
      modified = 1;
      // ...
   }

   void File::close()
   {
      if ( modified )
         // ...  
   }

           1 ( 
     4.11):

   enum { READ = 01, WRITE = 02 }; //   

   int main() {
      File myFile;

      myFile.mode |= READ;
      if ( myFile.mode & READ )
         cout << "myFile.mode is set to READ\n";
   }

        -   -
. ,   File    isRead()  isWrite():

                                                             ++   642

   inline int File::isRead() { return mode & READ; }
   inline int File::isWrite() { return mode & WRITE; }
   if ( myFile.isRead() ) /* ... */

      -      
 File.
           (&),     
   -.  ,    
.
      C++    bitset,  
   .     
 . (  bitset      
  4.12.)

                                    13.17

        ,    File   
      bitset  
.

                          13.9.    A

       .     
      .
          (  )  
   (::).    , 
          , 
          .
        ,
  ,     ,    
. (  17  18  ,      
  .)
           
  .         
 ,         .   
    .        
:

   class String {
   public:
      typedef int index_type;

      //   -     String::index_type
      char& operator[]( index_type )
   };

          :    , 
  . ,    operator[]() 

                                                                ++   643

  typedef index_type,    
operator[]()  ,      
 index_type:

   class String {
   public:
      // :  index_type  
      char &operator[]( index_type );
      typedef int index_type;
   };

         .   ,  
  -,   ,   
 .   .
        -    .
   (..      )
   ,      .  
     ,   ,  
   .    ,   
operator[]()     :

   class String {
   public:
      typedef int index_type;
      char &operator[]( index_type elem )
       { return _string[ elem ]; }
   private:
      char *_string;
   };

       ,    operator[](),
     index_type.     ,
      -,   index_type 
    operator[]().
    ,   _string      
operator[]().  ,  _string     operator[]()
 .    -   
      -.  
     ,     -
 ,     ,    
 .
          . ,  
- clear()     bkground, 
 :

                                                                ++   644

   class Screen {
   public:
      // bkground    ,
      //     
      Screen& clear( char = bkground );
   private:
      static const char bkground = '#';
   };

        -    
 ,    ,    
 .         
     ,    .  
        .  
  :

   class Screen {
   public:
      // ...
      // : bkground -  
      Screen& clear( char = bkground );
   private:
      const char bkground = '#';
   };

        bkground,  
 .
     ,    ,      
,      .    
   ,       
      .     
 ?
    ,      ,   ,
    ,     
     .   
operator[]()   String:

   class String {
   public:
      typedef int index_type;
      char& operator[]( index_type );
   private:
      char *_string;
   };

   //  operator[]()    index_type  _string
   inline char& operator[]( index_type elem )
   {
      return _string[ elem ];
   }

                                                            ++   645

    ,      typedef index_type 
   String::.,    
String::operator[]     ,    
.          ,
    -.
     -     . 
  ,        
,      . ,
    ,  
,    :

   class Account:
      // ...
   private:
      static double _interestRate;
      static double initInterestRate();
   };

   //   Account::initInterest()
   double Account::_interestRate = initInterest();

    _interestRate   -
Account::initInterest()   ,      
.
     ,   ,      
_interestRate     ,     
Account.      name    
  nameSize:

   class Account:
      // ...
   private:
      static const int nameSize = 16;
      static const char name[nameSize];
      // nameSize     Account
   const char Account::name[nameSize] = "Savins Account";

     nameSize     Account,  name 
 ,           
     ,    Account::name.
     ,    ,    
      .       
     . ,  
   typedef Money,    Account,  
Money   ,      
 :

                                                               ++   646

   class Account {
      typedef double Money;
      //...
   private:
      static Money _interestRate;
      static Money initInterest();
   };
   // Money      Account::
   Account::Money Account::_interestRate = initInterest();

         ,   
   .        
   ,         
. (       17  18.)

                 13.9.1.      

   , ,     ,    
 .         ,  
  .  ,     ,  
  ,      ,  
  .     ,   ,
    .
   ,     (  
 -    ),  
:

  1.    ,   
     .
  2.    1     ,    
         . ,   
            . (  
         8.5.)

:

   typedef double Money;
   class Account {
      // ...
   private:
      static Money _interestRate;
      static Money initInterest();
      // ...
   };

       Money     Account. 
    ,    
Money.    ,      
.   typedef Money ,    
   _interestRate  initInterest().

                                                          ++   647

   ,    - ,  
:

  1.        -
     . (        
      8.1.)
  2.   1    ,      
     .
  3.     ,    
        -.

,     -,  :

   int _height;

   class Screen {
   public:
      Screen( int _height ) {
         _height = 0; //    _height?  
      }
   private:
     short _height;
   };

       _height,     
Screen,        
 . ,      .
         ,      
  Screen,     ,   
  _height. ,    _height  
 ,       , 
          this:

   int _height;

   class Screen {
   public:
      Screen( long _height ) {
         this->_height = 0; //   Screen::_height
         //  :
         // Screen::_height = 0;
      }
   private:
      short _height;
   };

          ,   , 
         .  
      , 
    Screen.     
   _height. ,     

                                                             ++   648

  ,       , 
     :

   int _height;

   class Screen {
   public:
      Screen( long _height ) {
         ::_height = 0; //    
      }
   private:
      short _height;
   };

        ,     
      , 
    Screen,     -
:

   class Screen {
   public:
      // ...
      void setHeight( int );
   private:
      short _height;
   };

   int verify(int);

   void Screen::setHeight( int var ) {
     // var:   
     // _height:    
     // verify:    
     _height = verify( var );
   }

    ,     verify()  
  Screen.       
     ,    ,
   .
   ,      ,  
:

  1.     .
  2.   1    ,   ,  
            ,  
        .

                                     13.18

      ,      .

                                                          ++   649

                                13.19

      ,        
       (..  
  ,    ).

                                 13.20

        Type      Exersise 
  - setVal()? (,    
   .)      initVal 
   - setVal()?

   typedef int Type;
   Type initVal();

   class Exercise {
   public:
      // ...
      typedef double Type;
      Type setVal( Type );
      Type initVal();
   private:
      int val;
   };

   Type Exercise::setVal( Type parm ) {
      val = parm + initVal();
   }

    - setVal() .    , ?
  ,    Exercise  
typedef Type    initVal().

                            13.10.   A

   ,    ,  .   
 ,          public,
private  protected  .
           ,   
  .  ,        ,
    . :

                                                            ++   650

   class Node { /* ... */ }

   class Tree {
   public:
      // Node      Tree
      //    Tree::Node  ::Node
      class Node {...};

      // :     : Tree::Node
      Node *tree;
   };

   // Tree::Node     
   // Node      Node
   Node *pnode;

   class List {
   public:
      // Node      List
      //    List::Node  ::Node
      class Node {...};

   // :     : List::Node
   Node *list;
   };

          ,    :

   //  ,  
   class List {
   public:
      class ListItem {
         friend class List; //  
         ListItem( int val=0 ); // 
         ListItem *next; //    
         int value;
      };
      // ...
  private:
      ListItem *list;
      ListItem *at_end;
   };

     ,        
.          . 
   List       ListItem,
 ListItem  List  .       
       .   
  ListItem      List,   
 List       .   
   ,  ListItem      
List.
    ListItem    List ,   
      ,       
   . :

                                                           ++   651

   // :     
   List::ListItem *headptr;

        ,   .  ListItem
   List        .
     ListItem   List:

   //  ,  
   class List {
   public:
      // ...
   private:
      class ListItem {
         // ...
      };
      ListItem *list;
      ListItem *at_end;
   };

     ListItem         List,
    ListItem   .   
 List   ListItem  .   
 List:

   //  
   class List {
   public:
      // ...
   private:
      //  ListItem   
      class ListItem {
      //    
      public:
         ListItem( int val=0 );
         ListItem *next;
         int value;
      };
      ListItem *list;
      ListItem *at_end;
   };

    ListItem        ,
,     .   ?  
ListItem    List , ,       ;
        ,  
  .  -   
    ,        
 .
          ListItem.  
      :

                                                             ++   652
   class List {
   public:
      // ...
   private:
      class ListItem {
      public:
         ListItem( int val=0 );
         // ...
      };
   };
   // : ListItem   
   ListItem:: ListItem( int val ) { ... }

     ,   ListItem     . 
     ,  ListItem    
  List.      ListItem 
 .    :

   //      
   List::ListItem::ListItem( int val ) {
      value = val;
      next = 0;
   }

   ,      .  
List::          
ListItem.   ListItem    ,    .
     :

   // :   ListItem,   List::ListItem
   List::ListItem::List::ListItem( int val ) {
      value = val;
      next = 0;
   }

      ListItem    ,    
      .     
 :

   int List::ListItem::static_mem = 1024;

    ,  -   -   
     ,      
 .   ListItem     
.
         . , 
ListItem        :

                                                          ++   653

   class List {
   public:
      // ...
   private:
      //  
      class ListItem;
         ListItem *list;
         ListItem *at_end;
      };

      //       
      class List::ListItem {
      public:
          ListItem( int val=0 );
          ListItem *next;
           int value;
   };

        ListItem   
   List. ,   ListItem   List
 .         
 ,        
.           
.
         ,   
    .   list  at_end  List 
  ,  ListItem     ,  
  .       ,      List
    :

   class List {
   public:
      // ...
   private:
      //  
      class ListItem;
      ListItem *list;
      ListItem at_end; // :    ListItem
   };

         ? ,  
   ListItem,        
List.         ,
  List.  ,  ListItem  
   ,    List  .
        ,      . 
     ,    :

                                                              ++   654

   class List {
   public:
      // ...
   private:
      //  List::ListItem
      class ListItem;
      class Ref {
         // pli   List::ListItem*
         ListItem *pli;
      };
       List::ListItem
     class ListItem {
         // pref   List::Ref*
         Ref *pref;
      };
   };

     ListItem       Ref,   
pli   .
            ,
   .       ,
    . :

   class List {
   public:
      int init( int );
   private:
       class List::ListItem {
       public:
         ListItem( int val=0 );
         void mf( const List & );
         int value;
      };
   };

   List::ListItem::ListItem { int val )
   {
      // List::init() -    List
      //         List
      value = init( val ); // :   init
   };

           
 ,    .  -
 ListItem  this      . 
 this  ,   value   ,   
.   ListItem  this   ListItem*. 
   - init()    List    List*.
    - mf()   init()   -.
 , init()   ,    :

                                                             ++   655

   void List::ListItem::mf( List &i1 ) {
       memb = i1.init(); // :   init()  
   }

            , 
 ,    ,     
     (, ,   ). 
     typedef,   ,   . :

   class List {
   public:
      typedef int (*pFunc)();
      enum ListStatus { Good, Empty, Corrupted };
      //...
   private:
      class ListItem {
      public:
        void check_status();
        ListStatus status; // 
        pFunc action; // 
        // ...
      };
      // ...
   };

   pFunc, ListStatus  ListItem         
  List.  ,      ListStatus 
     ListItem   :

   void List::ListItem::check_status()
   {
      ListStatus s = status;
      switch ( s ) {
         case Empty: ...
         case Corrupted: ...
         case Good: ...
      }
   }

      ListItem  List     , 
        
 :

   List::pFunc myAction; // 
   List::ListStatus stat = List::Empty; // 

          :

   List::ListStatus::Empty

                                                              ++   656

       ,   
 . ?    ,    ,   
.

              13.10.1.      
                                    

   ,          .
,      ( , 
   -    )
  :

  1.    ,   
     .
  2.   1    ,     
     ,    .
  3.    ,   ,  
            .

:

   enum ListStatus { Good, Empty, Corrupted };
   class List {
   public:
      // ...
   private:
      class ListItem {
      public:
         //  :
         // 1) List::ListItem
         // 2) List
         // 3)   
         ListStatus status; //    
         // ...
      };
      // ...
   };

       ListStatus    
ListItem.    ,      List,  
 .         ,
  ListStatus.     
  ListStatus     ,  
 status.
      ListItem     ,  
  List,    List   :


                                                             ++   657
   class List {
   private:
      class ListItem {
      //...
      public:
         enum ListStatus { Good, Empty, Corrupted };
         // ...
      };

   class List::ListItem {
   public:
      //  :
      // 1) List::ListItem
      // 2) List
      // 3)   
      ListStatus status; //    
      // ...
   };

      ListStatus     
ListItem.    ,      List. 
    List  ,    
.   ListStatus     ,  
   ListItem.  , status  
      List.    List     
,          
,     ListItem.
   ,    -  , 
 :

  1.      -.
  2.   1    ,     
      .
  3.     ,      
     .
  4.    ,   ,   
          -.

        list   -
check_status()    :

                                                         ++   658
   class List {
   public:
      enum ListStatus { Good, Empty, Corrupted };
      // ...
   private:
      class ListItem {
      public:
         void check_status();
         ListStatus status; // 
         //...
      };
      ListItem *list;
   };

   int list = 0;
   void List::ListItem::check_status()
   {
      int value = list; //  list?
   }

    ,    list  check_status() 
    :

    value,    list   int.  List::list 
           value    ;
   ListItem         , 
     list;
   list    ,      -
    ListItem    ,   .

   ,    ,  list,   -
check_status(),     list  List. ,  
       ListItem,   
   ,   .  list  List 
 .      list  check_status()
,     .
           ,  
.     ,      
  ,       ,  
.      list   
  :

   void List::ListItem::check_status()
   {
      int value = ::list; // 
   }

     - check_status()       
ListItem,           -
,   list      :

                                                               ++   659
   class List {
   public:
      // ...
   private:
      class ListItem {
      public:
         // :     ::list
        void check_status() { int value = ::lis; }
        //...
      };
      ListItem *list;
      // ...
   };

   int list = 0;

     list     List.  
-,    ,    
,       .  
 check_status()    List,  
 ,   ,    
  list.

                                    13.21

     11    ,   iStack.  ,
   pushOnFull  popOnEmpty   
iStack.      iStack  
-,    main().

                       13.11.      A

            
 .        
.  ,   ,    
  , ..     ,  
  . :

   namespace cplusplus_primer {
      class Node { /* ... */ };
   }

   namespace DisneyFeatureAnimation {
      class Node { /* ... */ };
   }
   Node *pnode; // : Node      

   // :  nodeObj  
   //   DisneyFeatureAnimation::Node
   DisneyFeatureAnimation::Node nodeObj;

   // using-  Node     
   using cplusplus_primer::Node;
   Node another; // cplusplus_primer::Node

                                                              ++   660

         ,   (-, 
   )      .   
        
 ,     ,    
?       ,   
  ,       .  
     :

   // --- primer.h ---
   namespace cplusplus_primer {
      class List {
        // ...
      private:
         class ListItem {
         public:
            void check_status();
            int action();
            // ...
         };
     };
   }

   // --- primer.C ---
   #include "primer.h"

   namespace cplusplus_primer {
      // : check_status()      ,
      //   List
      void List::ListItem::check_status() { }
   }

   // : action()     
   //   ,    List
   //     
   int cplusplus_primer::List::ListItem::action() { }

      ListItem     
cplusplus_primer,    List,    ,
  cplusplus_primer.       
       
 ,     .
      ,   
  ? ,    someVal:

   int cplusplus_primer::List::ListItem::action() {
   int local = someVal;
   // ...
   }

          -,
      ListItem,     
List.       ,     ,
   13.10.     
cplusplus_primer       ,  

                                                            ++   661

   ,     -
action():

   // --- primer.h ---
   namespace cplusplus_primer {
      class List {
         // ...
       private:
          class ListItem {
          public:
             int action();
             // ...
         };
      };
      const int someVal = 365;
   }

   // --- primer.C ---
   #include "primer.h"

   namespace cplusplus_primer {
      int List::ListItem::action() {
         // : cplusplus_primer::someVal
         int local = someVal;
         // : calc()   
         double result = calc( local );
         // ...
     }

      double calc(int) { }
      // ...
   }

      cplusplus_primer   .
  List   someVal     , 
    primer.h.   calc()  
  ,     primer.C.
 calc()  action() ,     
.  calc()    cplusplus_primer,   
     ,     :

   // --- primer.h ---
   namespace cplusplus_primer {
      class List {
         // ...
      }
      const int someVal = 365;
      double calc(int);
  }

     calc()    action()     
 ,      action(),    
    action().

                                                           ++   662

            
,       : ,
  ,   ,   
  .
     ,       
    ,    . ,
   ,    .
,  action()     :

   cplusplus_primer::List::ListItem::action()

    cplusplus_primer::List::ListItem::   , 
,         
.      ListItem,    
 List     cplusplus_primer,   ,
    action().       
    ,       
 ,     .
   ,      ,   
 .    primer.h    
,   cplusplus_primer::List        .
   ,         .
         , 
       .    
   ,  ,     
 ,  primer.h.       
,      .  
  ,       .
    -   -     
   .         
 .       ,   
   primer.C.

                                13.22

     iStack,    13.21,  
 pushOnFull  popOnEmpty     LibException:

   namespace LibException {
      class pushOnFull{ };
      class popOnEmpty{ };
   }

  iStack     Container.  
      -,    main().

                                                                ++   663

                          13.12.   A

   ,    ,  .    
  ,  .   , 
    ,    ,   
,   .  -  
     .    
    ;   ,   
 .
           
 ,        .
   ,   ,      
,      ,   . 
        
 .       :

   void foo( int val )
   {
      class Bar {
      public:
         int barVal;
         class nested; //    
         };
         //   
         class Bar::nexted {
         // ...
      };
   }

             
 . ,   ,     .
       , 
 ,      ,  .
       . 
     :     
,          .
     ,    ,      
 .       , 
   ,    
. :

                                                            ++   664

   int a, val;

   void foo( int val )
   {
      static int si;
      enum Loc { a = 1024, b };
      class Bar {
      public:
         Loc locVal; // 
         int barVal;
         void fooBar ( Loc l = a ) { // : Loc::a
            barVal = val; // :  
            barVal = ::val; // :  
            barVal = si; // :   
            locVal = b; // :  
         }
      };
      // ...
   }

             
  ,    . 
 ,     -,  
  ,      ,
    ,     ,   
 ,     .   
  val  fooBar()   ,   val
  ,         
 .
