989

                      19.    C++

            
       . 
       
    .     
  ,    .
         (RTTI 
Run-time Type Identification),     
  ,       
.        :
,            
       . 
         
,     ,    
   ,      .

               19.1.     

   RTTI  ,      
   ,      .
    RTTI   C++   :

  dynamic_cast      ,
       .  
            , 
    l-,    ,   
  ,     ,    ;
  typeid      ,
     .

             
 dynamic_cast  typeid    ,     
  .  ,  RTTI    
          
  .         
.
    RTTI      , 
    ,   ,  
,         RTTI-
,     .   
   C++,     .

990

                       19.1.1.  dynamic_cast

    dynamic_cast     , 
            .  
   l-         
  .      dynamic_cast,   
   C++ ,     .
   l-       , 
dynamic_cast  .      
    .   l-  
  ,  .     
  .
          dynamic_cast, , 
  . ,      
     .   
  -   :

   class employee {
   public:
      virtual int salary();
   };

   class manager : public employee {
   public:
      int salary();
   };

   class programmer : public employee {
   public:
      int salary();
   };

   void company::payroll( employee *pe ) {
      //  pe->salary()
   }

}

        .  - payroll()
 company     employee,    
  manager  programmer.  payroll()   
- salary(),     ,
   manager  programmer,    ,  
 .
   ,  employee    ,    
,    - bonus(),   
salary()    .     
-  ,   employee:

991


   class employee {
   public:
      virtual int salary(); // 
      virtual int bonus(); // 
   };

   class manager : public employee {
   public:
      int salary();
   };

   class programmer : public employee {
   public:
      int salary();
      int bonus();
   };

   void company::payroll( employee *pe ) {
      //  pe->salary()  pe->bonus()
   };


     pe  payroll()     manager,  
 - bonus()    employee,   
manager   .   pe     programmer, 
  - bonus()   programmer.
           
  -.  bonus() ,     
   -   employee, manager  programmer.
       ,   , 
      ,  
       ,   
- .      
.
         ,   
 -,    dynamic_cast.
        ,  
     ,  -  .
,         -
bonus()   programmer.      
programmer,    ,       
  :

992

   class employee {
   public:
      virtual int salary();
   };

   class manager : public employee {
   public:
      int salary();
   };

   class programmer : public employee {
   public:
      int salary();
      int bonus();
   };

   ,  payroll()        
