     14.1.  BENCH.H (  TBench) 

    1:   // bench.h --   TBench 
    2: 
    3:   #ifndef __BENCH_H 
    4:   #define __BENCH_H 1 //   include 
    5: 
    6: #include <classlib\timer.h> 
    7: 
    8: typedef void (* testfn)(void); 
    9:	
  10: class TBench: public TTimer { 
  11: public:	
  12:  TBenchQ: TTimer() { }                                 
  13:  void Benchmark(long numTests, testfn tf); 
  14:  void Report(void); 
  15: }; 
  16:	
  17: #endif // __BENCH_H


     14.2.  BENCH.CPP (  TBench) 

    1:	// bench.  --   TBench 
    2: 
    3:	#include <stdio.h> 
    4:	#include "bench. h" 
    5: 
    6:	//    tf  numTests  
    7:	void TBench: : Benchmark (long numTests, testfn tf);
    8:	
    9:	printf( "Running %ld tests", numTests); 
    10:	Reset();   // Reset timer to zero 
    11:	Start() ;   // Start timing 
    12:	while (--numTests >= 0) 
    13:	   (* tf)(); // Call user test function 
    14:	Stop();    // Stop timing 
    15:	puts( "\nTests completed"); 
    16:	} 
    17:	
    18:	//    
    19:	void TBench::Report(void)      
    20:	{
    21:	   double result = Time();
    22:	   if (result < Resolution())	
    23:	      putsC'Results too small for accuracy"); 
    24:	   printf( "Elapsed time == %6f sec. \n", result); 
    25:	}


     14.3. TBENCH.CPP (  TBen h) 

    1:   #include <iostream.h> 
    2:   #include <stdio.h> 
    3:   #include "cen h.h" 
    4: 
    5:    #define NUMTESTS 20000 
    6: 
    7:   void Testfn(void); 
    8: 
    9:   main() 
   10:    { 
   11:       TBench test; 
    12: 
    13:	cout  "Testing sprintf() fun tion\n" ;
    14:	test.Benchmark(NUMTESTS,   Testfn); 
    15:	test. Report();                   
    16:	return 0;                            
    17:   } 
    18:	
    19:	void Testfn(void)             
    20:	{                                                                
    21:	 char cuffer[80]; 
    22:	double d = 3.14159; 
    23: 
    24:	sprintf(cuffer,   "%1f".  d); 
    25:        } 


     14.4.  VIRTUAL,CPP (   -) 

   1.   #include <iostream.h> 
   2. 
   3:	class TValue { 
   4:	protected: 
   5:	   int value; 
   6:	public: 
   7:	   TValue(int n) { value = n; }                     
   8:	   virtual int GetValue(void) { return value; }
   9:	};                      
   10:
   11:	class TMult:   public TValue { 
   12:	protected:               
   13:	   int multiplier; 
   14:	public:
   15:	   TMult(int n,  int m): TValue(n) { multiplier = m;   } 
   16:	   virtual int *GetValue(vpid)  {  return value * multiplier;   } 
   17:	}; 
   18:
   19:	main()
   20:	{ 
   21:	   TValue -basep;
   22:
   23:	basep = /few TValue(10);   
   24:	cout  basep->GetValue()   '\n';    //  10 
   25:	delete basep; 
   26:	 
   27:	basep = new TMult(10,  2);         .   // !!! 
   28:	cout  basep->GetValue()  '\n';    // !!!  20 
   29:	delete basep;
   30:	
   31:           return 0; 
   32:         }         



     14.5.  SET.H (  TSet) 

    1:	// set.h --   TSet 
    2: 
    3:	#ifndef __SET_H 
    4:	#define _SET_H 1  //   include 
    5: 
    6:	class TElem; 
    7:	typedef TElem *PTElem; 
    8:	typedef PTElem *PPTElem; 
    9: 
    10:	class TSet; 
    11:	typedef TSet *PTSet;   
    13:	class TSet {      
    14:	private:     
    15:	int max;    //    
    16:	int index;   //    
    17:	PPTElem set; //      PTElem 
    18:	protected:                                    " 
    19:	  virtual int CompareElems(PTElem pi, PTElem p2) = 0; 
    20:	public: 
    21:	  TSet( int n ) 
    22:	      { max = n;  index = 0;  set = new PTElem[n];   } 
    23:	   virtual ~TSet() 
    24:	     { delete[] set;   } 
    25:	   void AddElem(PTElem p); 
    26:	   int HasElem(PTElem p);
    27:	}; 
    28: 
    29.    #endif       //   __SET _H 


        14.6.  SET.CPP (  TSet) 

    1:  // set.cpp --   TSet 
    2:
    3:    #include <iostream.h> 
    4:    #include <stdlib.h> 
    5:    #include "set.h" 
    6:
    7:	//    . (    !) 
    8:	void TSet: :AddElem( PTElem p) 
    9:	{ 
    10:	  if (set == NULL) { 
    11:	    cout  "\nERROR: Out of memory"; 
    12:	    exit ( 1 ) ;                             
    13:	  }      
    14:	  if (index >= max) {                     
    15:	    cout  "\nERROR: Set limit exceeded";          
    16:	    exit(1);                              
    17:	  } 
    18:	  set[index] = p; 
    19:	  ++index; 
    20:	} 
    21: 
    22:	//  ,  ,  ,    
    23:	int TSet:: HasElem( PTElem p) 
    24:	{                 
    25:	  if (set == NULL)           
    26:	    return 0; //            
    27:	  for ( int i = 0; i < index; i++  ) 
    28:	    if (CompareElems(p, setfi]) ==0)       
    29:	        return 1; //                       
    30:	  return 0; //             
    31:	}                                      


     14.7.  TSET.CPP (  TSet) 

    1:	#include <iostream.h> 
    2:	#include <string.h>                                                     
    3:	#include "set.h"
    4:
    5:	class TElem {                                            
    6.	  private:            
    7:	     char *sp;    //   - 
    8:	  public: 
    9:               TElem(const char *s) { sp = strdup(s);   }
  10:	    virtual -TElem() { delete sp;   }                                   
  11:	    virtual const char GetString(void) {  return sp;   } ;                                                                 
  12:         }
  13: 
  14:	class TMySet:   public TSet {           
  15:	protected:                                                            
  16:	   virtual int CompareElems(PTElem p1,  PTElem p2);       
  17:	public:                                                                
  18:	   TMySet(int n): TSet(n) { }                 
  19: 	};
  20:
  21:	void Test(const char s,  PTSet setp); 
  22:	
  23: 	main()
  24:	{        
  25:	   TMySet thirties(12);    //   12  TElem              
  26:	
  27:	   thirties. AddElem(new TElemC'Sep"));                           
  28:	   thirties. AddElem(new TElem("Apr"));                            
  29:	   thirties. AddElem(new TElem("Jun"));                                   
  30:	   thirties. AddElem(new TElem("Nov")} ;  
  31:	  Test( "Jan", &thirties);              
  32:	  Test("Feb", &thirties);            
  33:	  Test("Mar", &thirties);                                  
  34:	  Test ("Apr", &thirties);                            
  35: 	  Test("May", &thirties);
  36:	  Test("Jun", &thirties);      
  37:	  Test("Jul",  &thirties); 
  38:	  Test("Aug", &thirties); 
  39:	  Test("Sep", &thirties); 
  40:	  Test("0ct",   &thirties);                               
  41:	  Test("Nov",  &thirties);                             
  42:	  Test("Dec",  &thirties);       
  43:	   return 0; 
  44:	} 
  45: 
  46:	// ,     s  setp 
  47:	void Test(const char *s,   PTSet setp)	
  48:	{ 
  49:	  TElem testElem(s); 
  50: 
  51:	if ( setp->HasElem( #testElem ) ) 
  52:	  cout  s  " is in the set\n"; 
  53:	else 
  54:	  cout  s  " is not in the set\n"; 
  55:           }
  56:		
  57:	//  ,     ,   -   
  58:	int TMySet: :ConpareElems( PTElem p1,  PTElem p2) 
  59:	{ 
  60:	   return (stricmp(p1->GetString(),   p2->GetString())); 
  61:	} 


     14.8. FRANCH.CPP (   ) 

  1: 	// franch.cpp 
  2:
  3:	#include <iostream.h>                    
  4:	#include <string.h>        
  5:
  6:	class Company {
  7:	private: 
  8:                char *name;                                                  
  9:	public:                                
10:	  Company(const char *s) {	
11:	    name = strdup(s); 
12:                cout << " In constructor for" ;
13:	    Display() ;
14: 	  }
15: 	  virtual ~Company() {
16: 	    cout << " In destractor for";
17:	    Display() ;
18:	    delete name;                                    
19:	  }
20:	  void Oisplay(void) { cout  name  '\n';   } 
21:	}
22: 
23:	class Jennys:  public Company {
24:	public: 
25:	  Jennys():  Company("Jenny's") { }
26:	}
27: 
28:	class McDougles:  public Company {    
29:	public:                                      
30:	  McDougles():  Company( "McDougles")  { }   
31:	}
32:	
33:	class BurgerOueen:  public Company {   
34:	public:                               
35:	  BurgerQueen():  Company ("BurgerQueen") { } 
36:	} ;                                                                    
37:	
38:	class Bob                                 
39:	:  public Jennys,                         
40:	   public McDougles {                                   
41:	}
42: 
43:	class Ted 
44:	  : public McDougles, 
45:	    public BurgerOueen { 
46:	}
47: 
48:	class Alice                        
49:	   : public Jennys,	
50:	     public McDougles,                   
51:	     public BurgerQueen {
52:	 }
53:		
54:	main() 
55:	{ 
56:	   Bob *bobp; 
57:	   Ted  *tedp; 
58:	   Alice *alicep; 
59: 
60:	  cout  "\nlnitializing Bob's restaurant\n"; 
61:	  bobp = new Bob; 
62:	  cout  "Initializing Ted's restaurant\n"; 
63:	  tedp = new Ted; 
64:	  cout  "Initializing Alice's restaurant\n"; 
65:	  alicep = new Alice; 
66: 
67:	  cout  "\nDeleting Bob's restaurant\n";
68:	  delete bobp; 
69:	  cout  "Deleting Ted's restaurant\n"; 
70:	  delete tedp; 
71:	  cout  "Deleting Alice's restaurant\n";	
72:	  delete alicep;	
73:	
74:	  return 0; 
75:	} 


     14.9. CONGLOM.CPP (  ) 

    1:	// conglom.cpp 
    2: 
    3:	#include <iostream.h>                         
    4: 	#include <string.h>
    5:	                            
    6:	class Company {                                               
    7:   	private: 
    8:              char *name; 
    9:   	public:                                    
    10:           Company (const char *s) {              
    11:               name = strdup(s);                               
    12:              cout  " In constructor for ";
    13:              Display();	
    14:       }                         
    15:      virtual ~Company {                      
    16:          cout  " In destructor for ";   
    17:          Display();                                           
    18:          delete name;        
    19:
    20:       void Display(void) { cout  name   '\n';   } 
    21:    }; 
    22:                                                        
    23:   class Jennys:   public Company {  
    24:   public:                                                   
    25:       Jennys():  Company(" Jenny's") { }  // 
    26:    };                                                               
    27: 
    28:   class McDougles:  public Company { 
    29:    public: 
    30:       McDougles(): Company ('McDougles") { } 
    31:   };	
    32: 
    33:   class BurgerQueen: public Company { 
    34:   public: 
    35:       BurgerQueenQ:  CompanyC'BurgerQueen") { }	
    36:   };                 
    37:
    38:   class Bob	
    39:       :  virtual public Jennys,	
    40:          virtual public McDougles {	
    41:   };	
    42: 
    43:   class Ted                                     
    44:       :  virtual public McDougles,	
    45:          virtual public BurgerQueen { 
    46:    }; 
    47:                                
    48 :   class Alice   
    49:       :  virtual public Jennys,      
    50:           virtual public McDougles, 
    51:          virtual public BurgerQueen {	
    52:   }; 
    53: 
    54:   class Corporation	
    55:       :  virtual public McDougles,     
    56:           public Ted,	
    57:          public Alice {	
    58:   private:	
    59:       char *name;	
    60:   public: 
    61:      Corporation():  McDougles(), Ted(),  Alice() 
    62:           { name = "Conglomerate Industries";   } 
    63:      void Display(void) { cout  name  '\n';   } 
    64:   }; 
    65:                                               
    66:   main()                
    67:   { 
    68:       cout  "\nForming a corporation\n";	
    69:       Corporation *cp; 
    70:       cp = new Corporation; 
    71:       cp->Display(); 
    72:
    74:    } 
