     12

      C++ 

        C++ -  
 .    ,  C++       
  ,      ,   .  
 13  ,         
- .       
 ,      C++. 
       C++ ("  ")    
 .  C++  "   ".      "  
",   C++       
    .  C++,  ., 
   " ".  , C++   ANSI    
     ,    
,  ,       
.   ,   C++     
    ,        
   .      ,  
C++        . 
        C++    80-   AT&T Bell Laboratories, 
     ANSI ,     
C++       .  ANSI   C++ 
,   ,   ,   ANSI , 
  C++.      C++ (,  
)    ANSI . 

       C++ 

      ,       ANSI ,    
C++.   ANSI   C++  .       
 ,      ,  ,   . 
      Borland C++   ,  
  ANSI ,    C++,  ,      
  .          
C++          .  .. 
 DOS bcc file.cpp  FILE.    C++.  
.      Borland C++,  bcc file  bcc 
file.cpp      .     C++ 
  ,     .    
 . 


     

         -,   Borland C++,  
   C++.      
,, , ,         ANSI  
  C++.         C++ 
 .  .H.      .  ., 
         ., 
      . 

      5        WELCOME.. 
 12.1     C++   .    
   DOS    bcc welcome  welcome. 
         
TargetExport  EasyWin   <Ctrl+F9> 

     12.1 WELCOME.CPP (  C++) 

    #include <iostream.h> 

    main() { 
       cout << "Welcome to Borland C++ programming!  \n" 
        return 0 ; 
    } 

          12.1   5.1. 
-,     IOSTREAM.H   STDIO.H 
(        ). -, 
  5      cout ("character 
out",  " ")   -    
 .         (  
,  ),   -   C++  
(" ").    <<   ,  
 ,   ,  WELCOME,     
-,  cout. 
           -.  
firstName  lastName      char *,   

    cout << lastName << "," << firstName << '\n'; 

   (lastName  ),    
  .     "\"   
'\'     .    
     iostream   .  
  ,     ,   : 

    cout << lastName 
    << "," 
    << firstName 
    << '\n' 

      -     
  ,   (,  int, long, double),  
 .,    .  count   long,   

    cout << "Count == " << count <<  '\n'; 

 ,   count   . 
        -      
.             
   III. 

     

        stdio    - 
,    printf()      
 C++. ,      15   - 
      .  prinf()  
        
 , ,        
. 


       C++ 

            C++,   
    ANSI   C++. 


     12.   C++     279 

    ANSI   C++        , , 
,   , , , , , 
  . 
    C++     ,   . 12.1 
   ( ),   C++.  C++  
   ,    . 

     12.1.   ANSI   C++ 

    asm 		default  	friend* 	protected* 	switch 	 void
    auto 		delete* 	goto 		public*  	template 	volatile 
    break		do  		if  		register	this* 	 while    
    case		double	inline*	return	throw* 
    catch* 		else 		int 		short 	try*  
    char 		enum 		long		signed 	typedef 	
    class*		extern	new*		sizeof	union 
    const		float		operator*	static	unsigned
    continue	for		private*	struct	virtual* 


    *  C++ 

    C++ ,         .   
ANSI    ,   . 

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

     C++     typedef-.   
   

    struct mystruct { 
    } ;

     mystruct x;.    
  ANSI ,    struct mystruct x;   
typedef   mystruct. 

     C++  char     8 .  ANSI      
,   Borland C++    8 . 

     ANSI      int,   sizeof('X') 
 sizeof(int).  C++     char,  
 sizeof('X')  sizeof(char). 

     

          ANSI C  C'++.  
      . 
    

     C++ 

    C++   ANSI   ,   BCPL  
  .   C++    
.      / /   .  
ANSI   

    /*   */ 

     C++  

    //   

     ANSI ,        C++, 
  : /*,   ,  */,   
.  ANSI        ,  
 C++    .      
  .  ANSI         
: 

    count++; /*  count   */	.      . . 


    280      III.   ANSI C++ 


     C++ ;    :

    count++;  //  count  

          ANSI .  
(    )   ANSI    C++:

    _result = count /*   */ + 100;

 , 

    result = count //  ??? + 100;

       ,    C++ 
          .

      

             
ANSI   C++.   12.2    .

     12.2.  COMMENTS.CPP (   ANSI   C++)

    1:     #include <iostream.h>
    2:
    3:     // ----------------------------------------------------------------------------------
    4:     //  :  
    5:     //  : V1.0
    6:     //  :     C++
    7:     // ----------------------------------------------------------------------------------
    8:
    9:     main()
    10:    {
    11:    cout  "A Brief C++ Commentary\n"; //  
    12:    cout  "\n"; //     
    13:
    14:    /*   ,      
    15:       .
    16:      C++   . */
    17:
    18:    cout  "// This is not a comment.\n\n"; //  
    19:    cout  "/* This also is not comment.*/ \n\n";
    20:    cout /*  . */  "This text is displayed.\n";
    21:    return 0;
    22:    }


    

            
    ,   COMMENTTS.CPP   . 
    Options\\Environment  Syntax
Higklighting\Customize,  Comment ,  Element   
      CFG). (  -.)

      3-7   C++   
