84

                      3.   ++

                         3.1. 

        ,  1,    
,   . ,     
  ,  ,       
.    :   , ,
   ,      .  
  . , 0   int, 3.14159   double.
     (,  )   escape-
 .     ( 
    ):
   escape-     \ooo,  ooo     
 .     .  ASCII-,  
  :

   \7 () \14 ( ) \0 (null) \062 ('2')

        L (, L'a'),  
  wchar_t    ,   
   ,      
  char, , ,    .
          ,  
    ++      
  0 (\0).

    'A'    ,    ""    
: ''  \0 ( ).

              ( char
 wchar_t),      . ,  

   "two" "some"

      twosome    .
         .  :

   "two" L"some"

  -      ,   
    .

                            3.2. 

   ,    .       ,
  .      
 ,  ,  :     
 ,    .      
    .

                          3.2.1.   

   ,   ,         
 .       .  
  :

     ,  r- ( read value    ),
              ,  
     ;
       ,   ,  l-
      ( location value   )  ,   r-
     ;   .

   

   ch = ch - '0';

 ch         . 
    (ch    '0'):  
      .   
:   ,    ch,  
.          l-
.      :

   salary + salary * 0.10 = new_salary;
   //  :     l-
   // :  -  l-
   0 = 1;
   // :   -  l-

   ++ ,         .  
       
 .     module1.C   , 
 fileName    .    ,  
      fileName.   
   :

   //  module1.C
   //   fileName
   // fileName ,    
   //        
   extern string fileName;

