                  14. ,    

      ,      
  , ..    
.    ,    
  .    
      
.      -  ,  
  .

                             14.1.  

       ,       
    .    
     ,    .

    ,      ,  ,   
  .       , 
      .  ,    
 ,     ,      .
    Data  ? ,     .

      C    ,  
   :

     ,   ,    -
.
        . -,   
    ,     (.. 
          
   C,   ).  -,   
 ,      (
        
).
          ? . 
       
   .  ,    
         
       .   
    ,      
,      .      
 1.

                            14.2.  

     -   ,     
 .       2:

   class Account {
   public:
      //    ...
      Account();
      // ...
   private:
      char *_name;
      unsigned int _acct_nmbr;
      double _balance;
   };

2        _name    string.
    C-,     
     14.4.

     Account,  ,   
:

   Account newAcct( "Mikey Matz", 0 );

              ,
 :

   Account( const char *name, double open_balance = 0.0 );

        , 
  .      ,
        .
      ,   .
   ,     ,     
.
     new  .
            :

   //      
   Account acct1( "Anna Press" );
   Account acct2 = Account( "Anna Press" );
   Account acct3 = "Anna Press";

    acct3       . 
   ,     acct1,  
 acct2.

   //    
   Account acct1( "Anna Press" );

         , 
  :

   // !   ,  
   Account newAccount();

       .      
 :

   if ( ! newAccount.name() ) ...
      //   ...

          .
   

   Account newAccount();
   //   newAccount,
   //    

      , 
   Account.    ,
   ,    :

   // :    ...
   Account newAccount;

     ,     ,   
,        ,   
 .        ,  
   ,      .  ,  
  ,     ,  
   ,       
   .  ,    
      Account,    
  .     Account  
:

   class Account {
   public:
      //      
      Account( const char*, double=0.0 );
      const char* name() { return name; }
      // ...
   private:
      // ...
   };

        Account    
     C-,     .
?   (, vector) ,   
         ,  
 .      
   . ,     
    Account:

   // :       Account
   Account *pact = new Account[ new_client_cnt ];

          ,   -
  .

   //      Account
   inline Account::Account() {
      _name = 0;
      _balance = 0.0;
      _acct_nmbr = 0;
   }

      :   ,   
     . ,  
    :

   //     Account  
   //   
   inline Account::
      Account()
      : _name(0),
        _balance( 0.0 ), _acct_nmbr( 0 )
   {}

        ,     . 
         .
          
  :

   inline Account::
   Account( const char* name, double opening_bal )
      : _balance( opening_bal )
   {
      _name = new char[ strlen(name)+1 ];
      strcpy( _name, name );
      _acct_nmbr = get_unique_acct_nmbr();
   }

         const  volatile (. 
13.3.5),    :

   class Account {
   public:
      Account() const; // 
      Account() volatile; // 
      // ...
   };

     ,       
 .     
,       . 
    ,      ,
     .  ,   
 const      
    .       
volatile.

      :

   //  -  
   extern void print( const Account &acct );
   // ...

   int main()
   {
      //   "oops"    Account
      //    Account::Account( "oops", 0.0 )
      print( "oops" );
      // ...
   }

         (     , 
 ,  ,    )   
.      Account  
        Account 
 print(),        .
      ,   "oops"
   Account,     .
   C++     explicit, 
,     :

   class Account {
   public:
      explicit Account( const char*, double=0.0 );
   };

        . (  
 explicit    15.9.2.)

                       14.2.1.   

       ,   , 
 .   ,      
;         
:

   //     
   Account::Account() { ... }
   iStack::iStack( int size = 0 ) { ... }
   Complex::Complex(double re=0.0, double im=0.0) { ... }

     :

   int main()
   {
      Account acct;
      // ...
   }

   ,     Account  
.     :

   1.   .     acct.
   2.  ,    .     acct
         :   main()   .
   3.     ,      ,
        .  acct   : 
         .
   4.     ,  - .  
      , acct  ,   .

     ,     ,  
 ,       .  Account   , 
   ,  .      
.    ,  ,   
,   ,   :  
   ,       
    , ,    .

                        14.2.2.     

          Account  ,    
  :

   class Account {
   friend class vector< Account >;
   public:
      explicit Account( const char*, double = 0.0 );
      // ...
   private:
     Account();
     // ...
   };

          Account,   
  ,    .  - Account 
   vector   ,  
