     14 

    -  

          ,     
     .     ,   
   .    ,  , 
         ,   
   ,     .  
    -,    
,   ,    . 

      

          ,   
   (. 14.1). ,    , 
  .     . 
    
      "-"
            1            
      "-" 

    . 14.1.   
    
           .   , 
  . 14.2,      
 ,        
.         ,  
       . 


    336 223 336 223 

     

      

     

        I          I     I   

       

    . 14.2.  ,     	.* 

          ;    
.      ,   
. 14.3,      
,        . 
  -    ,   
.           ,    
 . 

          	

      
        
    i	k	 1 
      
       ' 
    1	 
     		) 
      ^    
     i		^\ 
     		') 

    . 14.3.          

         -  -,  
   . ,  . 14.3    
      .    
       .     
         .   
     .     
        , 
  ,      . 

       

         . 
   TBase,   : 

    class TBase { 
    private:
        int count; //     
    public:
       TBase() { count=0; }        
       void SetCount(int n) { count=n; }    
       int GetCount(void) { return count; } 
    } ;	

         TBase     , 
    ,      C++,   
    .  count co   . 
  - TBase     ount. 
   TBase()  count  . - 

     14. -  337 224 

    SetCount()  count  . - GetCount()
   count.     TBase   
   ,    . 
    ,    ,    TBase, , 
 ,         
.     ,     
   ,        
 .      , 
 TBase     ,    
.   TBase,      , 
  ,   : 

    class TDerived:   public TBase { 
    public:	
      TDerived():  TBase() { }	
      void ChangeCount( int n )  { SetCount(GetCount() + n);   }. 
    }
 
       TDerived.      
       public, protected  private.   
     (TBase),   TDerived  