,  .    "" 
    ,   
  , ,    ..
     11-12       C++
 .  14-16    
ANSI .      C++   
   / /.
     18-19   :    
     .  20 , 
   ANSI        ,
  ,  ,    .

      

        C++    ANSI   
,      .    :

    char buffer[128];	//   
    gets(buffer);	//    
    cout  '\n';	//   
    cout  buffer 	'\n';             //       


     12.   C++     281 


    ,        ,   
  ,  ,     cout <<
'\n'; ,       .    
 ,     ,    : 

    char buffer[128];	//    
    gets(buffer);           //     
    / 
    cout  '\n';	//      
    / 
    cout  buffer   '\n';	//        

     /*  */ ""    .  
     ,     
C++ //     .     
    .     
 ,      ANSI . 

       - 

           
-  C++ (.  15).    ANSI      C++  
  ,     .  
,  ,        
,    ,     
  -         C++   
  .  , ,  
  ,   
     ,   . 
       -     
-  ,    II      
11.  cout (character out)  cin (character in)   
 .    ,    

    cout << ; 

 

    cout.put(c); 

     put()   cout,   ,      
   .   -,   
  cout,   ,      . 
 cout   ,       , 
,  put(). (       
.)    -     , 
 cout.    

    put(c);  // ??? 

    put().  

    cout.put(); 

  put(),   cout. 
           ,  
   cin,     ( ).  
    cin >> ; 

          . 
        - get  cin,    : 

    cin.get(c); 

        -,     
          
-.  12.3    . 
 Windows    ,  FILTER. CPP 
  DOS,   bcc filter. 

     12.3.  FILTER.CPP ( -  C++) 

    1:	#include <iostream.h> 
    2: 
    3:	main() 
    4:	{ 
    5:	  char ; 
    6: 
    7:	  while (cin.get(c)) 
    8:	     cout.put(c); 
    9:	  return 0; 
    10:	} 


    282	 III.   ANSI C++ 

     FILTER     -,  
-  DOS. ,  FILTER    
,   DOS filter <filter.cpp.    
     filter >prn.     , 
 filter <filename.ext >prn. 
     7  FILTER.  cin.get(c)   .  
cin.get(c)    int      
 .  8        . 

        

         - C++    
    .  12.4    
        . 
     Easy Win-  Windows  
 DOS-    .    
(, XXX   )  ,  ,  
  . 

     12.4.    GETVAL.CPP (   - 
    ) 

    1:	#include <iostream.h> 
    2:	#include <stdlib.h> 
    3: 
    4:	void test(void); 
    5: 
    6:	main() 
    7:	{ 
    8:	  double fp;  //   
    9:	  long k;  //   10: 
    11:	  cout  "Enter a floating-point value:   "; 
    12:	  cin  fp; 
    13:	  test(); 
    14:	  cout  "Value entered is:   "  fp   '\n'; 
    15:	  cout  "Enter an integer value:   "; 
    16:	  cin  k; 
    17:	  test(); 
    18:	  cout  "Value entered is:   "  k   '\n'; 
    19:	  return 0; 
    20:	} 
    21: 
    22:	void test(void) 
    23:	{ 
    24:	  if (!cin.good()) { 
    25:	    cout  "Error detected"; 
    26:	    exit(1); 
    27:	  } 
    28:	}

     11   ,    
    fp  double   12.   
   16   .     
-,         scanfO  
 ,       , 
 -    ,   
 -.        
,       ,       
  -.  C++     
   ! 
     13  17       
-.      testO,   
 22-28. 
        cin.good()  -,  
 TRUE,        .  
 cout. good()       .   
 ,  cin.good()  FALSE,  25-26  
    . 
           .   
     

    cin.clear(); 

      15    -   -. 


     12.   C++	283 


       

      ,    -   
 . ,      
    

    char *s = "Gently down the iostream"; 

 

    cout  s; 

          
