
                                     III

                      - 

     II      ++:   
(int  double),   (string  vector)  ,   
 .   III  ,      ,
   .

       ++   ,
    III.     .

    ,   ,   .

      , ,   
 ,      
.

   ,       
    .

                                       7. 
                                     7.1. 

    (    17)    
 ,    ,   
.  ,      .  
 ,   .

17  ,   ,    
    !  . .

                                   7.2.  

                         7.2.1.    

      ++    ,   
  int.  ++   .

                             7.2.2.   
                       7.2.3.    

   ++    .    
     .     
   ,    
.         ,
    .     
  ,      :   
     .
             
    .      
.

                               7.3.  

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

        .   
             
        ;
           .

   , swap()     ,  
   :
       .    
.
        .

                           7.3.1. -
   
    -,   l-   
  .
       - ? -, , 
          (.
   swap()). -,    
  . -,      .
    -,  
    ,    ,   
.
       .
       -,   
  ,          
  .  -   
 ,        .   
       
 .

333

         xx  foo().
   foo_bar()     ,  
,   foo_bar()    . 
  :

   class X;
   extern int foo_bar( X& );

   int foo( const X& xx ) {
      // :  
      //     
      return foo_bar( xx );
   }

       ,     
foo_bar().      :

   extern int foo_bar( X ); //   
   extern int foo_bar( const X& );

        xx,   :

   int foo( const X &xx ) {
      // ...
      X x2 = xx; //   
        
      // foo_bar()   x2,
      // xx  
      return foo_bar( x2 ); // 
   }

   -      .  ,
      ,   
   ,   ,   .  
,       :

   void ptrswap( int *&vl, int *&v2 ) { // int * & vl, int * & v2 ) {
      int *trnp = v2;
      v2 = vl;
      vl = tmp;
   }

   

   int *&v1;

335

   : v1        int.

                   7.3.2. -  -

       -,    -? 
 ,        , 
      .
        3.6,      
 ,     .
      ,       -
,      ,     :

   -     ,      
. :

   class Type { };
   void operate( const Type& p1, const Type& p2 );

   int main() {
      Type obj1;
      //  objl  

      // :      0
      Type obj2 = operate( objl, 0 );
   }

              
   (    ),   
.
        -   
 .       
 .
    ,        , 
  .    operator+():

   //   -
   operator+( Matrix *ml, Matrix *m2 )
   {
      Matrix result;
      //  
      return result;
   }

   ,    ,      
     .    -
     .   
    :

   &a + &b; // ,   

          ,  -    
.      :

   // &a + &b    Matrix
   // : ,  ...
   //     
   &a + &b + &c;


        ,      :

   &( &a + &b ) + &c;

    ,  -    .  ,
-    ,  . 
       , 

      l-,   . 

  .       Matrix   
 ,     .    
   Matrix:

   //   -
   operator+( const Matrix &m1, const Matrix &m2 )
   {
      Matrix result;
      //  
      return result;
   }

       ++   ,    :
     .

      ,  grow()  :

351


   Matrix& grow( Matrix* p ) 
   {
      Matrix *res;
      //     Matrix
      //  
      // res    
      //   *p  *res
      return *res;
   }


                               7.3.3. -

     ++     ,       ,
 , . , 

   void putValues( int[ 10 ] );

  ,     

   void putValues( int* );

        .    
