     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:	aBuddy.ShowValues(); 
    26:	return 0; 
    27:	} 
    28: 
    29:	void Buddy: :Showvalues(void) 
    30:	{ 
    31:	Pal aPal(1234); 
    22: 
    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:	} 


     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: } 


     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 &d) 
    31:	{ 
    32:	  cout  c1.s1  s2  '\n'; 
    33:	} 


     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)); 
    38: 	}
    39: 
    40:	long operator-(TStrOp a, TStrOp b) 
    41:	{ 
    42: 	  return (atol(a.value) - atol(b.value)); 
    43:	} 


     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"; 
    21:	cout  "\nValue of a == "  a.GetValueO; 
    22:	cout  "\nValue of b == "  b.GetValueO; 
    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:	} 


     15.6. SSOP.CPP (  ) 

    1:   #include <iostream.h> 
    2: 
    3:   class PseudoArray { 
    4:	private: 
    5:	  int valueO; 
    6:	  int value1; 
    7:	  int value2; 
    8:	  int valueS; 
    9:	public: 
    10:	  PseudoArray(int v0, int v1, int v2, int v3) 
    11:	  { value0 = v0; value1 = v1; value2 = v2; values = 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  "J == "  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; // " 
    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:	} 


    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"; 
    25:	   BrandNew *2 = new BrandNew; 
    26:	   BrandNew *b3 = new BrandNew; 
    27:	   BrandNew *54 = new BrandNew; 
    28:	   BrandNew *5 = 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:	}


     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; }
    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:	} 


     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	XI }             
    12:	  int GetY(void) { return	; }              
    13:	  friend ostreamS operator(ostream& os, TPoint &p); 
    14:	  friend istream& operator(istream& is, TPoint &p); 
    15:	}; 
    16: 
    17:	main() 
    18:	{ 
    19:	TPoint p; 
    20: 
    21:	cout  p  An'; 
    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:	ost ream& operator(ostream& os, TPoint &p) 
    32:	{ 
    33:	  os  "x == "  p.x  ",   == "  p.; 
    34:	  return os; 
    35:	} 
    36: 
    37:	istreamA operator(istream& is, TPoint &p) 
    38:	{ 
    39:	  is  p.x  p.y; 
    40:	  return is; 
    41:	}


     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 


     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,  12 = 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) == "  raax(d1,  d2)  '\n'; 
    16:	  cout  "nax(c1, c2) ~ "  max(c1, c2)  '\n'\ 
    17:	  return 0;                                                             
    18:	} 


     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 TOatabase { 
    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>::OoNothing(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   //     OB H 


     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:	    { nameCO] = 0;  }	
    11:	 Tfiecord(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	
    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'; t;
    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->Getflecord(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:	} 


     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:	{ 
    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:	} 


     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:	   ~TPersistO { 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 == "  tobject  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:	ostreamS operator (ostreamS os, TPersist &p) 
    27:	{ 
    28:	  os  "x == "  p.x  ",   == "  p.; 
    29:	  return os; 
    30:	}

     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 (ErrorS, e) { 
    36:	  e.Report(); 
    37:	   return -1; 
    38:      } 
    39:      return 0; 
    40:   } 
    41: 
    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, Sipart); 
    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 ErrprQ; //      
    79:     } 
    80: 
    81:	//  ,    
    82:	void 
    83:	Error::Report() 
    84:	{ 
    85:	  cout  "Domain error:" 
    86:	     " base:"  b 
    87:	     " exponent:"  e 
    88:	     endl; 
    89:	} 


     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:	{ 
    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 = nodf(e, Sipart); 
   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:	} 


     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(); 
    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 trapperQ 
    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:	}


     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);	//   (  ) 
    34          }
    35:      }
    36:      return(0); //      "Unreachable code"
    37:   }


     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 
    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:   } 