91

   ifstream input_file( fileName )

      ,     , 
 ,  -  .     
 . (  extern    8.2.)
              , 
     .     
 ,     ,   .   
           
  . (        
8.2.)

                        3.2.2.  

         ,   
    :

         ,  index (
     : Index    ,  INDEX  ,   
       #define);

92

       - ,    
     , : birth_date  salary;

                      3.2.3.  

          .  
  ,  ++ ,   
  .      
 (   new),     ,  
     .
         ,  
    . (       2.3. 
       ,   3.11  3.15,
     string  complex   .)

   int ival = 1024;
   int ival( 1024 );

   string project = "Fantasia 2000";
   string project( "Fantasia 2000" );

   double salary = 9999.99, wage = salary + 0.01;

            :

   int ival = int();
   double dval = double();

   vector< int > ivec( 10 );

          int().
(     vector   2.8.     .  
3.10   6.)

                                     3.3. 

             2.2.
   ,       
  .      
  ,     , ,
     ,      
         .

   5           :   
   (.  7.9).

     (*)       
     .   
    :

   string *ps;
   string* ps;

        :   
 ,          :

   //: ps2    !
   string* ps, ps2;

   int ival = 1024;

   //pi   
   int *pi = 0;

   // pi2   ival
   int *pi2 = &ival;

   // : pi  pi2   ival
   pi = pi2;

   // pi2   
   pi2 = 0;

        ,   :
        ,  
  .
      ,   pi      dval  
        .    
 ,       
   .
   ,  ,      ,   , 
   (,       - ). 
      void,  
    ,     :

   // : void*  
   //   
   void *pv = pi;
   pv = pd;

    ,    void*, ,     
 .
(       void  4.14.)


        ,   ,   
,   ,   (*). 
  :

   int ival = 1024, ival2 = 2048;

   int *pi = &ival;  

      ival,    
 pi:

    //    ival  ival2
      
    *pi = ival2;

   //    ival  rvalue  lvalue
   *pi = abs(*pi); // ival = abs(ival);

   *pi = *pi + 1; // ival = ival + 1; 


         (&)    int,  
  int*

   int *pi = &ival;

           int* (  int),  
    int, .. int**. 
   int**    ,      int. 
    ppi,     int*,   ival.   
  ival,    ppi   .

   int **ppi = &pi;

   int *pi2 = *ppi;

   cout << " ival\n"
        << " : " << ival << "\n"
        << " : " << *pi << "\n"
        << "  : " << **ppi << "\n"
        << endl;

         .  
  ,       :

   int i, j, k;
   int *pi = &i;

   // i = i + 2
   *pi = *pi + 2;

   //  ,   pi,  2
   pi = pi + 2;

        ,     .
   1        
,    .   char  1 ,
int 4  double 8,   2    char, int  double  
   2, 8  16.    ?  
       ,     1
  ,       .  
        ;  
     .
            
    :

   int ia[10];

99

   int *iter = &ia[0];
   int *iter_end = &ia[10];
   while (iter != iter_end) {
      do_something_with_value (*iter);
      ++iter;
   }

                           3.11

            
 -   ++     
  .   ,      ?
        ,   
   ?

                          3.4.  
    ++       ,   ,  
string    ++.

                       3.4.1.   

         ++.    
   :

   #include <cstring>

     char,       ,  
   .      ,


   const char *st = "  \n";

          st  
 .     ,   ?
          . 
    ,     1, 
    . :

   while (*st++ ) { ... }

st ,      . 
     , , ,  ,
      0.   ++  1 
 st        .
        ,   . ,
,       (    ),
     :

   int string_length( const char *st )
   {
      int cnt = 0;
      if ( st )
         while ( *st++ )
           ++cnt;
      return cnt;
  }

           :   
    (      )   
,      (   ,   
  ).

   // pc1     
   char *pc1 = 0;

   // pc2   
   const char *pc2 = "";

          
 -        
 .      ,
 .  :   .   .
 .

   #include <iostream>

   const char *st = "  \n";

   int main() {
      int len = 0;
      while ( st++ ) ++len;

      cout << len << ": " << st;
      return 0;
   }

       st  . ,   0
  ,    st,   .  
     ( ),      
,     .


102

         .  
,    .       ?

   int main()
   {
      int len = 0;
      while ( *st++ ) ++len;

      cout << len << ": " << st << endl;
      return 0;
   }

      ,      st   
 ,  ,       
.       ,    
  .
       :

   st = st  len;
   cout << len << ": " << st;

       - ,    .   :

   18:   

     ,         
. st        1. , , 
:

   st = st  len - 1;

     :

   18:   

     ,     . 

   st = st  len - 1;

  ,   ,    
 ,     st.  
    ,     .  
     ,     
.       .  

103

        ,
  st:

   const char *p = st;

    p      ,   st
:

   while ( *p++ )

102

                               3.4.2.  string

    string   ++     (
 ,      6).
      ,   ,  
       string.  
     string:

 , ,  .

   char *str = s1.c_str(); //  
   const char *str = s1.c_str(); // 

        string,    ,  
    . , ,  ,  
  :

   string str( "fa.disney.com" );
   int size = str.size();
   for ( int ix = 0; ix < size; ++ix )
      if ( str[ ix ] == '.' )
         str[ ix ] = '_';

     ,         . 
,      -  replace():

   replace( str.begin(), str.end(), '.', '_' );

replace()     ,      
2.8        12.

107

                              3.5.  const

     512 .

   int bufSize = 512; //   
   // ...
   for ( int index = 0; index < bufSize; ++index )
      // ...

      :  bufSize   l-,
     ,     
.         
(=)   (==):
     const   .
           :

        .      
?       ?

   double *ptr = &minWage;

        ?  minWage 
,    .   ,    
:

   *ptr += 1.40; //   minWage!

    ,          
        .  
     . 
           .
    ,       ? .  
 ,    const:

   const double *cptr;

 cptr      const double.    ,  
   ,  ,     . :

   const double *pc = 0;
   const double minWage = 9.60;

   // :    minWage   pc
   pc = &minWage;

   double dval = 3.14;
   // :    minWage   pc
   //  dval   
   pc = &dval; // 

   dval = 3.14159; //

   *pc = 3.14159; // 

          .   ,
        :

   pc = &dval;

            
 .  dval       , 
    dval  pc. (- ,    
 ,        
  .)
            
  .    ,  ,
      ,    
. :

   int strcmp( const char *str1, const char *str2 );

   (         7,    
.)
      . (    
     !).   
  ,   . :

   int errNumb = 0;
   int *const currErr = &errNumb;

    curErr      .  ,   
     ,     .
       curErr:

   do_something();

      errorHandler();
      *curErr = 0; // :   errNumb

   if ( *curErr ) {
   }

          :

   curErr = &myErNumb; // 

       
.

   const double pi = 3.14159;
   const double *const pi_ptr = &pi;

     ,    pi_ptr,     
    .

111

                                3.6.  

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

   int ival = 1024;

   // : refVal   int,   int*
   int &refVal = &ival;

   int *pi = &ival;
   // : ptrVal -   
   int *&ptrVal2 = pi;

    ,       ,    
 (        
).
            .
        refVal,
    ival  ,   refVal.

   int min_val = 0;

   // ival   min_val,
   //   refVal    min_val
   refVal = min_val;

          . -, 
       . -,
     ,   ,    .

     ++       ,
       . :

   //   

   //     next_value
   bool get_next_value( int &next_value );

   //  
   Matrix operator+( const Matrix&, const Matrix& );
 
      -  -?   :

   int ival;
   while (get_next_value( ival )) ...

      :

   int &next_value = ival;

(       
   7.)

115

                                  3.7.  bool

    bool      ,       signed,
unsigned, short  long,    :

   short bool found = false;

    bool     int.  true   1, 
false   0. :

              
  bool.   0   false,     true:

116

                                  3.8. 

     ,    ,    
,  ,    open_file()   1, 2
 3.
        .   :

   enum open_modes{ input = 1, output, append };

         0,   1   .    
    .
        .
               , 
     :

   void open_file( string file_name, open_modes om );

        ,     
 (     ),   .
        ,    
,     :

   open_file( "Jonah", 1 );

    ,     :

   //  
   for ( open_modes iter = input; iter != append; ++inter )

    ,     ,  
. :

   // point2d == 2, point2w == 3, point3d == 3, point3w == 4
   enum Points { point2d=2, point2w, point3d=3, point3w=4 };

            
 :

   void mumble() {

       Points pt3d = point3d; // : pt2d == 3
     
       // : pt3w   int
       Points pt3w = 3;

       // : polygon     Points
       pt3w = polygon; 

       // :    Points
       pt3w = pt3d;
   }

          
   int. :

119

                                3.9.  

   extern int get_size();

   // buf_size  max_files 
   const int buf_size = 512, max_files = 20;

   int staff_size = 27;

   // : 
   char input_buffer[ buf_size ];

   // :  : 20 - 3
   char *fileTable[ max_files-3 ];

   // :  
   double salaries[ staff_size ];

   // :   
   int test_scores[ get_size() ];

        ,      :

   const int array_size = 3;
   int ia[ array_size ] = { 0, 1, 2 };

   //   3
   int ia[] = { 0, 1, 2 };

       ,   ,   .
           
   ,    .

   const char cal[] = {'C', '+', '+' };
   const char cal2[] = "C++";

     ca1  3,  ca2  4 (   
  ).     :

   // :  "Daniel"   7 
   const char ch3[ 6 ] = "Daniel";

    ,     . 

   const int array_size = 3;
   int ix, jx, kx;

   // :    int*
   int *iar [] = { &ix, &jx, &kx };

   // error:   
   int &iar[] = { ix, jx, kx };


123
                         3.9.1.  

        :

   int ia[ 4 ][ 3 ] = {
      { 0, 1, 2 },
      { 3, 4, 5 },
      { 6, 7, 8 },
      { 9, 10, 11 }
   };

     ,     ,  
,  ,    .   
    ,   :

   int ia[4][3] = { 0,1,2,3,4,5,6,7,8,9,10,11 };

          .  
   :

   int ia[ 4 ][ 3 ] = { {0}, {3}, {6}, {9} };

        ,    .
           , 
    0.

   int ia[ 4 ][ 3 ] = { 0, 3, 6, 9 };

   

      ia[ 1, 2 ]

      ++,     , 
  .       1  2.
         , 
    2 (.     4.2). 
 ia[1,2]  ia[2].      .


124

                      3.9.2.    

           
 :

   ia;
   &ia[0]


           :

   //     
   *ia;
   ia[0]

        ,   :

   &ia[1];

       , 

   ia+1;

     . ,    
  :

   ia[1];
   *(ia+1);

      :

   *ia+1

   

   *(ia+1);

        ,    (
     4.13).    
  ia     ,   
  1.       .
          ,     
 ,    . :

   #include <iostream>

   int main()
   {
      int ia[9] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 };
      int *pbegin = ia;
      int *pend = ia + 9;

      while ( pbegin != pend ) {
         cout << *pbegin <<;
         ++pbegin;
      }
   }