,   .        
   cin   >> ( ) 

    char buffer[128];         //    
    cin  buffer;	    // ??? 

      , ,     127 , 
      ,  , , 
  ,   ,  .   
    . 
     ,      ,   
   12.5.     ,  
    24 .      
   ,    5. 

     12.5.  GETSTR.CPP (    
  ) 

    1:	#include <iostream.h> 
    2: 
    3:	main() 
    4:	{ 
    5:	  char s[25]; 
    6:	  char c; 
    7: 
    8: 	  cout  "Enter a 24-char string safely: \n"; 
    9:	  cin.get(s, 25, '\n'); 
    10:	  cout  "You entered:	"  s  '\n'; 
    11:	  if (cin.get(c) &&  != \n') 
    12:	    cout  "Maximum line length reached\n"; 
    13:	  return(); 
    14:	}
  
      9        
   -. - cin.get()  
  . 

                .   -   
                  . 
          . 
       ,  .   ,     '\'. 

             ,  
,    9, .     . 
     ,  ,     
      cin.get(),      
1112.  cin.get()     ,   .  
     . 
           -:  
  - cin.getline().   24-   
25-  (        
 )   : 

    char buffer[25]; 
    cin.getline(buffer, 25); 


      

     ,           
.   v,    ,  

    cout  v; 

  ,     .  
        ,    
,          . 
,     .     
int *p (    ),  

    cout  ; 


    284	 III.   ANSI C++ 

      .     
, , ,    C++    
 .

         -

      -    
 (      15).  12.6 
       dec, hex, 
oct       ,   
.

     12.6. CONVERT.CPP (     -)

    1:	#include <iostream.h>
    2:	#include <stdlib.h>
    3:
    4:	#define SIZE 35
    5:
    6:	main()
    7:	{
    8:	  int value;
    9:	  char s[SIZE];
    10:
    11:	  cout  "Value? ";
    12:	  cin.get(s, SIZE, '\n')
    13:	  value = atoi(s);
    14:	  cout  "Decimal=" << dec << value
    15:	  << "Hexadecimal=0x" << hex << value
    16:	  << "Octal=0" <<  oct << value  '\n' ;
    17:	  return 0;
    18:	}

     14-16         .    
 .    .  dec, hex,  oct,
      ,  
   .
     ,         
cout.width(n),      . ,   
 15      ,    :

    int value = 1234;	//  
    cout.width(15);	//    15 
    cout << value;	//    15 

           .
       .   
 , -  C++   
  ANSI   C++.  12.7    
,    CONVERT. PP.


     12.7.  CONVERT2.CPP (   )

    1:    #include <iostream.h>
    2:    #include <stdio.h>
    3:    #include <stdlib.h>
    4:
    5:    #define SIZE 35
    6:
    7:    main()
    8:    {
    9        int value;
    10:      char s[SIZE];
    11:      char buffer[80];
    12:
    13:      cout  "Value? ";
    14:      cin.get(s, SIZE, '\n');
    15:      value = atoi(s);
    16:      sprintf(buffer,"Decimal=%d Hexadecimal=%#x Octal=%#o\n"
    17:      value, value, value);
    18:      cout  buffer;
    19:      return 0;
    20:   }

      CONVERT. CPP  
sprintf()   16-17.  sprintf(),   STDLIB.H, 
     , -


     12.   C++     285 

   printf()    .  16
  %d (), %# (), %# ()
        . 
    18       .

        

           
   ANSI ,    C++. ,   
 int count,        
,        :

    int count;	//   void AnyFunction()
    {
      int count;       //  
     .....
    }

        count   
 .      count 
        .   
  C++       ::.
     :

    int count;     //  
    void AnyFunctionO
    {
      int count;                //  
      count = 1234;       //    
      ::count = 4321;     //     count
    }

        1234   count  4321  
.  ::count  C++,   
 count,    (. 12.1).


    . 12.1.        
  


     12.8     
C++.        C++: 
C++      ,       
,   ANSI . ,    ANSI ,    
  .

     12.8. SCOPE.CPP (   )

    1:    #include <iostream.h>
    2:
    3:    int k = 100; //  
    4:
    5:    main()
    6:    {
    7:
    8:      int i = 200; //  
    9:      cout  "Global k == "  k  '\n';
    10:     cout  "Local i == "  i  '\n';
    11:     {

    286      III.   ANSI C++ 


    12:	    int k = 300; 
    13	    cout << "Local k == " << k	<< '\n'; 
    14:	    cout << "Global k == " << ::k << '\n'; 
    15:	  } 
    16:	  return 0; 
    17:	} 

      3     100  
 k;    7   i,    
main().  9-10   k  i.    
     9,       
 k. 
     11-15   -,    
main().       ,    
    ( 11  15). ,    
    if, while   .  
    12      k, 
  300.    ANSI , C++   
      -.    
  k: ,    3,  ,  
  12.    k      
 ,   13    .  
    ,    
 ,     14. 
    ,   -,    
  .  ,      
 .      : 

    if (expression) { 
       int count = 0;	//      
                                   //  if 
       ......
   } 
   cout  "count == "  count  '\n';   // ??? 

           ,  
 count      if,   
  . 
          , 
  for.  ANSI     : 

    void f() 
    { 
       int i; 
       for (i=0;  i<MAX; !++ ) 
            .......
    } 

     C++        , 
   : 

     void f() 
    { 
       for (int i=0;   i<MAX;  i++) 
          .....
     }

              
for.    (   i=0;)  
 ,   ,  , i      
  f()     ,   for. 

     

        C++,   ANSI .  
    const    " ", 
       .    
  count  : 

    const int count =1234; 

        

    count++;	// ??? 

         const  
 ,    #define.   

    #define MAX 100 

    ,    100 (  
 100).     : 

    for (int i=0;  i<MAX;  i++) 


     12.   C++     287 

        100    
,         . 
       C++ ,      
  ,  .   const 
   : 

    const int MAX = 100; 

     for   ,       
 . 

               . C++ 
,      int,       
      . 

      Turbo Debugger    .   
  ,    #define. 

         .  ,    
        . 
       , 
   .  ,     
     .    
   ,     . 
         ,     
,         
. ,     ,   
  #define. 

      

          ,    C++, 
 ,      .    
 ,     ,   
    .      
   !       
  .    ,  ,  
       . 
      ,      
   ,       
.     for: 

    for (int i = 0;   i < MAX;   i++ ) { 
       AnyFunction(); 
    } 

     MAX  ,   Any Function()   
         . 
, AnyFunction()   : 

    void AnyFunction(void) { 
       cout  AnyValue  '\n';
       AnyValue++; 
    }

     , , ,      : 
  AnyFunctionO    for   : 

    for (int i = 0; i < MAX; i++) 
          cout  AnyValue  '\n'; 
          AnyValue++; 
    }

           .  
   ,    for: 

    cout  AnyValue  '\n'; 
    AnyValue++; 
    cout  AnyValue  '\n'; 
    AnyValue++; 
    cout  AnyValue  '\n'; 
    AnyValue++; 


    288	 III.   ANSI C++ 


    ,    *2 , ,    