.  . 14.4    TDerived   . 

      ,    
       
     ;   TDerived 
    class TDerived: public TBase { public: TDerivedO : TBaseQ { }_ 
          
            
         .     
 ,     

    .	. 14.4.    

            . 
      : 

      class TDerived:   public TBase { 

          TBase     
TDerived.    TBase      
 ,   TDerived       TBase. 
      : 

    class TDerived:   private TBase { 

          TBase     
TDerived.   TDerived       
 TBase.          protected. 

        

         TDerive  TDerive(). ,  
   ,       .  
,        
. (C++        
,    ,      .)  
TDerived   

    TDerived():   TBase()  {   }	

    TDerivedO    
      TBase().     
    ;    ,    
     . 
         TDerived     , 
,        . 
,     .    
 TDerived     : 

    Tderived():  TBase()  { cout  "I am being initialized\n";   }  

    338      III.   ANSI C++ 

    ,        
     TDerived()  : 

    lass TDerived:  public TBase {	4 
    public: 
      TDerived();	//    
      void ChangeCount(int n)  { Set?ount(GetCount() + n);   } 
    }

         .  TDerived()
.        ,   
  : 

   TDrived::TDerived()  //  
      :  TBase()    //     
    {
       //       
    }

              
   ( TDerived::),      
(TDerived()).   TDerived    TBase,   
      ,    
.      
  ,    .     
      . 

     -   

       count   .  ,  
count   TBase,  TDerived      . 
           . 
,  TDerived      . -   
,  ,   : 

    class TDerived:  public TBase { 
    private: 
      int secondCount; 
   public: 
    TDerived();     //   	
    void ChangeCount(int n) { SetCount(GetCount() + n); }       

  TDerived   TBase,      count. 
       secondCount.     
,      : 

    TDerived::TDerived ()  //  
    :  TBase()     //  - TBase 
    {  
       secondCount = 0;     //  - TDerived 
    } 
     

      TBase()    TBase.  
   secondCount     
   TDerived.     
 ,        
,      . . 14.5 
     TBase  Tderived. 
       TDerived    - ChangeCount 
      count,    
TBase. ,  count    TBase,   
ChangeCount()       .   
  - ChangeCount()  : 

    void ChangeCount(int n)  { count += n;   }	// ???                             

           ,   
count    ,    TBase    
  count.    count,  - ChangeCountO   
  -,  SetCount()   GetCount(): 

    void ChangeCount(int n) { SetCount(GetCount() + n);   } 


 14. -  339 

    TDerived  - count,       
  
       ____ 
    class TBase { private: 
    int count; public: 
    TBase() { count = 0; } void SetCount (int n) 
    { count = n; } int GetCount(void) 
    { return count; } 
    class TDerived: public TBase { private: 
    int secondCount; public: 
    TDerivedf) 
    void ChangeCount(int n) 
    { SetCount(GetCount() + n); } 
    -   ____ 

    . 14.5.   TBase  TDerived 

       

              . 
  ,    - . 
    .   ,  
     -  
. . 14.6       
    ,    . 

    ,       
      
      
      
     ^"^,/ 
      
    ->     
    -*     
          ,    
  "*     
            

    . 14.6. ,          

         ,   ,   
  ,   . 
            ,    .
                 
 . 
             ,  
       . 

     

     15 ,       . 

    340  III.   ANSI C++ 

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

    lass TAnyClass { 
    private:	* 
      int A;   //    TAnyClass 
      void fa(void);   //   "
   protected: 
     int  ;   //  TAnyClass     
     voidd fb(void) ;         //  "
      int  ;        //     ,     
      vold fc(void);         //   "

        TAnyClass       , 
.. - fa(), fb()  fc()      . , 
    -     
   . , - fa()   
   TAnyClass.      
- fa(),   " " 
TAnyClass.
      - fb()      
TAnyClass     . ,   
    .       
    - fb(). 
       - fc()        
 TAnyClass,       . 
      ,    . 
     private, protected    public   
       . 
  TAnyClass      
: 

    class TDerived: public TAnyClass { 
       //  -, -,   etc. 
    } ;

     public    private  protected.    
     ,      
.      . 
             
  public, protected  private. 
                      
         .      
   .           
                    
    . 
     ,        
,      .     

    class TBase {	.                                                            
    protected: 
       int x;
    public:
       int ; 
       ......
    }

          

    class TDerived:   private TBase {	//     "private" 
    public:	
      void f(void); 
    };
 
    - f()       ,    TBase. 
,  TBase       TDerived, 
       private.  

    class TDescendant:  public TDerived { 
    public: 
       void g(void); 

    a 14. -  341 

- g()           ,  
     protected  public. 

         

            . 
,   
    class  {         
    public:	
        int x;	' , 
       () { cout  "Inside A's constructor\n"; x = 0; } 
       void Display(void) { cout  "x == "  x  '\n'; }
    } ;

         :	

   class : private A {                                                   
   public:	
     B():  A()  { cout  "Inside B's constructor\n";  }   
    } ; 

          ,       
   .  ,        
          - 
Display (),    .     
 : 

    A objectA; 
    objectA. Display();        //   ,   Display()     

     ,  ,       
 '"A::Display()'  is not accessible..." 'A:: Display ()' ). 

     objects; 
    objects.Display();       // ??? DisplayO    

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

    class :   private  {	
    public:                       .    
      A::Display;               //   DisplayO          
      ():  () { cout  "Inside B's constructor^";   }                      
    }  

     A::Display;       , 
  ,    ,   .  
       ,   ,  
- Display()  .    Display()
        : 

     objectB;	
    objects. Display();  // Display() -    


        

      ,      , 
      ,  ,   
  ,    -   . 
    , ,     .   
      ,   , 
       .  
 ,   :	

    class TBase {	
    private: 
       char *basep; public: 
       TBase(const char *s)  { basep = strdup(s);   } 
       ~TBase() { delete basep;   } 
       const char GetStr(void)  {  return basep;   }
   } ; 
   
    
    342      III.   ANSI C++ 

     TBase       basep   
.      strdupO  
         
     basep.  -TBaseO 
   ,    TBase   
 .    : 

    TBase  president("George Washington"); 

        "George Washington",    
        basep  president.  
 ,   president,   : 

    cout  president.GetStr() ; 

     president    ,    
,   . . 14.7   president  . 


    ,		  
     president  TBase 
    char *basep 
       
    * 
          ,    
    Jnw 

    . 14.7.    TBase   


    ,     ,   TBase. 
     :	

    class TDerived:  public TBase { 
    private: 
       char *uppercasep; 
    public: 
       TDerived(const char *s); "TDerivedO  { delete uppercase?;   } 
       const char GetUStr(void)  {  return uppercasep;   } ; 

     ,   TBase, TDerived   , 
    uppercasep,    , 
  TBase,  ,   .  
     .  TDerived  
  : 

    TDerived::TDerived(const char *s) 
        :  TBase(s)   //     uppercasep 
    {
          strupr(strdup(s));         //   TDerived 
    }

     TDerived   TBase,  ,  
  s,      basep   
.         
   ,         
   struprO,   uppercasep    . 
         TDerived,      
:    ,     . 
    

    TDerived president!"George Washington"); 

    : 

    cout  "Original string:   "  president.GetStr()  '\n'; 
    cout  "Uppercase string:  "  president.GetUStrO  '\n': 

            
"George Washington".      ,     
 ,    : 

    Original string:  George Washington	
    Uppercase string:  GEORGE WASHINGTON


     14. -  343 

    . 14.8     president  . 

     ,     
     president  TBase 
    char *basep 
    char *uppercasep 
       
    "George Washington" 
    "GEORGE WASHINGTON" 
    
           , 
   

        . 14.8.    TDerived   


      ,  president,   
    .  . 14.8   
president    .      
,           
 . 
      TBase  ,     
basep.     : 

    ~TBaseO { delete basep;   } 

    ,  TDerived  ,    
 uppercasep.  ,     
 : 

    ~TDerivedO { delete uppercasep;   }

       ,      
    . (,    
    -. .  15.) C++ 
  ,       
. ,  . 14.8     president  
  C++     , 
  : 

    delete uppercasep;	

       C++    ,  : 

    delete basep; 

     ,     ,    . 
          
         
 (. 14.9).      
  ,     
  ,  ,      
  .        
,      ,    
        
 ,  .,        .	

      - 

       ,       
  -.      
-,    . ,   
 TBase  : 

    class TBase {	
    private: 
       char *sptr; }
    public: 
      TBase(const char *s) { sptr = strdup(s); } 
      ~TBase() { delete sptr; } ;
      void Display(void) { cout  sptr  '\n'; }	
    } 


    344      III.   ANSI C++ 



      i       

             

       

           

    . 14.9.  ,       
   


        ,       .  
TBase()  strdup()         
       sptr. 
 ~TBaseO   ,   TBase   
 c. - Display()     
     .      
TBase  

    TBase state("California") ; 

    state  

    state. Display() ;	

    , ,   ,  ,    
     "State: ".      
   : 

    cout  "State:   ";	
    state. Display(); 

            .  
      TBase    - 
Display()  .      
 : 

    class TState:  public TBase {	
    public:	
      TState(const char *s): TBase(s) { } 
      void Display(void) ; //  
    } ;

      ,      - 
Display().     Display  ,   
.     -  :	

    void TState::Display(void)	
   { 
       cout  "State:   "; //   
       TBase: : Display();    //   - 
   } 


     14. -  345 

     - Display()   "State: ",   
Display()     .  TBase::  DisplayO, 
  ,    -  TBase. 
         

    TState state("Ohio"); 
    state. Display();

        "State: Ohio". 
            . 
,        .  
     ,  : 

    class TPennsylvania:  public TState {	
    public:	
      TPennsylvania():  TState("Pennsylvanya") {}	
    };	

     TPennsylvania   TState.  ,  
 ,    "Pennsylvania"   
.    

    TPennsylvania Pennsylvania;	
    Pennsylvania.Display() ;                                                                      

    Pennsylvania    . 
         . ,  
 , coxp       
.       TState: 

    class TPopulation:  public TState { 
    private: 
       long population; 
    public: 
       TPopulation( long n,   const char *name ); 
       void Display(void); 
    }
 
     TPopulation        
    .     
 :	

    TPopulation::TPopulation(long n,  const char name) 
    private:	
       long population;	
    public: 
       TPopulation (long n, const char *narae) ; 
       void Display(void); 
    }; 

         ,   
TStateO       .  
       n    
 . 
              
  - Display()  : 

    void TPopulation::Display(void) 
    { 
       TState: .Display() ; 
       cout  "Population == "  population  '\n'; 
    } 

    -          
  TPopulation       : 

    TPopulation Nebraska(1570000,   "Nebraska"); 
    Nebraska. DisplayO;	


     

              , 
   ,  ,   
.      ,    
    .      
  ,    .   
       ,       
  ,    , 
  ,      . 
    Borland C++     (.  16).  
   TTimer    ,   
.       , 


    346  III.   ANSI C++

  TTimer,       
,       , 
 -  ,    , . 

     

       TTimer   (      )     Borland   C++   31         Timer 
  (      )        BI_OLDNAMES    #define 
  ,         
  -     ,   
,     ,           
  .     TTimer        ( 
         
,          ),           
        ,   DOS,    WINDOWS. 

       ,        
 ,  TTimer     TIMER.H 
 :

    class TTimer
    { 
     public:
       TTimer();	//         
       void Start();         //    
       void Stop();	//    
       void Reset();	//   
       int Status();       -  // 0==  ; 1==   
      double Time();         //     
      static double Resolution() ; // .  15 
    private: 
      .......
  }
 
     

     -    15.      
 ; - resolution()   TTimer.	

    ,    Borland C++,     
, ,    TTimer   
C:\BC45\INCLUDE\CLASSLIB.     T1MER.CPP  
 C:\BC45\SOURCE\CLASSLIB.         
  ,    TTimer. 
    ,     TTimer    
,        .  
     DOS,   ,    
TIMER.CPP (    C:\BC45\^OURCE\CLASSLIB),    
 (   ,  ,   ) :

    bcc -DTEST_TIMER -DBI_OLDNAMES -IC:\BC45\INCLUDE timer 

      TEST_TIMER     -D  
 main()   TIMER.CPP,       
.    BI_OLDNAMES ,   , 
,     3.1 , ,      
 Borland C++ 4.0.         
  -I    ,         
 . 
         TIMER.     
 : 

    Resolution:   8.39e-07 
     ms.,  actual time	=	0.024674	seconds. 
    100 ms.,  actual time	=	0.127362	seconds. 
    200 ms.,  actual time	=	0.222327	seconds. 
    300 ms.,  actual time	=	0.314453	seconds. 
    400 ms.,  actual time	=	0.393548	seconds. 
    500 ms.,  actual time	=	C.465171	seconds. 
    600 ms.,-actual time	=	0.630901	seconds. 
    700 ms.,  actual time	=	0.738385	seconds. 
    800 ms.,   actual time	=	0.775155	seconds. 
    900 ms.,   actual tin	=	0.836996	seconds. 

      ,    TIMER   , 
      ,   ,   
    . 
    

     14. -  347 

        

    ,      ,    
  TTimer .      
-         
    .   14.1    
 ,    . 
     ,     . 

     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

     TBench   TTimer.  TBenchO ( 12)  
   TTimer,       
   TBench.   Benchmark() ( 13)  
   numTests,   ,   ,  
tf,    ,     void   
   (. typedef   8).  - ReportO, 
   14,  p .	
      14.2     TBench. 
     ,   
  . 

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

      7-16  - Benchmark().   
  ,        
  - Reset()  Start(). 
      12-13      , 
     tf.      
 14     - Stop(),  
 .   15     ,   
.


    348  III.   ANSI C++ 

      19-25    - TBench  Report().  
 21  result ;   - 
Time().   22-23    ,     
  ,      . (     
,    .)   24  
    .             
  TBench,                  - BenchmarkO 
        .   
 - ReportO.   14.3    
,          . 
   BENCH.CPP  BENCH.CPP     
  TCLASDBS.LIB,     TTimer. 
    ,       
TURBOC.CFG,    : 

    3B-IC:\c 45\include 
    -LC:\c 45\lic 

        DOS      
 BENCH.CPP    TBENCH.CPP,   TBench. 

         teen h cen h cidss.lic 

           TBENCH  BENCH  
      45  BIDSS.LIB,  
     TTimer.   S  BIDSS 
  (small)  ,     DOS  
. (  19      
     DOS  WINDOWS).   , 
 TTimer,    TBench,     
,    WINDOWS.       
   DOS.	

     

        TBENCH   - TBENCH. 
(    ).     ,  
  TBENCH GPP,     DOS  make 
-ftbench.mak  Borland C++ MAKE EXE       
  ,  , ,   
    TBENCH.. (,  18  ,   
    ) 

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


      11    TBench   test.  C++ 
    (    
 Timer),    ,   test   
 , ,   .   
    - Benchraark() ( 14)  
    


     14. -  349 

 ,     .   15 
 - Report()  test    . 
  ( 19-25)     
 sprintfO.       
    Testfn() -  . 
      : 

    Testing sprintf() function 
    Running 20000 tests... 
    Tests completed 
    Elapsed time == 4.118690 sec. 

    
		
         ,      
 ,    .    
      .       
 , , -  - , 
     .  ,   
  , ,   ,    , 
   . 

      

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

     

           
,         , 
   ,    .   
  ,          
 ,      ,   -,    
         . 
             
  C++,   .   
C++             
   ,    (. 14.10  14.11). 

                
   
       
             
,       
        
       
       I 
        

    . 14.10.         
 

           ,   
 .      ,   ,   
  ,     .     ,     
    : 

    A aObject;	//			                                     
     bObject;	//			  
     cObject:	//			  


    350  III.   ANSI C++ 

    			
      
         	 
      
     	1     	     	V 
    -	^    	--       -"   1 
    !      i	-	|   -  
     -      
     -
    		
     
         		

    . 14.11.        -, 
   

                
,     .  ,  , 
  cObject     : 

     *;	//        * 
     = ScObject; //    cObject 

            *,   *,    
   ,      . , ,  
 ,      ,       (, 
     ,      ). 
    ,          
.        ,       
     . 
         ,  ,  "   , 
   .      
,      ,      
    ,    .  
       ,      . 
        ,     
    .         
   ,    ,    
   virtual. ,     
  vf(): 

    class A { 
       public:	
          virtual void vf() ;
    }; 

         ,    
,     .     - 
    virtual.      
 ,  .     
,     . 
      ,   ,     , 
 - vf(): 

    class :   public A { 
     public: 
       virtual void vf(); //       

      ,  ,   ,  
     -,    
.   vf()         
  . 
            : 

    class  :    public      {
    public: 
       virtual void vf();
    }
    
    
     14. -  351 

          virtual  , 
      . ,     
D        D    vf(),     
       virtual.    
  .      *,    
 cObject  *.    ,  
  vf()  ,    : 

    p->vf();	 

           ,   . ,  
        ,  
  .       ,    
vf(),   .       ,  
  vf(),   .       
     ,    . 

         

             
.           
  . 
    ,         
    ,  ,   .   
   (   ),      
Shape  : 

    class Shape { 
    public:	
        ..........  // ...   
        virtual void DrawO; 
    }: 
 
       ,     ,  ,  
     . ,  ,   
-    Shape, ,    
(,   ,      ): 

    Shape *picture[100];	//     Shape 

       ,  picture    100 
 Shape.       Shape*, ,  
        ,    
  Shape      .    
Shape    Draw(),    , 
 DrawO     picture: 

    int i = 0; 
    while (i < 100 && _picture[i] != 0)  { 
       //    Draw()             
       picture[i]->Draw(); 
       i++; 
    }

        ,    ,     
      !       
 ,   ,        
     .OBJ-.   (  ) 
     Shape,       
picture,   while     Draw()   
   . 
     ,           
 ,     picture,   
      Shape.     
,   Draw()  . ,   
  Circle ()  Line ()   Shape: 
    
    class Circle:   public Shape { 
    public: 
       virtual void Draw(); 
    }; 
    
    class Line: public Shape { 
    public: 
       virtual void Draw(); 
    }; 

          Draw   
Circle  Line    .    
     ,    
 . ,      picture  
 : 


    352  III.   ANSI C++

    picture[]= new	Circle;
    picture[]	= new	Circle; 
    picture[]	= new	Line; 
    picture[]	= new	Circle; 
    picture[]= new        Line; 

          Shape  ,   ,  
     ,      
. ,     ,   
    picture,        
  ,      , 
   Shape. 

       - 

        ,    -, 
  -,        
  ,    : 

    class TValue { 
    protected: 
       int value;	
       TValuefint n) { value = n;  }	
       int GetValue(void) { return value;  } 
   }; 

           TValue,   
    :	

    Tvalue (10);	//   Tvalue x 
    cout  x.GetValue()  '\n';                 //  10 

         TValue.    
        . 
    TMult: 

    class TMult:   public TValue { 
    protected: 
       int multiplier;	
    public: 
       TMult(int n,  int m):  TValue(n) { multiplier = m;   } 
        int GetValue(void) { return value * multiplier;   } 

     TMult   - GetValueO   , 
 value * multiplier.      TMult   
 - GetValue(),  ,    , 
  ,    . 

    TMult y(10,  2);	//   TMult                         
     cout  .GetValueO  '\n';                 //  20                                                                     

    , . ,        
TSingle  TDouble.       tvp  
TValue  : 

    TValue *tvp = new TValue(10); 

       - GetValueO  ,   
 : 

    cout  tvp->GetValue()  '\n';	//  20 

    ,         TMult  
  - GetValue(): 

    TMult *tmp = new Tf -t(10,  2); 
    cout  tmp->GetValue()  '\n';	//  20 

      C++,     ,  
       

    TValue basep;	//    TValue 

       : 

    basep = new TMult(10, 2);         // !!!    

     basep        TValue,   
     TMult,     
basep.   ,      : 

    cout  basep->GetValue()  '\\';         // ???  10 

      ,       20. 
 ,  C++  basep    TValue. 
, C++  - GetValueO   


     14. -  353 
     Borland C++4.5 

 TValue.   ,  C++  - GetValue()
 ,      . 
        ,  GetVa()  
-.   -    
  (  ,     
).   -    
( ).  -     
       . 

     

    Borland C++    -,   
          
.     . 

      14.4   TValue  TMult,     
,    .    GetValue()   
-.        DOS 
 EasyWin-. 

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

      8  16   GetValue()  
  virtual.    ,   
- GetValue()    .  
    - GetValue()
   .     
-, C++       .   , 
    ,    .  
        
,      . 
     -   ,    
-. ,   , -  
 .  ,       
GetValue()  -      TMult
( 11-17)   ,      TValue ( 3-9). 
,     virtual     
   . 
       VIRTUAL  ,      10 
 20      - GetValueO.   
21   basep   TValue.   23  new  
  TValue,    10 ( 24), 


    354  III.   ANSI C++ 

    ,    ( 25)   27   
  TMult. Co:       
C++  basep       ,     
      TValue.  basep   
  TMult,  28   20. 
          24  28.    
,     ,    . 
        ?    
,    ,    ,    
 - GetValueO  .    ,  
     ,     . 

      - 

      -   ,   
  . 
           ,   ,  
      = 0. ,   
   class AbstractClass    {  
   public: 
      virtual void f1(void);         //   - 
      virtual void f2(void) = 0;  //   - 
    } 

   f2()  -   -.     
- f2(),     -,   . 
          -,   
 .    ,    
 .      ,    
    .  C++      
,          .   ,         
    AbstractClass.      
 : 

    AbstractClass anyObject;	// ??? 

         "Cannot create an instance of class 
'AbstractClass' (He    'AbstractClass'). ,  - 
 AbstractClass     -. , 
- f1()   f2(): 

   void AbstractClass: :f1(void) 
   {
      f2();	//    - 
      .....      //    f1() 
    }

         ,     
 : 

    class MyClass:   public AbstractClass { 
    public:
      virtual void f2(void);	//                    
      .........
     }

       MyClass    -,   
   = 0. (      MyClass .) 
-      - f2(): 

    void MyClass::f2(void) 
    {

    _	//  - 

    }

         -,   
,  ,      AnyClass::f1(),   
MyClass::f2().       - 
        ,     
-,        
   .     ,  
       . 
    ,    - ,   
   <MnClass: 

    MyClass myObject; 

     	

    myObject.f1 ();	

        f1(),   f2()  MyClass.	: 


    14. -  355 

          
      .  TSet 
      .   
14.5   ,     . 
    ,     . 

     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       /I   __SET _H 
    
         TSet    ,  
        
.     6, C++     
,   : 

    class TElem;   //   	

           TElem    
 ,    .. ,    
     TElem.    
    TSet  -    
 ,     .     
 ,   . PTElem   
 TElem*.  , PTElem -     TElem. 
, PPTElem     PTElem, ..   
    TElem.   TSet  PPTElem   
       PTElem. 
      1011       
  TSet   PTSet      TSet. 
    TSet   10 -  
  typedef-. 
        TSet ( 13-27).   
-     .    TSet 
     PTElem,    
,  PPTelem     . ( set 
        TElem.)  max  index 
   . 

    

      -  TSet   ,    
,   ,   .   
         , 
   .         
,      ,   ,  
 , 

      19    - CompareElems  
   TSet.   -    
 ,     ,  


    356  III.   ANSI ++

,       . 
,  - CompareElems()  ,    
 TElem,     1  p2 . ,    
    CompareElemsO,       
  ,    TElem. ,   
 -  ,   ,   
,    TSet.    " 
   22-26.    ( 21-22) 
  -     new   
  PTElem.   ( 23-24)   , 
   TSet      . 
    - AddElem()  HasElem() ( 25-26)    
,    .    ( 14.6) 
  . (    ,   
,   ,   .  --  
   .) 

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


     SET.CPP     AddElemO  HasElemO. ,  
    ,    ,   
. ,    ,   .     
    TElem. 
         AddElemO ( 8-20)     
 TElem,   .   10-17   
          
  . (      
 ,         
 .)   18-19  TElem,    
 ,    set. 
    - HasElemO ( 23-31)  ,   
 TElem   .  HasElemO   
 -  CompareElemsO   28.    
      ,    !   
      ,   
 .   -,    28 
      . 
      TSET.CPP ( 14.7)    TSet  
TElem    ,   ,  
 30 : , ,   .  ,  
 -       
 ,    .   
       
TSET.CPP  SET.CPP.   DOS,   bcc tset set, 


     14. -  357 

         
TSET.EXE,   .            
  TSET.IDE          Project      <Ctrl+F9>  
      EasyWin-. 

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

         TSet    TElem.  
 TElem   .   5-12   
,TElem,    .  -  
 . 
    

    358  III.   ANSI ++

      14-19    TSet    TMySet. 
       ,  
     TSet().   ,  
  16   - CompareElems(),  
     TSet. 
        ,    - 
CompareElems().      stricmp()  
  .         
- GetStringO  TElem (.   11).    
 51 HasElem()  -     58-61 
CompareElems().       Turbo Debugger  
       ,   
 CompareElemsO. 
        -   
     . 
        SET        
.     SET.CPP     TSet. 
      SET.H. 
           TSet    
   TElem. He    SET.CPP  
   .  TSet    
   .    
  . 
        ,    ,  TSet, 
       -  
 ,   - .    
     ,    
     .     
 ,   (    )   
      .	

          

    -  ,   ,   . 
     ,     
    ,     
  . ,   ,   
  : 

    class TBase { 
    private: 
       char *sp1; 
    public: 
       TBase(const char *s) 
           { sp1 = strdup(s);   }	
    virtual ~TBase() 
        { delete sp1;   } 
    };

      TBase       
  strdupO        sp1.  
   ,    TBase    . 
    ,     TBase: 

    class TDerived:  public TBase { 
    private: 
       char *sp2; 
    public: 
       TDerived(const char *s1,  const char *s2):  TBase(s1) 
            { sp2 = strdup(s2);   } 
       virtual ~TDerived() 
          { delete sp2;   } 
    }; 

        ,     sp2. 
   TBase(),     ,   
       ,  
 . 
      TDerived    , ,    
  . ,      TBase,  
    TDerived    , ,  
  ,          
      .     
  : 


    C++      14. -  359 

    TBase *pbase; 
    pbase = new TDerived("String 1",  "String 2"); 

      ,     ,    
  pbase: 

    delete pbase; // !!! 

      ,  pbase     TBase  
    TBase   ,   
 pbase. ,  pbase      
 TDerived,         ,   
  . 
       ,     
       ,   
 . ,    ,   
  ,      ,   
    .

     

                
       
 ,          
-  ,    .  . 14.12 
   . 

            I             I                I 

                                             -

    . 14.12.   

       

              
    .  ,     ,    D 
     : 

    class D:  public A,  public ,  public  {	
     ......                          
    }; 

             
   .      
 : 

    class D:  public A,   public ,   {
     ........
    };

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

    class D:  public A,  public  { 
    public: 
         void f(void); 
         .......
     };

     - f()  ,     
Display()  .    ,   
         
:

    
    360 247  III.   ANSI C++

   void D::f( void )
   { 
       Display();      // ??? ;   
       :: Display ;    //  -  
       :: DisplayO;    //  -  
   }
    
         

     ,    ,    
     .   ,     
  ,    D     :	

    lass D:  public A,  public ,  public  { 
    public:
       D():  (),   (),   ()  {  }	//   
       ........
     };

          

    class D:   public A,   public ,   public  { 
    public: 
        D() ;	// ,    
        ......
    }; 

      , ,   : 

    D:.D() 
       :  (),   (),   () 
    {	
        ... //    D() 
    }
 
    ,     ,    
 .     ,     
 .     ,   ,  
      .  ,  
      , -  . 

     

    ,    ,     
  .      , 
   ,     cc  
 -  .        
    . 

        

           ,   
       .  
      , ,   ,  
      . ,    
      ,   
,        ,      , 
   . 
     ,       , 
     .    
  ,     
     ,   , 
     ,      
" ",        . 
      ,      , 
  ,    ,   
 14.8.        
     :  (Bob),  
Ted)   (Alice).     ,  
        
       ,   
 . 


     14. -  361 

     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:	} 
    
    
    362	 III.   ANSI C++

                                                            Jennys	)          I            Ob		) 
    
    
    I        Company	)	[      McDougles	]	,	) 
    
    
                                                       i     BurgerQueen	1	Alice	) 

