     15 

      C++ 

          ,   . 
       ,   
  ,        . 
         ANSI   C++,    ,  
       .   
    .        
 C++, ,   ,  ,   
 ,        
.       Borland C++  4.0  
 ,        
  string ANSI C++. 

     

             .  
          
   ,       
    . 
    ,        ,  
    ,   .  C++    
    ,       
 .        
   .      ,  , 
, ,  ,      ,  
  . 
      C++     .     
     ,       
 .        C++. 

     
       -  C++,    goto.  goto,
    ,     
 .  ,      . 
   ++       .

      

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

    class AClass { 
    private: 
       double value;         //    
    public: 
       AClass() { value = 3.14159; } 
    }; 

     AClass     value  double.  
     3.14159.    
,         value 
 .      ,   
  -. 
    , ,      ,   
  AClass: 

    class BClass { 
    private: 
         AClass anObject;	//    AClass public: 
          void ShowValue(void) 
          { cout  anObject.value;   }	// ??? }; 

     anObject  AClass    BClass. - ShowValue()
  value  anObject.    , 
  value    AClass   - AClass 
   . 
       AClass   private  protected  
 value   .     ,   
 ,       .      
BClass    AClass,        
 ,   BClass        
 AClass. ,     value    AClass, 
   value     . 
         BClass   AClass.  BClass 
    value       ,  
          
 .       friend  , 
       .     
clacc      value  AClass.  ,  
,   BClass         
AClass,      .   
  AClass: 

    class AClass { 
        friend class BClass;     //  BClass -   AClass 
   private: 
       double value;                //    AClass  BClass!!! 
   public: 
       AClass() { value = 3.14159; } ;	
   }

           friend class 
BClass,      BClass   
     AClass.      
   ,   ,       
    AClass.  BClass    
   "" AClass.  ,    
   .      friend 
    .     .
              . 
       ,           
   ,        
   .        
  . 


    368 136  III.   ANSI C++ 

            ,  
 .     ,     
 ,    ()  
       . 
               
       .  
  -    .
              ,   
         
         . 
      15.1 ,       
     . 

     15.1.  FRIEND.CPP (  ) 

    1:	#include <iostream.n> 
    2: 
    3:	class Pal { 
    4:	   friend class Buddy;	//	Buddy -   Pal 
    5:	private: 
    6:	   int x;	//	  Pal  Buddy 
    7:	protected: 
    8:	   void doublex(void) { x *= x; }	//	  Pal  Buddy 
    9:	public: 
    10:	   Pal() { x = 100; }	//	   
    11:	   Pal(int n) { x = n; }	// 
    12:	}; 
    13: 
    14:	class Buddy { 
    15:	private: 
    16:	   Pal palObject;	//	   Buddy 
    17:	public: 
    18:	   void ShowValues(void);	//	   
    19:	}; 
    20: 
    21:	main() 
    22:	{ 
    23:		Buddy aSuddy; 
    24: 
    25:		aSuddy.ShowValues(); 
    26:		return 0; 
    27:	} 
    28: 
    29:	void Buddy::Showvalues(void) 
    30:	{ 
    31:		Pal aPal(1234); 
    32: 
    33:		cout << "\nBefore, palObject.x	==" << palObject.x; 
    34:		palObject.doublex() ; 
    35:		cout << "\nAfter, palObject.x	==" << palObject.x; 
    36:		cout << "\naPal.x == " << aPal.x << "\n'; 
    37:	} 

     FRIEND       Pal.   4 
,    Buddy     Pal.    
-  Buddy        
  Pal. ,  Pal       
  Buddy. 
      16   Buddy    Pal.   
 ,    ,   Buddy      
Pal, - ShowValues()       
   - doublex() ( 29-37)  Pal. 

       

         ,      
     . ,    , 
    .    
         
 . ,         
"". 
         ,     
   ,        
     .  AClass  BClass   
,     :


    15.   C++     369 137 

    class lass;	//     

    class AClass { 
        friend class BClass; 	// BClass      AClass 
        ... 			//    
     }; 

    class BClass { 
       friend class AClass;	// AClass      BClass 
	...				//   ; 
     };

     -           
    .       
    ,    -,   
    ,     
   BClass.    AClass  -  
 -  BClass   ,    
BClass  . 

      
    
       (  )  
 .         
     .     
  C++   . 
         -,  
  "" .      
      ,   . 
      15.2      
   . 
     FRIENDFN   , One  Two.     
 3    One  Two.   6  14  
    Show().      Show()
 ,  Show()      
   One  Two. (,  ,   
 ,       ,     
      .) 

     15.2.   FRIENDFN.CPP (  ) 

    1: #include <iostream.h> 
    2: 
    3: class Two;	//    
    4: 
    5: class One { 
    6:     friend void Show(0ne &c1, Two &c2); 
    7: private: 
    8:    char *s1;	//   One   Show() 
    9: public: 
    10:  One() { s1 =	"Testing "; } 
    11: }; 
    12: 
    13: class Two { 
    14:     friend void	Show(0ne &c1, Two &c2); 
    15: private: 
    16:     char *s2;	//   Two   Show() 
    17: public: 
    18:    Two() { s2 =	"one, two, three"; } 
    19: }; 
    20: 
    21: main() 
    22: { 
    23:  One c1; 
    24:  Two c2; 
    25: 
    26:  Show(c1, c2); 
    27:  return 0; 
    28: } 
    29: 
    30: void Show(0ne &c1, Two &c2) 
    31: { 
    32:    cout << c1.s1 << c2.s2 << '\n'; 
    33: } 


    370      III.   ANSI C++ 

     Show()      cl  2   
.   Show()   ,   
Show()         -, 
 . 
    ,    Show() ( 30-33)   
,   s1  s2.   Show()  
 ,      - s1  s2, 
   .  ""     
     . 

     - 

          .    
  -  .  -  
      ,     . 

     15.3.  FRIEfJDMF.CPP (  -) 

    1:	#include <iostream.h> 
    2: 
    3:	class One;  //	   
    4: 
    5:	class Two { 
    6:	private: 
    7:	  char *s2; //	   Two 
    8:	public: 
    9:	  Two() { s2 = "one, two, three"; } 
    10:	  void Show(0ne &c1); 
    11:	}; 
    12: 
    13:	class One { 
    14:	   friend void Two::Show(One &c1); 
    15:	private: 
    16:	   char *s1; //	   One   Two:: Show 
    17;	public: 
    18:	   One() { s1 =	"Testing "; } 
    19:	}; 
    20: 
    21:	main() 
    22:	{ 
    23:		One c1; 
    24:		Two c2; 
    25: 
    26:		c2.Show(c1); 
    27:		return 0; 
    28:	} 
    29: 
    30:	void Two::Show(One &c1) 
    31:	{ 
    32:	  cout << c1.s1 << s2 << '\n'; 
    33:	} 

        FRIENDFN ( 15.2),    FRIENDMF 
  , One  Two.   One   
 - ( 14)       
   (Two::),    
  Show(). 
              
   ,  ,    
 -,    ,  - 
.   One,    Two::Show(), 
  Two     . 
                
  Show() ( 3033).      One &cl. 
  Show()    Two,    
     Two.  c1.s1      
  32   ,    Show()   
One,     s1. - Show()   
 s2 ,      Two,   
 Show(). 
      Show()    Two,     this, 
   ,     -.   
     Two ( 24),   
 Show()    ( 26). 


    15.   C++     371 

      

             
.  ,      
    ,   ,   
 (+)   (-).    ,  TAnyClass, 
          
 :

    TAnyClass c1, 2, ; 

           :	

     = 1 + 2; 

             
,      . ,    
 (+)   .   (-)   
.       ,   
  . ,    (!),  , 
    .       (-). 
 -count    count. 
            
,   ++,  .   
  ,       - 
.    ,     
    - . 

     - 

           
   ZZ: 

    class ZZ { 
    public: 
       friend ZZ operator+(ZZ a, ZZ b); 
       friend ZZ operator-(ZZ a, ZZ b);
       friend ZZ operator*(ZZ a, ZZ b);
       friend ZZ operator/(ZZ a, ZZ b);
       // ...  ,     
    };
 
      ZZ    -. (  
 ZZ     ,    .)  
  operator+(), operator-(), operator*()  operator/().    
     +, -, *  /  ,  
    C++       operator  
  ,   . 15.1. 

     15.1.  ,     

	*	/	+	-	%	^	&	| 
	~	!	,	=	<	>	<=	>= 
	++	--	<<	>>	==	!=	&&	|| 
	*=	/=	%=	~=	&=	|=	+=	-= 
	<<=	>>=	->	->*	[]	()	new	delete 

     +, -, *  &     ,    
 .  ., .*, ::  sizeof    . 
 ,  =, (), []  ->    
-. (    -   
 ). 
       ZZ      
  .      

    friend ZZ operator-(ZZ a, ZZ b); 

       ,    operator+()
       .    
 ZZ (      ),     
   ZZ. (     
 "" .        
.)   ,   .operator+()    
,    ,   ,  
  . ,   operator+()
   ,    ZZ. 


    372      III.   ANSI C++ 


     
          
.       ,  ,  
   ; ,      
     ..      
 (,   ,    ), 
  . 

      operator+()    . , 
operator+()     .      
feeblewitz() operator+(),      : 

    feeblewitz(a, b); 

           ZZ.  feeblewitz  operatof+,   
     : 

    operator+(a, d);	//  feeblewitz(a, b) 

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

     + ;	//  operator+(a,  b),1 

      + b  operator+(a, b)         
      .      
     .      C++  
,      .       
  ,      . 
      ,     , 
   .   15.4  , 
      .    
    ,     
       . 

     15.4.  STROPS.CPP (  ) 

    1:	#include <iostream.h> 
    2:	#include <stdlib.h> 
    3:	#include <string.h> 
    4: 
    5:	class TStrOp { 
    6:	private: 
    7:	   char value[12]; 
    8:	public: 
    9:	   TStrOp() { value[0] = 0; } 
    10:	   TStrOp(const char *s); 
    11:	   long GetValue(void) { return atol(value); }
    12:	   friend long operator+(TStrOp a,  TStrOp b); 
    13:	   friend long operator-(TStrOp a,  TStrOp b); 
    14:	}; 
    15: 
    16:	main() 
    17:	{
    18:		TStrOp a = "1234"; 
    19:		TStrOp b = "4321";
    20: 
    21:		cout << "\nValue of a == " << a.GetValue(); 
    22:		cout << "\nValue of b == " << b.GetValue(); 
    23:		cout << "\na + b + 6 == " << (a + b + 6); 
    24:		cout << "\na - b + 10 == " << (a + b + 10) 
    25:		<< "\n" ; 
    26:		return 0; 
    27:	} 
    28: 
    29:	TStrOp::TStrOp(const char *s) 
    30:	{ 
    31:	  strncpy(value, s, 11);
    32:	  value[11] = 0; 
    33:	}
    34: 
    35:	long operator+(TStrOp a,  TStrOp b) 
    36:	{ 
    37:	  return (atol(a.value) + atol(b.value)); 


     15.   C++     373 

    38:     }
    39: 
    40:	long operator-(TStrOp a, TStrOp b) 
    41:	{ 
    42:	  return (atol(a.value) - atol(b.value)); 
    43:	} 

     STROPS      : 

    Value of a == 1234 
    Value of b == 4321 
    a + b + 6 == 5561 
    a - b + 10 == -3077 

               
 ,   main().   18-19   
  TStrOp    b.      
     : 

	TStrOp a("1234"); 
	TStrOp b("4321"); 

               
 ,    23  24     
 : 

    ( + b + 6) 
    ( - b + 10) 

              ; 
    C++  ,     
  . 
      ,    12-13,  
        TStrOp.  
  ,       ZZ. ,  
     .    
    (    ),   
 ,    .   ,   
  ,     . 
          35-43.  
      ,    
    C++.     
    operator+  operator-,    
 ,        
 TStrOp. 
        ,       
   TStrOp.     
 -     b   .  
       atol 
(ASCII to long),       STDLIB.H. 
       . 

      - 

         ,     
 15.5.     STROPS.CPP ( 15.4),    
   -.

     15.5.  STROPS2.CPP (  -) 

    1:	#include <iostream.h> 
    2:	#include <stdlib.h> 
    3: 
    4:	#include <string.h>	
    5:	class TStrOp { 
    6:	private: 
    7:	   char value[12]; 
    8:	public: 
    9:	   TStrOp ()  { value[0] = 0;   } 
    10:	   TStrOp(const char *s); 
    11:        long GetValue(void) { return atol(value); } 
    12:	   long operator+(TStrOp b); 
    13:	   long operator-(TStrOp b); 
    14:	}; 
    15: 
    16:	main() 
    17:	{ 
    18:	  TStrOp a = "1234"; 
    19:	  TStrOp b = "4321"; 


    374      III.   ANSI C++ 


    21:	cout << "\nValue of a == " << a.GetValue(); 
    22:	cout << "\nValue of b == " << b.GetValue(); 
    23:	cout << "\na + b +   6 == " << (a + b + 6); 
    24:	cout << "\na - b + 10 == " << (a - b + 10) 
    25:	<< '\n'; 
    26:	return 0; 
    27:	} 
    28: 
    29:	TStrOp::TStrOp(const char *s) 
    30:	{ 
    31:	  strncpy(value, s, 11); 
    32:	   value[11] = 0; 
    33:	} 
    34: 
    35:	long TStrOp::operator+(TStrOp b) 
    36:	{ 
    37:	    return (atol(value) + atol(b.value)); 
    38:	} 
    39: 
    40:	long TStrOp::operator-(TStrOp b) 
    41:	{ 
    42:	   return (atol(value) - atol(b.value)); 
    43:	} 

       -,    
12-13     32-33.       
TStrOp,          ,  
,      .  ,  - 
  this,   ,     . 
,      ,   ,  
.      operator+()   
 ,   this->value  .value. 
    -  -    
.     operator+()   12  : 

    long operator+(TStrOp a,  TStrOp b);      // ??? 

       ,  operator+()    TStrOp  
 -   this,    .  
  this     b     ,  
  ,  operator+(),   
.      TStrOp  operator+()  
this.value  b.value ( 37).     . - 
operator-()    this , ,    
       ( 40-43). 

       

     ,      ,  
  .          
 ,     . 
          ,     
      .   
        , 
   (  ,     ). 
,     STROPS2.CPP ( 12.5)   
private:   6: friend long operator-(TStrOp a); 
           13   , 
  ,       . 
-   13    this. Ho ,  
  ,    this.    
   ,    ,    
  main():

    long operator-(TStrOp a) 
    {
      return -atol(a.value); 
    }

        ""   ,  return  
      ,    
TStrOp.   ,     , 
       . ,  
    main() (    20): 

    cout << "\n -   == " << - ; 


    15.   C++     375 

          .   
,         
  .   C++      
 ,    ! 
           ,    
   -.     
 STROPS2.CPP,      14 ( 
   TStrOp): 

    long operator-(void); 

      -      
    -   this,  
- operator-()       
    this->value.     
,       main(): 

    long TStrOp::operator-(void) { 

    	return -atol(value);    //  -atol(this->value) 

   } 


     -     value  
 .     ,  , 
     main(): 

    cout << "\n-a == " << -; 


      

             
      . , 
,       TStrOp  
STROPS2.CPP ( 15.5)  : 

    TStrOp myValue = "9876"; 

     TStrOp  ,     
        .   
    myValue    : 

    long x = myValue;         // ???	

    ,    ,    TStrOp  
      .   
        
    TStrOp    . 
         TYPE(),  TYPE   
,       . ,   
     TStrOp     
      : 

    operator long() { return atol(value);  } 

           
 C++      .     
    TStrOp    , 
  TStrOp    ,   , 
 .. 

       

          []   
  - ,     ,  
        .    
( 15.6)    []  , 
      -. 
      ,  PseudoArray    
     .   
       5-8.   13 
 - operator[]    .  
        . 

     15.6. SSOP.CPP (  ) 

    1:   #include <iostream.h> 
    2: 
    3:   class PseudoArray { 


    376      III.   ANSI +

    4:	private: 
    5:	  int value0; 
    6:	  int value1; 
    7:	  int value2; 
    8:	  int value3; 
    9:	public: 
    10:	  PseudoArray(int v0, int v1, int v2, int v3) 
    11:	  { value0 = v0; value1 = v1; value2 = v2; value3 = v3; } 
    12:	  int Getlnt(unsigned i); 
    13:	  int operator[](unsigned i); 
    14:	}; 
    15: 
    16:	main()
    17:	{ 
    18:	  PseudoArray pa(10, 20, 30, 40); 
    19: 
    20:	  for (int i = 0; i <= 3; i++) 
    21:	   cout << "pa[" << i << "] == " << pa[i] << '\n'; 
    22:	  return 0; 
    23:	} 
    24: 
    25:	int PseudoArray::Getlnt(unsigned i) 
    26:	{ 
    27:	 switch (i) { 
    28:	  case 0: return value0; //  : break   
    29:	  case 1: return value1; // (.. return)
    30:	  case 2: return value2; // 
    31:	  case 3: return value3; //  
    32;	  default: return value0; 
    33:	  } 
    34:	} 
    35: 
    36:	int PseudoArray::operator[](unsigned i) 
    37:	{ 
    38:	  return Getlnt(i); 
    39:	} 

      12        
   - GetlntO,     
 .          
   : 

    PseudoArray pa(10, 20, 30, 40); 
    cout << "Value #2 == " << pa.Getlnt(1);	//  - 

       ""    .   
   ,      
: 

    cout << "Value #2 == " << [1];    // pa     

      21    .  [1]  [2], 
,       ,     
    .    
      (    
   )   -   .     
      ,        
 . ,     13 : 

    int operator[](char ); 

    ,    ''  'D', 
      '',      
   C++,      ,  
 .  ,  []      ,   
      ,    
   .    - 
  []  : 

    int PseudoArray::operator[](char ) 
   {
       return Getlnt(c - 'A'); 
   }

             
. ,       20-21 
 : 

    for( char  = '';  <='D'; ++)
       cout  << "[" <<  << "] == " << [] <<'\'; 


     15.   C++     377 

       

       operator()      
,   .   operator()  
        .  , 
     .       
.        , 
  : 

    class TAnyClass { 
       int x; 
    public: 
       int operator()(void); 
       TAnyClass(int n) { x = n; } 
    }; 

      TAnyClass       
 int operator()(void);.    void   
 .  -   : 

    int TAnyClass::operator()(void) { 
       return x; 
    }	

        operator()      
 TAnyClass.       : 

    main() 
    { 
      TAnyClass object = 100; 
      int q = object(); //    ! 
      cout << q; 
      return 0; 
   } 

            object(),     
  object.operator()();. 

         

          ->   operator->(). 
     - .    -> 
  TAnyClass  : 

    class TAnyClass { 
       int x, ; 
     public: 
       TAnyClass(int xx, int yy) { x = xx;  = yy; } 
       TAnyClass* operator->();	
       int GetX(void) { return x; }	
       int GetY(void) { return y; } 

    - operator->()   ,    
  .         TAnyClass. 
   : 

    TAnyClass* TAnyClass::operator->() { 
      cout << "\nAccessing member: ";	
      return this; 
    }
 
           ->  
  .      
 ,    this,   ,   
  -.       
    TAnyClass  : 

    main() 
    {	                                            
       TAnyClass t(100, 200); 
       cout << t->GetX() << '\n'; 
       cout << t->GetY() << '\n ; 
       return 0; 
    }	


    378	 III.   ANSI C++ 

             -> 
   - GetX()  GetY()  t  TAnyClass.  
 ,    ,   : 

    cout << (t.operator->())->GetX() << '\n'; 
    cout << (t.operator->())->GetY() << '\n'; 

         ,  
 GetX()  GetY(): 

    Accessing member: 100 
    Accessing member: 200 


        

       C++      
      ++  --.   , 
    TAnyClass: 

    class TAnyClass { 
       int x; 
    public: 
       TAnyClass(int xx) { x = xx; } 
       int operator++() { return ++x; }	 //  ++object 
       int operator++(int) { return x++; ) //  object++ 
       int operator--() {  return --x; }   //  --object 
       int operator--(int) { return --; } //  object--
       int GetX(void) {  return x;  } 
    }; 

    - operator++()      
  TAnyClass;     . - 
operator++(int)        
TAnyClass; C++     . 
       ,    
  .   v  TAnyClass  ++v  
   ++.     
x.operator++();.  v++     ++  
,  x.operator++(0);.     . 
        ,     
 C++,   main()   :	

    main()
   { 
      TAnyClass t(100); 
      cout << "t == " << t.GetX() << "; ++t == " << ++t << '\n'; 
      cout << "t == " << t.GetX() << "; t++ == " << t++ << '\n'; 
      cout << "t == " << t.Getx() << "; --t == " << --t << '\n'; 
      cout << "t == " << t.Getx() << "; t-- == " << t-- << '\n';
  } 


     ,    

         ,   
   . 
      C++ " "   .   
    . 
      C++       .  ,  
     operator*()  operator=(), C++ 
     *= b   ,     
operator *=().
          .   
  ,   . ,   
 ,      C++  . 
        .     , 
  . 15.1.     #  ##. 

        

     ,   ANSI ,   malloc()  free() 
          
   .    ,  ,   C++,
  new  delete.   ,   
 ++ new  delete     malloc()  
free().     C++       
     ANSI    C++. 


     15.   C++     379 

          ,  new  
delete    ,   .  , new  delete  
         
 .    new  delete     
     . 


     
        free()   ,   
  new,      malloc()   ,   
    delete.    
   ANSI   C++    
 ,   ,     
,  new   delete      Borland  
        . 


      new 

       new   ,   ,  +  =. 
 new     ,     
         
 . 
      new      

        void * operator new(size_t size);. 

     new      
   .     
 ,  .     
,    null (). 
      15.7  ,      
new    .       
 .         
   ,       . 

     
      6 OVERNEW.CPP   #pragma warn -aus.   
     ,     
 ,     .

       new,    13   
  38-48,      .   
,   0,       
new  null. (     , , ,  
     .)    
,       ,  
    size.     
  . 

    15 7. OVERNEW.CPP ( new) 

    1:	#include <iostream.h> 
    2: 
    3:	//   "pragma"     
    4:	// ,          
    5: 
    6:	#pragma warn -aus
    7:	
    8:	class BrandNew {            
    9:	private:                                  
    10:	   int x;                                   
    11:	public: 
    12:	   BrandNew () ;
    13:	    void * operator new(size_t size); 
    14:	};
    15:
    16:	char buf[ 512 ] ;
    17:	int index;
    18: 	
    19:	main()
    20:	{
    21:        cout << "\nCreating local instance";
    22:  	   BrandNew b1;	
    23: 
    24:  	   cout << "\nAllocating space via new"; 


    380      III.   ANSI C++ 

    25:	  BrandNew *b2 = new BrandNew; 
    26:	  BrandNew *b3 = new BrandNew; 
    27:	  BrandNew *b4 = new BrandNew; 
    28:	  BrandNew *b5 = new BrandNew; 
    29:	  return 0; 
    30:	} 
    31: 
    32:	BrandNew::BrandNew() 
    33:	{ 
    34:	  cout << "\nlnside constructor"; 
    35:	  x = index; 
    36:	}
    37: 
    38:	void *BrandNew::operator new(size_t size) 
    39:	{ 
    40:	  cout << "\nlnside overloaded new. Size == " << size; 
    41:	  if (index >= 512 - sizeof(BrandNew)) 
    42:	     return 0; 
    43:	  else { 
    44:	    int k = index; 
    45:	    index += sizeof(BrandNew); 
    46:	    return &buf[k]; 
    47:	  } 
    48:	}
 

     delete 

     delete      .    
  new,     delete    
,  .     
delete    

    void operator delete(void *p); 

     .      
  : 

    void operator delete(void *p,  size_t size); 

         C++    
     . 
         delete   BrandNew  
 OVERNEW ( 15.7)  -    : 

    void BrandNew::operator delete(void *p) 
    {
       cout << "\nDeleting object at " << p; 
    } 

          .  
      ,   delete  
     .     
    .     
 main()    return: 

    delete b2; 
    delete b3;
    delete b4;
    delete b5; 

       ,   ,  , 
,     delete.   
   ,    
  delete      
     new. (.   
"    "    .) 

    
        C++   
    ,     new, 
    . ,   
BrandNew * = ::new BrandNew      
BrandNew  new. , ::delete      
delete,   .


     15.   C++     381 

        new   

    ,  new      ,  
 null.     ,   
   ,  C++    - 
  _new_handler,   :	

    typedef void (*vfp)(void);	
    vpf _new_handler;                                                                  
                                                                               
     typedef  ,     . 
-        ,    
     set_new_handler(),   
   NEW.:	

    vfp set_new_handler( vfp );      

        ,     C++. , 
          
: 

    void memerr(void)	
    {
      fputs("\n\nOut of memory\n", stderr);    
      exit(1);	
   }

        memerr()     , 
   ,   memerr  set_new_handler() 
 :	

    set_new_handler(memerr);                                                   

       set_new_haadler()   " Borland C++."


     
        set_new_handler    "Borland ++. 
 ." 


       

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

     
      ,        
   .       
   ,       
     , ,     
. 

        ,    , , 
    -  ,   
.        
,         .    
    ,       
  .    ,      
            
 . 
     C++         
 ,  -:     
.

      

            , 
C++   ,  ,     .
,     -:


    382      III.   ANSI C++ 

    class TAnyClass { 
    private:  
       int i; 
       double r; 
    public: 
       TAnyClass() 
          {i = 0;   r = 0;   } 
       TAnyClass(int ii, double rr) 
            { i = ii;   r = rr;   } 
     }; 

       TAnyClass    -   
   - i  r,   ,   
 .      : 
   TAnyClass(),   i  r  
,   ,    
 .    v1  v2  TAnyClass 
 : 

    TAnyClass v1; 
    TAnyClass v2(100, 3.14159); 

        ,  v2,  
   v3: 

    TAnyClass v3 = v2; 

         v2  v3    
-  v3.  , -   v2 
      v3,    : 

    TAnyClass v3; 
    v3.i = v2 i, //    
    v3.r = v2.r; // 


    
             
.  -  ,    
. 

           ,   
          . 
        ,   
 .    ,    .

      

             
,    .     
,     ,  ,  
 . (   Borland C++  
   ,      .) 
         CLASS( const CLASS& ).    
,         
 .     TAnyClass  - 
    : 

    #include <string.h> 
    class TAnyClass { 
    private: 
       int i;        //    	
       double r;   //    
       char *s,     //  -
    public:
       TAnyClass() 
          { i = 0;   r = 0;  s = NULL,   } 
       TAnyClass(int ii, double rr, const char *ss) 
          { i = ii;   r = rr;  s = strdup(ss);   } 
       ~TAnyClass() { delete s;  }                                              
       const char *GetStr(void) { return s;  } 
       TAnyClass(TAnyClass &copy),  //   

      TAnyClass(TAnyClass &copy)      
(TAnyClass),    ,     &    
TAnyClass,       .  
  -     (   
 ): 

     15.   C++     383 

    TAnyClass::TAnyClass(TAnyClass &copy) { 
      cout << "Inside TAnyClass1s copy constructor\n"; 
      i = copy.i; 
      r = copy.r; 
      if (copy.s) 
         s = strdup(copy.s); 
      else	
        s = NULL; 
    } 

           . ,    
      .   
   i  r     .    
 ,     s,    
strdup(           
.  ,       ,  
   NULL.     TAnyClass 
   ,      s   
       .      
  : 

    TAnyClass v1; 
    TAnyClass v2(1, 2, "A test string"); 
    TAnyClass v3 = v2;       //    

    C++      TAnyClass   
  ,  ,       
      TAnyClass ( 
    ),    
        . 
     ,   TAnyClass   .  
     ,     
,  ,     -.  
  ,     . 

     
    C++      - ,  
   ,  ,     . 


      

            ,  
-           ;  
 ,     .  
  ,     TAnyClass  
 : 

    TAnyClass v1; 
    TAnyClass v2(1, 2, "A test string"); 
    v1 = v2;    // ??? 

       v1      v2  
     v2  v1.  ! 
       ,   
.       -  , 
          ,  
     ,    
 . 
         ,   
  . C++       
,        . 
     ,      ,   
 =.     
                   
      void operator=(const CLASS&). 

,       TAnyClass  
     : 

    class TAnyClass { 
    private: 
      int i; //    
      double r; //    
      char *s; //  -! 
    public: 
       ......    // .    TAnyClass 
       void operator=(const TAnyClass &copy);    //   


    384     III.   ANSI C++ 

     operator=   ,  
     TAnyClass.    
 .  ,    b    TAnyClass,  
   = b     b.   
    : 

    void TAnyClass::operator=(const TAnyClass &copy) 
    {
       cout  "\nlnside TAnyClass's operator* function; 
       if (this == &copy) 
         return; 
       delete s; 
       i = copy.i; 
       r = copy.r; 
       s = strdup(copy.s); 
   } 

      ,       .    
if    this     .  
       . , 
    

    TAnyClass v1, v2; 
    v1 = v1; 

        
 - . 
    (   if?)
       if    (  ), 
  s. (     , 
,      s   
    delete.) 
          ,   
    .    ,  
   ,     
    . 

     operator=    

              
   .     
    operator=    
 *this: 

    TAnyClass::TAnyClass(TAnyClass &copy) 
    { 
       s = NULL;	  	//      
       *this = copy;	//     
    } 

         ( ),  
   . ,   
    NULL,   pa  
  ,    .  ,  
     . 

       - 
    
             <<  >>   
     -   
   .    ,  
  ,      . 

      

       IOSTREAM.H     
     : 

    extern istream_withassign _Cdecl cin;   //    
    extern ostream_withassign _Cdecl cout; //    
    extern ostream_withassign _Cdecl cerr; //     
    ewtern ostream_withassign _Cdecl clog; //      

     istream_withassign  ostream_withassign     
 ""   -  ios.       
     ,     , , 
  ,   Borland C++       
    -. 


    15.   C++     385 
     Borland C+ +4.5 

        -   
  .  -    
,  char, short, int, long, char* (,   ,  
 ),  float, double,  long double  void* (  
-). 
      ,  cin, cout, cerr  clog   .   
        ,  
   -    : 

    cin >> v;        //  v   	
    cout << v;      //  v                     

         ,  -  
 ,    : 

    cout << "Balance == $" << balance << " (dollars)\n";	

               
  - (.   ios    
IOSTREAM.H).   - flags(),  setf()  unsetf()  
   . ,   
       hex  cout 
    - setf()  ios: 

    unsigned v = 12345;	
    cout << "Before:  " << v << '\n';	//    ()     
    cout.setf(cout.hex);			//                 
    cout << "After:    " << v << '\n'	//           

         ,   ,   
    . 

      

       IOMANIP.H (    
   IOSTREAM.H)    
 ,       
  . ,     
hex      : 

    unsigned v = 12345; 
    cout << "In hexadecimal v == " << hex << v << '\n'; 
    cout << "In decimal v == " << dec << v << '\n'; 

         dec   cout  
     .    
  .     
       : 

    cout << "Enter value in hex: "; 
    cin >> hex >> v;	
    cout << "Value in decimal == " << dec << v;	

     . 15.2   -,   
  IOMANIP.H  IOSTREAM.H. 
        . ,  
setbase(int n)        n. ( 
n  ,     ,     
  ANSI      
.)	

     15.2.   -	

    	 

    dec		     
    endl		  ;    
    ends		      
    flush		    
    hex		     
    lock(ios &ir)	     ir   - 


    386      III.   ANSI C++ 


     . 15.2 

    			 

    oct				     
    resetiosflags( long f)	  ,   f
    setbase(int n)		     n (0, 8, 10  16) 
    setiosflags( long f)	  ,   f 
    setfil( int c)		       
    setprecision( int n)	       
                               n
    setw(int n)			   n
    unlock(ios &ir)		     ir  
					-
    ws				   

    -      ostream&, 
 ,    ostream.      
,    IOSTREAM.H     
&ostream. ,         
 : 

    ostream& bell(ostream& os) { 
	    return os << "\a";	// '\a' -    
    } 

     bell()   &ostream,      os  
ostream&.          : 

    cout << bell << "Ding!"; 

       -     
. ,      v  
         : 

    cout.width(8); 
    cout << v << '\n';       //  v    
    cout << v << '\n';       //  v     

     width ,       
 .  -   fill(charc), 
        
 ,  precision(int n),      
   n. 

         

            ,  
int, long, double, char*  ..        <<, 
   ,     ,   
  . 
      15.8      TPoint   
    ,  , ,  
 .       
,       TPoint   , 
    20  23. 

     15.8.  POINTOUT.CPP (   ) 

    1:      #include <iostream.h> 
    2:
    3:	class TPoint { 
    A:	private: 
    5:	   int x,  y; 
    6:	public: 
    7:	  TPoint() { x =  = 0;   } 
    8:	  TPoint( int xx, int yy ) { x = xx;  = yy; }


    15.   C++     387 

    9:	  void PutX( int xx )	{ x = xx;   }                                       
    10:	  void PutY(int yy)	{ y = yy; }                                                                                                   
    11:	  int GetX(void)	{ return x;  }                                         
    12:	  int GetY(void)	{ return y;  }                                        
    13:	  friend ostream& operator<<(ostream& os, TPoint &p);                     
    14:	}; 
    15:				
    16:	main()                                                                 
    17:	{ 
    18:	   TPoint p;		
    19:				
    20:	   cout << p << '\n' ;
    21:	   p.PutX(I00); 
    22:	   p.PutY(200); 
    23:	   cout << p << '\n';
    24:	   return 0; 
    25:	} 
    26: 
    27:	ostream& operator<<(ostream& os, TPoint &p)                                                                                                   | 
    28:	{ 
    29:	  os << "x == " << p.x << ",   == " << p. ;                               
    30:	  return os; 
    31:	} 

      13    - operator<<()
    .  -  
  ostream&,        os  ostream&  
     TPoint. 
      operator<<()    TPoint,    29 
    -    ,   
  .  ,       
  ,    os  ostream   
-    .   ,   
 os.        :	

     == 0,   == 0  == 100,   == 200 

      operator<<()    ostream,   
       . ,  p1, 2  
    TPoint,       : 

    cout << 1 << ";   " << 2 << ";   " << ; 

              
,      .   
    : 

    ((((cout << 1) << ";   ") << 2) << ";   ") << ; 

        ,        ? 
    
         

            >>,    
  C++     .   15.9  
 - POINTOUT.CPP     
   . 

     15.9.  POINTIN.CPP (   ) 

    1:	#include <iostream.h> 
    2: 
    3:	class TPoint { 
    4:	private:	
    5:	   int x, y; 
    6:	public: 
    7:	  TPoint() { x =  = 0; } 
    8:	  TPoint(int xx, int yy){  = ;  = ; } 
    9:	  void PutX(int xx) { x = ; }     
    10:	  void PutY(int yy) {  = ; }   
    11:	  int GetX(void) { return x; }             
    12:	  int GetY(void) { return ; }              
    13:	  friend ostream& operator<<(ostream& os, TPoint &p); 
    14:	  friend istream& operator>>(istream& is, TPoint &p); 
    15:	}; 
    16: 
    17:	main() 


    388      III.   ANSI C++ 

    18:	{ 
    19:	  TPoint p; 
    20: 
    21:	  cout << p << '\n'; 
    22:	  p.PutX(I00); 
    23:	  p.PutY(200); 
    24:	  cout << p << '\n'; 
    25:	  cout << "\nEnter x and  values: "; 
    26:	  cin >> p; 
    27:	  cout << "\n You entered: " << p; 
    28:	  return 0; 
    29:	} 
    30: 
    31:	ostream& operator<<(ostream& os, TPoint &p) 
    32:	{ 
    33:	  os  "x == "  p.x  ",   == "  p.; 
    34:	  return os; 
    35:	} 
    36: 
    37:	istream& operator>>(istream& is, TPoint &p) 
    38:	{ 
    39:	  is >> p.x >> p.y; 
    40:	  return is; 
    41:	}
 
      14   TPoint   -  
  operator>>(),       
,    ,   ostream  istream. 
       ( 37-41)         
 is  istream.    is,    
      . 
               . 
   ,  .    
   26        , 
     27. 

     

            
 ,    c    
   .      , 
   . ,    
 ,       , 
    . 

      

         ,   
 .  ,     
,    : 

    template<class T> void f(T param) 
    { 
       //   
    } 

        template<class >,  
,       . (  
     ,  .) ,   , 
        .    
  (* param)   (& param).    
      : 

    template<class >  f(int ,  b) { 
           //   
    } 

         f()        
     a       b. 
       . ,   
   : 

    double f(int a, double b); 

       ,     . , 
 f()   ,    ,    
   double. 


    15.   C++     389 

       ,       
  c ,     
.   15.10      min()  
m().     ,      .

     15.10.  MINMAX.H (  min  max) 

    1:	// minmax.h --   min   
    2: 
    3:	#ifndef __MINMAX_H 
    4:	#define __MINMAX_H 1 //   #indlude 
    5: 
    6:	template<class >  max(T ,  b) 
    7:	{ 
    8:	  if ( > b)           
    9:	    return a;                                                               
    10:	  else                                                                . 
    11:	    return b;                                                              
    12:	} 
    13:	
    14:	template<class T>  min(T ,  b) 
    15:	{                                                                  
    16:	  if (a < b)                                                 .           
    17:	    return a;          
    18;	  else                 
    19:	    return b;      
    20:	}                                                                      
    21: 
    22:	#endif //  __MINMAX_H 

       6.      , 
     m().  ,  m()
    .   ,    
8-11     ,    ,  
    .   min()   
. 
          .   
m()       : 

     template<class T1,  class T2> T1 max(T1 a,  T2 b) 

        m()     1,   
  :    1,    2.
     ,   ,     
,        
.   15.11    .

     15.11.  FTEMPLAT.CPP (  ) 

    1:	#include <iostream.h> 
    2:	#include "minmax.h" 
    4:	int max(int a,  int b); 
    5:	double max(double a,  double b); 
    6:	char max(char a, char b); 
    7:	
    8:	main()       
    9:	{        
    10:	  int i1 = 100,  i2 = 200;                                           . 
    11:	  double d1 = 3.14159, d2 = 9.87654; 
    12:	  char c1 = 'A', c2 = 'z'; 
    13: 
    14:	  cout << "max(i1, i2) == " << max(i1, i2) << '\n'; 
    15:	  cout << "max(d1, d2) == " << max(d1, d2) << '\n'; 
    16:	  cout << "nax(c1, c2) == " << max(c1, c2) << '\n';
    17:	  return 0;                                                             
    18:	} 

      4-6      m().   
        
 m().   m()       
     .     m()
   14-16.       
 ,   . 


    390      III.   ANSI C++ 


      

          ,  
 .        
  .    ,     
   .   15.12   
 ,          
.      ,      . 

     15.12.  DB.H (  TDatabase) 

    1:	// db.h --    TDatabase 
    2: 
    3:	#ifndef __DB_H 
    4:	#define __DB_H   1               //   include 
    5: 
    6:	template<class T> 
    7:	class TDatabase { 
    8:	private: 
    9:	    *rp;           //    
    10:	   int num;          //   
    11:	public: 
    12:	   TDatabase(int n) 
    13:	       { rp = new T[num = n]; } 
    14:	   ~TDatabase()
    15:	       { delete[] rp; } 
    16:	   void DoNotning(void); 
    17:	    &GetRecord(int recnum); 
    18:	}; 
    19: 
    20:	template<class T> 
    21:	void TDatabase<T>::DoNothing(void) 
    22:	{ 
    23:	} 
    24: 
    25:	template<class T> 
    26:	 &TDatabase<T>::GetRecord(int recnum) 
    27:	{ 
    28:	   *crp = rp; //     =    
    29:	  if (0 <= recnum && recnum < num) 
    30:	     while (recnum--  > 0) 
    31:	        crp++; 
    32:	  return *crp; 
    33:	} 
    34: 
    35:	#endif //   __B_H 


          : 

    template<class T> class TDatabase { 
        // ,      
    }

        ,   . (  
     ,  .)    
   ,  ,   .. TDatabase  
  .        
 : 

    template<class T> 
    class TAnyClass { 
      ....
    } 

         : 

    template<class T1, class T2, class T3> class TAnotherClass { 
      ...       
    }	

             -, 
  - ,     
 . ,   , TDatabase   9 
      r: 

    T *r; 


    15.   C++     391 

            ,    
       .   ,  
TDatabase   12-13      ,   
       num    
. (          
,   .)   15      
   delete[]       
,   . 
      17  - GetRecord(),    
  ,    recnum.     
 ,      . 
    -     ,   
       TDatabase.     
 . ,   -   
   ,       
,     .  DoNothing(), ,   
 ,    ,     
-  : 

    template<class T> 
    void TDatabase<T>::DoNothing(void) 

     -   template<class >.   
    (void),   (TDatabase<T>)  
    (::).     
 (DoNothing(void))   (,   ).   
       -  . 
      25-33     - 
    ,   .  
GetRecord()      .    crp   
       r.  ,  
r    ,     TDatabase.  
 29 ,    recnum   
.  ,  while   30-31    recnum 
      r      . 
        31.      
 ++      ,   
 .   Database    ,   ,  
  r,  ,    r++. , 
       ,   
      r  
 sizeof(T).        
  -  . 
      32     r   
,    ,  r.    
 ,        ,   
   . 
              
,     .   15.13 
    TDatabase. 

     15.13. CTEMPLAT.CPP (  ) 

    1:	#include <iostream.h> 
    2:	#include <string.h> 
    3:	#include "db.h"   
    4:	
    5:	class TRecord {    
    6:	private: 
    7:	   char name[41];                                                          
    6:	public:	
    9:	  TRecord()
    10:	    { name[0] = 0;  }	
    11:	  TRecord(const char *s)	
    12:	    { Assign(s);  }             
    13:	  void Assign(const char *s)                     
    14:	    { strncpy(name,  s, 40);  }     
    15:	  char *GetName(void)	
    16:	    { return name;  }	
    17: 	}
    18:	
    19:	main()      
    20:	{                    
    21:	  int rn;                         //   	
    22:	  TDatabase<TRecord> db(3);       //     TRecords	


    392      III.   ANSI C++ 

    23:	  TDatabase<TRecord*> dbp(3); //      
    24:	  TDatabase<TRecord> *pdb;    //     
    25:	  TDatabase<TRecord*> *ppdb;  //       TRecord , 
    26: 
    27:	  cout << "\n\nDatabase of 3 TRecords\n"; 
    28:	  db.GetRecord(O).Assign("George Washington"); 
    29:	  db.GetRecord(1).Assign("John Adams"); 
    30:	  db.GetRecord(2).Assign("Thomas Jefferson"); 
    31:	  for (rn = 0; rn <= 2; rn++) 
    32:	    cout << db.GetRecord(rn).GetName() << '\n';
    33: 
    34:	  cout << "\n\nDatabase of 3 TRecord pointers\n"; 
    35:	  dbp.GetRecord(0) = new TRecord("George Bush"); 
    36:	  dbp.GetRecord(1) = new TRecord("Ronald Reagan"); 
    37:	  dbp.GetRecord(2) = new TRecord ("Jimmy Carter"); 
    38:	  for (rn = 0; rn <= 2; rn++) 
    39:	    cout << dbp.GetRecord(rn)->GetName() << '\n';
    40: 
    41:	  cout << "\n\nPointer to database of 3 TRecords\n"; 
    42:	  pdb = new TDatabase<TRecord>(3); 
    43:	  pdb->GetRecord(0).Assign("John Adams"); 
    44:	  pdb->GetRecord(1).Assign("Thomas Jefferson"); 
    45:	  pdb->GetRecord(2).Assign("Aaron Burr"); 
    46:	  for (rn = 0; rn <= 2; rn++) 
    47:	    cout << pdb->GetRecord(rn).GetName() << '\n'; 
    48: 
    49:	  cout << "\n\nPointer to database of 3 TRecord pointers\n"; 
    50:	  ppdb = new TDatabase<TRecord *>(3); 
    51:	  ppdb->GetRecord(0) = new TRecord("Dan Quayle"); 
    52:	  ppdb->GetRecord(1) = new TRecord("George Bush"); 
    53:	  ppdb->GetRecord(2) = new TRecord("Walter Mondale"); 
    54:	  for (rn = 0; rn <= 2; rn++) 
    55:	    cout << ppdb->GetRecord(rn)->GetName() << '\n'; 
    56: 
    57:	  return 0; 
    58:	} 

        DB.H ( 3),  
   TDatabase,     TRecord 
       TDatabase.  TRecord  ,  
    -    name.  
TRecord       ,     
TDatabase        . 
      2225        
  .   

    TDatabase<TRecord> db(3); 

    db    TDatabase  
 TRecord   ,    .   
 (3)   db,    TDatabase. 
            ,    
  . ,      
 100      : 

    TDatabase<double> dbd(100); 

      TDatabase      
,    . ,   
   ,      . 
      23-25      
.   23   dbp  TDatabase,    
  TRecord,   24   pdb    TDatabase  
    TRecord.       
,          . 
       TDatabase   ,      
 . ,   28 

   db.GetRecord(0).Assign("George Washington"); 

  GetRecord(0)       0.    
 - Assign()      . 

    15.   C++     393 

      34-55      
TDatabase.    35: 

    dbp.Getfiecord(O) = new TRecord("George Bush"), 

      GetRecord()    ,   
     .      
 TRecord,           , 
     GetRecord(). 
     ,          
       ,   
TDatabase.         , 
  ,  ,      
 . 

    
    ,        .   
   TDatabase,    TRecord. , 
  TDatabase,       . 
          
     ,   
     .	


     

        ,    . 
 ,        , 
        C++     . 

      - 

        -    ,   
,       (.  9). - 
   ,      this.  
 -      . 
      -     . ,  
 TFirstClass    -  : 

    double (TFirstClass::*myfnptr)(void); 

     ,   myfnptr   -  
TFirstClass,        
 .   myfnptr ,   , 
        ,   
 : 

    void (TFirstClass::*myfnptr)(int, int); 

         ,   - 
  myfnptr,    ,    
  .         
,   , . 
      15.14   ,   
     - .	

     15.14. MFNPTR.CPP (  -) 

    1:	#include <iostream.h> 
    2:	#include <iomanip.h> 
    3: 
    4:	class TFirstClass { 
    5;	private:         
    6:	   int count;
    7:	public:         
    8:	  TFirstClass() { count = 0; }
    9:	  int Access(void) ;           
    10:	}
    11:	
    12:	int (TFirstClass::*myfnptr)(void); 
    13: 
    14:	main() 
    15:	{ 


    394      III.   ANSI C++ 

    16:	  int i ; 
    17:	  TFirstClass fc; 
    18: 
    19:	  cout << "\nCall Access the normal way:\n"; 
    20:	  for ( i = 0 ; i < 9 ; i++ ) 
    21:	     cout << setw(8) << dec << fc.Access() ; 
    22:	  cout << "\n\nCall Access via the member function pointer\n"; 
    23:	  myfnptr = &TFirstClass::Access; 
    24:	  for ( i = 0 ; i < 9; i++ ) 
    25:	     cout << setw(8) << dec << (fc.*myfnptr)() ; 
    26:	  cout << "\n\nMember function pointer and a dynamic instance\n"; 
    27:	  TFirstClass *fp = new TFirstClass ; 
    28:	  for (i = 0; i < 9; i++) 
    29:	    cout << setw(8) << dec << (fp->*myfnptr)(); 
    30:	  return 0; 
    31:	} 
    32: 
    33:	int TFirstClass::Access(void) 
    34:	{ 
    35:	  return count++ ; 
    36:	} 

      12 myfnptr    -  
TFirstClass,       . 
     - ,   . 
      main()   17   fc  TFirstClass,  
 21  - Aceess()  TFirstClass   fc 
 . ( Access()     
         - !) 
      25   - Access()   
 myfnptr.      23  myfnptr 
  - Access()  TFirstClass.    
   ( 12)   ( 23)  
,      main(): 

    int (TFirstClass::*myfnptr)(void) = &TFirstClass::Access; 

        -     
 . 
         . 
           . 
    ,  n   ,  f    
TFirstClass,    n   Access(): 

    n = (fc.*myfnptr)(); 

      .*        
    -  .    
 (f.*myfnptr) ,     ()  
  ,  .*. ,  ,   
   

    n = f.Access(); 

       -   ,   
  -. ,    27-29 
  MFNPTR ( 15.14).   fp   
   TFirstClass.    

    (fp->*myfnptr)(); 

       29  - Access()  
,   fp.   ->*   
    .  ,   , 
  ()    ,  ->*.  
    : 

    fp->Access(); 

        - ,    
    . ,    TFirstClass  
   double   balance,       
   : 

    double TFirstClass::*dataPtr; 

     dataPtr      -  
 TFirstClass,      .  
    ,  : 

    dataPtr = &TFirstClass::balance ;	


     15.   C++     395 

              balance 
 

   double TFirstClass::*dataPtr = &TFirstClass::balance;	

     ,  dataPtr     balance  
TFirstClass.   dataPtr      , 
     ,    balance   
  TFirstClass.       
 .  , ,    
 balance: 

    f.balance = 123456 ;
    cout  "\nBalance=" << fc.*dataPtr ;

     fc.*dataPtr  ,    -, 
       .*.   , ,  
   ,     
 . 

      

     -      
      .   
 -     - 
   static. 

   clacc TAnyClass {
   public:
      static void GlobalInit( void ) ;
      ...
   );

    -    .   
-   ,   ,       
   : 

    void TAnyClass::GlobalInit(void) 
    {
      ... // ,    
    }

     -    ,    
  this          - 
.    -     , 
  : 

    TAnyClass::GlobalInit();	//    Globallnit() 

    ,   -,  Globallnit(), 
 ,     TAnyClass. 
          -. ,   

    class TAnyClass {	
    private:	
       static char ;   //  ,   
    public: 
       char GetC(void) { return c; } 
    };	

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

    char TAnyClass:: = 'q'; 

         ,   
- TAnyClass.      TAnyClass, 
 . ,        
- GetC(),   'q':	

    TAnyClass ;	
    cout  .GetC();         //               


        	

             , 
,      .    
     ,     " 
".	
      15.15      .  
        
 ,      ,  
  new,     . 


    396      III.   ANSI C++ 

     15.15.  PERSIST.CPP (    ) 

    1:	#include <iostream.h> 
    2:
    3:	class TPersist { 
    4:	private: 
    5:	   int x, y; 
    6:	public: 
    7:	   TPersist(int a,  int b) { x = ;   = b;   }              //  
    8:	   ~TPersist() { x = 0;   = 0;  }                             //  
    9:	   void *operator new(size_t,  void *p) {  return p;   } 
    10:	   friend ostream& operator<<(ostream &os, TPersist &p); 
    11:	}; 
    12: 
    13:	char object[sizeof(TPersist)]; 
    14: 
    15:	main() 
    16:	{ 
    17:	  TPersist *p = new(object) TPersist(10,   20); 
    18:	  cout << *p << endl; 
    19:	  cout << "Address of object == " << &object << endl; 
    20:	  cout << "Address of *p     == " << &(*p) << endl; 
    21:	  p->TPersist::~TPersist();   //    
    22:	  // p->~TPersist();        //   ++  4.0;     3.1 
    23:	  return 0; 
    24:	} 
    25: 
    26:	ostream& operator<<(ostream& os, TPersist &p) 
    27:	{ 
    28:	  os << "x == " << p.x << ",   == " << p.; 
    29:	  return os; 
    30:	}
 
      TPersist  new   .  
     ,     
    void.     17  new 
     TPersist   , 
   13.  new(object)   
- ,      object. 
   new     
 ,        
    ,      
TPersist   ,    . 
          object    
.   ,       
 . 
        PERSIST   Turbo Debugger. 
    object       
  <F8>.    17  ,    
   10  20;  ,    
new        . 
      21     ,  . 
     ,   
         
   .     
      : 

    p->TPersist::~TPersist();     //    

    C++     ,   ,   
    .  ,   
     Turbo Debugger.    , 
 ,        - 
 ( 8  21). 
      ,       
,      : 

    p->~TPersist();	//    

     Borland C++3.1    .	


    15.   C++     397 


        

        -  . 
,   TAnyClass    -   , 
       : 

    class TAnyClass {	
    private:
       int x, ; 
    public: 
      TAnyClass() { x = 0;  = 0;  } 
      ...
    }

           ,    
  : 

    class TAnyClass { 
    private: 
       int x, ; 
    public: 
       TAnyClass(): x(0), y(0) { } 
       ...
    };
 
        TAnyClass       
  ,         
.         ,   
   -     
. (     .) 
            . 
, ,   TAnyClass  -  TNewClass 
   TNewClass    : 

    class TNewClass { 
       int x; 
    public: 
      TNewClass(int n) { x = n;  }	
      ....
    }

       TAnyClass    TNewClass,  
TAnyClass    .     
  ,      
 ,  : 

    class TAnyClass { 
    private: 
      int x; 
      TNewClass z;            // - 
    public: 
       TAnyClass(int n): z(n) //  z
             { x = n; }       //  x 
      ........
    }
     
      x,   ,   int.  z    TNewClass. 
  TAnyClass     
n,    z    . 
      n    . 
     -,    
,    ,  : 

    class TAnyClass { 
    private: 
       int x;	
       TNewClass a, b, z;          	       //  -  public: 
       TAnyClass(lnt n): a(n), b(n.), z(n) //  ,   z 
           { x = n; } 	                   //   
       ...
    }; 
 

       	

            ,    
: typedef, struct    .     
  ,      .    
        , 
      . 


    398      III.   ANSI C++ 

     typedef        
. ,   TAnyClass typedef    
CLASS_COUNTER     int: 

    class TAnyClass { 
    public: 
       typedef int CLASS_COUNTER;        //   typedef 
	 static CLASS_COUNTER ClassCount;  //   
    public: 
       TAnyClass() { ClassCount++; }
       ~TAnyClass() { ClassCount--; } 
    };

           CLASS_COUNTER  
 ClassCount.      ,   
    : 

    TAnyClass::CLASS_COUNTER TAnyClass::ClassCount; 

           .    
TAnyClass  typedef CLASS_COUNTER,     
 ClassCount,     .    
        
  : 

    TAnyClass *cp1,  *2,  *;     //     . 
    1 = new TAnyClass;	//    
    2 = new TAnyClass;	// 
     = new TAnyClass;	// 
    cout << "There are " << TAnyClass::ClassCount << " objects\n"; 

       3.  ,     
 ClassCount.     .   
  : 

    class TAnyClass { 
    public: 
       struct ClassStruct { 
          int x; int y; 
       }; 
    };

           TAnyClass::ClassStruct: 

    TAnyClass::ClassStruct k; 

    k     ,    : 

       k.x = 1; 
       k.y = 2; 

             
     : 

    class TAnyClass { 
    public: 
       class ClassClass { 
          int x; 
          int y; 
       public: 
          ClassClass() { x = 1;  = 2;  } ;
          int GetX(void) { return x;   } 
          int GetY(void) { return y;   } 
       }; 
    }; 

         TAnyClass      
ClassClass.         : 

    TAnyClass::ClassClass k; 
    cout << "k.x == " << k.GetX() << '\n'; 
    cout << "k.y == " << k.GetY() << '\n'; 

            ,   
 . (       
  :        
     .    ,  
        ,   . 

      fast this 

     Borland C++ 3.1     ,   
  fast this,       
 - .   


     15.   C++  399

 , -     this, 
  ,     -.  
    TAnyCIass 

    TAnyClass anyvar; 

  -  TAnyClass 

    anyvar.AnyFunction();	

     AnyFunction()   this,    
 anyvar.  , this    - 
-  . 
               
this  , Borland C++   this  ,   
,    this       
-     .  ,  
   fast this      
,   ,     
 -,    ,    
- .
      this    si.   this 
    ds:si,      
   ds   .    fast 
this,   DOS,     -. ,  
  : 

     - myprog 

        fast this    
Options\Project\Compiler\Code Generation    FastThis. 
      ,      ,  
   #pragma: 

    #pragma option-po 

     ,    ,   
  -.      
 -    : 

    #pragma option-po


    
         ,    fast this   
    .      
    -   
  si.     ,   
fast this  .     
         
fast this. 


      

        "" ANSI C++,    ,  
    ,      
,  .    C++  
        ,  
   . ,    
,   Borland's Object Windows (OWL),   
  ,    ,   . 

     
              2  
 Mastering Windows Programming with Borland C++ 4. 

            
.         
  . 
             ,    
,   .     
   ,   ,   
   . 
             (throw) 
,    .    
 , ,      . 
(        .) 


    400      III.   ANSI C++ 

              
(catch) ,    . , 
  ,   
 . 
           , 
 (try)    ,   
.  ,       
        , 
   . 

        

     ,        
  C++.   ,    
 ,    . 
        ,     .  
   ,     ,   
 . 
            
. ,   ,      
   . 
            
 throw,  -  : 

    throw 1; 

                
    .       : 

     throw "overflow"; 

      . -      
         
 .      (    const 
char*)   catch: 

    catch  (const char* message) {	
         cout << "Error!  -- " << message << endl; 
        //   ...       .
    }

     catch        
     .       
.      ,    
  catch.       , 
         , 
 .        
       . 
      .   
   . 

     
      ,       
 ,        Borland C++   
 . ,       ,  
 ,    .    
- ,    ,      
 . ,  ,     
 .           
    ,   , ,   
   ?        , 
          
        .

           
  , ,     
       ,   
     ,      
,           
  .      
            
     ,    
 .         
 ,      .


     15.   C++     401 

       

           ?  . 
   ,     
 .      ,    
    . ,     
Overflow,     - ,   : 

    class Overflow{}; 

             
 (,  ): 

    throw Overflow(); 

          Overflow,   
     . -     
       catch: 

    catch (Overflow)  { 
        cout << "Overflow detected!" << endl; 
    }	

        Overflow    
 .     -  
( ). 
     ,    throw    
Overflow,    catch     
(    ,  ).  ,     
,     ,  , 
     : 

    catch (Overflow overObject) { 
       // ... 
     }

          catch   overObject.   
  catch    overObject   ,  
  ,   . 
    ,       
-,       catch.  :	

    class Overflow { 
       void Report()  { cout << "Error: overflow" << endl; } 
    };

      Overflow  - Report(),   
  (      , 
      -).   catch  
 Report()        : 

    catch (Overflow overObject)	{ 
       overObject.Report();	//   Report            
    }                    


        

            
      .  
 ,      
  : 

    int AnyFunction() 
    { 
       if (conditionA)        //    condition* 
         throw "Big trouble!";  //    
      if (conditionB)        //    conditions 
        throw Overflow();     //    Overflow 
     return 123;	//   ,   
   } 

      conditionA  (    ),  
      "Big trouble!".  
  conditionB,     Overflow, 
           
.      ,    
,   123 ,   .	


    402      III.   ANSI C++ 

          . 

              
  ,    
 . 
              
 ,     throw.	
           ,  
      . 

       .  AnyFunction()   
 , ,    ,  
     Overflow.   catch  
   .     
,   ,  ()  
,    ,     . 
             
   catch. ,    
   Error      catch   : 

    catch (Error e) { 
        //      Error 
    }
    catch (const char* message) { 
       //     
    }


       try 

              try, 
, ,  ,     .   
      ,   
  try: 

    try { 
       int x = AnyFunction();	//   
    } 
    catch (Error e)  {  //      
       e.Report();	//   Report  Error 
       exit(-1);        //     
    }

     try     ,   
   .     catch  
   try.    ,   , 
  ,     .   , 
  try       catch, 
   ,   . 
      try     catch   
 ,         
 . ,  try     try  
 catch    , ,    
Function()   ,   
   try: 

    try { 
       FunctionA();     //       
       try {            //   try,    
                        //  
          FunctionB();  //       
       } 
       catch (Error e) {   //   ,  
                           //  FunctionB()
          e.Report();      //      
                           // Report{) 
       } 
    } 
    catch (Error e) {  //   ,  
                       //  FunctionA()
       e.Report();     //      
                       // Report() 
    }
 

     15.   C++     403 


      try 

        ,    
 try    .   try     
(   ):	

    try {	
        cout << " we go!" << endl;	
        int  = AnyFunction();	
        cout <<  "x == " << x << endl;	
    }	

      try   .      
AnyFunction(),      .  
  AnyFunction   ,  try  
.  ,       
      .	

     
       ,      
 AnyFunction()    ,   
    . 

      try       catch ( 
    try).     ,  
    ,    
,  AnyFunction(): 

    try { 
       cout << " we go!" << endl, 
       int x = AnyFunction();	
       cout << "x == " << x << endl; 
     }	
     catch (char* message) { 
       cout << "Error1  -- " << message << endl;
       exit(-1);   //  
    }
    catch (Overflow) { 
       cout << "Overflow!" << endl, 
       exit(-2);   //  
    } 
   //         

             
AnyFunction().    ,    
 ,         
 .     ,   catch 
,       
,     . ,   AnyFunction()
  ,   try  
,      
catch, a    ,     ,  
   . 
         catch    exit()
   .       
  ,        
.	
       , ,     catch   
try      .    
  catch        
Overflow.     ,   
 catch,     . , , 
  AnyFunction      
NewException.    g()     ,   
      g(),      
     char*  Overflow. 
      ,     catch      
  ,    ,   
     .   
 ,     catch    
   

   
         
    . ,   
    C++  ,   ,  
,  -1   ,      
.      ,   
   


    404      III.   ANSI C++ 

 .  ,    .  
     o  ,   
      .   
  ,   -  ,  
        . 
        
        
       
 . 


        

      EXCEPT.CPP ( 15.16)   
  ANSI C++.      
  DOS  WINDOWS.   DOS   bcc except,  
          
EXCEPT.EXE.         
   EasyWin-. 

     
        .  ,   
  ,      
"Unreachable code at line 78" (    78).    
 throw  .    ,   
(,    ,      ),  throw 
  78    Error      
.   .   ,  
Error  ,      . 


     15.16.  EXCEPT.CPP (   ANSI C++) 

    1:	//    ANSI C++ 
    2: 
    3:	#include <iostream.h> 
    4:	#include <math.h> 
    5: 
    6:	//   
    7:	void Instruct(); 
    8:	double Pow(double b, double e); 
    9:	double Power(double b, double e); 
   10: 
    11:	//    
    12:	class Error { 
    13:	  double b;   //  
    14:	  double e;   //   
    15:	public: 
    16:	   Error() 
    17:	        { cout << "Error in source code!" << endl; } 
    18:	   Error(double bb, double ) 
    19:	       : b(bb), e(ee) { }
    20:	   void Report(); 
    21:	};  
    22: 
    23:	int main() 
    24:	{ 
    25:	  Instruct(); 
    26:	  try { 
    27:	    double base, exponent, result; 
    28:	    cout << "base? "; 
    29:	    cin >> base; 
    30:	    cout << "exponent? "; 
    31:	    cin >> exponent; 
    32:	    result = Power(base, exponent); 
    33:	    cout << "result == " << result << endl; 
    34:	  } 
    35:	  catch (Error& e) { 
    36:	    e.Report(); 
    37:	     return -1; 
    38:       } 
    39:       return 0; 
    40:   } 
    41: 


     15.   C++     405 

    42:	void Instruct() 
    43:	{ 
    44:	  cout << "Power Demonstration\n\n";             
    45:	  cout << "This program displays the result of raising\n"; 
    46:	  cout << "a value (base) to a power (exponent). To\n"; 
    47:	  cout << "force an exception, enter a negative base\n"; 
    48:	  cout << "and a fractional exponent (e.g. -4 and 1.5)\n"; 
    49:	  cout << "Or, enter a zero base and an exponent less tnan\n"; 
    50:	  cout << "zero.\n\n"; 
    51:	} 
    52: 
    53:	//     
    54:	double Pow(double b, double e) 
    55:	{ 
    56:	  return exp(e * log(b) ); 
    57:	}
    58: 
    59:	//  b     
    60:	double Power(double b, double e) 
    61:	{ 
    62:	  if (b > 0.0) return Pow(b, e); 
    63:	  if (b < 0.0) { 
    64:	   double ipart; 
    65:	   double fpart = modf(e, &ipart); 
    66:	   if (fpart == 0) { 
    67:	      if (fmod(ipart, 2) != 0) //  , ipart -   
    68:	          return -Pow(-b, e); 
    69:	      else 
    70:	         return Pow(-b, e); 
    71:	   } else 
    72:	      throw Error(b, e); 
    73:	  } else { 
    74:	    if (e == 0.0) return 1.0; 
    75:	    if (e < 1.0) throw Error(b, e); 
    76:	    return 0.0; 
    77:	  } 
    78:	  throw Error(); //      
    79:     } 
    80: 
    81:	//  ,    
    82:	void 
    83:	Error::Report() 
    84:	{ 
    85:	  cout << "Domain error:" 
    86:	    << " base:" << b 
    87:	    << " exponent:" << e 
    88:	    << endl; 
    89:	} 

     EXCEPT   Power(),    
     . ,   
      . 

            . 
           . 

            
.         
(),    ( )   
   (  ). 
         ,  Power 
  ,    Error. 
,          
Power   ,    Error, 
  b  : 

    throw Error(b,  e); 

       main().  Power    try,  
   : 
    try {       
        double base, exponent, result    
        // ...      
        result = Power(base, exponent); 
        cout << "result == " << result << endl; 
    } 


    406      III.   ANSI C++ 

      Power   ,  
 result     ,   try 
 .  catch      try: 

    catch (Errors e) {	
       e.Report(); 
       return -1; 
    } 
    return 0; 

      Power   ,    
Error   .   catch  - 
Report  Error     ,  
,      Power. 

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

    ,       
.       -1,   , 
 0,    .     ,  
  DOS  Windows    . 
     ,       
  ,    ,   
 ,   new  .      , 
      ,     
     ,  
 . 

       

              
 ,      .  
     ,      
   . ,  AnyFunctionO   
     : 

    void AnyFunction() throw(Error); 

           (   ) 
 throw() ,   AnyFunction()   
   Error.    , 
  AnyFunctionO      
 .    ,   ,   
    throw     ,   
    .     ,  
 AnyFunction()      Error. 
      ,     
,       , 
       : 

    void AnyFunction() throw(Error, char*, OtherType); 


        

       EXCEPT.CPP    
 ,      . 
,  ,  ,     
,  ,      
.      ,  
 ,     ,  , 
   ,    
 .     15.17,    
     ( DOS    EasyWin-). 
       .  . 15.1 
   . 


     15.   C++     407 


    Power Demonstration 

    This program displays the result of raising 
    a ualue (base) to a power (exponent). To 
    force an exception, enter a negative base 
    and a fractional exponent (e.g. -4 and 1.5) 
    Or, enter a zero base and an exponent less than 
    zero. 

    base? -4 
    exponent? 1.5 
    Domain error: base:-4 exponent: 1.5 
    Error detected: Try again! 
    base? 8 
    exponent? -9.99 
    Domain error: base:8 exponent:-9.99 
    Error detected: Try againI 
    base? 2 
    exponent? 16 
    result == 65536 
    Program is ending normally. 

    . 15.1.    2 


     15.17. 2. (     ) 

    1:	//        
    2: 
    3:	#include <iostream.h> 
    4:	#include <math.h> 
    5: 
    6:	//     ,  
    7:	//  ,       Power() 
    8:	Class Error; 
    9: 
    10:	//   (     Power)   
    11:	void Instruct();	
    12:	void Run(); 
    13:	double Pow(double b, double e); 
    14:	double Power(double b, double e) throw(Error); 
    15:		
    16:	//       
    17: 	class Error {
    18:	   double b;  // Base
    19:	   double e;  // Exponent	
    20:	public: 
    21:	   Error() 
    22:	        { cout << "Error in source code!" << endl; } 
    23:	   Error( double bb, double	) 
    24:	            : b(bb), e(ee) { } 
    25:	   void Report(); 
    26:	}; 
    27: 
    28:	int main () 
    29:	{ 
    30:	  Instruct(); 
    31:	  for (;;) { 
    32:	     try { 
    33:	        Run(); 
    34:	        cout << "Program is ending normally. " << endl; 
    35:	        return 0; //       
    36:	    } 
    37:	    catch (...) { 
    38:	       cout << "Error detected: Try again!" << endl; 
    39:	    } 
    40:	  } 
    41:	} 
    42: 
    43:	void Instruct()
    44:	{ 


    408      III.   ANSI C++

    45:	  cout << "Power Demonstration\n\n"; 
    46:	  cout << "This program displays the result of raising\n"; 
    47:	  cout << "a value (base) to a power (exponent). To\n"; 
    48:	  cout << "force an exception, enter a negative base\n"; 
    49:	  cout << "and a fractional exponent (e.g. -4 and 1. 5)\n"; 
    50:	  cout << "Or, enter a zero base and an exponent less than\n"; 
    51:	  cout << "zero.\n\n"; 
    52:	} 
    53: 
    54:	//   (   main ()) 
    55:	void Run() 
    56:	{ 
    57:	  try { 
    58:	     double base, exponent, result; 
    59:	     cout << "base? "; 
    60:	     cin >> base; 
    61:	     cout << "exponent? "; 
    62:	     cin >> exponent; 
    63:	     result = Power(base, exponent); 
    64:	     cout << "result == " << result << endl; 
    65:	  } 
    66:	  catch (Error& e) { 
    67:	    e.ReportO; //     
    68:	    throw e; //       
    69:	  } 
   70:	} 
   71: 
   72:	//   Power 
   73:	double Pow(double b, double e) 
   74:	{ 
   75:	  return exp(e * log(b)); 
   76:	} 
   77: 
   78:	//  b    
   79:	double Power(double b, double e) throw(Error) 
   80:	{ 
   81:	if (b > 0.0) return Pow(b, e); 
   82:	if (b < 0.0) { 
   83:	   double ipart; 
   84:	   double fpart = modf(e, &ipart); 
   85:	   if (fpart == 0) { 
   86:	      if (fmod( ipart, 2) != 0) // .o. ipart -  
   87:	         return -Pow(-b, e); 
   88:	      else 
   89:	        return Pow(-b, e); 
   90:	    } else 
   91:	       throw Error(b, e); 
   92:	 } else { 
   93:	   if (e == 0.0) return 1.0; 
   94:	   if (e < 1.0) throw Error(b, e); 
   95:	   return 0.0; 
   96:	  } 
   97: 	} 
   98:
   99:	//  - Report ()  Error() 
   100:	//  ,    
   101:	void 
   102:	Error::Report() 
   103:	{ 
   104:	  cout << "Domain error: " 
   105:	  << " base:" << b 
   106:	  << " exponent: " << e 
   107:	  << endl; 
   108:	} 

             
. -,         8. 
     ,       
  . ,  Power()   throw  
  ,        
 Error (    ). 
           Error . 
   ,     . 

     15.   C++     409 


    
              
       . , ,  
       
,  EXCEPT2.CPP. 

           main()  
""  for  : 

    for (;;) { 
       // ..    " " 
    } 

          ,    return 
  ,  .     
      . 
      for,   try,     catch, 
  Run(),   .    
      try,   
Run(),    catch     : 

    try { 
      Run(); 
      //     return 
    } 
    catch (...)  { 
       //       
    } 

       Run()    ,  
,     .      
: ,  return       
,  exit().	
       catch    ,   
   ,    . 
       Run() (   
 ,   main()).    , 
          
     . 
       Run()       main().  
    try   Pow() ( ,   
,  Power())      
. ,  catch  : 

    catch (Error& e) 
      e.Report();	//     
      throw e;	//      main() 
    } 

      Power()   ,   catch 
 - Report()     .  
   catch     .  
      Run()   
  ,    .   catch ( 
   )   main()     
,   return,  ,    
.	

       

       ,     
 ,    : "  ,  
     ?"    
.        
 (       )   , 
      catch     
    .  
 ,        
  ,       
C++,   .     
unexpected(), terminate()  abort().       
. 

    410      III.   ANSI C++ 

         ,   
  catch,    unexpected(). 
 ,       catch, 
   . -  
unexpected()   terminate(),    . 
           
      ( , 
     ),   
  terminate().    terminate()  
 abort(),    . 
     abort()         
  .     . 
          
     unexpected() , , 
terminate(),       
    abort().      , 
       . (  ,  
  ,     !) 

     
         Borland C++  terminate   
  abort(),    ANSI C++.  Borland C++ 4.5  
termiate()   .       abort(), 
      ,    
,      terminate()  abort().

      ,  ,      
  ,   2.   
 (  .).     main()  : 

    int main() { 
      Instruct(); 
      Run(); 
      return 0; 
   } 

    Pow Demonstration	
    rhis program displays the result of raising	
     value (bass) to a power (exponent). To	
    Force an exception, enter a negative base	
    Domian error: se

     Program Aborted

    . 15.2. Windows-  EasyWin-   ,   
 -    

          Run()  
   unexpected(),   terminate(), 
,   ,   abort(),   
.   DOS,   ,  
 "Abnormal program termination" (  ). 
WINDOWS  EasyWin- ,   ,  
 . 15.2.       ,  
        
. 


     16.   C++ 411 

        unexpected()  terminate()    
   . ,   
  unexpected()      
      .  
       .   
   terminate()       
  ,  ,     
    . 
      abort()   .   abort()   
     "", ""  " -". 

      unexpected()  terminate() 

         -  
     set_unexpected(), 
    EXCEPT.H.     
unexpected_function,       . 
           
 ,      catch   
 ,     . 
         -  
    set_terminate(),    
  ..     terminate_function,  
     . 
       set_unexpected()  set_terminate()    
 -.       
 ,     ,     
 . ,    -: 

    void unexpectedHandler(); 

     , ,    (   
 ).       : 

    set_unexpected(unexpectedHandler); 

        ,      
-,    : 

    unexpected_function savedAdress;	//    
    savedAdress = set_unexpected(unexpectedHandler);	//   
    // ...      
    set_unexpected(savedAdress);    //    
    // ...      

      terminate()  ,  
 set_unexpected()   set_terminate(),   
 terminate_function. 
      15.18     
   .     
       
,        
.         DOS- 
 EasyWin-. 

     15.18.  UNEXPECT.CPP ( - unexpected()  
terminate()) 

    1:	#include <iostrearo.h> 
    2:	#include <except.h> 
    3: 
    4:	#define MAXERR 10 
    5: 
    6:	// , ,      
    7:	class HaxError 
    8:	{ 
    9:	);
    10: 
    11:	//     
    12:	class Error {      
    13:	public:      
    14:	  Error();    // 
    15:	  void Say();  // ,     
    16:	private: 
    17:	  static int count; //      /    
    18:	}; 
    19:	
    20:	//  ,   
    21:	void Run() throw(Error);                        
    22:	void trapper(); 


    412      III.   ANSI C++ 


    23:	void zapper(); 
    24: 
    25:	//       Error 
    26:	int Error::count; 
    27: 
    28:	void main()
    29:	{ 
    30:	  set_unexpected(trapper); 
    31:	  set_terminate(zapper); 
    32:	  for (;;) 
    33:	  { 
    34:	     try { 
    35:	       Run(); 
    36:	     } 
    37:	     catch (Error e)  { 
    38:	        e.Say(); 
    39:	     } 
    40:	  } 
    41:	} 
    42: 
    43:	// ,    
    44:	void Run() throw(Error) 
    45:	{ 
    46:	   // throw ErrorO; 
    47:	   throw "An unknown exception object"; 
    48:	} 
    49: 
    50:	//     
    51:	void trapper()
    52:	{ 
    53:	  cout << endl << "Inside trapper function" << endl; 
    54:	  throw Error(); 
    55:	} 
    56: 
    57:	//     
    58:	void zapper() 
    59:	{ 
    60:	  cout << endl << "Inside terminate function" << endl; 
    61:	  cout << endl << "Exiting program with error code -1" << endl; 
    62:	  exit(-1); 
    63:	} 
    64: 
    65:	//   Error 
    66:	Error::Error() 
    67:	{ 
    68:	  count++; 
    69:	  if (count > MAXERR) 
    70:	     throw MaxError(); 
    71:	} 
    72: 
    73:	//   Error,   
    74:	void Error::Say() 
    75:	{ 
    76:	  cout << "Error: count = " << count << endl; 
    77:	}
 
      UNEXPECT       
.  . 15.3    . 
          .   
rrr,     , ,    
  MAXERR,      10.   
Error   ,     . 
      Error,     , 
    count.   count  
,      ,    
  ,    .   count  ,  
          
.  -  Error     
  . - Say()    count. 
          Error,   count    
 MAXERR,      
.        , 
    Error  (     
 ). 


     15.   C++     413 

    Inside trapper function 
    Error: count = 1

    Inside trapper function 
    Error: count = 2 

    Insade trapper function 
    Error: count = 3 

    inside trapper function 
    Error: count = 4	

    Inside trapper function 
    Error: count = 5

    Inside trapper function 
    Error: count = 6 

    Inside trapper function 
    Error: count = 7

    Inside trapper function 
    Error: count = 8 

    Inside trapper function 
    Error: count = 9 

    Inside trapper function 
    Error: count = 10 

    Inside trapper function 

    Inside terminate function 

    Exiting program with error code -1 


    . 15.3.    UNEXPECT 


      main()  -,  
    unexpected()  terminate().     
 ""  for,   ,     . 
    try   Run(),   catch  
   Error,   Run().    
 catch         
- Say()  . 
     Run()    ,  
  ,       
 .     
 trapper()  ,     . 
   . 5.3,     10  
. 
            
    .     
     Error: 

    throw Error(); 

           
   ,    10  ,   
    Error     
      MaxError (.  
 Error).        catch,  
    zapper.     
  . (    
       return).	


    414      III.   ANSI C++ 

       ,       , 
    .   , 
        
,      .    
  ,      
  . 
      ,       
 ,   Run() ,    
    . ,   
    : 

    void Run() throw(Error) 
    { 
      throw "An unknown exception object"; 
    }

         ,   
 trapper()      , 
  Run().   ,    
 catch      const char*  
char*.  trapper(),   ,    
 ,    ,   , 
Error.       
   catch   main(),   
  . 
      ,       ,  
      Error.   
        
 exit(). 

         

          ,   
,     , .. 
   .    
   -xd   .    
   Options\Project...,   C++ Options 
   Exception handling\RTTI. (RTTI  Runtime Type Information, 
    ). ,    
Enable exceptions  Enable destructor cleanup. 
    
    
          
 ObjectWindows- (OWL).

          ,  

        , 
           . 

      ,       . 
      AnyClass  : 

    int AnyFunction() 
    { 
      AnyClass object(123); 
       // ... 
       if (condition) throw Error(); 
       return object.Value(); 
    )

          AnyClass.    
,      . ,  
   ,    , 
        , ,   
      . 
      -xd      
   ,       
object         
 .    ,   
    ,   ,    
     ,    
 . ( ,       
     ,  OWL.) 

     15.   C++     415 

     ,       . 
    ,       
   : 

    int AnyFunction()
    { 
      AnyClass *p = new AnyClass(123);
      // ... 
      if (condition) {
        delete p;	//         
        throw Error();      //    AnyFunction 
      } 
      delete p;	//  ,   condition  
      return 123;	//  .  ,  
                                    //   
    } 

       delete   ,  
      : 

    int AnyFunction() 
    { 
      AnyClass *p = new AnyClass{123); 
      // ... 
      delete p; 
      if (condition) throw Error(); 
      return 123; 
    }

     ,  condition,    , 
    ,     
. 

        

           
  ,      .     C++   
       ,  
  -,        , 
    . 
           
   throw,       
: 

    class AnyClass { 
    public: 
      AnyClass()
         { if (condition) throw Error(); } 
     ~AnyClass() { } 
   }; 

         ,    
  ,     
,    e .   
       ,  
    ,    . 
  : 

    class AnyClass { 
       OtherClass x; 
    public: 
       AnyClass(): x(123) 
          { if (condition) throw Error();  } 
          ~AnyClass() { } 
    }; 

       OtherClass   AnyClass  ,  
     .   OtherClass 
  ,     
.     ,   AnyClass 
 , , ,  AnyClass  .   
     ,   : 

    class AnyClass { 
      OtherClass a, b, ; 
    public: 
      AnyClass: a(), b(), c() { } 
      ~AnyClassO { } 
    }; 


    416      III.   ANSI C++ 

       b   ,    
,     AnyCIass.      
 (      ,  b  
 ),     ,   AnyCIass  
. 

       

            .  
    -,    Raise(). , 
       : 

    Class Error { 
       void Raise()
	{ throw Error();  }  
    }; 

         Error,  

    e.Raise();	//     Error 

  ,       
.    ,   Error,  
        , 
         : 

    throw ;	//       


          

      ,  new   , 
      ,      
 C++,    null ()    . 

     
        new  no-,      
,       NEW.H     main()
 :

     set_new_handler(0) ;


     , ,    : 

    AnyCIass *p;		//    AnyCIass 
     = new AnyClass();	//    AnyCIass,   
    if ( == 0} Error();		//  ,     

        new      
AnyCIass    ,  ,  new    
    .   ,   
    new    . 
          new.  
   op new     
xalloc,     E.  : 

    class xalloc : public xmsg { 
    public: 
        xalloc(const string &msg, size_t size), 
        size_t requested() const; 
        void raise() throw(xalloc); 
   private: 
        size_t siz; 
   }; 

          new  
,  ,  try,      
catch    xalloc. ,    : 

    try { 
       = new AnyClass(); 
      // ...  ,   
    }
    catch (xalloc ) { 
      cout << "Memory error:  << x.why() << endl; 
      exit(-1);    
    }


    15.   C++ Borland C++4.5     417 

     xailoc      xmsg,    
  EXCEPT.H. -     
string (     ).     ,  
       catch,   
- why().       
      x.requested(). 
          try   main()   
  . ,   main()   
 :   

    void main()	
    {	
       for (;;) {  
          try { 
            Run();	//   
          } 
          catch (xailoc x) { 
            cout << "Out of memory" << endl; 
            exit(-1);        //       
         } 
      }
   }

             Run(), 
 .       
 Run() (   ,   Run())    
main(),         catch. 
      15.19    MEMERR   12, 
       new 
 ""    .     
    DOS-  EasyWin-. 

     
      MEMERR    ,    
         .  
  ,  MEMERR       DOS, 
Windows   . (     
.)   ,     
.       . 


     15.19. MEMERR.CPP (    new) 

    1:	#include <iostream.h> 
    2:	#include <new.h> 
    3:	#include <except.h> 
    4:	#include <cstring.h> 
    5:	#include <stdlib.h>	
    6: 
    7:	struct q { 
    8:	  int ia[1024];	// 2048-  
    9:	}; 
    10: 
    11:	char *pool;	//     
    12: 
    13: 	int main() 
    14:	{	
    15:	 struct q *qp;	//    q 
    16: 
    17:	try {	
    18: 	   pool = new char[512]; 	//             
    19:	}
    20:	catch (xalloc) {	
    21:	  cout << "Not enough RAM to run program" << endl;   
    22:	  exit(-1); 
    23:	} 
    24: 
    25:	for (;;) {	//    " " 
    26:	 try {	//  try     
    27:	     qp = new q;	//     q 
    28:	      cout << qp << '\n	';//    
    29:	 } 
    30:	 catch (xailoc x) {	//     new  
    31:	   delete pool;	//                  
    32:	   cout << "Error: " << x.why() << endl; //       
    33:	   exit(-1);	//   (  ) 


    418      III.   ANSI C++ 

    34: 	    } 
    35: 	  } 
    36: 	  return(0); //      "Unreachable code" 
    37:	} 
    
      MEMERR ,      
    .    ""  
 2048-   q   ,   new  
   xalloc.  catch,   
,   why() (   xmsg)   
   . 
       ,       
  ,       32. 
,    ,     
   -    ,  
      .   
   why()         
   . 
             
  .       
   ,      
     .      
  ,     ,  
          
  . 

     
        ,      ,  
    512   1  2,     
  ,         
 General Protect Fault (GPF) (  )    . 
      


        

       ANSI C++        
  ,   :       
   .        
     ,     
    ANSI     C++   ,   
  .          
 sizeof (,       ), 
      ,   
     . 
     typeid    Type_info,    
.    typeid      
,  int  double,    , ,    
  .      typeid  
     (   )   . 
      typeid       
TYPEINFO.H.     Type_info,    
 -,     .    , 
name(),      .    
 : 

    int count; 
    cout << typeid(count).name() << endl; 

       int      count.  
       : 

    int i; 
    int j;
    if (typeid(i) == typeid(j)) 
       cout << "i and j are the same types' << endl; 

         !=      . 

    if ( typeid( i ) != typeid( j ) ) 
       cout << "i and j are not the same types" << endl, 

         before()      
 (    ,   
   ,   name() ): 

    if ( typeid(i).before(typeid(d) ) ) 
       cout << "i's type name is lexically before d's type name"; 


    15.   ++    419 

        typeid      Type_info 
(,    ),   
   Bad_typeid.    
typeid    catch,   . 

     
         Borland,  
,   typeid      
  .  ,   - 
 .

      15.20    typeid.   
      DOS-  EasyWin-. 
(       ,   
  .) 

     15.20.  TYPEINFO.CPP (  typeid) 

    1:	#include <iostream.h> 
    2:	#include <string.h> 
    3:	#include <typeinfo.h> 
    4:		
    5:	class AnyClass { 
    6:	public: 
    7:	   AnyClass(int c) { count = c; } 
    8:	private: 
    9:	   int count; 
    10:	}; 
    11: 
    12:	#pragma warn -use 
    13: 
    14:	int main() 
    15:	{ 
    16:	   char c; 
    17:	   int i; 
    18:	   double d; 
    19:	   AnyClass a(1); 
    20:	   AnyClass *pa = new AnyClass(2); 
    21: 
    22:	   cout << "Name  " << typeid(c).name() << endl; 
    23:	   cout << "Name  " << typeid(i).nameO << endl; 
    24:	   cout << "Name  " << typeid(d).name() << endl; 
    25:	   cout << "  " << typeid(a).name() << endl; 
    26:	   cout << "Name  " << typeid (pa).name() << endl; 
    27:	   cout << "Name  " << typeid (* pa).name() << endl; 
    28:	   cout << "Name  " << typeid(typeid(i)).nane()<< endl; 
    29: 
    30:	   cout << endl; //     
    31: 
    32:	//       , 
    33:	//     ++ 4. 
    34:	/* 
    35:	   if (typeid(c) != typeid(d)) 
    36:	      cout << typeid(c).name() << "	and " 
    37:	      << typeid(d).name() << "are not equivalent." << endl; 
    38:	*/ 
    39: 
    40:	// ,   name()    
    41:	//    .    .    
    42: 
    43:	  if (typeid(c) != typeid(d)) {
    44:           cout << typeid(c).nante() << " and cout << typeid(d).name() << " are not equivalent." << endl; 
    45:       }
    46:
    47:
    48:     //           
    49:      if ( typeid( a ) == typeid( *pa ) ) 
    50:         cout << "a and *pa are objects of the same types" << endl; 
    51:
    52:    //     Bad_typeid 
    53       try { 
    54:        cout << endl; 
    55:        cout << "Using a dereferenced null pointer typeid" << endl; 
    56:        int *p; // Define a pointer 
    57:        p = 0;  // Assign null to the pointer 
 

    420      III.   ANSI ++

    58:	   cout << "Name : " << typeid(*p).name() << endl ; 
    59:	   cout << "Borland C++ bug. This SHould not appear. " << endl; 
    60:	   p = new int; // Never executes 
    61:	   delete p;   // Never executes
    62:	 } 
    63:	 catch (Bad_typeid x) { 
    64:	    cout << "Error: typeid exception thrown" << endl; 
    65:	    // ... exit program or take other action here 
    66:	 } 
    67: 
    68:	 delete pa; 
    69:	 return 0; 
    70:    } 

        .   
  ,         AnyClass: 

    cout << "Name :  " << typeid(pa).name() << endl; 
    cout << "Name :  " << typeid(*pa).name() << endl; 

    typeid       AnyClass*. 
      *    
AnyClass. 
        Type_info    , 
  typeid: 

    cout << "Name :  " << typeid(typeid(i)).name() << endl; 

       Borland C++     name()  
     .     
   : 

    if (typeid(c) != typeid(d)) 
        cout << typeid(c).name() << " and " << typeid(d).name() << " are not equivalent." << endl; 

         "char and double are not 
equivalent",     "char and char are not equivalent".  
     name()   
    d    . 
      , ,  
   : 

    if (typeid(c) != typeid(d)) { 
       cout << typeid().name() << " and "; 
       cout << typeid(d).name() << " are not equivalent." << endl; 
    } 


     
             .  
   ,    .


     

             ,  , ,  
   . 
            .    
  ,           
  . 
               
      C++   . 
           ,  
 C++.      ,  
 (/)   (*),   ,    (-)   
(~).       , 
        ,
  . 
       new  delete  ,      
       . 
        new  ,     
 .         
 set_new_handler(). 
            ,  , 
         ,  
          
  . 
            ,  , 
     ,   
   . 
    

     15.   C++ 421 

       -  ,    ios, 
      IOSTREAM.H.  
   -  cin, cout, cerr  clog.  ios  
       (<<)     (>>)  
      C++. 
          -,         
iomanip.h  iostream.h,        
 -.       . 
            -  
        .   
          -. 
             , 
     . 
        C++,    ,      
-,  -,        
,     ,    
,       fast this. 
        ,   
  .    
    ,       
.        
 ,   ,   try  
       catch. 
                       , 
    ,     . 
            ,   
   unexpected().   unexpected()  
 terminate(), ,   ,   abort()  
  .     unexpected()      
terminate()  -      .   
  abort() . 
          typeid      
  .     Type_info,   
   .      typeid  
  . 

     

    15.1.  ,     . 
  ,    TEngine (),    
TFuel (),         
level,   ""     .   
    ,   TEngine   
    level  TFuel. 
    15.2.      (*)   (/)  
 TStrOp   15.5. 
    15.3.     ++  --   TStrOp  
 15.5.       . 
    15.4.        TStrOp 
  double   15.5. 
    15.5.     TFruit ()   orange ()  
TFruit,  orange       
grapefruit (). 
    15.6.       
 TFruit   15.5. 
    15.7.       TFruit  
 15.5.	
    15.8.  ,      min()  
 15.10.
    15.9.   ,  100     
   TDatabase   15.12.    for  
        8- . 

     III.   ANSI C++ 422