,     .   , 
          ! 
    ? ,         
          "".  
, C++     .  
       inline (), 
       .   ,    12.9. 

     12.9.  INLINE.CPP (  ) 

    1:   #include <iostream.h> 
    2:
    3:  inline int max(int a,   int b) 
    4:         { 
    5:            if (a >= b) 
    6:                 return a; 
    7:             else 
    8:                return b; 
    9:          } 
    10: 
    11:	main() 
    12:	{ 
    13:	  int , , z; 
    14: 
    15:	  cout  "X? "; 
    16:	  cin  x; 
    17:	  cout  "Y? "; 
    18:	  cin  y;                                            
    19:	  z = max(x, ); 
    20:	  cout  "max(a, b) == "  z  '\r\'; 
    21:	  return 0; 
    22:	}

         ,     
 inline,     3.     ( 
4-9).        ,     
,     .   
, " ,     3-9,   
      ,    
.       , 
    ,         
     . 
          ,    
. ,   19  ()     
        z. ,  
  Borland C++    m()   19. 
        , 
  19,        : 

    if (x >= ) 
      z = ; 
   else 
      z = ; 

    ,    , . ,  max() 
    ,     . 

     

        .    
 ,     , ,  , 
     ,    . 
,     ,   ,     
  .   ,  ,  ,  
       ,    
  .      Turbo Debugger 
        , 
   .    C++   
   ,      . 


     12.   C++         289 
    10  Borland C++4.5 

        new  delete 

             
 ANSI   C++.  malloc(), farmalloc(), farcalloc(), free(), farfree()  
         C++.  C++  
    new  delete,    
  ,    ,   . 
     new     .  
free (?!),   ,  new,     
  new.   ,  new  delete   
,   .   , ,     
 , C++     , 
 new  delete.       
       
   .      
  - ,    13. 
        ,   double,    
 ,    

    double *dp = new double; 

         ,   new   
 : 

    double *dp = new(double); 

      ,      .  dp 
 ,       ,  malloc()  
 .     ,   
  dp,  

    dp = 3.14159; 

       ,    : 

    delete dp; 

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

     ,     

           , 
   .   new  , 
        
.       : 

       new   ; 
       new  . 

       C++  new        
   ,     
.    ANSI C++    new 
    ,    ,  
 15.       C++,    
 ,    new   , 
  ,        . 
         NEW.H      
   main()         new: 

    set_new_handle(0); 

     set_new_handler()    ( 
 ),  new.     new 
    . 

    

         C++   Borland C++ 4.3, , 
   set_new_haadler( 0 )      
  .         , 
      "abnormal termination" 
( ),      new 
      . 
    

    290	 III.   ANSI C++ 
	

     12.10 ,   new     
,   ,  ,     
 .        
EasyWin-         DOS. 
      "Unreachable code" 
( ). (   ,     
 .) 

     12.10.  MEHERR.CPP ( new  ,    
 ) 

    1:	#include	<iostream.h> 
    2:	#include	<new.h> 
    3:	#include	<stdlib.h> 
    4: 
    5:	struct q	{ 
    6:	  int ia[1024]; // 2048-  
    7: 	};
    8.	 
    9:	main() 
    10:	{ 
    11:	struct q	*qp; //    q 
    12: 
    13:	set new handler(O); //  new      
    14:	for (;;)  { //    " " 
    15:	   qp = new q; //     q 
    16:	   cout 	qp,  ' \n ' ; //,    
    17:	   if (qp == 0) { //    new 0 : 
    18:	      cout  "Out of memory\n"; //     
    19:	     exit(1);	//      1 
    20:	  } 
    21:	} 
    22:	return 0	;//      "unreachable code" 
    23:	}
 
          5-7    
 1,024  .     new  
2,048-  (  ,      
).     ,    
 , ,  ,  new     . 
     new     ,   0  
set_new_handler().   ""  for   
.     new   2048-   
    qp.     , 
  qp.  qp  ,   ,  new    
 ,         
      exit().     
     (    ,   
   ): 

    0x1366 
    0x18 
    ...........
    0x0000 
    Out of memory 

        set_new_handler(), new  
   ,   .   
 ,     set_new_handler(), 
     .   : 

    0x1366 
    0x18 
    .......
    0x0000 
    Abnormal program termination 

          ,   
 ,        exit().  
  new   ,    
 . 

     

       new   ,   
          
 new ,      ,   
   .  ,    ,  
     "Abnormal program tenainatjon" ( 


     12.   C++     291 


).      ,  
   .    ,  
        , 
    15. 

        

     MEMERR.CPP ,     ,  
         new. 
