21

                                             I

                                         C++


   ,   ,    :

    ;

    ,  .

            ,
    ( ) . .
         
 . ,  ,     .  
   :       .
        .  
     FORTRAN, C  Pascal.
++    .    
   .   III  ,  
++   : ,  ,
 .
    70-        
 (    ).     
   .  ++    
.         
  ,  , ,    ..
,   ,    .
    .    
  ,  CLU, Ada  Modula-2.   IV  
    ++.
   -     
   (   ) 
  (   ).
  -. , , -     
,       ( ) 
 ,  ,     . 
         
,     .    ,
 - ,   Simula, Smalltalk  Java. 
 V     -
  ++.
       ++   - , 
  ,   .    
,         . 
    : ++     
.
     I       ++.    
         
,      . ,  
         13,  
         .

++   22

      ,     .   
          , 
        , ,  
. ,   ,    .     
 .       
  ,      ++.
     1    :   , ,
,  (statements)  .    
++ ,   ,    
/.
     2      , ,  -
.      , 
  ++,      
 .     ,  ,  
 . ,      ++, 
     .
   ,     2   . 
   ,     . 
       ,    
  .   3      .
       2.

                                    1. 

         :   ,
  ,   ,  
  .    
 ++ ,     
, ,   ,      
   .     ,   ++
.

                                 1.1.  

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

++   23

        .
          .
      .
    .

      1, 2  4 ,        .
       .    .

   3a.    .
   3b.       .
   3c.       .   
               .

      .   ,   ,  .
 ,  ,        .
   :

        .

     :   ,     
   .

     ,  .

       .

       . 
        ,    
 ++.

                            1.2.    C++

    ++   ,  ,   
,  .      ++ ,  
  ++    .  
 ++:

   int book_count = 0;
   book_count = books_on_shelf + books_on_order;
   cout << "  book_count: " << book_count;

         . book_count
  ,   (  )
 .      ,  
  (   book_count),     
(   ). 0   .  book_count 
 0.
      .     , 
 book_count,      
books_on_shelf  books_on_order. ,    
  -        .

++   24

       . cout    ,
  , <<   .     cout  
      ,    
("  book_count: "),  ,    ,
   book_count.     
    :

     book_count: 11273

  book_count  11273     .
        ,  . ,
 ,     ,   
readIn().       
   sort(), compact()  print().
     ++         main().  
      :

   int main()
   {
      readIn();
      sort();
      compact();
      print();

      return 0;
   }

           main(), 
     readIn().      
 , ,     main(),
  .
       :   , , 
   .      .
              
,  .    
     .
        main()    readIn(), sort(),
compact()  print().   

   return 0;

    return     .  
return    (   0),  
   .     
0      main(). ( ++ ,
  main()  0  ,   return  
.)

++   25

      ,       .
-,     readIn(), sort(), compact()  print().
    :

   void readIn() { cout << "readIn()\n"; }
   void sort() { cout << "sort()\n"; }
   void compact() { cout << "compact()\n"; }
   void print() { cout << "print ()\n"; }

    void ,   ,    
.       ,  
     ,   .    
  ,    .
           .
         .
       ,   ,  ,   
:   (, bookstore)  ,  
. ,     ,   
 .  bookstore.h       ++
. ( ,     ++ 
  :    .)
    bookstore.c       .  
 UNIX,         ,
 .C    ++ ,    bookstore.C
   ++.
      ,    DOS,     
 ,       
   ++.     .cpp 
.cxx: bookstore.cpp, bookstore.cxx.
     ++        
 (     ,     ++ 
 ). ,     
++,       .
   ,    ++  (  
):

                                                               ++   26

   #include <iostream>

   using namespace std;

   void readIn() { cout << "readIn()\n"; }
   void sort() { cout << "sort()\n"; }
   void compact() { cout << "compact()\n"; }
   void print() { cout << "print ()\n"; }

   int main()
   {
      readIn();
      sort();
      compact();
      print();

      return 0;
   }

    iostream      / (
:    ).       cout,
   . #include   ,
         iostream.
(     1.3.)
      

   #include <iostream>

 

   using namespace std;

       using. ,   
 ++ (,  cout),     std   
    ,       ,   
  . (       2.7
 8.5.)1
           ,  prog1.C,  
 .  UNIX     :

   $ CC prog1.C

    $     . CC  
 ++,    UNIX-.   
     .
     ,       ,
   .    