.
   ,   ,    C++  
 :

            
    (     );
     ,       , 
            ,   
     ,      (.
       -  
     17).

                         14.2.3.  

           
  .       
    .  
   ,    .
  ,    ,    
    .
          . 
    .    Account 
,        ,  
 .
           
  (    const).  
:

   inline Account::Account( const Account &rhs )
      : _balance( rhs._balance )
   {
      _name = new char[ strlen(rhs._name) + 1 ];
      strcpy( _name, rhs._name );
      //  rhs._acct_nmbr 
      _acct_nmbr = get_unique_acct_nmbr();
   }

     :

   Account acct2( acct1 );

 ,       
Account.     ,    ;   , 
 acct2  .  ,    
,     .  
      ,  
     .     
. (       14.6.)

                              14.3.  

         -,  
,            
  delete.
           ,  ,  
  .
        -,     
    .
        ,         
.      ,    
,     ,     ,  
 new.
          .  
  ,      
      . , 
      
  Timer,        
 .       .
      ,      (  
),   :

(1) #include "Account.h"
(2) Account global( "James Joyce" );
(3) int main()
(4) {
(5)     Account local( "Anna Livia Plurabelle", 10000 );
(6)     Account &loc_ref = global;
(7)     Account *pact = 0;
(8)
(9)     {
(10)       Account local_too( "Stephen Hero" );
(11)       pact = new Account( "Stephen Dedalus" );
(12)    }
(13)
(14)    delete pact;
(15) }

      ? :     global
  (2);        local  local_too  
(5)  (10) ,    ,   ,   (11). 
  loc_ref     (6),    pact 
 (7)     .      
 ,     global.   
 ,   (     ,  (11)),
     ( (7)).
       :    global,
   (2),           
delete   (14).     ,    
 .       
 ,      .
          
    .    
      ,   ,    
,      ,   C++ 
.4
4 .     [LIPPMAN96b],    
    ,   
.

     ,         
 (    ).
   ++        delete 
,    ,     
:

   // :   

   if (pact != 0 ) delete pact;


                      14.3.1.   

          .  
       new (.  8.4). 
.   :

   char *arena = new char[ sizeof Image ];

    ,       Image,  
    .   :

   Image *ptr = new (arena) Image( "Quasimodo" );

     .    ptr 
,    arena.  ,    ptr,
     Image,    
  .  ,   new() 
      .
       Quasimodo,    - 
  Esmerelda,      arena :

   Image *ptr = new (arena) Image( "Esmerelda" );

     Quasimodo    ,     
    .      
Image,      delete:

   // :    ,    
   delete ptr;

,   ,      ,    
.        Image:

   ptr->~Image();

        
 new.
   , ,  ptr  arena         ,
  delete  arena

   //   
   delete arena;

      Image,   arena   char*, 
    ,    delete 
   ,  .

               14.3.2.    

          
,          
   . ,   

   Account acct( "Tina Lee" );
   int swt;
   // ...
   switch( swt ) {
   case 0:
      return;
   case 1:
      // - 
      return;
   case 2:
      //  - 
      return;
   //   
   }

      return.  
Account ,          . 
      , 
 .     return    case
   break  ,      
:

                        14.4.    

         ,    
. :

   Account table[ 16 ];

   16  Account.    
   .     
        .
   :

   Account pooh_pals[] = { "Piglet", "Eeyore", "Tigger" };

    ,  :

       ,    .    
 ,      :

   Account pooh_pals[] = {
      Account( "Piglet", 1000.0 ),
      Account( "Eeyore", 1000.0 ),
      Account( "Tigger", 1000.0 )
   };

           , 
      :

   Account pooh_pals[] = {
      Account( "Woozle", 10.0 ), // 
      Account( "Heffalump", 10.0 ), // 
      Account();
   };

           ,  
   .       
  new,       ,  
  .         .
   

   Account *pact = new Account[ 10 ];

  ,   ,      Account,
     .

          14.4.1.  ,    A

       ,   ,  
 :    ,     
  ,   ,    
 .
         ,    
 new:

   #include <utility>
   #include <vector >
   #include <new>
   #include <cstddef>
   #include "Accounts.h"

   typedef pair<char*, double> value_pair;

   /* init_heap_array()
    *    -
    *       
    *  
    * init_values:     
    * elem_count:    
    *  0,      
    * init_values
    */

    Account* Account::init_heap_array(vector<value_pair> &init_values, 
                                      vector<value_pair>::size_type elem_count = 0 )
   {
      vector<value_pair>::size_type vec_size = init_value.size();

      if ( vec_size == 0 && elem_count == 0 )
         return 0;

      //     elem_count,
      // ,  elem_count == 0,   ...
      size_t elems = elem_count ? elem_count : vec_size();

      //      
      char *p = new char[sizeof(Account)*elems];

      //      
      int offset = sizeof( Account );
      for ( int ix = 0; ix < elems; ++ix )
      {
         //  ix- 
         //     ,
         //   ;
         //       
         if ( ix < vec_size )
            new( p+offset*ix ) Account( init_values[ix].first,
                                        init_values[ix].second );
         else new( p+offset*ix ) Account;
      }
      // :    ;
      //     
      return (Account*)p;
   }

       ,    