,  ""  for    
,         
    ,      
. -       ,  
 return   22 "".   ,  
   ,      
   ,      
  -  . 
         new    
,   ,   . ,   
    MEMERR: 

    struct q { 
    int ia[1024];	// 2048-  
    }; 

  new       q: 

    struct q *qp;	//       q 
    qp =     new q;                    //       q 

     new  ,  qp     
 q.       ,    
  .      , 
        delete. 
         new: 

    delete qp;  //    ,   qp 

          ,   
       ,    
.  MEMER.CPP,      , 
     . 

     

           
.   ,      
    . 

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

    double *dp;	//      
    dp = new double;	//       dp 
    *dp = 3.14159;	//     
    delete dp;	//   .    
    *dp = 9.9999;	// ???  !    ! 
    delete dp;               // ???  !    

      ,    .  
 dp    ,    
     .   
,      ,    , 
     ,          
     .     
,         . ,  
         new: 

    dp = new double;	//      
    *dp = 3.14159;	//     
    delete dp;	//  .    dp  ! 
    ....................
    dp = new double;  // ,         
	                 //  
    *dp = 9.9999;       //      
    delete dp;            //  .   ,      dp! 


    292      III.   ANSI C++ 

      

       new     
  .  12.11 ,   .

     12.11. NEWSTR.CPP (    new)

    1:	#include <iostream.h>
    2:
    3:	#define SIZE 80
    4:
    5:	main()
    6:	{
    7:
    8:	  char *sp
    9:	  sp = new char[SIZE];
    10;	  cout 	"String?";
    11:	  cin.getlinefsp, SIZE);
    12:	  cout  "You entered:"  sp  '\n';
    13:	  delete[]	sp;
    14:	  return 0
    15:	}

     9     SIZE    
     sp.  10-12  
 ,     .
     13    . Borland C++ 
      :

    delete sp; // ???,    Borland C++

        C++,    
  delete[],  ,   ,  
 . ( Borland C++      , 
   ,    .)
       C++       
.  C++  2.0, ,  15    delete(SIZE]
sp;. Borland C++  SIZE       
    delete.

      

    ,       ,   ,  new 
delete        
  ,     .  12.12  .

     12.12. NEWARRAY.CPP (    new)

     1:     #include <iostream.h>
     2:
     3:     #define COUNT 100 //   
     4:
     5:     main()
     6:     {
     7:     int *array; //   
     8:     int i; //  
     9:
    10:     array = new int[COUNT];
    11:
    12:     //  
    13:     for (i = 0;  i < COUNT;  i++)
    14:     array[i] = i;
    15:
    16:     //   
    17:     for (i = 0;  i < COUNT;  i--) {
    18:       cout.width(e);
    19:       cout  arrayfi];
    20:     }
    21:     delete[] array;
    22:     return 0;
    23:     }

         7      .
  10  new     100  . 
    0  99   


     12.   C++     293 


    8-     for   17-20.  21 
 ,      delete[]  , , 
    ,  ,   ,   
  . 

       

        -    (  
)        . 
,       10x20  
  .     

    int (*matrix)[20]; 

      matrix,      20 .   
    (*matrix),     
  ,    *.      
;         ,  matrix 
     .      
 10x20      matrix,    : 

    matrix = new int[10][20]; 

     new    . ,   
,  matrix.    8x8x8   : 

    int (*matrix)[8][8]; 
    matrix = new int[8][8][8]; 

      matrix      , 
 ,  matrix      
8x8.  new    ,    
   8x8,     . 
             
 ,       . , 
   v  40,    

    int (*matrix)[20]; 
    matrix = new int[v][20]; 

      40x20  
 ,  matrix.     
  ,       : 

    delete[] matrix; 


      

         ,     
     . ,    
           
   f29q(),    BattingAverage(). , , 
  . 
           .   , 
     .    
   ,    ""   
DrawEllipse, DrawCircle, DrawSquare, DrawLine  .. , ,  
   ,  ellipse, circle, square  line, , 
  ,   ,   : 

    DrawEllipse(ellipse); 
    DrawCircle(circle);
    DrawSquare(square); 
    OrawLine(line); 

       ,          ( 
 Draw)    ?     : 

    Draw(ellipse); 
    Draw(circle); 
    Draw(square); 
    Draw(line); 

         ,   
.  C++         , 
      .  "",  
  ,      .   Draw  
       , 
    ,      .   
C++    ,   ,      . 


    294	 III.   ANSI C++ 

      ,     