127

                               3.10.  vector

        ,  :

   vector< int > ivec( 10, -1 );

         -1.

         :

   int ia[ 6 ] = { -2, -1, , 1, 2, 1024 };

      vector   .   
       :

   vector< int > ivec( ia, ia+6 );
   // 6  ia   ivec

            ,   
 :

   //  3 : ia[2], ia[3], ia[4]
   vector< int > ivec( &ia[ 2 ], &ia[ 5 ] );

            
    vector    
   . :

   vector< string > svec;

   void init_and_assign()
   {
      //    
      vector< string > user_names( svec );
      // ...

     //     
     svec = user_names;
   }


       ++  vector, string  ,  
        STL.

      STL,       
.       ,    :

   vector< string > text;

           . , 
push_back()    .

   string word;
   while ( cin >> word ) {
      text.push_back( word );
      // ...


             :

   cout << " : \n";
   for ( int ix =0; ix < text.size(); ++ix )
      cout << text[ ix ] << ' ';

        :

   cout << " : \n";
   for ( vector<string>::iterator it = text.begin(); it != text.end(); ++it )
      cout << *it << ' ';

        .    STL    
:

   vector<int> ivec;

  :

   ivec[0] = 1024;

           ivec;    
  size().
  (          6.)

130

                                     3.11.  complex

131

                                     3.12.  typedef

      ,     typedef? 
    ,      
.  ,       
,       (.    3.14), 
     -  (.  13.6).
      ,       . 
   typedef    .
    :

   typedef char *cstring;

      cstr   :

   extern const cstring cstr;

   ,   :

   const char *cstr

     .  const   cstr,    
   char:

   char *const cstr;

133

                           3.13.  volatile

      volatile (,  ),  
      ,  ,
   .    , 
         .
    volatile   ,

                                   3.14.  pair

          .    
   :

   #inc1ude <uti1ity>

   ,  

   pair< string, string > author( "James", "Joyce" );

  author  pair,     .
            first  second:

   if ( Joyce.first == "James" && Joyce.second == "Joyce" )

   (     pair        6 
     12.)

                                    3.15.  

    

   String();

   ,        . 
  :

   String str1;

 str1   .
     , String(const String&),  ,   
    .
        ,   

138

  .    String    
      .

   String namel( "Yadie" );
   String name2( "Yodie" );

   // bool operator==(const String&)
   if ( namel == name2 )
      return;
   else
      // String& operator=( const String& )
      namel = name2;

       ,    .
       ,    
,       (inline). 
     ,      
. (     7.6.) -,
  ,    .    
 ,    ,     inline:
          
 this.
     :

   String namel( "orville" ), name2( "wilbur" );
   namel = "Orville Wright";

this  ,   name1    
.
   this     ,     . 

   obj[ 1024 ];
   ptr->size();

  size()  this  ,   ptr.  
  this   obj.  this ( *this),
   . ( this     13.4.)
           :
 ,        ,  
 .    ,     
this:   

          ,    . (
      15.2.   20.4  20.5  
      iostream.)
           String. 
 operator<<   -,       
 _string.     :  operator<<
  String,    friend (
    15.2),    (inline)
     .       : c_str()
     .   
  :

   inline ostream& operator<<( ostream& os, const String &s )
   {
      return os << s.c_str();
   }

      ,   String.   
        ,     "the"
 "it"    .

   #include <iostream>
   #inc1ude "String.h"

   int main() {
      int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,
      theCnt = 0, itCnt = 0, wdCnt = 0, notVowel = 0;

      //  "The"  "It"
      //     operator==( const char* )
      String but, the( "the" ), it( "it" );

      // operator>>( ostream&, String& )
      while ( cin >> buf ) {
         ++wdCnt;

         // operator<<( ostream&, const String& )
         cout << buf << ' ';

         if ( wdCnt % 12 == 0 )
            cout << endl;

         // String::operator==( const String& ) and
         // String::operator==( const char* );
         if ( buf == the | | buf == "The" )
           ++theCnt;
         else
         if ( buf == it || buf == "It" )
            ++itCnt;

         // invokes String::s-ize()
         for ( int ix =0; ix < buf.sizeO; ++ix )
         {
            // invokes String:: operator [] (int)
            switch( buf[ ix ] )
            {
               case 'a': case 'A': ++aCnt; break;
               case 'e': case 'E': ++eCnt; break;
               case 'i': case 'I': ++iCnt; break;
               case 'o': case '0': ++oCnt; break;
               case 'u': case 'U': ++uCnt; break;
               default: ++notVowe1; break;
            }
         }
      }

      // operator<<( ostream&, const String& )
      cout << "\n\n"
           << ": " << wdCnt << "\n\n"
           << "the/The: " << theCnt << '\n'
           << "it/It: " << itCnt << "\n\n"
           << ": " < <notVowel << "\n\n"
           << "a: " << aCnt << '\n'
           << "e: " << eCnt << '\n'
           << "i: " << ICnt << '\n'
           << "o: " << oCnt << '\n'
           << "u: " << uCnt << endl;
   }


    :      ,  
    (        6).  
 :

   Alice Emma has long flowing red hair. Her Daddy says when the
   wind blows through her hair, it looks almost alive, 1ike a fiery
   bird in flight. A beautiful fiery bird, he tells her, magical but
   untamed. "Daddy, shush, there is no such thing," she tells him, at
   the same time wanting him to tell her more. Shyly, she asks,
   "I mean, Daddy, is there?"

   : 65
   the/The: 2
   it/It: 1
   : 190
   a: 22
   e: 30
   i: 24
   : 10

                                   3.29

       (+) ,      
     String.   :

   class String {
   public:
      // ...
      String operator+( const String &rhs ) const;
      // ...
   };

146