,   ,       
 .
         ,     
p          , 
   ,    :

     14.3 ,    new  
      .     
new      Account   
  .     
     ,    
  .  delete   :

   delete [] ps;

   ?   ps ( ,     
 init_heap_array())    ,    
  new,      
.       :

   void Account::dealloc_heap_array( Account *ps, size_t elems )
   {
      for ( int ix = 0; ix < elems; ++ix )
         ps[ix].Account::~Account();
      delete [] reinterpret_cast<char*>(ps);
   }

     ps,  p       , ps    
  Account,  p     char.  p   ix- ,
  ix-   Account.   p    ,  ,
      .
         :

                               14.4.2.  

                       14.5.   

          (  
   14.2). ,  ,    
 ,       /. 
       (,  _name   ,
   string):

   inline Account::Account( const char* name, double opening_bal )
      : _name( name ), _balance( opening_bal )
   {
      _acct_nmbr = het_unique_acct_nmbr();
   }

             
.     ,     , 
   .     ,  
  ,   , 
  .     name   string,
    _name.  _balance  
opening_bal.
       :      
       ? ,  
 

   inline Account::Account( const char* name, double opening_bal )
      : _name( name ), _balance( opening_bal )
   {
      _acct_nmbr = het_unique_acct_nmbr();
   }



   Account( const char* name, double opening_bal )
   { 
      _name = name;
      _balance = opening_bal;
      _acct_nmbr = het_unique_acct_nmbr();
   }

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

   inline Account::Account()
   {
      _name = "";
      _balance = 0.0;
      _acct_nmbr = 0;
   }

    .     
     string,   
_name.  ,   _name   .
           .
,   ,     
,        .   
      Account:

   inline Account::Account() : _name( string() )
   {
      _balance = 0.0;
      _acct_nmbr = 0;
   }

       _name   .   
   string .   ,  
 :

   inline Account::Account()
   {
      _balance = 0.0;
      _acct_nmbr = 0;
   }

               .
,   ,    _balance:  
    ?    , 
  ,      ,   
   (  ).  
 :

   //   
   inline Account::Account() : _balance( 0.0 ), _acct_nmbr( 0 )
   {}

           - 
 .       ,   
  :

   class ConstRef {
   public:
      ConstRef(int ii );
   private:
      int i;
      const int ci;
      int &ri;
   };

   ConstRef::ConstRef( int ii )
   { // 
      i = ii;  // 
     ci = ii;  // :    
     ri = i;   // : ri  
   }

            
-   .       
.     :

   // :     
   ConstRef::ConstRef( int ii ) : ci( ii ), ri ( i )
   { i = ii; }

           ,  
 .       Account:

   class Account {
   public:
      // ...
   private:
     unsigned int _acct_nmbr;
     double _balance;
     string _name;
   };

        

   inline Account::Account() : _name( string() ), _balance( 0.0 ), _acct_nmbr( 0 )
   {}

 : _acct_nmbr, _balance, _name.  ,   
(    - ),  
,       . , 
 :

   inline Account::Account( const char* name, double bal )
      : _name( name ), _balance( bal )
   {
      _acct_nmbr = get_unique_acct_nmbr();
   }

  : _balance, _name, _acct_nmbr.
           
        , 
      :

   class X {
      int i;
      int j;
   public:
      //  ?
      X( int val )  : j( val ), i( j )
      {}
      // ...
   };

   ,      i  j  
 val,     i  ,    
  j.     
  (    )   :

   //  
   X::X( int val ) : i( val ) { j = i; }

                               14.6.   A

           , , :

   Account newAcct( oldAcct );

    .    ,  
 ,   ,     .
  ,      