.   12.13     
   .

     12.13.  OVERLOAD.CPP ( )

    1:      #include <iostream.h>
    2:
    3:      int square(int a);
    4:      double square(double a);
    5:      long double square(long double a);
    6:
    7:      main()
    8:      {
    9:      int x = 10;
    10:     double  = 20.5;
    11:     long double z = 30.75;
    12:
    13:     cout  square(x)  '\n';
    14:     cout  square(y)  '\n';
    15:     cout  square(z)  '\n';
    16:     return 0;
    17:     }
    18:
    19:     int squarefint a)
    20:     {
    21:     return a * a;
    22:     }
    23:
    24:     double square(double a)
    25:     {
    26:     return a * a;
    27:     }
    28:
    29:     long double square(long double a)
    30:     {
    31:     return a * a;
    32:     }

        square()    3-5. 
   , ,   ,  ,  
        .   
  19-32  , ..   .
      13-15 ,      
.      , ,   
  .    ,    
  squarelnt(), squareDouble()  squareLongDouble(). , ,
   ,       ,
 .            
 (  ,        
),       ,  ,
        .

       

      C++       
.  ,       
,   . ,   ,  
   ,  :

    int sum(int a,  int b,  int c,  int d)
{
    return a + b +  + d;
}

           (  vl  v2) 
    :

    cout  sum(v1,  v2,  0,  0);

      ,       
  sum()  ,  ,   
 .   -1   
    d,     
,     .


     12.   C++     295 

             , 
     .  
         
    :

    int sum(int a,  int b,  int  = 0,  int d = 0);

      sum()       ,   
     .     
          
.        d,     
,    .    :

    cout  sum(1,  2);		//  == 1,  b == 2,    == 0,  d == 
    cout  sum(1.  2,  3);	//  == 1,  b == 2,   == 3,  d == 
    cout  sum(1,  2, 3, 4);	//  == 1, b == 2,  == 3, d == 4

     12.14        
.        
  ,      .
   CenterO -    :

    *** Error: You goofed big time! **** < Press any key to continue >

     CENTER. CPP   12.14  Easy Win- 