-14-13-     FRANCH.CPP

     . 14.13    FRANCH.CPP.   
   Company,      
  Jennys, McDouglas  BurgerQueen.    ""- 
   Com, , ,    
name ( 8),   - Display() ( 20).
      , ,   ,   ( 
38-52).  Bob        Company  
Jennys  McDougles.  Ted     McDougles  
BurgerQueen. Alice,     ,   
    Company  Jennys, McDougles  BurgerQueen.   
 ,   , ,    
    ,       
 ,      ( 56-58). 

     

             
,  ,    FRANCH.CPP.  
  ,   -    
     . 

       FRANCH.CPP    .   
    ,        
  . ,      
 -   .       
,          
.   Corporation,     
  McDougles      :

    class Corporation 
         public McDougles,	// ??? 
         public Ted, 
         public Alice {       
    public: 
         ..........
    };

     ,  . 14.13,   ,   Corporation 
    . (    .) Ted  Alice  
   McDougles.       Corporation, 
      McDougles.  
      ,  "McDougles is inaccessible 
because [it is] also in Ted" (McDougles ,     Ted). 
            
 ,     ,  
    .    Corporation    
   McDougles.    ,  Ted   
McDougles,    McDougles,    Corporation.  
    Corporation     
 - McDougles.     
,     Corporation  ,   
,  Ted. 
      (  ) ,   , 
  ,      
  ,      
      -,   