,       .
       ,  ,   
   ,  ,  
,    .   
   Account:

   class Account {
   public:
     // ...
   private:
     char *_name;
     unsigned int _acct_nmbr;
     double _balance;
   };

  ,       :

   inline Account::Account( const Account &rhs )
   {
      _name = rhs._name;
      _acct_nmbr = rhs._acct_nmbr;
      _balance = rhs._balance;
   }

           
:

        :

     Account newAcct( oldAcct );

          :

     extern bool cash_on_hand( Account acct );
     if ( cash_on_hand( oldAcct ))
     // ...

          :

    extern Account
    consolidate_accts( const vector< Account >& )
    {
      Account final_acct;
      //   
      return final_acct;
   }

      :

    vector< string > svec( 5 );

    (      string    
      ,        
       string.)

        :

     svec.push_back( string( "pooh" ));


          
    .    ,   
  ,      
, , ,   Account.
       newAcct._name  oldAcct._name   
   C-.  oldAcct        
,  newAcct._name     .  
,  newAcct  ,  _name,    
 oldAcct.     .
           
 .    ,     Account  
   .    ,  
       Account.  
  ,    :

   inline Account::Account( const Account &rhs )
   {
      //    
      _name = new char[ strlen(rhs._name)+1 ];
      strcpy( _name, rhs._name );

      //     
      _acct_nmbr = get_unique_acct_nmbr();

      //      
      _balance = rhs._balance;
   }

         
 .     :

  1.     .   
      ,  -   .
  2.     -   ,
           (
       ,     1,   ).    
              -
       .    ,    
      ,     ,   
        ,      .

      ,  Account   :

   class Account {
   public:
      Account();
      Account( const char*, double=0.0 );
      // ...
   private:
      Account( const Account& );
      // ...
   };


                14.6.1.  ,   


 (
                 14.5.   

      Account,   _name  string:

   #include <string>

   class Account {
   public:
      // ...
   private:
      unsigned int _acct_nmbr;
      double _balance;
      string _name;
   };

 )


    ,    _name  C-    string? 
      ?    
  ?        .
       .  
     ,    
. ,     Account  _name
 ,    :

   newAcct._name = oldAcct._name;

   ,   ,  -.  

   Account newAcct( oldAcct );

     Account.      
 ,        , 
      .
    ,   - ,    
 .      ?  ,
      - .   
     .     
     ,   
     .     
  ,      ,  
 ,     .
        string    ,  _name
    .     
 Account    (    ):

   inline Account::Account( const Account &rhs )
   {
      _acct_nmbr = rhs._acct_nmbr;
      _balance = rhs._balance;

      //   C++
      //    
      //  ,   
      _name.string::string( rhs._name );
   }

          Account 
      _name,     
 ,      . 
     .    , ?

   //   ...
   inline Account::Account( const Account &rhs )
   {
      _name = rhs._name;
      _balance = rhs._balance;
      _acct_nmbr = get_unique_acct_nmbr();
   }

     ,       
.       string 
  string       
   string    .  
:

   inline Account::Account( const Account &rhs )
      : _name( rhs._name )
   {
      _balance = rhs._balance;
      _acct_nmbr = get_unique_acct_nmbr();
   }

      ,    . (   
,   _name    rhs._name,        
 .)      ,  
   -    
.

                             14.7.   A

            
    .    
      
   :

   newAcct = oldAcct;

      newAcct 
  oldAcct.    
 :

   inline Account& Account::operator=( const Account &rhs )
   {
      _name = rhs._name;
      _balance = rhs._balance;
      _acct_nmbr = rhs._acct_nmbr;
  }

    ,         , 
      . ,  
  Account,   _name    char*, 
     _name,   _acct_nmbr.
      ,      ,
      :

   //     
   className& className::operator=( const className &rhs )
   {
      //     
      if ( this != &rhs )
      {
          //     
      }
      //  ,   
      return *this;
   }

     

   if ( this != &rhs )

     ,    