:

   //    putValues()
   void putValues( int[ 10 ] );
   void putValues( int* );
   void putValues( int[] );

         :

          
    ,     .    ,
          . 
        ,      
    ,    :

   void putValues( const int[ 10 ] );

         .    
       .     
    .  :

   void putValues( int[ 10 ] ); //   int*

   int main() {
       int i, j [ 2 ];
       putValues( &i ); // : &i is int*;
       //     
       putValues( j ); // : j -  0-  - int*;
      //     

         ,    
  int*   .    , 
   ,  .
      C-   ,  
  .         
    .      ,
   0.      
 .
        -    
.       ,    
   .

   //  -     10 
   void putValues( int (&arr)[10] );

   int main() {
      int i, j [ 2 ];

      putValues(i); // :
      //      10 
      putValues(j); // :
      //      10 
      return 0;
   }

             
  . (      6.  
     .)
          putValues() ,   
    :

   template <class Type>
   void putValues( Type *ia, int sz )
   {
      //  ,   
   }

       .     
    ,  . :

   putValues( int matrix[][10], int rowSize );
 
    matrix    ,     
  .    matrix :

   int (*matrix)[10]

   ,    *matrix  -   
  . 

   int *matrix[10];

 matrix       int.
           .   
 matrix         int.    
,        . 
   ,    , 
.

        7.3.4.      

          
,   . , vector<int>  
.     ,   - 
        .
 vector<int>    . , 
   putValues()  :


   const lineLength =12; //    

   void putValues( vector<int> vec )
   {
       cout << "( " << vec.size() << " )< ";
       for ( int i = 0; i < vec.size(); ++1 ) {
         if ( i % lineLength == 0 && i )
            cout << "\n\t"; //  
         cout << vec[ i ];
         // ,    ,
         //  
         if ( 1 % lineLength != lineLength-1 && i != vec.size()-1 )
            cout << ", ";
       }
       cout << " >\n";
   }

   ,   putValues()  .   
          .
    ,   
  .

   ,        , 
,       :

   void putValues( const vector<int> & ) { ...

          
                   7.3.5.    

   ,       , 
 ,      ,   
 :

   char *screenInit( int height = 24, int width = 80, char background = ' ' );

          (
 ),        
    .   
    background,     height  width.

   //  screenInit('?',80,' ')
   cursor = screenInit('?');

   // ,  screenInit(24,80,'?')
   cursor = screenInit( , ,'?');

              .
         ,   
.

   // : width     ,
   //     height
   char *screenlnit( int height = 24, int width, char background = ' ' );

            .  
:

   // tf.h
   int ff( int = 0 );

   // ft.
   #include "ff.h"
   int ff( int i = 0) { ... } // 

         ,   
   ( ),     .
     ,       
   ,   .
            
 .
         :

   file int ff( int a, int b, int  = 0 ); // ff.h

   ,        b?
  ,       :

345

   int ff( int a, int b = 0, int  = 0 ); // 

      :

   int ff( int a, int b = 0, int  ); // 

  ,     ff(),  b  
,     .    
    .     ff()  :

   int ff( int a = 0, int b, int  ); // 

          ,  
:

   int aDefault();
   int bDefault( int );
   int cDefault( double = 7.8 );

   int glob;

   int ff( int a = aDefault() , int b = bDefau1t( glob ) , int  = cDefault() );

       ,       .
   cDefault()   ,    
ff()    .

                              7.3.6. 

            . 
      (...),  
  .    ,   
       .
       :

   void foo( ... );
   void foo( parm_list, ... );

346

         .    
    ,    
  .      .
         printf()
  .     C-:

   int printf( const char* ... );

    ,     printf()     
 const char*.   ,  , ,
     .     
,    %,     .
, 

   printf( "hello, world\n" );

   . 

   printf( "hello, %s\n", userName );

  .  %     ,   s,
  ,         .
          
       .
,     .
   ,    :

   void f();
   void f( ... );

      f()     ,     
   . 

   f( someValue );
   f( cnt, a, b,  );

    . 

   f();

     .

                               7.4.  

         return.   
.      ,    
.  return     :

   return;
   return expression;

    ,     
 void.  return    ,  
  . (  return  
break,    5.8.)
    ,    void     , 
   return,    .

      ,  grow()  :

351


   Matrix& grow( Matrix* p ) 
   {
      Matrix *res;
      //     Matrix
      //  
      // res    
      //   *p  *res
      return *res;
   }


       ,     
 :

        ,    
       . (     
        8.3.)       
     ,   . :

   // :     
   Matrix& add( Matrix &m1, Matrix &m2 )
   {
      Matrix result:
      if ( m1.isZero() )
         return m2;
      if ( m2.isZero() )
        return m1;
      //    
      // :     
      //  
      return result; ????????????   
   } 

      l-.     
     . :

   #include <vector>

   int &get_val( vector<int> &vi, int ix ) { return vi [ix]; }

   int ai[4] = { 0, 1, 2, 3 };
   vector<int> vec( ai, ai+4 ); //  4  ai  vec

   int main() {
      //  vec[0]  1
      get_val( vec, 0 )++;
      // ...
   }

         
    const:

   const int &get_val( ... )

    ,  l-  ,  
  ,     
   IntArray   2.3.

351

   7.4.1.        

    glob  . (  8    
    .)    
           
,         .

      ,    
     (  ,     .  
         7.5.);

         .    
,    ,   
      
      ,  
  ,   ,  , 
.

                                    7.5. 

   ,    (percolates) ,      , 
 rgcd()   .
       ,   
() .        . , 
,    .

   unsigned long factorial( int val ) 
   {
      if ( val > 1 )
         return val * factorial( val-1 );
      return 1;
   }

                                 7.6.  

   , ,   inline     .
   ,      
 . ,   (,  rgcd())    
    (      ).  
1200      .     
   , ,   . 
            
  . ,   - size() 
 IntArray   2.3.
         compute.C  draw.C,
        min().   
,   : ,    
   ,       .

                      7.7.   extern "C" A

   ,        
   .
   ,      ,    
      :

   //      
   extern "C" void exit(int);

   //      
   extern "C" {
      int printf( const char* ... );
      int scanf( const char* ... );
   }

   //      
   extern "C" {
      #include <cmath>
   }

      ,    ,    ,  
    .
          .     main

           extern "C"  
 .    ,    
++.         . ,
extern "Ada"  ,    Ada; extern "FORTRAN"  
FORTRAN  ..         extern 
++.   8.2  ,         
  .

        7.8.  main():    

          7.8.1.      

18     CommandOpt    Web-
 Addison-Wesley.

                         7.9.   

         sort(),   , 
    ,    .
   ,       ,  
     compare()   (  
   6.10).

                         7.9.1.    

                      7.9.2.   

              
,  lexicoCompare  &lexicoCompare.   
  :

   int (*pfi)( const string &, const string & ) = lexicoCompare;
   int (*pfi2)( const string &, const string & ) = &lexicoCompare;

      :

   pfi2 = pfi;
   pfi = lexicoCompare;

        ,     
,   ,      
,        ,
      .    
   .     
    .

   int calc( int, int );
   int (*pfi2s)( const string &, const string & ) = 0;
   int (*pfi2i)( int, int ) = 0;

   int main() 
   {
      pfi2i = calc; // 
      pri2s = calc; // :  
      pfi2s = pfi2i; // :  
      return 0;
   }

           
                                      7.9.3. 

         ,   . 
    .      , 
     :

   #include <iostream>

   int min( int*, int );
   int (*pf)( int*, int ) = min;

   const int iaSize = 5;
   int ia[ iaSize ] = { 7, 4, 9, 2, 5 };

   int main() 
   {
      cout << " : min: "
           << min( ia, iaSize ) << endl;

      cout << " : min: "
           << pf( ia, iaSize ) << endl;
      return 0;
   }

   int min( int* ia, int sz ) 
   {
      int minVal = ia[ 0 ];
      for ( int ix = 1; ix < sz; ++ix )
         if ( minVal > ia[ ix ] )
            minVal = ia[ ix ];
      return minVal;
   }

            :

   pf( ia, iaSize );
   (*pf)( ia, iaSize );

372

       ,     ,  
    .
   ,      ,     
    .     , 
 -      .

                        7.9.4.    

        . :

   int (*testCases[10])();

      ,    ,   
  .
        ,    
typedef:

   // typedef    
   typedef int (*PFV)(); // typedef    
   PFV testCases[10];

      .
    ,    testCases,  
:

   const int size = 10;
   PFV testCases[size];
   int testResults[size];

   void runtests() 
   {
      for ( int i = 0; i < size; ++i )
         //    
         testResults[ i ] = testCases[ i ]();
   }

          ,  
  . :

   int lexicoCompare( const string &, const string & );
   int sizeCompare( const string &, const string & );

   typedef int ( *PFI2S )( const string &, const string & );

   PFI2S compareFuncs[2] =
   {
      lexicoCompare,
      sizeCompare
   };

        compareFuncs,      
  :

   PFI2S (*pfCompare)[2] = compareFuncs;

         :

   (*pfCompare)

     ,  pfCompare  . [2]  
  :

   (*pfCompare) [2]

   PFI2S  ,     typedef,   . 
  ,  int      const
string &.     ,    &lexicoCompare.
          compareFuncs,    
    :

   (*pfCompare)[ 0 ];
   compareFunc[ 0 ];

      lexicoCompare  pfCompare,    
 :

   //  
   pfCompare [ 0 ]( string1, string2 ); //  
   ((*pfCompare)[ 0 ])( string1, string2 ); //  


                           7.9.5.    

     ,     .  
     ?     
   ,   :

   int sort( string*, string*, int (*)( const string &, const string & ) );

        typedef    sort() 
:

   //   typedef 
   //  sort()  
   typedef int ( *PFI2S )( const string &, const string & );
   int sort( string*, string*, PFI2S );

         lexicoCompare, 
    :

   //      
   int lexicoCompare( const string &, const string & );
   int sort( string*, string*, PFI2S = lexicoCompare );

    sort()   :

1 void sort( string *sl, string *s2,
2            PFI2S compare = lexicoCompare )
3 {
4    //  
5    if ( si < s2 ) {
6       string elem = *s1;
7       string *1ow = s1;
8       string *high = s2 + 1;
9        
10      for (;;) {
11         while ( compare ( *++1ow, elem ) < 0 && low < s2) ;
12         while ( compare( elem, *--high ) < 0 && high > s1)
14            if ( low < high )
15               1ow->swap(*high);
16            else break;
17      } // end, for(;;)
18          
19      s1->swap(*high);
20      sort( s1, high - 1 );
21      sort( high +1, s2 );
22    } // end, if ( si < s2 )
23 }

   sort()      (C.A.R.Hoare).  
 .      s1  s2.  
,       
.   ,  s1  s2       
 s1    s2 ( 5).



      main(),      :

   #include <iostream>
   #include <string>

   //       
   int lexicoCompare( const string &, const string & );
   int sizeCompare( const string &, const string & );
   typedef int (*PFI)( const string &, const string & );
   void sort( string *, string *, PFI=lexicoCompare );
   string as[10] = { "a", "light", "drizzle", "was", "falling",
                     "when", "they", "left", "the", "museum" };

   int main() {
       //  sort()      compare
       sort( as, as + sizeof(as)/sizeof(as[0]) - 1 );
       //   
      for ( int i = 0; i < sizeof(as)/sizeof(as[0]); ++i )
          cout << as[ i ].c_str() << "\n\t";
   }

   , ,     ,    
     . :

   int (*ff( int ))( int*, int );

   ff()   ,     int  
   

   int (*)( int*, int );

       typedef   .  PF 
 typedef,  ,  ff()    :

   //   typedef 
   //   
   typedef int (*PF)( int*, int );
   PF ff( int );

           .   
  . ,   ff()  :

   // typedef    
   typedef int func( int*, int );
   func ff( int ); // :   ff() - 


                7.9.6.   ,   extern "C"

       ,    
.      . ,
 pf   -:

   extern "C" void (*pf)(int);

   ,         
,            
  . ,   -    
++ (      ),  .
   ,     ++      
++ .      ,
    .

            ,   
  ++,       -? 
  typedef. :

   // FC   :
   // -    int,    
   extern "C" typedef void FC( int );
   // f2() - C++    -
   //   -
   void f2( FC *pfParm );