DOS-.     ,  
  CenterO,    
  .

     12.14.  CENTER.CPP (    )

    1:      #include <iostream.h>
    2:      #include <string.h>
    3:
    4:      #define SIZE 128
    5:
    6:      void Center(const char *instr, char outstr,
    7:                           int width = 0, char IFill = '-', char rFill = '-');
    8:
    9:      main()
    10:     {
    11:     char instr[SIZE]; //  
    12:     char outstr[SIZEJ; //  
    13:     int length; //    
    14:
    15:     cout  "Enter a string:";
    16:     cin.getline(instr, SIZE / 2);
    17:     length = strlen(instr);
    18:
    19:     Center(instr, outstr);
    20:     cout  outstr  '\n';
    21:     Center(instr, outstr, length + 8);
    22:     cout  outstr  '\n';
    23:     Center(instr, outstr, length + 16, '*', '*');
    24:     cout  outstr  '\n';
    25:     Center(instr, outstr, 78, '<', '>');
    26:     cout  outstr  '\n';
    27:
    28:     return 0;
    29:     }
    30:
    31:     // instr:    
    32:     // outstr:    
    33:     // width:     
    34:     // fill:     
    35:
    36:     void Center(const char *instr,  char *outstr,
    37:                           int width, char IFill, char rFill)
    38:     {
    39:     if (outstr == 0) //      
    40:        return;           //   -  
    41:
    42:     if (instr == 0) { //    ,
    43:          *outstr = 0; //   
    44:                             //  . }
    45:          return;
    46:     }
    47:     //  .    



    296      III.   ANSI C++ 


    48:	int len = strlen(instr); 
    49:	if (width < len) 
    50:	   width = len; 
    51:	int wd2 =	(width - len) / 2; 
    52: 
    53:	int i; //	  	    for 
    54: 
    55:	for ( i = 0; i < wd2; i++) 
    56:	   outstr[i]	= IFill; 
    57:	for ( /*i= i*/; i < wd2 +	len; i++) 
    58:	   outstr[ij	= instr[i - wd2]	 
    59:	for (/*i	= i*/; i < width;	i++) 
    60:	   outstr[i]	= rFill; 
    61:	outstr[i]	= 0; //    
    62:	} 

         Center()     : 

    void Center(const char *instr, char *outstr, int width = 0,  char IFill = '-',  char rFill = '-'); 

            .   
    const        
.        , 
         
,     Center().    , 
        : 

    Center(instr,  outstr); 

         ,    
.    Center       
,    .    
        
   . 

     

      9  ,   ,   
       .    

    int i = 1234; 
    int *p = &i; 

   i,   1234,  
   ,  i.      
     i: 

    cout  "i == "  i  '\n'; 
    cout  "i == "  *p '\n'; 

     *  ,    , 
 . 
            
         ,   
,          .   
   ,      
.  ,      , 
 -. 
    C++         
 .   ,   , , 
 ,    , ,    , 
    alter ego (" ").     
   .
    ,  ,    ,   
  . 

        

          . ,   
      ,    
    .     
12.15  Easy Win-  DOS-.      
    . 

     12.15. REFVAR.CPP (  ) 

    1:     #include <iostream.h> 
    2: 
    3:     main() 
    

     12.   ++     297 


    4:	{ 
    5:	int ivar = 1234;	//    
    6:	int *iptr = &ivar	;    //    ivar 
    7:	int &iref = ivar;	//    ivar 
    8:	int *p = &iref;	//    iref 
    9: 
    10:	cout  "ivar ==	"  ivar  '\n' ; 
    11:	cout  "*iptr ==	"  *iptr  '\n' ; 
    12:	cout  "iref ==	"  iref  '\n'; 
    13:	cout  "*p == "	 *p  '\n' ; 
    14:	return 0; 
    15:	} 

      5-8   .     ivar, 
  1234. .       
iptr,    ivar.     .  

    int &iref = ivar; 

 iref     .   iref,   
 ivar    ,     .  
, iref   ivar  .     
12,  

    cout  "iref == "  iref  '\n'; 

  ivar  1234.     iref 
      ivar  . 
        8      ,  
 ,   iref.  6  8   
.     ,   ivar.  
   13,     *    
 ivar.  . 12.2     
 12.15.    ,  iref    
   *.     
   ivar  1234 


    . 12.2.     12.15, REFVALCPP 


          :  
 ,     .  REFVAL.CPP  
          iref: 

    int anotherlnt;        
    iref = anotherlnt;       // ??? 

        iref   anotherlnt.   
  anotherlnt ,   iref ,  
  ivar.        ,  
  . ,  

    iref = 4321; 

 ivar  4321.     ,  
      ,   
 (   !). 
       ,       
.  7,  ,      

    int &iref;       // ??? 

       ,      
      (   
   ),     .  
     . 

     
    
            
:       extern,    
  ,       
      (.  13). 


    298      III.   ANSI C++ 


      ,     
.       . 
   13  14  12.15  : 

    iref++ ; 

      ,  ivar - ,   
 iref.        iref. 
         . ,    
   .  

     int &iref = 1234;	// ??? 

     ,  1234   
,   iref  .  , ,  
      : 

    int  = 1234;	//     
   const   int &iref = x;           //  iref      

       ,      
,    . ,    
         : 

    cout  "iref = "  iref  '\';        //    

          : 

     = 4321;        //   

     iref  ,  , ,     
   : 

    iref = 4321;   // ???  

             
 "Cannot modify a const object in function main()" (  
    mainO). 

         

       ,      
  .        
  ,     . 
    ,         
       .  
   ,   ,    
  .  12.16     TAX.   
 5    .      
Easy Win-  DOS-,         
.          . 

     12.16. . (     ) 

    1: #include <iostream.h> 
    2: 
    3: #include <stdlib.h> 
    4: #include <stdio.h> 
    5; 
    6: double GetDouble(const char *prompt);             
    7: void GetData(double Spaid, double irate);   
    8: void Calculate(const double paid, const double rate, 
    9;  double &list, double &tax); 
    10: 
    11: main ()
    12: { 
    13:  double paid, rate, list, tax; 
    14:  GetData(paid, rate); 
    15:  Calculate(paid, rate, list, tax); 
    16:  printf('List pride = $%8.2f\n", list); 
    17:  printf("Tax paid = $%8.2f\n", tax); 
    18:  return 0; 
    19: } 
    20: 
    21: //       
    22: double GetDouble(const char prompt) 
    23: { 
    24:  char s[20]; //    
    25:  printf (prompt); //    
    26:  scanf ("%20s", s); //     
    27:  return atof(s); //    
    28:  } 
    

         12.   C++     299 


    29: 
    30:	//          
    31:	// :   paid  rate 
    32:	void GetData(double &paid, double &rate) 
    33:	{ 
    34:	paid = GetDouble("Price paid?"); 
    35:	rate = GetDouble("Tax rate (ex: .06)?"); 
    36:	} 
    37: 
    38:	//         
    39:	// :   list  tax                             
    40:	void Calculate(const double paid, const double rate, 
    41:	double &list, double &tax) 
    42:	{ 
    43:	list = paid / (1 + rate); 
    44:	tax = paid - list; 
    45:	}

        .     
        , 
  : 

    double GetDouble(const char *prompt); 

           
    . ,     
  &, ,       
(..  GetData     ): 

    void GetData (double &paid, double &rate); 

       GetData.     GetDouble  
-,   ,    
   paid  rate: 

    paid - GetDouble("Price paid?"); 
    rate = GetDouble("Tax rate (ex: .06)?"); 

     paid  rate  ,   , 
  GetDouble(). ,   main()   
    ,   paid  rate: 

    GetData(paid, rate); 


     

         gj ,  , 
    ,       
 .         
     .   
 ,   ,   GetData  
 paid  rate 

    ,  ,         , 
      .   , paid 
 rate,    .     ,  
 ,      (,   
).   , list  tax,     . 
    , ,   : 

    void Calculate(const double paid, const double rate, double &list,  double &tax) 

     Calculate    list  tax   : 

    list = paid / (1 + rate);	
    tax = paid - list;                     

       ,    , 
  Calculate().     , 
    ,    main 
  : 

    Calculate(paid, rate, list, tax); 

    ,   ,    , 
  .    Calculate  
  . 
           .   , 
    C++  .    
    ,    : 

    void f(int *ip) 
    *ip = 1234; 


    300      III.   ANSI C++ 


           ,  
    ip,     

    f(&ivar);   //   ivar 

       *ip = 1234;   1234  
ivar,     f().    
,   : 

    void f(int &ir) 
    ir = 1234; 

     ip   ir,    1234.  

    f(ivar);   //  ivar   

   :  ivar no   
f(),   ir   ivar,  ivar  1234. 

         

           ,    
,   . ( ,    
     ).  , 
  

    double &ref(double d); 

     ,    
     ,   -   . 
     12.17       
   .     
 ,      , ,  
 ,      ,  
  ANSI .    C++     
,     ,  ,   
    .    ,  
     0  9,    
.        <Enter>. 

     12.17.   REFFUNC.CPP ( )

    1:	&include <iostream.h> 
    2:	&include <stdlib.h> 
    3:	&include <string.h> 
    5:	&define FALSE 0 
    6:	&define TRUE 1 
    7:	&define SIZE 10 
    8:	&define BUFLEN 20 
    9: 
    10:	double &ref(int index); 
    11:	void ShowArray(void); 12: 
    13:	double array[SIZE]; 14: 
    15:	main() 
    16:	{ 
    17:	int done = FALSE, index; 
    18:	char buffer[BUFLEN]; 
    19: 
    20:	for (index = 0; index < SIZE; index++) 
    21:	   ref(index) = index; //   ! 
    22:	while (Idone) { 
    23:	  ShowArray(); 
    24:	  cout  "\nEnter index from 0 to 9, Enter to quit:"; 
    25:	  cin.getline(buffer, BUFLEN); 
    26:	  done = (strlen(buffer) ==0); 
    27:	  if (Idone) { 
    28:	    index = atoi(buffer); 
    29:	    cout  "Enter floating-point value:"; 
    30:	    cin,getline(buffer, BUFLEN); 
    31:	    ref(index) = atof(buffer); 
    32:	  } 
    33:	} 
    34:	return 0; 
    35:	} 


     12.   C++     301 

    36:
    37:	double &ref(int index) 
    38:	{ 
    39:	  if ((index < 0) | | (index >= SIZE)) 
    40:	    index = 0; 
    41:	  return array[index]; //	     
    42:	} 
    43: 
    44:	void ShowArray(void) 
    45:	{ 
    46:	  cout  "\nArray :\n"; 
    47:	  for (int i = 0; i < SIZE ; i++) 
    48:	     cout << "["  i  "] == "  array[i]  '\n'; 
    49:	} 

            
 13.           
.          ,  
         
 main().       
     (,   
  ),         
 main().
      10      ref().   
     ,  
 -.      
   ,       
 ,     21. ,    
  .    21   
   ,    ref(index). 
     .  
    . 
      31       . 
      atof()   
ref(index).  ref  ,      : 

    ref(index) = atof(buffer); 

            
 , ,   ,   ,   
..       . 
         37-42.  if  
     ,    return  
 41          
  .  ,    41   
   .      
.   ref()     ,   
        ,   
  main(). 

     

        C++   ,  -   . 
C++   ,    .   ,  
   ANSI ,    C++,   ,   
ANSI ,   C++. ,    C++  
  ANSI . 
         ,    ,  
.  Borland C++,       C++. 
        ,  
 ,   C++,    
    ..     .. 
       C++    ,      
ANSI .  C++          
  ANSI . 
        C++     .   
   . 
        C++   :    
 ,     typedef-,  
char   8- .  C++   ,  
'X',   char.  ANSI      int. 
         //    C++  ,  
 ,    .  C++  ANSI  
( /*  */)      . 
 C++      ANSI . 
    
    
    302	 III.   ANSI C++ 


      -    ( )   ( ) 
    .   cout    
 .   cin    .   
         . 
          
-   get().      
    cin.get()  cin.getlineO. 
-  .       C++  
 (.  13). 
               
   -,  cout.width().   
,       
  dec, hex  oct.     
  ( ,  sprintf )       
 . 
      C++         . 
,        for.  
    ,    . 
,       .   
     ,     
 .       
 ::,      .   
  count,   ::count    . 
          C++   
 const  #define    .   
        
   .  , Turbo Debugger  
,    const,   #define.   
, ,     ,    
     .  ,   
 ,   C++, ,  #define  
 ,     . 
            
  ..       , 
   ,     , 
   . ,    inline, 
 register,    ,    
.    ,    . 
(     ,   
  .) 
              
  new  delete.    C++  new  
delete      ,  
malloc(), calloc(), free()  .        
  C++. ,  new, ,    
delete. ,     malloc(),   
 free(). 
         .     
       ,     
   .       
   ,  DrawCircle  DrawEllipse.    
      . 
,           ! 
               
  .        
 ,    . 
    ,    &,   .    
,      ,    
   (   ),    
       . 
          ,  
   ,    .  , 
   -,     .  
,        .  
        , 
          . 

     

    12.1.   -,  -, 
    . 
    12.2.   12.4 ,      ,  
      . 


     12.   C++     303 