,       
,      ,     ,
     .
         Account:

   Account&Account::operator=( const Account &rhs )
   {
      //     
      if ( this != &rhs )
      {
         delete [] _name;
         _name = new char[strlen(rhs._name)+1];
         strcpy( _name,rhs._name );
         _balance = rhs._balance;
         _acct_nmbr = rhs._acct_nmbr;
      }
      return *this;
   }

        , , ,  :

   newAcct = oldAcct;

  :

  1. ,        .
  2.  ,     ,  ,    
        .
  3.     ;    ,
         .
  4.    ,     .
  5.         
              
      .
  6.   ,   ,    1-
     6,         .

         Account ,  _name  
 string,     

   newAcct = oldAcct;

   ,      
:

   inline Account&Account::operator=( const Account &rhs )
   {
      _balance = rhs._balance;
      _acct_nmbr = rhs._acct_nmbr;
      //        
      name.string::operator=( rhs._name );
   }

           Account 
 - _acct_nmbr.     
   ,  _name     string:

   Account&Account::operator=( const Account &rhs )
   {
      //     
      if ( this != &rhs )
      {
          //  string::operator=( const string& )
          _name = rhs._name;

          _balance = rhs._balance;

           _acct_nmbr?
      }
      return *this;
   }

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

                       14.8.   A

              
,   . ,     :

   bool sufficient_funds( Account acct, double );

         
 acct   -  Account.  
     :

   bool sufficient_funds( Account &acct, double );
   bool sufficient_funds( Account *pacct, double );

     Account.    
    (.     
    7.3).
             , 
 ,      .  
 :

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

        

   Matrix a, b;
   // ...
   //     operator+()
   Matrix c = a + b;
   a = b + c;

            
  ,  Matrix      .  
  ,  , ,   .
        :

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

      .   ,   
result      ,    . (
    ,     .)
           
.       :

   //      
   //     ,    
   Matrix& operator+( const Matrix& m1, const Matrix& m2 )
   {
      Matrix *result = new Matrix;
      //   ...
      return *result;
   }

     :    ,      
      delete     
.
         ,   
   ,    :

   //    ,
   //       
   void mat_add( Matrix &result, const Matrix& m1, const Matrix& m3 )
   {
      //  

   }

    ,   ,     
  ,     


   //   
   Matrix c = a + b;

       :

   //   
   if ( a + b > c ) ...

          ++.    
   ,     :

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

         ,    
-:

   //   
   //      
   void operator+( Matrix &result, const Matrix& m1, const Matrix& m2 ) name result
   {
      //  
   }

        ,    
,     . :

   Matrix c = a + b;

      

   Matrix c;
   operator+(c, a, b);

          ,    .
   ,      
       .    
:

    classType functionName( paramList )
   {
      classType namedResult;
      //  -  ...
      return namedResult;
   }

      ,     
:

   void functionName( classType &namedResult, paramList )
   {
       //        namedResult
   }

         
 .     ,   
           .
            C++. 
  

   Matrix c = a + b;

  . ,    
 ,     :

   Matrix c;
   c = a + b;

     .   :

   for ( int ix = 0; ix < size-2; ++ix ) {
      Matrix matSum = mat[ix] + mat[ix+1];
      // ...
   }



   Matrix matSum;
   for ( int ix = 0; ix < size-2; ++ix ) {
      matSum = mat[ix] + mat[ix+1];
      // ...
   }

   ,      ,   , 
         
 .  ,     

   Point3d p3 = operator+( p1, p2 );

  :

   //   C++
   Point3d p3;
   operator+( p3, p1, p2 );



   Point3d p3;
   p3 = operator+( p1, p2 );



   //   C++
   //    
   operator+( p3, p1, p2 );

.
     ,      
  . ?      
,        . 
    ,        
 .
      ,       
 .         
 (      ),  ,  
     ,     
 .
            
  ,   ,    
   ,     
. ,     ,     
. ,  

   Point3d p3;
   p3 = operator+( p1, p2 );

  :

   //   C++
   Point3d temp;
   operator+( temp, p1, p2 );
   p3.Point3d::operator=( temp );
   temp.Point3d::~Point3d();

     (Michael Tiemann),   GNU C++,   
     (return value language
extension).       [LIPPMAN96b].    Inside the
C++ Object Model ([LIPPMAN96a])      
 .