.          
     ,       
 . 
      14.9 ,    Corporation, 
   ,     
  McDougles. 
     CONGLOM  Easy Win-   , 
          
    .      
  DOS,     . 

     14.9. CONGLOM.CPP (  ) 

    1:	// conglom.cpp 
    2: 
    3:	#include <iostream.h>                         
    4: 	#include <string.h>
    5:	                            
    6:	class Company {                                               
    

    14. -  363 

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

     Company  ,      Jennys, McDougles, 
BurgerQuen, Bob, Ted  Alice, ,    
        
-. , Bob    : 

    class Bob 
    : virtual public Jennys,	
       virtual public McDougles { 
    

    364  III.   ANSI C++ 

   virtual     
 ,     ,   Jennis  
McDougies,         . 
,   Ted  Alice ( 43-52)    . 
        Corporation ( 54-64).   , 
   McDougies, Ted  Alice. ,   
McDougies           
 Corporation,   ,  Ted  Alice     McDougies. 

     

    Ted  Alice          
 5657.        ,     
   Corporation       . 


     Borland ObjectBrowser 

     ,  ,   ,    
      .  
    Microsoft Windows,    ObjectBrowser 
         .  
     ,      Windows 
   DOS. 
    ObjectBrowser   ,      . 
  CONGLOM   EasyWin- (    , 
   2)   View\Classes     
     (. 14.14). 
      CONGLOM     -  
,      ios, streambuf      , 
   Borland C++. 
         (, McDougies)    , 
   (. 14.15).       
     .      F (), 
V (), I (  )  V ( )   
 ,   . 

     14.14.  CONGLOM    

    . 14.15.  McDougies   ObjectBrowser 


     

    .     ,    
  .        . 

         , 
     . 

             . 
 ,      ,   . 

               
.  ,     .  
,      ,   
   . 

           -  - 
       .

          ,   
,   -  ,   
  . 


    14. -  365 

            
    .        
     .     
    ,   .   
  (public, protected  private)    
  . 

          . 
,        ,  
      . ,   
   ,      . 

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

          , 
    .      
        
.           
        . 

         -,   
 .  -  ,    
 ,         . 

                
    ,     .  
        
-,         
  ,  . 

      -   ,    
. C++   -   
    .    
-   = 0.  ,     
  ,  .     
  . ,    , 
 ,      
-  . 

           . 
          
      .    
      ,   
  ,     ,  . 

     

    14.1.        ,  
  ,   ,    .  
 ,       
    . (     
  .         
   .) 
    14.2.      TBench ( 14.1  14.2), 
          
    . 
    14.3.    ,    
   0  15,       .  
   -, , ,   
   .   -, 
   ,   .    
  ,  0100100011001011,  1  
   . 
    14.4.  ,  ^    
 . 
    14.5.     14.4  ,  
          . 
    14.6.   ,      14.5, 
,   ,  ,  
     ,  . 
,   -  "*."   
     .,  ' ,  
       . 
    

    366	 111.   ANSI C++ 