1         ++  
.    ,    . 
,  ,  ,  
 ,   using   .

                                                                ++   27

 ,         .
       :

    .    ,  
     ++, . :

   int main( { //    ')'
      readIn(): //     ':'
      sort();
      compact();
      print();

      return 0 //     ';'
   }


    .       ++  
   . ,  10   .  "hello",   
   ,   .       
    ,    ,    
    .

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

   readIn()
   sort()
   compact()
   print()

    ++           ,
   ,  .    
 .       .
:

   int age = 10;
   double price = 19.99;
   char delimiter = ' ';
   bool found = false;

                                                           ++   28

      : age, price, delimiter, found, 
  ,    ,  
.        10,
  19.99,      false.
           .
  age,   int,    double, :

   age = 33.333;

   age    33. ( 
,          4.14.)
     ++    ,    
,  ,  , , . :

   //      string
   #include <string>
   string current_chapter = "";

   //      vector
   #include <vector>
   vector<string> chapter_titles(20);

    current_chapter    string,  
"".  chapter_titles    20   .
   

   vector<string>

     ,   
string.       20  ,  :

   vector<int> ivec(20);

    ,        
 ,   .   
      .  ++ 
   .      
 ++,   ,  , , , 
,   ++.      
/.
            ++,    2 
   .

                   1.2.1.   

          , . 



                                                               ++   29

   {
      readIn();
      sort();
      compact();
      print();
      return 0;
   }

    readIn(),   sort(), compact()  
print().
      ,    :   1 
 0.      sort()  compact()   . 
  - ,   print()    
.         if.  
  readIn() ,     
:

   // readIn()    
   //     int
   int readIn() { ... }
   // ...

   int main()
   {
      int count = readIn();

      //     1,
      //   sort()  compact()
      if ( count > 1 ) {
         sort();
         compact();
      }
      if ( count == 0 )
         cout << "  \n";
      else
         print();

      return 0;
   }

     if     : 
sort()  compact()     ,  count  1. 
  if       , 
 , ..  count  0.     , 
  print(). (   if    5.3.)
        
 ,   .    

                                                           ++   30

    ,       true  false.
:

   int main()
   {
      int iterations = 0;
      bool continue_loop = true;
      while ( continue_loop != false )
      {
         iterations++;
         cout << "   " << iterations << "\n";
         if ( iterations == 5 )
           continue_loop = false;
      }

      return 0;
   }

        while   ,    
 iterations    5   continue_loop  
 false. 

   iterations++;

   iterations  . (  
   5.)

                             1.3.  

           
 #include.       (#),
      . , 
  ,   (  
     ).
    #include      .  
    :

   #include <some_file.h>
   #include "my_file.h"

          (<>), ,    
  ,       
. (         
.)   ,     , 
     ,     .
      #include.  
 ,         

                                                              ++   31

,         .
     .  :

   #ifndef BOOKSTORE_H
   #endif

     #ifndef ,     BOOKSTORE_H 
. (BOOKSTORE_H    ;    
 .)      
 #endif.        #ifndef  # endif.
   

   #define BOOKSTORE_H

   BOOKSTORE_H.   
   #ifndef,   , 
    bookstore.h    
   ,          .
      
      . :

   int main()
   {
   #ifdef DEBUG
         cout << "  main()\n";
   #endif

      string word;
      vector<string> text;

      while ( cin >> word )
      {
   #ifdef DEBUG
         cout << " : " << word << "\n";
   #endif
         text.push_back(word);
      }
      // ...
   }

     DEBUG  ,     
:

   int main()
      {
          string word;


                                                           ++   32

      while ( cin >> word )
      {
         text.push_back(word);
      }
     // ...
   }

       :

   {
      cout << "  main()\n";

      string word;
      vector<string> text;

      while ( cin >> word )
      {
         cout << " : " << word << "\n";
         text.push_back(word);
      }
      // ...
   }

            
    -D (     
 -).  UNIX-    
  DEBUG   :

   $ CC -DDEBUG main.C

    ,    . , 
 ,    ++   .  ++ 
   __cplusplus ( ). 
   __STDC__. ,     
 . :

   #idfef __cplusplus
      //  ++ 
      extern "C";
      // extern "C"    7
   #endif
   int main(int,int);

       (    
)   __LINE__  __FILE__.  __LINE__
    ,  __FILE__   
.    :

                                                          ++   33

   if ( element_count == 0 )
      cerr << ". : " << __FILE__
           << " : " << __LINE__
           << "element_count    0";

     __DATE__  __TIME__     .
      assert(),  
    ,    ,  
    .     
     .    
   

   #include <assert.h>

assert.h       .   C++ 
       ,   C,    ,
  C++.    ++     cassert. 
    ++      
   .h     c ( 
,      C++   ,
     ).
        #include   
 . 

   #include <cassert>

      cassert.    ,
    ++,    std, 
assert()     ,         
 using-:

   using namespace std;

             

   #include <assert.h>

   using- :  assert()    2.
(      
   .   8.5    
.)

                       1.4.   

        ;    
  .     ,
     ,   . 

2    ,      ,
        .

                                                               ++   34

       
    .
    ++    .    ,    ,  
/*     */    .  
     ,     :
   /*  */  . :

   /*
    *        C++.
    *     ,   
    * - . 
    *  Screen    13.
   */
   class Screen {
      /*     */
   public:
      void home(); /*     0,0 */
      void refresh ();/*   */
   private:
      /*   " " */
      /*      */
      /*      */
      /* ( ).     */
      /* "private:" */
      int height, width;
   }

      ,    , 
  . ,   width  height 
       .  
    .     ,
      . , 
,      .
          .  
    .    
:

   #include <iostream>

   /*  /* */    .
    *  " "  ,
    *   .        
   */

   int main() {
      cout << ", \n";
   }

             
   :

   /* * /

                                                            ++   35

     */       ,
    .
       .   
 //    .       
 .     Screen   
 :

   /*
    *       C++.
    *     ,   
    * - . 
    *  Screen    13.
   */

   class Screen {
   //    
   public:
      void home(); //     0,0
      void refresh (); //  
   private:
      /*   " ". */
      /*      */
      /*      */
      /* ( ).     */
      /* "private:" */
      int height, width;
   };

          .  
        ,  ,  /*
 */,      .

                           1.5.    /

      ++   iostream, 
        /.
     ,   ,   
 cin.   ,   ,    cout.
  , cerr,     
.          .
      /   
 :

#include <iostream>

           
  <<:


                                                              ++   36

   int v1, v2;
   // ...
   cout << " v1  v2 = ";
   cout << v1 + v2;
   cout << "\n";

    "\n"       . 
"\n"      endl.

   cout << endl;

    endl     (    ),  
   . (   
 20.)
      . ,      
:

   cout << " v1  v2 = " << v1 + v2 << "\n";

   string file_name;
   // ...
   cout << "  : ";

            (>>):

   cin >> file_name;

    ,    ,  :

   string ifile, ofile;
   // ...
   cout << "     : ";
   cin >> ifile >> ofile;

         ?    
    2.2,   ,   

   string word;
   while ( cin >> word )
   // ...

         ,     .


   ( cin >> word )

                                                             ++   37

 false,    . (      20.) 
   ,      cin 
   cout:

   #include <iostream>
   #include <string>
   int main ()
   {
      string word;
      while ( cin >> word )
         cout << " : " << word << "\n";
      cout << "  !";
   }

           :

   riverrun, past Eve and Adam's

            
,      :

    : riverrun,
    : past
    : Eve,
    : and
    : Adam's

     !

(  6     ,       .)

                              1.5.1.  /

    iostream    /.  , 
    ,      . 
     ,      
:

   #include <fstream>

         ,     ofstream:

   ofstream outfile("name-of-file");

   ,     ,   :


                                                               ++   38

   if ( ! outfile ) // false,    
      cerr << "  .\n"

   ifstream infile("name-of-file");

         ,     ifstream:

   if ( ! infile ) // false,    
      cerr << "  .\n"

       ,      in_file 
      ,   ,   ,
 out_file.

   #include <iostream>
   #include <fstream>
   #include <string>

   int main()
   {
      ifstream infile("in_file");
      ofstream outfile("out_file");
      if ( ! infile ) {
         cerr << "   .\n"
         return -1;
      }
      if ( ! outfile ) {
        cerr << "   .\n"
        return -2;
      }

      string word;
      while ( infile >> word )
         outfile << word << ' ';
      return 0;
   }

     20  /   .   
  ,      ,  
  .