employee.     dynamic_cast    
 programmer      - bonus():

   void company::payroll( employee *pe )
   {
      programmer *pm = dynamic_cast< programmer* >( pe );
      //  pe     programmer,
      //  dynamic_cast    pm 
      //     programmer
      if ( pm ) {
         //  pm   programmer::bonus()
      }
      //  pe      programmer,
      //  dynamic_cast  
      //  pm   0
      else {
         //  -  employee
   }


   

   dynamic_cast< programmer* >( pe )

   pe   programmer*.   , 
pe     programmer,     : 
 dynamic_cast  0.
    ,  dynamic_cast    .  ,
   ,    ,  . 
    . dynamic_cast ,  
    C++,    
.
       pe      programmer, 
 dynamic_cast    pm    
  programmer.    pm   0.  

993

pm,  company::payroll()  ,   pm   programmer.
  ,    - programmer::bonus()  
 .   dynamic_cast  ,  pe  
  manager,  ,      , 
  - programmer::bonus().
    dynamic_cast      
     .    
  (downcasting).  ,  
   ,   .
        
  ,    .  
   .    dynamic_cast
  ,       
,  ,     .
            dynamic_cast 
   0:      
 . :

   void company::payroll( employee *pe )
   {
      programmer *pm = dynamic_cast< programmer* >( pe );

      //  : pm    
      static int variablePay = 0;
      variablePay += pm->bonus();
      // ...

   }

   ,  dynamic_cast,   ,  
   .    
company::payroll()    :

   void company::payroll( employee *pe )
   {
      //  dynamic_cast   
      if ( programmer *pm = dynamic_cast< programmer* >( pe ) ) {
         //  pm   programmer::bonus()
      }
      else {
         //  -  employee
      }
   }

  dynamic_cast     pm
     if.  ,    
  . ,   ,
,  pm   :  ,   dynamic_cast 
  pe    programmer.    
  0    else.      
     ,    

994

-    dynamic_cast  ,   pm 
  ,    .
       dynamic_cast     
   .       l-
       .   
dynamic_cast :

   dynamic_cast< Type & >( lval )

 Type&     ,  lval l-   .
 lval     Type&    ,  lval
    ,       
Type.
        (.  3.6),   
     (..  
dynamic_cast )   .    
, 

   if ( programmer *pm = dynamic_cast< programmer* >( pe ) )

   

   if ( programmer &pm = dynamic_cast< programmer& >( pe ) )

          
dynamic_cast  . ,   
 :

   #include <typeinfo>
   void company::payroll( employee &re )
   {
      try {
         programmer &rm = dynamic_cast< programmer & >( re );
         //  rm   programmer::bonus()
      }
      catch ( std::bad_cast ) {
         //  -  employee
      }

         dynamic_cast 
  bad_cast.  bad_cast    ; 
         <typeinfo>.
(         .)
        dynamic_cast  ? 
    .     
        (   )
;   ,     
    (.  11).

995

                            19.1.2.  typeid

    ,    RTTI,   typeid,   
  .         
    -,         
. ,       ,  typeid
    :

   #include <typeinfo>

   programmer pobj;
   employee &re = pobj;
   //   name()    ,  type_info
   //   C- "programmer"
   coiut << typeid( re ).name() << endl;

    re  typeid   employee.    re     
   ,  typeid ,     
programmer (  employee,    re). ,  
,     <typeinfo>,      
.
     typeid?    ,   
,      ,   
.        , 
         , 
            
       .  typeid
       . , 
       .   
   ,  typeid    :

   int iobj;
   cout << typeid( iobj ).name() << endl; // : int
   cout << typeid( 8.16 ).name() << endl; // : double

       ,     ,  typeid
  ,      :

   class Base { /*    */ };
   class Derived : public Base { /*    */ };
   Derived dobj;
   Base *pb = &dobj;
   cout << typeid( *pb ).name() << endl; // : Base


996

    typeid   Base, ..   *pb.    Base 
 ,  typeid  Base,  ,  
 pb,   Derived.
   ,   typeid,  . :

   #include <typeinfo>
   employee *pe = new manager;
   employee& re = *pe;
   if ( typeid( pe ) == typeid( employee* ) ) // 
      // - 
   /*
   if ( typeid( pe ) == typeid( manager* ) ) // 
   if ( typeid( pe ) == typeid( employee ) ) // 
   if ( typeid( pe ) == typeid( manager ) ) // 
   */

      if    typeid  ,
 ,   ,   . 
,  

   typeid( pe ) == typeid( employee* )

 .   ,  :

   //   
   pe->salary();

      salary()    manager.
 typeid(pe)    .    ,  pe 
,        typeid   
   .  typeid(pe)   pe, ..
  employee.      typeid(employee*), 
     .
       *pe    typeid  
  ,    pe:

   typeid( *pe ) == typeid( manager ) //  
   typeid( *pe ) == typeid( employee ) // 

      *pe    ,    ,
   typeid     
manager.
         :

997


   typeid( re ) == typeid( manager ) // 
   typeid( re ) == typeid( employee ) // 
   typeid( &re ) == typeid( employee* ) // 

   typeid( &re ) == typeid( manager* ) // 

        re      ,
   typeid   ,    re. 
    &re   , , 
   , .. employee*.
       typeid     type_info, 
    <typeinfo>.    , 
   ,  typeid. (   
   .)

                           19.1.3.  type_info

      type_info   ,   
        C++:

   class type_info {
      //    
   private:
      type_info( const type_info& );
      type_info& operator= ( const type_info& );
   public:
      virtual ~type_info();

      int operator==( const type_info& );
      int operator!=( const type_info& );

      const char * name() const;
  };

            
type_info,          :

   #include <typeinfo>
   type_info t1; // :    
                      // :   

        type_info   
typeid.

   type_info t2 (typeid( unsigned int ) );

        .     
type_info,  ,  ,    typeid.
(      .)

998


   typeid( re ) == typeid( manager ) // 
   typeid( *pe ) != typeid( employee ) // 

    name()  C-   ,  
type_info.        :

   #include <typeinfo>

   int main() {
      employee *pe = new manager;
      // : "manager"
           cout << typeid( *pe ).name() << endl;

      - name()    
<typeinfo>.
        ,    
 C++,    - name()  type_info. 
   ,   RTTI     
  type_info   -.  , 
   RTTI   ,  
   .  ,    ,
    , :

  - ;
     , ..   
     .

        RTTI   
  ,   type_info.    type_info 
 ,   dynamic_cast  ,  
   RTTI. ,   
   RTTI   extended_type_info,
  type_info.    dynamic_cast  
,     type_info,   typeid, 
 extended_type_info.  ,     RTTI
.

999

   #include <typeinfo>
   //  typeinfo    extended_type_info

   void func( employee* p )
   {
      //    type_info*  extended_type_info*
      if ( eti *eti_p = dynamic_cast<eti *>( &typeid( *p ) ) )
      {
         //  dynamic_cast  ,
         //     extended_type_info  eti_p
      }
      else
      {
         //  dynamic_cast  ,
         //     type_info
     }


    dynamic_cast  ,   typeid   
extended_type_info, ..     RTTI, 
  .      
 RTTI.

                                 19.1

     ,          
 :

   class X { ... };
   class A { ... };
   class B : public A { ... };
   class C : public B { ... };
   class D : public X, public C { ... };

       dynamic_cast  ?

   (a)  D *pd = new D;

   (b)  A *pa = new C;
      A *pa = dynamic_cast< A* > ( pd );

   (c) B *pb = new B;
     C *pc = dynamic_cast< C* > ( pa );
     D *pd = dynamic_cast< D* > ( pb );

1000

   (d) A *pa = new D;
     X *px = dynamic_cast< X* > ( pa );

                             19.2

   ,     dynamic_cast  
?

                              19.3

        19.1,    ,
      dynamic_cast   *pa 
 D&:

   if ( D *pd = dynamic_cast< D* >( pa ) ) {
      //   D
      }
      else {
         //   A


                                  19.4

     ,          
 :

   class X { ... };
   class A { ... };
   class B : public A { ... };
   class C : public B { ... };
   class D : public X, public C { ... };

            :

   (a) A *pa = new D;
     cout << typeid( pa ).name() << endl;

   (b) X *px = new D;
      cout << typeid( *px ).name() << endl;

   (c) C obj;
      A& ra = cobj;
      cout << typeid( &ra ).name() << endl;

   (d) X *px = new D;
     A& ra = *px;
     cout << typeid( ra ).name() << endl;


1001


                       19.2.   

             
    . C++  
    ,      
   .      C++  
 11,  ,     ,  
  (  )    
  try-.
        ,   
   .     ,  
,      , 
 .

                19.2.1. ,    

     11        , 
-   iStack:

   class popOnEmpty { ... };
   class pushOnFull { ... };

       C++  ,  ,  
  ,  .        
?
        Excp,     
.     -,   
:

   class Excp { ... };
   class popOnEmpty : public Excp { ... };
   class pushOnFull : public Excp { ... };

     ,    ,    
.     ,    :

   class Excp {
   public:
      //    
      static void print( string msg ) {
          cerr << msg << endl;
      }
   };


1002

         .  Excp 
       , 
:

   class Excp { ... };

   class stackExcp : public Excp { ... };
      class popOnEmpty : public stackExcp { ... };
      class pushOnFull : public stackExcp { ... };

   class mathExcp : public Excp ( ... };
     class zeroOp : public mathExcp { ... };
     class divideByZero : public mathExcp { ... };

         
   .     
.          
 . , ,      
   Excp.   Excp,    
: stackExcp (      iStack)  mathExcp (
,     ). , 
      : popOnEmpty 
pushOnFull       ,  ZeroOp 
divideByZero      .
       ,    
,     .

             19.2.2.    

   ,   , ,  ,  -
push()  iStack  :

   void iStack::push( int value )
   {
      if ( full() )
         // value   -
         throw pushOnFull( value );
      // ...
   }

     throw    :

   1.  throw      pushOnFull,  
      .
   2.      - 
      pushOnFull    ,    1.   
       .
   3.  ,    1,    
      .

1003

      - ( 2)? 

   throw pushOnFull( value );

  ,      throw.  
    ,      ,   
     .   
      (-), 
 ,     .  
 - ,   1.     ,   
  .
    -    , 
 throw,        ,   
:

   void iStack::push( int value ) {
      if ( full() ) {
        pushOnFull except( value );
        stackExcp *pse = &except;
        throw *pse; // -   stackExcp
           }
           // ...
   }

    *pse   stackExcp.   - 
stackExcp,  pse       pushOnFull.
  ,    throw,   -
  .      catch-
 pushOnFull.
   ,   throw,     ,
      -.  throw 
- push()  iStack   , :

   pushOnFull  ,    int,  
   ;
   pushOnFull     ,    
    ;
 pushOnFull     . ,    
      (.  17.1).


                 19.2.3.    

       ,     
   ,    
 . ,   pushOnFull 
   stackExcp  Excp.

   int main() {
       try {
            // ...
      }
      catch ( Excp ) {
         //   popOnEmpty  pushOnFull
      }
      catch ( pushOnFull ) {
         //   pushOnFull
   }


1004

     catch-  . ,  
     try-.    
,    ,  .  
 Excp     pushOnFull,   , 
      . 
 :

   catch ( pushOnFull ) {
      //   pushOnFull
   }
   catch ( Excp ) {
      //   
   }

catch-      .  catch-
        ,  
   .
       ,     
         ,
  . ,   main(),  ,
   pushOnFull    ,  ,
      catch-.  
 ,    :

   catch ( pushOnFull eObj ) {
      //  - value()  pushOnFull
      // .  11.3
      cerr << "   " << eObj.value()
      << "   \n";
   }
   catch ( Excp ) {
      //  - print()  
     Excp::print( " " );
   }

       11.3,   catch-  
       .  
        ,  
 ,      catch-  

1005

      . 
  , ..   ,  
  .     
     .
      catch- (     catch)
     .    
 ,   .  eObj 
  -   ,    
      .
      ,     
.  catch-     -,
  throw,      .  
  ,       ; 
        . 
  ,      (  ), 
  (      ).
  11      , 
  catch-    - 
    .    

   throw;

    ,     catch- 
 ? ,      , 
mathFunc()    divideByZero?

   void calculate( int parm ) {
      try {
          mathFunc( parm ); //   divideByZero
      }
      catch ( mathExcp mExcp ) {
         //   
         //   -  
         throw;
      }
   }

          divideByZero  ,  
,   mathFunc()?   mathExcp,   
   catch-?
   ,   throw    -. 
     divideByZero,    
   .  catch-  mExcp  
   divideByZero,     
MathExcp.       catch-,  
  -,   .
,        :

1006

   class pushOnFull {
   public:
      pushOnFull( int i ) : _value( i ) { }
      int value() { return _value; }
      ~pushOnFull(); //   
   private:
      int _value;
   };

     ?     ,  catch-:

   catch ( pushOnFull eObj ) {
      cerr << "   " << eObj.value()
      << "   \n";
   };

    eObj     catch-
 ,    pushOnFull  ,  eObj  
  .      -,
    ,     catch-  
  ?          
 .  , ?  catch-  
,      ,   -
      catch-.

                 19.2.4. -   

     -    , 
 catch-  ,     
   . ,  - value(),
    pushOnFull,    catch- Excp:

   catch ( const Excp &eObj ) {
      // :   Excp  - value()
      cerr << "   " << eObj.value()
      << "   \n";
   }

          
 ,     catch-  
 Excp      -  
:

1007

   //   ,   
   class Excp {
   public:
      virtual void print( string msg ) {
         cerr << " "
         << endl;
      }

   class stackExcp : public Excp { };
   class pushOnFull : public stackExcp {
   public:
      virtual void print() {
         cerr << "   " << _value
          << "   \n";
      }

   // ...
   };
   };




    print()     catch-  :

   int main() {
      try {
        // iStack::push()   pushOnFull
      } catch ( Excp eObj ) {
         eObj.print(); //    ,
                          //      
     }
}

        pushOnFull,   print() ,
 eObj.print()   :

    

    print()     Excp,     
.  ?
   ,     catch-     ,
  .     catch-, , 
   ,   , eObj    Excp
   .  eObj     Excp,  
pushOnFull.       , 
      :

1008

   int main() {
      try {
         // iStack::push()   pushOnFull
      } catch ( const Excp &eObj ) {
         eObj.print(); //   
         // pushOnFull::print()
      }
}

             Excp,  
 eObj       -  pushOnFull,   
   ,    pushOnFull. 
catch-     print(),   
 ,     :

      879   

    ,       
,    -.

                  19.2.5.     

     ,   catch-    
  ,  ,     
  (.  11.3).
           
.      (,   
   ),      .
 ,    .  ,   
        , 
   ,     
. (     8.1.)
,         
     :

   class PTR {
   public:
      PTR() { ptr = new int[ chunk ]; }
      ~PTR { delete[] ptr; }
   private:
      int *ptr;
   };

          manip()   mathFunc():

1009

   void manip( int parm ) {
      PTR localPtr;
      // ...
      mathFunc( parm ); //   divideByZero
      // ...
   }

    mathFunc()    divideByZero,   
.     catch-   
manip().   mathFunc()    try-,  manip() 
  .       . 
   manip()     
    ,         
mathFunc().  ,   localPtr   , 
  ,  , ,    , 
    .
    ,      C++  
,      : 
   ;     .  
    , ,      
,        (, ,   PTR
),          
      . , 
     ,     
,     ,    .
    auto_ptr,     (.  8.4),  
  ,    PTR.       
     .     
   auto_ptr,  ,     
   -     .

                      19.2.6.  

       (.  11.4)   
  ,       .
  ,       
 .
        -   ,   
 ;       -.
,    bad_alloc    C++ -
     throw(), ..  
  :

1010

   class bad_alloc : public exception {
      // ...
   public:
      bad_alloc() throw();
      bad_alloc( const bad_alloc & ) throw();
      bad_alloc & operator=( const bad_alloc & ) throw();
      virtual ~bad_alloc() throw();
      virtual const char* what() const throw();
   };

   ,   -    const  volatile, ,
, what()   ,       .
          
  .     -,  
   ,        
   :

   #include <stdexcept>
   // <stdexcept>   overflow_error
   class transport {
      // ...
   public:
      double cost( double, double ) throw ( overflow_error );
      // ...
   };

   // :     ,  
   //      
   double transport::cost( double rate, double distance ) { }

           ,
  ,     -  .
         
   ,   :

1011

   class Base {
   public:
      virtual double f1( double ) throw();
      virtual int f2( int ) throw( int );
      virtual string f3() throw( int, string );
      // ...
   }

   class Derived : public Base {
   public:
      // :     ,
      //   Base::f1()
     double f1( double ) throw( string );
     // :    ,    Base::f2()
     int f2( int ) throw( int );
      // :   f3()  
        
       string f3( ) throw( int );
      // ...
   };

            
,   ?       ,  
           
  -  :

   // ,     
   void compute( Base *pb ) throw()
   {
     try {
         pb->f3( ); //     int  string
     }
     //  ,   Base::f3()
      catch ( const string & ) { }
      catch ( int ) { }
   }

    f3()   Base ,     
  int  string. ,  compute()  catch-
   .    f3()  
 Derived   ,    Base,  
      Base    
.
     11    ,       ,
   ,    . 
    ,        
,   . ,     ,
          ,
  . :

1012

   class stackExcp : public Excp { };
   class popObEmpty : public stackExcp { };
   class pushOnFull : public stackExcp { };

   void stackManip() throw( stackExcp )
   {
      // ...
   }

  ,  stackManip()   
   stackExcp,   popOnEmpty  pushOnFull. ,  ,
  ,     , ..
      .  popOnEmpty 
pushOnFull    stackExcp,     
 stackManip().

              19.2.7.    try-

      ,        try-.  try-
  . (     11.2.) :

   int main() {
      try {
         //   main()
      } 
      catch ( pushOnFull ) {
         // ...
      }
      catch ( popOnEmpty ) {
         // ...

   }

 try-   catch-   . 
    ,      
,     .
 try-    . ? 
   :

   _( _ )
      //   :
      : 1(1 ) , //  1
      2(2 ) , //  2
      //  :
   { /* ... */ }

1  2     ,  
,   .

1013

       Account,    14.   
 :

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

    ServiceCharge(),     _balance, 
 .    ,   
  ,  ,   
   Account?
    try-    :

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

   }

    try-     ,  catch-,
   ,     , 
  ,    ServiceCharge().
  try-    , ,
  ,    ,   
.    Account  try-  
 :

1014

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

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

     19.2.8.       C++

           ,  
      .   
C++   ,      
     .    
         
      .
       exception. 
     <exception>     
,     .  exception
  :

   namespace std {
      class exception
      public:
         exception() throw();
         exception( const exception & ) throw();
         exception& operator=( const exception & ) throw();
         virtual ~exception() throw();
         virtual const char* what() const throw();
     };
   }

        C++, exception  
  std,       .

1015

     -        ,
 ,     . 
  ,        -
,     .   ,
      exception.
        what(), 
 C-     . ,
  exception,   what()  ,  
 -.
   ,       exception  
 throw(), ..    .  
 - ( ,  catch- 
exception),  ,   ,    
  .
     exception,       , 
       , 
    :     
.
         , 
    . ,     
     .   
   :

   namespace std {
      class logic_error : public exception { //  
        public:
           explicit logic_error( const string &what_arg );
      };

      class invalid_argument : public logic_error { //  
      public:
         explicit invalid_argument( const string &what_arg );
     };

     class out_of_range : public logic_error { //  
     public:
        explicit out_of_range( const string &what_arg );
     };

     class length_error : public logic_error { //  
     public:
         explicit length_error( const string &what_arg );
     };

    class domain_error : public logic_error { //   
     public:
        explicit domain_error( const string &what_arg );
    };
   }

       invalid_argument,    
 ;   ,     
  ,    out_of_range, 
length_error       ,  
  .

1016

     , ,  ,    
. ,    ,    
.       :

   namespace std {
      class runtime_error : public exception { //   
      public:
         explicit runtime_error( const string &what_arg );
      };

       class range_error : public runtime_error { //  
       public:
          explicit range_error( const string &what_arg );
       };

       class overflow_error : public runtime_error { // 
       public:
          explicit overflow_error( const string &what_arg );
       };

       class underflow_error : public runtime_error { //  
       public:
          explicit underflow_error( const string &what_arg );
      };
 }

       range_error,     
 .  overflow_error   
 ,  underflow_error    .
    exception       bad_alloc, 
  new(),        
(.  8.4),     bad_cast,   , 
   dynamic_cast     (.  19.1).
     operator[]   Array   16.12 ,  
   range_error,    Array  
:

1017


   #include <stdexcept>
   #include <string>

    template <class elemType>
    class Array {
    public:
        // ...
        elemType& operator[]( int ix ) const
        {
          if ( ix < 0 || ix >= _size )
          {
              string eObj =
                  ":    Array<elemType>::operator[]()";
              throw out_of_range( eObj );
          }
          return _ia[ix];
        }
        // ...
      private:
         int _size;
         elemType *_ia;
   };

          
   <stdexcept>.   
   eObj  string.      
  - what():

   int main()
   {
      try {
         //  main()  ,    16.2
      }
      catch ( const out_of_range &excep ) {
         // :
         // :    Array<elemType>::operator[]()
         cerr << excep.what() << "\n";
         return -1;
      }
}

             try_array()
  ,     operator[]()  Array 
  out_of_range,    main().

                        19.5

        :

   #include <stdexcept>

   (a) void operate() throw( logic_error );
   (b) int mathErr( int ) throw( underflow_error, overflow_error );
   (c) char manip( string ) throw( );

1018

                         19.6

   ,      C++  
     ;    
.

                             19.7

       catch-   try-:

   #include <stdexcept>

   int main() {
      try {
          //     
      }
      catch( exception ) {
      }
      catch( runtime_error &re ) {
      }
      catch( overflow_error eobj ) {
   }
}

                              19.8

   C++:

   int main() {
       //   
}

    main() ,     , 
  .     
,   ,     abort() (
    <cstdlib>)   main().

                19.3.     A

            
(.  9.2). ,       :

   1.  -.

1019

   2.   .
   3.     .

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

                      19.3.1. -

            
     ,    
     ,     


   func( args );

    -       :

   object.memfunc( args );
   pointer->memfunc( args );

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

   namespace NS {
      class ZooAnimal { /* ... */ };
         void display( const ZooAnimal& );
   }

   //   Bear     NS
   class Bear : public NS::ZooAnimal { };

   int main() {
      Bear baloo;
      display( baloo );
      return 0;
}

1020

    baloo    Bear.    display()  
 ,       ,    , 
   ,     Bear    
ZooAnimal.       display(const
ZooAnimal&),     NS.
              -
   ,    ,      , 
        (.  15.10).   
   ,    ,    
  -   . ,  
  display()   - ZooAnimal:

   namespace NS {
       class ZooAnimal {
       friend void display( const ZooAnimal& );
       };
   }

   //   Bear     NS
   class Bear : public NS::ZooAnimal { };

   int main() {
       Bear baloo;
       display( baloo );
       return 0;
  }

    baloo  display()   Bear.     ZooAnimal
 display()  ,      
NS,      .    NS     .
   display()   Bear,    ZooAnimal
-    .
    ,       , 
   ,      ,  
-    :

    ,    ;
    ,     ,     
        ;
    ,         .

            -
      .   18.4  , 
 -     ,  
 -  ,      :

1021

   class ZooAnimal {
   public:
       Time feeding_time( string );
       // ...
   };

   class Bear : public ZooAnimal {
   public:
      //  ZooAnimal::feeding_time( string )
      Time feeding_time( int );
      // ...
   };

   Bear Winnie;
   // : ZooAnimal::feeding_time( string ) 
   Winnie.feeding_time( "Winnie" );

   - feeding_time(int),    Bear, 
feeding_time(string),   ZooAnimal,   Bear. 
-    Winnie  Bear,     
       Bear,  
  feeding_time(int).     ,  
.
           -
     ,  
   -      
  using-:

   class Bear : public ZooAnimal {
   public:
      // feeding_time( int )     ZooAnimal
      using ZooAnimal::feeding_time;
      Time feeding_time( int );
      // ...
};

      feeding_time()      Bear ,
,    :

   Winnie.feeding_time( "Winnie" );

       - feeding_time( string ).
          
 -          ,
   . :

1022


   class Endangered {
   public:
      ostream& print( ostream& );
      // ...
   {;

   class Bear : public( ZooAnimal ) {
   public:
       void print( );
       using ZooAnimal::feeding_time;
       Time feeding_time( int );
       // ...
   };

   class Panda : public Bear, public Endangered {
    public:
       // ...
    };

   int main()
   {
       Panda yin_yang;

       // : :  
       //      Bear::print()
      //       Endangered::print( ostream& )
      yin_yang.print( cout );

     // :  Bear::feeding_time()
    yin_yang.feeding_time( 56 );
}

      - print()     Panda
   Bear::print(),   Endangered::print().   
       ,       
        . 
    Panda     print().
   - feeding_time()    Panda
  ZooAnimal::feeding_time()  Bear::feeding_time()  
     Bear.       
    ,       
,   Bear::feeding_time().

 19.3.2.      

            : 
   .   ,  
        
 .
     15.9  ,     
     ,   
       
  .   
 :         

1023

explicit.        
    .
    ,     - . , 
     ZooAnimal:

class ZooAnimal {
   public:
    // : ZooAnimal ==> const char*
    operator const char*();
    // ...
};

     Bear      ZooAnimal.  
 Bear   ,   const char*,   
   Bear  const char*:

   extern void display( const char* );
   Bear yogi;
   // : yogi ==> const char*
 display( yogi );

          explicit  
  :       . 
   ZooAnimal:

   class ZooAnimal {
   public:
        // : int ==> ZooAnimal
       ZooAnimal( int );
       // ...
};

          int   ZooAnimal. 
  .  ZooAnimal   
   ,      :

   const int cageNumber = 8788l
   void mumble( const Bear & );
    // : ZooAnimal( int )  
    mumble( cageNumber );


1024

        Bear     mumble(), 
   .

                 19.3.3.    

              
 .      ,  
        
 .       ,  
 (     9.3):

              
     ;
               
      ;
            l- 
     .

      ,        ,
  :

   extern void release( const ZooAnimal& );
   Panda yinYang;
   //  : Panda -> ZooAnimal
  release( yinYang );

     yinYang  Panda      ,
    .
     15.10  ,      
,  :

   class Panda : public Bear,
                        public Endangered
   {
       //  ZooAnimal::operator const char *()
   };

   Panda yinYang;

   extern void release( const ZooAnimal& );
   extern void release( const char * );

   //  : Panda -> ZooAnimal
   // : release( const ZooAnimal& )

  release( yinYang );

    release(const char*),   release(ZooAnimal&)  
:  ,   -   
 ,   ,      

1025

const char*    ZooAnimal::operator const char*(), 
   .   
  ,      
  release(const ZooAnimal&).
           
       ,   
. ,      ,   
   .     Bear
,   ZooAnimal,  Bear    Panda.   
   release(const Bear&):

   extern void release( const ZooAnimal& );
   extern void release( const Bear& );

   // : release( const Bear& )
   release( yinYang );

        .   
           
   ,       
.       void*.
             ,
   void*. ,     :

   void receive( void* );
  void receive( ZooAnimal* );

         Panda* 

   receive(ZooAnimal*).

           
         ,  
    . , Panda   Bear
 Endangered.      Panda,  
 Panda       .   
        ,   
:

   extern void mumble( const Bear& );
   extern void mumble( const Endangered& );
   /* :  :
      *       
      * void mumble( const Bear& );
      * void mumble( const Endangered& );
   */

   mumble( yinYang );

1026

           :

   mumble( static_cast< Bear >( yinYang ) ); // 

            
,            
     . (  
     dynamic_cast,      19.1.)
        ,   
    ZooAnimal    :

   extern void release( const Bear& );
   extern void release( const Panda& );

   ZooAnimal za;

   // :  
   release( za );

          release(const char*). 
  ,      
 ,     const char*(). 
          
,  release(const Bear&)    ,  
  release(const char*):

   Class ZooAnimal {
   public:
      // : ZooAnimal ==> const char*
      operator const char*();
      // ...
   };

   extern void release( const char* );
   extern void release( const Bear& );

   ZooAnimal za;

   // za ==> const char*
   // : release( const char* )
  release( za );


                                   19.9

      :


1027

   class Base1 {
   public:
      ostream& print();
      void debug();
      void writeOn();
      void log( string );
      void reset( void *);
      // ...
   };

   class Base2 {
   public:
      void debug();
      void readOn();
      void log( double );
      // ...
   };

   class MI : public Base1, public Base2 {
   public:
      ostream& print();
      using Base1::reset;
      void reset( char * );
      using Base2::log;
      using Base2::log;
      // ...
   };


          :

   MI *pi = new MI;
   (a) pi->print(); (c) pi->readOn(); (e) pi->log( num );
  (b) pi->debug(); (d) pi->reset(0); (f) pi->writeOn();


                              19.10

      :

   class Base {
   public:
      operator int();
      operator const char *();
      // ...
   };

   class Derived : public Base {
   public:
      operator double();
      // ...
  };

             
?  ,      
   ,    (  ):

1028

(a) void operate( double );
   void operate( string );
   void operate( const Base & );

   Derived *pd = new Derived;

(b) void calc( int );
   void calc( double );
   void calc( const Derived & );

   Base *pb = new Derived;

   operate( *pd );
   operate( *pb );

