

                       6

                   ꥪ  㦤.
                            -. 

         饭  ந . ந
        -  ⮥,   䥪⨢ ।⢮ ।
       .     㦥 饬
       ,  ॡ  ९ணࠬ஢  ࠭樨.
         ந ᮢ  ࣠ 騩
       䥩  ᪮쪨 ࠧ묨 ᠬ ⠪,   㣨
        ணࠬ  㤥 ࠧ ࠡ  ꥪ⠬
        ᮢ.  ⨥ 㠫쭮 㭪樨, ஥
        ᯮ짮 ꥪ 騬 ࠧ 
         ,   ⨯  ⠤ ࠭樨 ⥭.
       ᭮ 祭 ந ᮢ - 
       ணࠬ  ࠦ 魮 ᮢ.

   6.1   ⪨ 

    ⨥   ஢,   
   裡  㣨 ﬨ,  魮   
    । 稥 ⠪ 痢.   㦨 
   ।⠢ ⨩, ⠥ ,  ।⠢ 
   ⨩. ⨥ ந   ন騥 
   몮 ।⢠ 㦠  ।⠢ ᪨ 痢,
   묨 ᫮,  ࠦ 魮  ᠬ. ਬ,
    㦭  㣮쭨 易  ᮡ, ⠪ 
     ।⠢  ⨥ 䨣, .. ᮤঠ  饥
   ⨥. ⮡ ।⠢  ணࠬ 㦭  㣮쭨
     ⮬  ᪠  ,    䨣ࠬ, 
    ।  㦭  㣮쭨 ⠪, ⮡ 뫮 ,
       騩  - 䨣.   ᫥, 
   ⥪  ⮩ ⮩ ,     ᭮ ⮣,
    筮 뢠 ꥪ⭮-ਥ஢ ணࠬ஢.
    ⮨   ࠧ:
   $$6.2   ਨ  ਬ஢  ⨥ ந
         , 娨 ᮢ  㠫 㭪権.
   $$6.3  ⨥  㠫 㭪権  ࠪ
         ᮢ,  訥 ਬ  ᯮ짮.
   $$6.4 ந    祭 ਬ
   $$6.5  ⨥ ⢥ ᫥  
              אַ  ,
         뢠 ᯮᮡ ࠧ襭  , 
          ⢥ ᫥.
   $$6.6 㦤 堭 ஫ 㯠.
   $$6.7 ਢ  ਥ ࠢ ᢮  
         ந ᮢ.
    ᫥  ⠪  ਢ ਬ, ᯮ騥
     몠.

   6.2 ந 

   㤨,   ணࠬ  㦠 ன
   .    ᯮ짮, ਬ, ⠪  :

              struct employee {            // 㦠騥
                   char*     name;         // 
                   short     age;          // 
                   short     department;   // ⤥
                   int       salary;       // 
                   employee* next;
                   // ...
               };

    next 㦭  뢠  ᯨ᮪ ᥩ  㦠
    ⤥ (employee).  ஡㥬 ।  
    ࠢ饣 (manager):

              struct manager {
                 employee emp;    //  employee  ࠢ饣
                 employee* group; // 稭 ⨢
                 short   level;
                 // ...
              };

   ࠢ騩 ⠪  㦠騬, ⮬  employee
   ࠭  童 emp ꥪ manager.  祫  魮
   祢,   ࠭ 童 emp 祬  ⫨砥 
   㣨 童 . ⥫   manager (manager*)
     㪠⥫  employee (employee*), ⮬
    ᢮ ᯮ짮   㣮.  ⭮,
    ᯥ樠 ⢨  ꥪ manager   ᯨ᮪
   ꥪ⮢ ⨯ employee. ਤ  ᯮ짮  ਢ
   ⨯ manager*,   ᯨ᮪ ᥩ employee  
   童 emp.  襭 ᨢ    筮 ⠭묨.
   ࠢ쭮 襭 ⮨  ⮬, ⮡ ⨯ manager  ⨯
   employee  ன ⥫쭮 ଠ樥:


            struct manager : employee {
              employee* group;
              short level;
              // ...
            };

    manager  ந  employee, , , employee
     ᮬ  manager.  童 group  
   manager  童  employee (name, age  ..).
       ᪨ ⭮襭 ᫥ 筮 ࠦ  
   ५  ந ᮢ  :

                employee
                   ^
                   |
                 manager

   筮 ,  ந  ᫥  , ⮬
    ⭮襭   뢠 ᫥.   
   뢠 㯥ફᮬ,  ந - 稭 ᮬ. 
    ନ  뢠 㬥, ᪮ ꥪ ந
    ᮤন ꥪ ᢮  .  ந
     ᢮   ⮬ ᫥,    ᮤন
      ।  㭪権.
        । employee  manager,  ᮧ ᯨ᮪
   㦠,      ࠢ騬:

            void f()
            {
              manager m1, m2;
              employee e1, e2;
              employee* elist;
              elist = &m1;           //  m1  elist
              m1.next = &e1;         //  e1  elist
              e1.next = &m2;         //  m2  elist
              m2.next = &e2;         //  m2  elist
              e2.next = 0;           //  ᯨ᪠
            }

   ᪮ ࠢ騩   㦠騬, 㪠⥫ manager*
    ᯮ짮  employee*.    ६ 㦠騩 
   易⥫쭮  ࠢ騬,  ⮬ employee* 
   ᯮ짮  manager*.
        饬 砥, ᫨  derived  騩  
   base,  㪠⥫  derived    ८ࠧ ⨯
   ᢠ ६, 饩 ⨯ 㪠⥫  base. ⭮
   ८ࠧ  㪠⥫  base  㪠⥫  derived  
   ⮫쪮 :

           void g()
           {
             manager mm;
             employee* pe = &mm;  // ଠ쭮

             employee ee;
             manager* pm = &ee;  // 訡:
                                 //  直 㦠騩  ࠢ騬

             pm->level = 2;      // :  ࠧ饭 ee
                                 //   童 `level'  뤥﫠

            pm = (manager*) pe;  // ଠ쭮:  ᠬ  pe
                                 //  ஥  ꥪ mm ⨯ manager

            pm->level = 2;       // ⫨筮: pm 㪠뢠  ꥪ mm
                                 // ⨯ manager,     ࠧ饭
                                 // 뤥   童 `level'
         }

   묨 ᫮, ᫨ ࠡ  ꥪ⮬ ந   १
   㪠⥫,    ᬠਢ  ꥪ  .
   ⭮ ୮. ⬥⨬,   筮 ॠ樨 ++ 
   । ᪮ ஫  ⥬, ⮡ ᫥ ८ࠧ
   ⨯,  ⮬, ஥ ᯮ짮  ᢠ pe  pm,
   稢訩  १ 㪠⥫ ⢨⥫쭮  ஥  ꥪ
   ॡ㥬 ⨯ (. $$13.5).

   6.2.1 㭪樨-童

      த employee  manager ᠬ  ᥡ
    ᫨誮 ,     ᮡ . ⮬ 
     㭪樨:

         class employee {
           char* name;
           // ...
         public:
           employee* next;  // 室  饩 , ⮡
                            //  뫮 ࠡ  ᯨ᪮
           void print() const;
           // ...
        };

        class manager : public employee {
           // ...
        public:
           void print() const;
           // ...
        };

    ⢥   .  ࠧ 㭪-童
   ந  manager  ᯮ짮 童  
   employee?  童   employee  ᯮ짮
   㭪樨-童 ந  manager?  童 
    employee  ᯮ짮 㭪,   童 ꥪ
   ⨯ manager?  ⢥      ॠ
   몠, ⮡  ᨬ쭮 ᮮ⢥⢮  ணࠬ?
      ᬮਬ ਬ:

        void manager::print() const
        {
           cout << "  "    << name << '\n';
        }

    ந   ᯮ짮   饩  ᢮
     ࠢ  ᥬ 㣨 童, ..  㪠
    ꥪ. ।,   ꥪ,   ஥
   this, ⮬ ४ 饭  name 㤥 this->name. ,
    ࠭樨 㭪樨 manager::print() 㤥 䨪஢ 訡:
   童 ந   ।⠢ ࠢ 㯠  
   童   ,  name 㯭  ⮩ 㭪樨.
          ࠭,   ᬮਬ
   ୠ⨢ 襭: 㭪-童 ந  
      童 ᢮  .  ᠬ ⨥
   ⭮ (⮣) 童  直 , ᪮  㯠
     筮  । ந .  㦥
   㤥 筮  ᭥,  ᯮ  童 ,
   ᬮ  㭪樨-童  㧥 ⮣ . ਤ
   ᬮ  室 䠩 ணࠬ,  ந
   , ⥬ ᫥  㭪  ᮢ.  
   ᭮ ᪠ ந   㦥   .. ,  ࠩ
   , ⮬⥫쭮,  ᪮॥ ᥣ ॠ쭮. 㦭 ,  
   , ᯮ짮   童 饭 (. $$6.6.1).
        ࠢ, ᠬ  襭  ந  -
   ᯮ짮 ⮫쪮 騥 童 ᢮  :

           void manager::print() const
           {
             employee::print();   //    㦠

             //    ࠢ
           }

   ⬥⨬,   :: 室, ᪮ 㭪 print()
   ८।   manager.  ୮ ᯮ짮 
   ⨯筮  ++. ஦ ணࠬ ᠫ :

          void manager::print() const
          {
             print();   //    㦠

             //    ࠢ
          }

    १  稫  ४ᨢ ᫥⥫쭮 맮
   manager::print().

   6.2.2   

     ந ᮢ 㦭 . ᫨ 
      ,      뢠  㪠
   ࠬ஢, ᫨ ⠪   :

          class employee {
            // ...
          public:
            // ...
            employee(char* n, int d);
         };

         class manager : public employee {
            // ...
         public:
            // ...
           manager(char* n, int i, int d);
         };

   ࠬ       ।
    ந .  ⮬ ᫥  
   㯠  , 騩 童 ந :

         manager::manager(char* n, int l, int d)
            : employee(n,d), level(l), group(0)
         {
         }

      employee::employee()   ⠪
   ।:

         employee::employee(char* n, int d)
             : name(n), department(d)
         {
           next = list;
           list = this;
         }

    list   ᠭ  ᪨ 童 employee.
      ꥪ ᮢ ᮧ ᭨ : 砫 , ⥬
   童 , , ᠬ ந . ⮦  
   ⭮ 浪: ᭠砫 ᠬ ந , ⥬ 童,
    ⥬ .    ᮧ  浪 ᠭ 
    ,  㭨⮦   ⭮ 浪.

   6.2.3  ᮢ

   ந  ᠬ  ᢮ ।    ᮬ:

          class employee { /* ... */ };
          class manager : public employee { /* ... */ };
          class director : public manager { /* ... */ };

    ⢮ 易  ᮡ ᮢ 筮 뢠
   娥 ᮢ. 筮  ।⠢ ॢ,  뢠
   娨   饩 ன   :

          class temporary { /* ... */ };
          class secretary : public employee { /* ... */ };

         class tsec
           : public temporary, public secretary { /* ... */ };

         class consultant
           : public temporary, public manager { /* ... */ };


   ,    ++  ࠧ뢠 ࠢ 横
    (஡  ⮬   $$6.5.3).   
   ਢ ᮢ  :


   6.2.4  ⨯

   ⮡ ந  뫨   㤮 ମ ⪮
   ᠭ,  ॠ樨 몠   襭 :   
   ந ᮢ ⭮ ꥪ,   ᬮ 㪠⥫ base*?
     ᭮ ᯮᮡ ⢥:
     [1] ᯥ, ⮡ 㪠⥫  뫠  ꥪ ⮫쪮
          ⨯ ($$6.4.2);
     [2]      ⨯, ஥ ᬮ ஢
         㭪樨;
     [3] ᯮ짮 㠫 㭪樨 ($$6.2.5).
   ⥫    몭 ᯮ  ஥஢
   ⥩ ᮢ (⢮, , ᯨ᮪  ..).  
   砥 [1]  稬 த ᯨ᪨, .. ᯨ᪨ ꥪ⮢ 
   ⨯. ᮡ [2]  [3]  ᮧ ࠧத ᯨ᪨, ..
   ᯨ᪨ ꥪ⮢ ᪮쪨 ࠧ ⨯ ( ᠬ , ᯨ᪨
   㪠⥫   ꥪ). ᮡ [3] -  ᯥ樠  
   ᫥ ⨯ ਠ ᯮᮡ [2]. ᮡ    ਠ
    樨 ᯮᮡ [1]  [3];  㦤   8.
      砫 㤨 ⮩ ᯮᮡ   ⨯, .. ᯮᮡ [2].
   ਬ  ᠬ manager/employee  ८। ⠪:

            struct employee {
               enum empl_type { M, E };
               empl_type type;
               employee* next;
               char*     name;
               short     department;
               // ...
           };

           struct manager : employee {
              employee* group;
              short     level;
              // ...
          };

     ।,   㭪,  
    ந쭮 㦠饬:

         void print_employee(const employee* e)
         {
            switch (e->type) {
            case E:
              cout << e->name << '\t' << e->department << '\n';
              // ...
              break;
           case M:
             cout << e->name << '\t' << e->department << '\n';
             // ...
             manager* p = (manager*) e;
             cout << "level" << p->level << '\n';
             // ...
             break;
           }
         }

    ᯨ᮪ 㦠  ⠪:

         void f(const employee* elist)
         {
           for (; elist; elist=elist->next) print_employee(elist);
         }

        襥 襭, ᮡ   ணࠬ,
   ᠭ  祫,    ⢥ ⮪:
   ࠭   ஢, ᪮쪮 ࠢ쭮 ணࠬ
   頥  ⨯.   ணࠬ  ਢ  訡
    .  -  ணࠬ 뢠 ஢ 
   ⨯. ன -   ४⥫ 㪠뢠   
   祭  ⨯.  訡 筮   
    ᠭ ணࠬ,  ᮢᥬ    
   ᥭ   ਢ ணࠬ,  ᮡ,
   ᫨   ணࠬ, ᠭ - 㣨. 
   㤭  ⠪ 訡 ⮬,  㭪樨 ⨯ print() 
    ⠪, ⮡  뫮 ᯮ짮 魮 ᮢ:

               void print(const employee* e)
               {
                 cout << e->name << '\t' << e->department << '\n';
                 // ...
                 if (e->type == M) {
                    manager* p = (manager*) e;
                    cout << "level" << p->level << '\n';
                    // ...
                 }
               }

    if,  ਢ  ਬ, ᫮   让
   㭪樨, ࠡ饩   ந묨 ᠬ.    
   ,  ,  ந室  ᠬ . ஬ ⮣, 
   类    㦠 ॡ   
    㭪 ணࠬ, .. 㭪, ஢  ⨯. 
   १ 室 ࠢ   ணࠬ, 㢥稢 ⥬
   ᠬ ६  ⫠  ⥩.
       묨 ᫮,  ᯮ짮   ⨯ ॢ 訡 
   㤭ﬨ  ᮯ஢ ணࠬ. 㤭 १ 
      ணࠬ,  ᯮ짮  ⨯ ⨢
   ਭ樯 쭮  뢠 .  㭪,
   ࠡ   ⨯,   ।⠢  ᯥ䨪
   ॠ樨 类 , 饣 ந  ,
   ᮤঠ饣  ⨯.

   6.2.5 㠫 㭪樨

     㠫 㭪権  ८ 㤭, 騥
    ᯮ짮  ⨯.    뢠 㭪樨,
     ८।   ந . ࠭
    稪 ᯥ ࠢ쭮 ᮮ⢥⢨  ꥪ⠬ 
   ਬ塞묨   㭪ﬨ:

           class employee {
             char* name;
             short department;
             // ...
             employee* next;
             static employee* list;
           public:
             employee(char* n, int d);
             // ...
             static void print_list();
             virtual void print() const;

          };

   㦥 ᫮ virtual (㠫쭠) 뢠,  㭪 print()
     ࠧ ᨨ  ࠧ ந ,  롮 㦭
   ᨨ  맮 print() -   ࠭.
    㭪樨 㪠뢠       
   ८।  ந . । 㠫쭮 㭪樨
      ⮣ ,  ஬  뫠 
   ᠭ (᫨ ⮫쪮     㠫쭮 㭪樥,
   . $$6.3). ਬ:

            void employee::print() const
            {
               cout << name << '\t' << department << '\n';
               // ...
            }

    ,  㠫 㭪  ᯮ짮,  ᫨
    ந ᮢ   .  ந  
    易⥫쭮 ८। 㠫 㭪, ᫨  ⠬
    㦭.  ஥ ந   ।
   ⮫쪮  㭪樨,    ⢨⥫쭮 㦭:

             class manager : public employee {
                employee* group;
                short     level;
                // ...
             public:
                manager(char* n, int d);
                // ...
                void print() const;
             };

    㭪樨 print_employee() 﫨 㭪樨-童 print(),  
   ⠫  㦭. ᮪ 㦠 ந  employee ($$6.2.2).
      ⠪:

             void employee::print_list()
             {
               for ( employee* p = list; p; p=p->next) p->print();
             }

      㦠饬    ᮮ⢥⢨  ⨯
     . ⮬ ணࠬ

            int main()
            {
               employee e("J.Brown",1234);
               manager m("J.Smith",2,1234);
               employee::print_list();
            }

   ⠥

            J.Smith 1234
                    level 2
            J.Brown 1234

    ,  㭪  㤥 ࠡ   ⮬ 砥,
   ᫨ 㭪 employee_list() 뫠 ᠭ  ࠭᫨஢   ⮣,
     㬠  ந  manager! 祢,  
   ࠢ쭮 ࠡ 㠫쭮 㭪樨 㦭   ꥪ 
   employee ࠭  㦥 ଠ  ⨯.  ࠢ,
   ॠ樨  ⢥ ⠪ ଠ樨 ᯮ  㪠⥫.
    㪠⥫ ࠭ ⮫쪮  ꥪ⮢   㠫묨
   㭪ﬨ,    ꥪ⮢  ᮢ,      
   ꥪ⮢ ந ᮢ. ⥫쭠  ⢮ ⮫쪮
    ᮢ,   ᠭ 㠫 㭪樨. ⨬, 
    ᯮ짮  ⨯,    ࠢ 㦭 ⥫쭠
   .
         ᫨  맮 㭪樨  㪠  ࠧ襭
     ::, ਬ,  맮 manager::print(),
    堭 맮 㠫쭮 㭪樨  .  
   맮 ਢ   ᪮筮 ४ᨨ. 筥  㭪樨
      ⥫ 䥪: ᫨ 㠫쭠 㭪
    ⠭ ( ⮬  祣 筮),   맮
    樥 :: ந室 ⠭ ⥫ 㭪樨. 
   䥪⨢ ᯮᮡ 맮,   ਬ
     ,   㠫쭠 㭪
   頥  㣮    ⥬  ꥪ⮬. ਬ ⠪
    - 맮 㭪樨 manager::print(). ᪮ ⨯ ꥪ
      ᠬ 맮 manager::print(),  㦤 ।
       㭪樨 employee::print(),   㤥
   뢠.

   6.3 ࠪ 

     室  ᮬ employee ⥬,     
   ࠧ㬭 । 㠫 㭪. ,   㣨
   . , ਬ,  shape, ।⠢
   ࠪ⭮ ⨥ (䨣),  ண  ᮧ ꥪ.
    shape ਮ⠥  ⮫쪮     ஬
   ந . 稭  ,   
   ᫥ । 㠫 㭪権  shape:

              class shape {
                 // ...
              public:
                 virtual void rotate(int) { error("shape::rotate"); }
                 virtual void draw()  { error("shape::draw"): }
                 //   ,  ᮢ ࠪ 䨣
                 // ...
              };

    ꥪ ⨯ shape (ࠪ⭮ 䨣) , 
   ᮢ襭 ᫥ :

             shape s;  // ᫨: ``䨣 ''

    ᫥ ⮬,     ꥪ⮬ s ਢ
    訡.
         㠫 㭪樨  shape   
   㠫.  㠫 㭪  㠫쭮 ,
    樠 = 0:

             class shape {
               // ...
             public:
               virtual void rotate(int) = 0; //  㠫쭠 㭪
               virtual void draw() = 0;      //  㠫쭠 㭪
             };

   ,  ஬  㠫 㭪樨, 뢠 ࠪ.
   ꥪ ⠪  ᮧ :

             shape s;   // 訡: ६ ࠪ⭮  shape

   ࠪ   ᯮ짮 ⮫쪮  ⢥ 
    㣮 :

             class circle : public shape {
               int radius;
             public:
               void rotate(int) { } // ଠ쭮:
                                    // ८। shape::rotate
               void draw();         // ଠ쭮:
                                    // ८। shape::draw

               circle(point p, int r);
             };

   ᫨  㠫쭠 㭪  ।  ந
   ,    ⠥ ⠪,   ந  ⮦
    ࠪ.  ⠪ 室  ॠ뢠
    ⠯:

             class X {
             public:
               virtual void f() = 0;
               virtual void g() = 0;
             };

             X b;   // 訡: ᠭ ꥪ ࠪ⭮  X

             class Y : public X {
               void f();  // ८। X::f
             };

             Y b;   // 訡: ᠭ ꥪ ࠪ⭮  Y

             class Z : public Y {
               void g();  // ८। X::g
             };

             Z c;   // ଠ쭮

   ࠪ  㦭   䥩  筥
   -  ⠫ ॠ樨. ਬ,  樮
   ⥬ ⠫ ॠ樨 ࠩ ன⢠  
   ⠪ ࠪ ᮬ:

             class character_device {
             public:
                virtual int open() = 0;
                virtual int close(const char*) = 0;
                virtual int read(const char*, int) =0;
                virtual int write(const char*, int) = 0;
                virtual int ioctl(int ...) = 0;
                // ...
             };

   騥 ࠩ  ।  ந  
   character_device.
        ᫥  ࠪ⭮      ᭮
   ।⢠  ⮣, ⮡  祭 ணࠬ.

   6.4 ਬ 祭 ணࠬ

   ᬮਬ ணࠬ ᮢ ᪨ 䨣  ࠭. 
   ⢥ ࠧ ᯠ   :
     [1]  ࠭:  㭪権     ஢
          ࠡ  ࠭;  ⮫쪮 ⠪ ﬨ, 
         窨, ;
     [2] ⥪ 䨣: ⢮ । 䨣 饣 
         (ਬ, אַ㣮쭨, 㦭)  ⠭
         㭪樨  ࠡ  ;
     [3] ਪ ணࠬ:  । 䨣, ⭮
          ,  ࠡ騥   㭪樨.
    ࠢ,    ணࠬ ࠧ묨 쬨  ࠧ
   ࣠   ࠧ ६, 祬  筮 ᮧ
    ᫥ 浪.  ⮬ ⢥  㤭,
   ᪮, ਬ,  ࠧࠡ稪   筮 ।⠢
    ⮬,     筮   㤥 ᯮ짮. 
   ਬ 㤥 ࠦ  䠪. ⮡ ਬ  ⨬
   ࠧ, ⥪ 䨣 쬠 ࠭祭,  ਪ
   ணࠬ ਢ쭠. ᯮ ᮢ襭 ਬ⨢
   ।⠢ ࠭, ⮡  ⥫,  設
   ண  ᪨ ।, 㬥 ࠡ  ⮩ ணࠬ.
       ࠭   ࠧ ணࠬ,
      ⮬ ⥪ 䨣  ਪ ணࠬ.

   6.4.1  ࠭

   砫 뫮    ࠭  , ⮡  
   ભ ࠧ  ஢ﬨ ॠ樨.   
   ⮬⥫,  ⮬ ࠭ ஬᭮ 襭: ⨫
   ணࠬ஢, ਭ   ( 㭪権-童, 㠫
   㭪権, 짮⥫᪨ 権  ..),  ᯮ
   , ࠬ 㭪権  뢠  ஢
    ..   祭  ணࠬ  , 
   ஢, ⮡ ᯮ짮 ﬨ ++, 
    ।뢠  ⠫.
        ࠭ ।⠢  㬥 ᨢ ᨬ  ࠢ
   㭪ﬨ put_point()  put_line().    裡  ࠭
   ᯮ  point:

           // 䠩 screen.h

           const int XMAX=40;
           const int YMAX=24;

           struct point {
               int x, y;
               point() { }
               point(int a,int b) { x=; y=b; }
           };

           extern void put_point(int a, int b);
           inline void put_point(point p) { put_point(p.x,p.y); }

           extern void put_line(int, int, int, int);
           extern void put_line(point a, point b)
              { put_line(a.x,a.y,b.x,b.y); }

           extern void screen_init();
           extern void screen_destroy();
           extern void screen_refresh();
           extern void screen_clear();

           #include <iostream.h>

       맮 㭪権, 뤠 ࠦ  ࠭ (put_...), 室
     㭪樨 樠樨 ࠭ screen_init(). 
     , 뢠饩 ࠭, ⠭   
   ⮫쪮 ᫥ 맮 㭪樨  ࠭ screen_refresh().
   ⥫  㡥,   ࠭ ந室
      ஢  祭  ᨢ, ।⠢騩
   ࠭. ਢ 㭪樨  ।   ࠢ
   ࠭:

           #include "screen.h"
           #include <stream.h>

           enum color { black='*', white=' ' };

           char screen[XMAX] [YMAX];

           void screen_init()
           {
             for (int y=0; y<YMAX; y++)
                 for (int x=0; x<XMAX; x++)
                     screen[x] [y] = white;
           }

   㭪

           void screen_destroy() { }

   ਢ    ⨭.  ॠ ⥬ 筮
   㦭  㭪樨 㭨⮦ ꥪ.
        窨 뢠, ⮫쪮 ᫨    ࠭:

          inline int on_screen(int a, int b)  // ஢ઠ 
          {
             return 0<=a && a <XMAX && 0<=b && b<YMAX;
          }

          void put_point(int a, int b)
          {
             if (on_screen(a,b)) screen[a] [b] = black;
          }

    ᮢ   ᯮ 㭪 put_line():

          void put_line(int x0, int y0, int x1, int y1)
          /*
             ᮢ १ אַ (x0,y0) - (x1,y1).
             ࠢ אַ: b(x-x0) + a(y-y0) = 0.
              稭 abs(eps),
              eps = 2*(b(x-x0)) + a(y-y0).
             . Newman, Sproull
             ``Principles of interactive Computer Graphics''
             McGraw-Hill, New York, 1979. pp. 33-34.
           */
           {
             register int dx = 1;
             int a = x1 - x0;
             if (a < 0) dx = -1, a = -a;

             register int dy = 1;
             int b = y1 - y0;
             if (b < 0) dy = -1, b = -b;

             int two_a = 2*a;
             int two_b = 2*b;
             int xcrit = -b + two_a;
             register int eps = 0;

             for (;;) {
               put_point(x0,y0);
               if (x0==x1 && y0==y1) break;
               if (eps <= xcrit) x0 +=dx, eps +=two_b;
               if (eps>=a || a<b) y0 +=dy, eps -=two_a;
             }
           }

    㭪樨  ⪨   ࠭:

         void screen_clear() { screen_init(); }

         void screen_refresh()
         {
          for (int y=YMAX-1; 0<=y; y--) { //  孥 ப  
             for (int x=0; x<XMAX; x++)   //   ⮫  ࠢ
                 cout << screen[x] [y];
             cout << '\n';
          }
        }

    㦭 ,    । ࠭  ன
   ⥪  १ ࠡ ࠭,    .

   6.4.2 ⥪ 䨣

   筥  । 饣  䨣. । 
    ⠪, ⮡   뫮 ᯮ짮 (  ᮬ
   shape)  ࠧ , ।⠢   䨣
   (㦭,   ..).  ⠪   ࠡ
    类 䨣ன ᪫⥫쭮   䥩, ।塞
   ᮬ shape:

          struct shape {
             static shape* list;
             shape* next;

             shape() { next = list; list = this; }

             virtual point north() const = 0;
             virtual point south() const = 0;
             virtual point east() const = 0;
             virtual point west() const = 0;
             virtual point neast() const = 0;
             virtual point seast() const = 0;
             virtual point nwest() const = 0;
             virtual point swest() const = 0;

             virtual void draw() = 0;
             virtual void move(int, int) = 0;
           };

          ࠭ 㭪樥 draw(),    
     move().    ⭮⥫쭮  㣠,
   ᯮ ⨥ 祪 ⠪.  祭 祪 ⠪
   ᯮ  ஭ ᢥ  : north - ᥢ, ... ,
   neast - ᥢ-⮪, ... , swest - -.  
   ⭮ 䨣 ᠬ ।   祪  ।,
    ᮢ 䨣.  shape::shape() 
   䨣  ᯨ 䨣 shape::list.  ஥ ⮣ ᯨ᪠
   ᯮ 童 next, 室騩   ꥪ shape. ᪮
    ᫠  ꥪ ⨯ 饩 䨣,  shape । 
   ࠪ .
            १ אַ 㦭 㪠  窨  
    楫.  ᫥ 砥 १ 㤥 ਧ⠫,  楫
     .  楫 뢠,   室 
   窠 ⭮⥫쭮 筮 窨, .. ᫥  ࠢ  :

            class line : public shape {
            /*
              १ אַ ["w", "e" ]
              north() ।  - ``  業 १ 
              ⠪   ᥢ,  ᠬ  ᥢୠ 窠''
            */
              point w, e;
           public:
              point north() const
                { return point((w.x+e.x)/2,e.y<w.y?w.y:e:y); }
              point south() const
                { return point((w.x+e.x)/2,e.y<w.y?e.y:w.y); }
              point east() const;
              point west() const;
              point neast() const;
              point seast() const;
              point nwest() const;
              point swest() const;

              void move(int a, int b)
                 { w.x +=a; w.y +=b; e.x +=a; e.y +=b; }
              void draw() { put_line(w,e); }

              line(point a, point b) { w = a; e = b; }
              line(point a, int l) { w = point(a.x+l-1,a.y); e = a; }
           };

   筮 । אַ㣮쭨:

           class rectangle : public shape {
           /*   nw ------ n ----- ne
                |                  |
                |                  |
                w         c        e
                |                  |
                |                  |
                sw ------ s ----- se
           */
              point sw, ne;
           public:
              point north() const { return point((sw.x+ne.x)/2,ne.y); }
              point south() const { return point((sw.x+ne.x)/2,sw.y); }
              point east() const;
              point west() const;
              point neast() const { return ne; }
              point seast() const;
              point nwest() const;
              point swest() const { return sw; }

              void move(int a, int b)
              { sw.x+=a; sw.y+=b; ne.x+=a; ne.y+=b; }
              void draw();

              rectangle(point,point);
            };

   אַ㣮쭨 ந   窠.  ᫮, ⠪
    室  ⭮⥫쭮   祪:

            rectangle::rectangle(point a, point b)
            {
              if (a.x <= b.x) {
                 if (a.y <= b.y) {
                    sw = a;
                    ne = b;
                 }
                 else {
                    sw = point(a.x,b.y);
                    ne = point(b.x,a.y);
                 }
               }
               else {
                 if (a.y <= b.y) {
                    sw = point(b.x,a.y);
                    ne = point(a.x,b.y);
                 }
                 else {
                    sw = b;
                    ne = a;
                 }
               }
             }

   ⮡ ᮢ אַ㣮쭨,  ᮢ  १:

             void rectangle::draw()
             {
               point nw(sw.x,ne.y);
               point se(ne.x,sw.y);
               put_line(nw,ne);
               put_line(ne,se);
               put_line(se,sw);
               put_line(sw,nw);
             }

    ⥪ 䨣  । 䨣  㭪樨  ࠡ
    :

            void shape_refresh();      // ᮢ  䨣
            void stack(shape* p, const shape* q); //  p  q

   㭪  䨣 㦭, ⮡ ࠡ  訬 ਬ⨢
   ।⠢ ࠭;      䨣. ⬥⨬,
     㭪   ,  䨣  :

            void shape_refresh()
            {
              screen_clear();
              for (shape* p = shape::list; p; p=p->next) p->draw();
              screen_refresh();
            }

   ,   ⢨⥫쭮 ࢨ᭠ 㭪,  
    䨣  㣮.  ⮣  ।  (south()) 
   䨣  ࠧ  ᥢ஬ (north()) 㣮:

            void stack(shape* p, const shape* q) //  p  q
            {
              point n = q->north();
              point s = p->south();
              p->move(n.x-s.x,n.y-s.y+1);
            }

       ।⠢ ⥯,   ⥪  ᮡ⢥
   ன , த饩 ணࠬ, ,   த ⮫쪮
    䠩  ।ﬨ 䨣  ࠭᫨஢
   । 㭪権.  ࠢ  ᬮ ।  䨣,
   ᯮ짮  ⮣ 㯫묨  㭪ﬨ.


   6.4.3 ਪ ணࠬ

   ਪ ணࠬ ।쭮 . ।  䨣
   myshape (᫨  ᮢ,    ),  ⥬
   ਢ 㭪 main(),  ன    ﯮ. 砫
    ᠭ 䨣 myshape:

             #include "shape.h"

             class myshape : public rectangle {
               line* l_eye;   //  
               line* r_eye;   // ࠢ 
               line* mouth;   // 
             public:
               myshape(point, point);
               void draw();
               void move(int, int);
             };

          ⤥묨 ᨬ묨 ꥪ⠬ 
   ᮧ   myshape:

             myshape::myshape(point a, point b) : rectangle(a,b)
             {
               int ll = neast().x-swest().x+1;
               int hh = neast().y-swest().y+1;
               l_eye = new line(
                   point(swest().x+2,swest().y+hh*3/4),2);
               r_eye = new line(
                   point(swest().x+ll-4,swest().y+hh*3/4),2);
               mouth = new line(
                   point(swest().x+2,swest().y+hh/4),ll-4);
             }

   ꥪ, ।⠢騥   , 뤠 㭪樥 shape_refresh()
    ⤥쭮.  ਭ樯    ࠡ ᨬ 
   ꥪ my_shape,  ஬  ਭ.    ᯮᮡ
       饣 ᪨ ꥪ myshape.
      ᤥ ,    .  ⨯ ""
    ।,   ᮢ뢠  㭪樨 draw():

             void myshape::draw()
             {
               rectangle::draw();
               int a = (swest().x+neast().x)/2;
               int b = (swest().y+neast().y)/2;
               put_point(point(a,b));
             }

    䨣 myshape ᢮   ꥪ  
   rectangle     ꥪ⮢ (l_eye, r_eye  mouth):

             void myshape::move(int a, int b)
             {
               rectangle::move(a,b);
               l_eye->move(a,b);
               r_eye->move(a,b);
               mouth->move(a,b);
             }

   , । ᪮쪮 䨣  㤥  :

             int main()
             {
               screen_init();
               shape* p1 = new rectangle(point(0,0),point(10,10));
               shape* p2 = new line(point(0,15),17);
               shape* p3 = new myshape(point(15,10),point(27,18));
               shape_refresh();
               p3->move(-10,-10);
               stack(p2,p3);
               stack(p1,p2);
               shape_refresh();
               screen_destroy();
               return 0;
             }

       ⨬   ,  㭪樨,  shape_refresh()
    stack(), ࠡ  ꥪ⠬, ⨯  뫨 ।
    ᫥ ।  㭪権 (, ⭮, ᫥
    ࠭樨).
      稢襥   ﯮ:

             ***********
             *         *
             *         *
             *         *
             *         *
             *         *
             *         *
             *         *
             ***********
          *****************
             ***********
             *         *
             * **   ** *
             *         *
             *    *    *
             *         *
             * ******* *
             *         *
             ***********

    饭 ਬ ஢  㤠 䨣  㦤.

   6.5 ⢥ ᫥

    $$1.5.3  $$6.2.3 㦥 ਫ,      ᪮쪮
     ᮢ.  ,   ᠭ  ᫥ :
     㪠   . ᬮਬ  ஢,
    ன ࠫ ⢨ ।⠢ ⠭⭮ ⥪
   ᮢ task,  ᡮ  뤠 ଠ樨 ᯥ稢 
    displayed.   㥬 ꥪ⮢ ( 
   satellite)  । ⠪:

            class satellite : public task, public displayed {
               // ...
            };

    । 筮 뢠 ⢥ ᫥.
   ⭮, ⢮ ⮫쪮  אַ   뢠
   ⢥ ᫥.
        ᥬ ।   satellite  
   ꥤ 権 ᮢ task  displayed:

           void f(satellite& s)
           {
             s.draw();    // displayed::draw()
             s.delay(10); // task::delay()
             s.xmit();    // satellite::xmit()
           }

    㣮 ஭, ꥪ ⨯ satellite  । 㭪 
   ࠬ஬ ⨯ task  displayed:

           void highlight(displayed*);
           void suspend(task*);

           void g(satellite* p)
           {
             highlight(p);   // highlight((displayed*)p)
             suspend(p);     // suspend((task*)p);
           }

   祢, ॠ ⮩  ॡ ண (⮣)
     ࠭: 㦭 㭪  ࠬࠬ task 
   displayed । ࠧ  ꥪ ⨯ satellite.
        㠫 㭪権, ⢥, 맮  ⠪ 믮
   ࠢ쭮:

           class task {
              // ...
              virtual pending() = 0;
           };

           class displayed {
              // ...
              virtual void draw() = 0;
           };

           class satellite : public task, public displayed {
              // ...
              void pending();
              void draw();
           };

    㭪樨 satellite::draw()  satellite::pending()  ꥪ
   ⨯ satellite  뢠 ⠪ ,  ᫨    ꥪ⮬ ⨯
   displayed  task, ᮮ⢥⢥.
      ⬥⨬,  ਥ ⮫쪮  ⢥ ᫥
   ࠭稢  ॠ樨 ᮢ displayed, task 
   satellite.  ⠪ 砥  satellite    task 
   displayed,     㣮  (᫨, 筮, task  
   ந  displayed  ).   砥 
   .

   6.5.1 ⢥ 宦  

           ᮡ
    ⭮ 宦   . ⨬,
    task  displayed  ந묨  link, ⮣
    satellite  㤥 室 :

            class task : public link {
               // link ᯮ  뢠 
               //   ᯨ᮪ (ᯨ᮪ ᯥ)

               // ...
            };

            class displayed : public link {
               // link ᯮ  뢠 
               // ࠦ ꥪ⮢ (ᯨ᮪ ࠦ)

               // ...
            };

    ஡  .  ࠧ ꥪ link ᯮ
    ࠧ ᯨ᪮,   ᯨ᪨  䫨   㣮.
   筮,  ᪠ 筮    童 
   link,    ᤥ ४⭮,   ᫥饬 ࠧ.
   ᪨ ꥪ satellite  ।⠢ ⠪:

     ਢ ਬ,  騩    
   ।⠢  ࠧ묨 ꥪ⠬ (. $$6.5.3).

   6.5.2 襭 筮

   ⢥,    ᮢ   㭪樨-童
    묨 :

            class task {
               // ...
               virtual debug_info* get_debug();
            };

            class displayed {
               // ...
               virtual debug_info* get_debug();
            };

    ᯮ짮  satellite  筮 㭪権
     ࠧ襭:

            void f(satellite* sp)
            {
             debug_info* dip = sp->get_debug(); //訡: 筮
             dip = sp->task::get_debug();       // ଠ쭮
             dip = sp->displayed::get_debug();  // ଠ쭮
            }

   ,  ࠧ襭 筮 嫮⭮, ⮬
     ࠭  ᥣ ।  㭪 
   ந :

            class satellite : public task, public derived {
               // ...
               debug_info* get_debug()
               {
                 debug_info* dip1 = task:get_debug();
                 debug_info* dip2 = displayed::get_debug();
                 return dip1->merge(dip2);
               }
            };

    ᠬ  ଠ    satellite ᮢ.
   ᪮ satellite::get_debug()  ८। 㭪権
   get_debug()    ᮢ, ࠭,   
   㤥 뢠  类 饭  get_debug()  ꥪ
   ⨯ satellite.
       ࠭   , 騥  ।
     ⮣    , 祬   . ⮬
   ணࠬ   㪠뢠    ᯮ, ஬
   ,   ᯮ짮 ⢨⥫쭮 筮.  ࠢ
   ᯮ짮  ᮢ  ਢ   .  設⢥
   砥,  ᫨  ᮢ,   , ᪮
     ᯮ ।⢥  ꥪ⮢ ந .
       筠 ஡,      㭪樨  
   ,  ࠧ 祭, 㦤  $$13.8  ਬ
   㭪樨 draw()  ᮢ Window  Cowboy.
       ᫨ 筮  , 譥 㪠뢠 
       饭   童.  ⭮, ᫨
   ⢥ ᫥  ᯮ,  筮
   ᯮ짮 祭 ⨯ "-   ". 
    ணࠬ    אַ   
   ᯠᠥ   訡 (祬, ।),   ன
   娨 ᮢ. ਬ,  㭪樨  $$6.2.5

           void manager::print()
           {
             employee::print();
             // ...
           }

   ।,  employee - אַ    manager.
    ⮩ 㭪樨  , ᫨ employee  ᢥ
    ᮬ  manager,   אַ   㭪樨
   print() . , -   ᫥騬 ࠧ ந
   :

           class employee {
             // ...
             virtual void print();
           };

           class foreman : public employee {
             // ...
             void print();
           };

           class manager : public foreman {
             // ...
             void print();
           };

    㭪 foreman::print()  㤥 뢠,  
   猪 । 맮  ⮩ 㭪樨.  
   让   ८  㤭:

           class foreman : public employee {
             typedef employee inherited;
             // ...
             void print();
           };

           class manager : public foreman {
             typedef foreman inherited;
             // ...
             void print();
           };

           void manager::print()
           {
             inherited::print();
             // ...
           }

   ࠢ ⥩ ,  ⭮ ,  ⭮ 
    ⨯, ࠭,  訥 ᪮쪮 ⨯
   inherited   䫨⮢   㣮.  饬- 
   ,  襭  ⨯ inherited 來  .

   6.5.3 㠫  

    । ࠧ ⢥ ᫥ ᬠਢ
    ⢥ 䠪, 騩   ᫨ﭨ ᮢ
    ⥣஢ ᨬ ᮧ訥 ணࠬ.
    ᠬ ᭮ ਬ ⢥ ᫥, ,
     (  砩),  ᠬ ⮩   ᯮᮡ
    ਬ.
       ਬ ⢥ ᫥ ।
   筮    ᠬ,  ᬠਢ
    "᪨"  .  - 筮 
   ஥஢ ᮢ⭮.  設⢥ 砥  ⮣ 
   ॡ ᮡ ⨫ ணࠬ஢, ⢥ ⫨騩
    ⮣,   ⮫쪮  ᬠਢ.   ந
      ⥫쭠 ࠡ. 筮 
   ᢮  ८।   ᪮쪨 㠫
   㭪権 (. $$13.2  $$8.7).    -
      ଠ. ᪮ ++ -   ண ஫
   ⨯, 魮 ଠ樨  ⮫쪮   㪠 ⮣,
     騬   . ᮡ ⠪ 㪠 
   㦨 㠫  .
       㠫    ᯮ짮  ।⠢
   "" ,   ⨧஢ ࠧ묨
   ᯮᮡ:

           class window {
             //  ଠ
             virtual void draw();
           };

     ᬮਬ ⮫쪮   饩 ଠ樨  
   window - 㭪 draw().  । ࠧ  ࠧ
   , ।⠢騥  (window).   । ᢮
   ( ࠧ) 㭪 ᮢ (draw):

            class window_w_border : public virtual window {
               //  "  ࠬ"
               // ।, 易  ࠬ
               void draw();
            };

            class window_w_menu : public virtual window {
               //  "  "
               // ।, 易  
               void draw();
            };

    ⥫  ।   ࠬ  :

            class window_w_border_and_menu
                  : public virtual window,
                  public window_w_border,
                  public window_w_menu {
                  //  "  ࠬ  "
                  void draw();
            };

    ந    ᢮⢠ . ⮡
   ᯮ짮 樥   ᢮,  
   ࠭஢,      ꥪ  window ᯮ
    ।⠢ 宦   window  
   ந .   ᯥ稢 ᠭ window 
    ந   㠫쭮  .
        ᫥騬 ࠧ ࠧ ⠢ ꥪ 
   window_w_border_and_menu:



   ⮡ 㢨 ࠧ    㠫 ᫥,
   ࠢ  㭮  㭪  $$6.5, 뢠騬 ⠢ ꥪ
    satellite.   ᫥     
   ,   㪠  㠫, 㤥 ।⠢
   ⢥ ꥪ⮬ ⮣ . ⨢,  
   ,   ᠭ ᫥   㪠 
   㠫, 㤥 ।⠢ ᢮ ᮡ⢥ ꥪ⮬.
             㭪樨 draw().   ᫨誮
   㤭,   ஦ ணࠬ   誠.
   砫  ᠬ  ⥬,   ࠧ    :

              void window_w_border::draw()
              {
                 window::draw();
                 // 㥬 ࠬ
              }

              void window_w_menu::draw()
              {
                window::draw();
                // 㥬 
              }

     .   祢,   ᫥㥬 ࠧ ।
   ⠪ 㭪権  ᫮ ⢥ ᫥ ($$6.2.1), 
   ࠡ⠫ ४᭮. ,  ந  ᫥饣 ஢
    誠:

             void window_w_border_and_menu::draw() // 誠!
             {
                window_w_border::draw();
                window_w_menu::draw();

                // ⥯ 樨, ⭮騥 ⮫쪮
                //    ࠬ  
              }

        ଠ쭮.  筮, ᭠砫 믮
    樨, 室   ᮢ,  ⥬ , 
   ⭮ ᮡ⢥  ந ᠬ.   १
   㭪 window::draw() 㤥 뢠 !  設⢠
   ᪨ ணࠬ    譨 맮,  
   ⨭  ࠭. 筮  뤠  ࠭ ࠥ .
      ⮡  誨,  ⢮  ⠪ ᯥ譮. 
   ⤥ ⢨, 믮塞  ᮬ,  ⢨,
   믮塞   .  ⮣    
   㭪 _draw(),  믮 㦭 ⮫쪮  
   ⢨,  㭪 draw() 㤥 믮   ⢨ 
   ⢨, 㦭    .   window
    ᢮   譥 㭪樨:

            class window {
             //  ଠ
             void _draw();
             void draw();
            };

     ந ᮢ 䥪  :

             class window_w_border : public virtual window {
                //  "  ࠬ"
                // ।, 易  ࠬ
                void _draw();
                void draw();
             };

             void window_w_border::draw()
             {
                window::_draw();
                _draw();   //  ࠬ
             };

   쪮  ந  ᫥饣 ஢ 
   ⫨稥 㭪樨, ஥      
   맮 window::draw(), ᪮ ⥯ 뢠 window::_draw()
    ⮫쪮  ࠧ:

             class window_w_border_and_menu
                 : public virtual window,
                 public window_w_border,
                 public window_w_menu {

                 void _draw();
                 void draw();
             };

             void window_w_border_and_menu::draw()
             {
               window::_draw();
               window_w_border::_draw();
               window_w_menu::_draw();

               _draw();  // ⥯ 樨, ⭮騥 ⮫쪮
                         //    ࠬ  
             }

    易⥫쭮   㭪樨 window::draw()  window::_draw(),
    稥    ࠧ  ᮪.
        ⮬ ਬ  window 㦨 ࠭饬 饩 
   window_w_border  window_w_menu ଠ樨  । 䥩
    饭   ᮢ. ᫨ ᯮ ⢥
   ᫥,  魮 ଠ樨  ॢ ᮢ ⨣
   ⥬,   ଠ ।   ॢ  
   ,    ⠭ 㯭 ᥬ ᮢ  
   㧫 ᠬ.  १    䥪:
   ७ ॢ      ᯮ  ࠭⢮
       ᮢ ॢ,   ᮢ ஦
    ⢮ 易 ꥪ⮢.
       ⢥, ⮡    ᮢ-쥢 ८।﫨
   㭪樨, ।  饬 㠫쭮  . 
   ࠧ   쥢   ᢮ ਠ 権,
   ⫨  㣨.    window   㭪
    get_input():

             class window {
              //  ଠ
              virtual void draw();
              virtual void get_input();
            };

      ந ᮢ  ᯮ짮  㭪,
    뢠  ⮬,   ।:

            class window_w_banner : public virtual window {
               //  "  "
               void draw();
               void update_banner_text();
            };

            void window_w_banner::update_banner_text()
            {
              // ...
              get_input();
              //  ⥪ 
            }

    㣮 ந  㭪 get_input()  ।,
    뢠  ⮬,   㤥 ᯮ짮:

            class window_w_menu : public virtual window {
               //  "  "
               // ।, 易  
               void draw();
               void get_input(); // ८। window::get_input()
            };

     । ᮡ   ந  ᫥饣
   ஢:

            class window_w_banner_and_menu
               : public virtual window,
               public window_w_banner,
               public window_w_menu
            {
               void draw();
            };

   ஫ 筮  㡥,   -
   । ࠧ 㭪樨:

            class window_w_input : public virtual window {
               // ...
               void draw();
               void get_input();  // ८। window::get_input
            };

            class window_w_input_and_menu
               : public virtual window,
               public window_w_input,
               public window_w_menu
           {             // 訡:   window_w_input 
                         // window_w_menu ८। 㭪
                         // window::get_input
              void draw();
           };

   ࠭ 㦨  訡,  ࠭ 筮
     ᯮᮡ:    window_w_input 
   window_w_menu 㭪, ८। "㭪-⥫", 
   - ࠧ ࠭ 筮:

           class window_w_input_and_menu
             : public virtual window,
             public window_w_input,
             public window_w_menu
          {
             void draw();
             void get_input();
          };

    ⮬  window_w_input_and_menu::get_input() 㤥
   ८।  㭪樨 get_input(). ஡ 堭 ࠧ襭
   筮 ᠭ  $$R.10.1.1.

   6.6 ஫ 㯠

        (private), 饭 (protected)
    騬 (public):
        童  X  ᯮ짮 ⮫쪮 㭪樨-童 
         X.
       饭 童  X  ᯮ짮 ⮫쪮 㭪樨-童
          X,  ⠪  㭪樨-童   
       ந  X ᮢ (. $$5.4.1).
       騩 童  ᯮ짮   㭪樨.
    ࠢ ᮮ⢥     㭪権  
   : 㭪樨, ॠ騥  (   童), 㭪樨,
   ॠ騥 ந  (  童 ந ) 
    ⠫ 㭪樨.
       ஫ 㯠 ਬ ࠧ  ᥬ . 
   ஫ 㯠  ,   魮 砥 .
    砥,  묨   㭪樨-童, ⠭  ..
   ࠢ  묨 童, ।⠢騬 :

               class X {
               private:
                 enum { A, B };
                 void f(int);
                 int a;
               };

               void X::f(int i)
               {
                 if (i<A) f(i+B);
                 a++;
               }

               void g(X& x)
               {
                 int i = X::A;     // 訡: X::A  童
                 x.f(2);           // 訡: X::f  童
                 x.a++;            // 訡: X::a  童
               }

   6.6.1 饭 童

    ਬ 饭 童,    window 
   ।饣 ࠧ.  㭪樨 _draw() ।砫 ⮫쪮 
   ᯮ짮  ந , ᪮ ।⠢﫨 
    ⥩,  ⮬  뫨  㤮 
     饣 ਬ.  뫨   ந⥫
   ਠ   ࠧ 㭪権.  㣮 ஭, 㭪樨 draw()
   ।砫  饣 ਬ.  ࠧ稥  ࠧ,
   ࠧ 䥩 ᮢ window    - 饭 䥩
    騩 䥩:

              class window {
              public:
                virtual void draw();
                // ...
              protected:
                void _draw();
                // 㣨 㭪樨, 㦠騥 ந⥫ ਠ
              private:
                // ।⠢ 
              };

    ࠧ  ஢   ந , ⠪, 
   window_w_border  window_w_menu.
       䨪 _ ᯮ   饭 㭪権, 
    ॠ樨 ,  饬 ࠢ: , 稭騥  _ ,
     ⢮   ணࠬ,   饣
   ᯮ짮. , 稭   ᨬ ન,
      (  童).
         ࠪ,   ஡ ਬ:

              class X {
              //  㬮砭 ⭠  
                 int priv;
              protected:
                 int prot;
              public:
                 int publ;
                 void m();
              };

    童 X::m   童  ࠭祭:

              void X::m()
              {
                priv = 1;   // ଠ쭮
                prot = 2;   // ଠ쭮
                publ = 3;   // ଠ쭮
              }

    ந    ⮫쪮  騬  饭
   童:

              class Y : public X {
                 void mderived();
              };

              Y::mderived()
              {
                priv = 1;    // 訡: priv  童
                prot = 2;    // ଠ쭮: prot 饭 童, 
                             // mderived() 童 ந  Y
                publ = 3;    // ଠ쭮: publ 騩 童
              }

    쭮 㭪樨 㯭 ⮫쪮 騥 童:

              void f(Y* p)
              {
                p->priv = 1;  // 訡: priv  童
                p->prot = 2;  // 訡: prot 饭 童,  f()
                              //    童 ᮢ X  Y
                p->publ = 3;  // ଠ쭮: publ 騩 童
              }

   6.6.2    ᠬ

    童      , 饭
    騩:

              class X {
              public:
                int a;
                // ...
              };

              class Y1 : public X {  };
              class Y2 : protected X { };
              class Y3 : private X { };

   ᪮ X - 騩    Y1,   㭪樨, ᫨ 
   室,  () ८ࠧ Y1*  X*,  ⮬
      㯭 騥 童  X:

              void f(Y1* py1, Y2* py2, Y3* py3)
              {
                X* px = py1;  // ଠ쭮: X - 騩   Y1
                py1->a = 7;   // ଠ쭮
                px = py2;     // 訡: X - 饭   Y2
                py2->a = 7;   // 訡
                px = py3;     // 訡: X -    Y3
                py3->a = 7;   // 訡
              }

     ᠭ

              class Y2 : protected X { };
              class Z2 : public Y2 { void f(); };

   ᪮ X - 饭   Y2, ⮫쪮   童 Y2,
    ⠪   童  ந  Y2 ᮢ ( ⭮
   Z2)   室 ८ࠧ뢠 () Y2*  X*.
   ஬ ⮣     騬  饭 童  X:

              void Z2::f(Y1* py1, Y2* py2, Y3* py3)
              {
                X* px = py1; // ଠ쭮: X - 騩   Y1
                py1->a = 7; // ଠ쭮
                px = py2;   // ଠ쭮: X - 饭   Y2,
                            //  Z2 - ந  Y2
                py2->a = 7; // ଠ쭮
                px = py3;   // 訡: X -    Y3
                py3->a = 7; // 訡
              }

   , ᬮਬ:

              class Y3 : private X { void f(); };

   ᪮ X -    Y3, ⮫쪮   童 Y3
     室 ८ࠧ뢠 () Y3*  X*.
   ஬ ⮣     騬  饭 童
    X:

              void Y3::f(Y1* py1, Y2* py2, Y3* py3)
              {
                X* px = py1;  // ଠ쭮: X - 騩   Y1
                py1->a = 7;   // ଠ쭮
                px = py2;     // 訡: X - 饭   Y2
                py2->a = 7;   // 訡
                px = py3;     // ଠ쭮: X -    Y3,
                              //  Y3::f 童 Y3
                py3->a = 7;   // ଠ쭮
              }

   6.7  

   ᫨ । 㭪樨 operator new()  operator delete(),
   ࠢ       ᢮ 㪨.  ⠪ ,
   (    ), ᤥ  , 㦠饣 
     ந ᮢ. ⨬,  ॡ ᢮
   㭪樨 ࠧ饭  ᢮    employee ($$6.2.5)
      ந ᮢ:

              class employee {
                // ...
              public:
                void* operator new(size_t);
                void operator delete(void*, size_t);
              };

              void* employee::operator new(size_t s)
              {
                // ⢥   `s' ⮢
                //   㪠⥫  
              }

              void employee::operator delete(void* p, size_t s)
              {
                // `p'  㪠뢠    `s' ⮢,
                // ⢥ 㭪樥 employee::operator new();
                // ᢮    ୮ ᯮ짮
              }

   祭  ᥩ  筮 ࠬ ⨯ size_t ⠭
   祢.  - ࠧ ᢮ ꥪ.  㤠 ⮣
   㦠饣  ࠬ 砥 祭 sizeof(employee),  
   㤠 ࠢ饣 - sizeof(manager). ⮬ ᮡ⢥
   㭪樨   ࠧ饭   ࠭ ࠧ 
   ࠧ頥 ꥪ. 筮,   ࠭  ࠧ (
   㭪 ࠧ饭 饣 祭)  ஢ ࠬ
   size_t  맮 operator delete(),  ⮣     ,
   祬 㭪樨 ࠧ饭  ᢮ 饣 祭.
         ࠭ । 㦭 ࠧ,   ।
   㭪樨 operator delete()?  ⨯, 㪠  operator delete(),
   ᮮ⢥ ⨭ ⨯ ꥪ,  ;  ᬮਬ
   ⠪ ਬ:

              class manager : public employee {
                int level;
                // ...
              };

              void f()
              {
                employee* p = new manager; // ஡
                delete p;
              }

    ⮬ 砥 ࠭  ᬮ ࠢ쭮 । ࠧ. 
     砥 㤠 ᨢ, 㦭  ணࠬ.  
   । 㠫     employee:

              class employee {
                // ...
              public:
                // ...
                void* operator new(size_t);
                void operator delete(void*, size_t);
                virtual ~employee();
              };

    ⮩    ஡:

             employee::~employee() { }

    ᢮  㤥 ந室   (  
   ࠧ ⥭),   ந  employee  ⠪ 㤥
   㦤 । ᢮  (⥬ ᠬ 㤥 ⠭
   㦭 ࠧ), ᫨ ⮫쪮 짮⥫ ᠬ  । .
    ᫥騩 ਬ ன ࠢ쭮:

             void f()
             {
               employee* p = new manager; // ⥯  ஡
               delete p;
             }

   饭 ந室   (ᮧ ࠭஬) 맮

            employee::operator new(sizeof(manager))

    ᢮   맮

            employee::operator delete(p,sizeof(manager))

   묨 ᫮, ᫨ 㦭  ४ 㭪樨 ࠧ饭 
   ᢮  ந ᮢ,   ।
   㠫    ,   ᯮ짮
    㭪樨 ᢮ ࠬ size_t. 筮,  뫮
    ஥஢ 몠 ।ᬮ ।⢠, ᢮騥
   짮⥫  ⮩ ஡.  ⮣ 짮⥫ "᢮" 
     । २  ⨬쭮,    
   ⥬.
       饬 砥, ᥣ   । 㠫
      ᮢ,  ⢨⥫쭮 ᯮ 
   , ..  ꥪ⠬ ந ᮢ ࠡ , ,
   㤠 , १ 㪠⥫   :

            class X {
               // ...
            public:
               // ...
               virtual void f(); //  X  㠫쭠 㭪, ⮬
                                 // ।塞 㠫 
               virtual ~X();
            };

   6.7.1 㠫 

     㠫 , ⢥ : " 
       㠫묨?" ᫨ ⢥ ⪮ - .
       ⢥: ",    
   ॡ㥬 䥪".
           㠫, ᪮  ࠢ쭮
   ஥ ꥪ     ⨭ ⨯.  ⮣,
    -  ᮢᥬ 筠 㭪.   ⢮
    㭪ﬨ ࠢ ,    
   㭪権.   㭪権-童  ⫨砥  ⥬, 
    뢠   ꥪ⮢. ⥫쭮  
   㪠⥫  .
         ࠭祭  , ᫨ । 㭪,
   ᮤঠ 맮    ஥ ꥪ.
    㤠筮, ᪮ । 뢠 㦭 ᮧ  ꥪ,
      ⨭ ⨯. ਬ,  ࠭樨 
    室 ᤥ  ॢ, ।⠢饣
   ࠧࠥ ࠦ.  ॢ   㧫 ࠦ ࠧ
   . ⨬,  㧫,  ᮤঠ 騥  ࠦ
   樨, 㦭 ஢ ⮫쪮  ࠧ.   ॡ
   㠫쭠 㭪 ࠧ  㧫 ࠦ.
         ࠢ "㠫 "  ⠭묨
   ࠬ  ࠬ஢  ࠬ ஢,
   ࠬ஬  㦨 ⨯ १:

            class expr {
               // ...
            public:
               expr();  // ⠭ 
               virtual expr* new_expr() { return new expr(); }
            };

   㠫쭠 㭪 new_expr()  頥 ⠭⭮
   樠஢ ꥪ ⨯ expr, ࠧ饭  ᢮ .
    ந   ८। 㭪 new_expr() ⠪,
   ⮡  頫 ꥪ ⮣ :

            class conditional : public expr {
               // ...
            public:
              conditional();  // ⠭ 
              expr* new_expr() { return new conditional(); }
            };

    砥, ,  ꥪ  expr, 짮⥫ 
   ᮧ ꥪ  "筮 ⠪  ⨯":

            void user(expr* p1, expr* p2)
            {
               expr* p3 = p1->new_expr();
               expr* p4 = p2->new_expr();
               // ...
             }

   ६ p3  p4 ᢠ 㪠⥫ ⭮,  室饣
   ⨯.
         ᯮᮡ  । 㠫 
   ஢, 뢠 樥 ࠧ,   
    ⥫쭮  ᯥ䨪 樨 ஢:

             class expr {
                // ...
                expr* left;
                expr* right;
             public:
                // ...
                   // ஢ `s'  `this'
                inline void copy(expr* s);
                   // ᮧ  ꥪ,   ᬮ this
                virtual expr* clone(int deep = 0);
             };

   ࠬ deep 뢠 ࠧ稥  ஢ ᮡ⢥
   ꥪ (孮⭮ ஢)  ஢ ᥣ ॢ,
   ୥ ண 㦨 ꥪ (㡮 ஢). ⠭⭮
   祭 0 砥 孮⭮ ஢.
       㭪 clone()  ᯮ짮, ਬ, ⠪:

              void fct(expr* root)
              {
                expr* c1 = root->clone(1);  // 㡮 ஢
                expr* c2 = root->clone();   // 孮⭮ ஢
                // ...
              }

    㠫쭮, 㭪 clone() ᯮᮡ ࠧ ꥪ
    ந  expr .
       饥 ஢  । ⠪:

              void expr::copy(expression* s, int deep)
              {
                if (deep == 0) { // 㥬 ⮫쪮 童
                   *this = *s;
                }
                else { // ன  㪠⥫:
                   left = s->clone(1);
                   right = s->clone(1);
                   // ...
                }
              }

   㭪 expr::clone() 㤥 뢠 ⮫쪮  ꥪ⮢ ⨯
   expr (   ந  expr ᮢ), ⮬  
   ࠧ       ꥪ ⨯ expr, 騩
   ᮡ⢥ :

              expr* expr::clone(int deep)
              {
                expr* r = new expr();  // ந ⠭⭮ ࠦ
                r->copy(this,deep);    // 㥬 `*this'  `r'
                return r;
              }

    㭪 clone()  ᯮ짮  ந  expr
   ᮢ, ᫨     童- (   ࠧ
   ⨯ 砩):

              class arithmetic : public expr {
                // ...
                //  童-  =>
                //  ᯮ짮 㦥 । 㭪 clone
              };

    㣮 ஭, ᫨  童-,  㦭 ।
   ᮡ⢥ 㭪 clone():

             class conditional : public expression {
                expr* cond;
             public:
                inline void copy(cond* s, int deep = 0);
                expr* clone(int deep = 0);
                // ...
             };

   㭪樨 copy()  clone() ।  ᢮  
   expression:

             expr* conditional::clone(int deep)
             {
               conditional* r = new conditional();
               r->copy(this,deep);
               return r;
             }

             void conditional::copy(expr* s, int deep)
             {
               if (deep == 0) {
                  *this = *s;
               }
               else {
                  expr::copy(s,1);  // 㥬  expr
                  cond = s->cond->clone(1);
               }
             }

   । ᫥ 㭪樨 뢠 ⫨稥 饣
   ஢  expr::copy()   ࠧ  expr::clone()
   (.. ᮧ  ꥪ  ஢  ). ⮥
   ஢ 뢠   ।  ᫮
   権 ஢  ࠧ. 稥  copy()  clone()
   ⭮ ࠧ  樥 ᢠ  ஬
   ஢ ($$1.4.2)  ⭮ ࠧ  㭪ﬨ
   _draw()  draw() ($$6.5.3). ⬥⨬,  㭪 copy()  
   㠫쭮.      ⠪, ᪮ 㠫쭠
   뢠  㭪 clone(). 祢,   樨
   ஢  ⠪ ।  㭪樨-⠭.

   6.7.2  ࠧ饭

    㬮砭  new ᮧ 㪠  ꥪ  ᢮
   .  , ᫨  ࠧ ꥪ  । ?
   ⮣   ८। 樨 ࠧ饭. ᬮਬ
   ⮩ :

          class X {
             // ...
          public:
            X(int);
            // ...
          };

   ꥪ  ࠧ   , ᫨   㭪
   ࠧ饭 ⥫ ࠬ:

          //  ࠧ饭  㪠 :
          void* operator new(size_t, void* p) { return p; }

      ࠬ  樨 new ᫥騬 ࠧ:

          char buffer[sizeof(X)];

          void f(int i)
          {
            X* p = new(buffer) X(i); // ࠧ X  buffer
            // ...
          }

   㭪 operator new(), ᯮ㥬 樥 new, 롨ࠥ
   ᮣ᭮ ࠢ ᮯ⠢ ࠬ஢ ($$R.13.2). 
   㭪樨 operator new()    ࠬ஬ size_t.
    ⨬ ࠬ஬ ࠧ  । 樥
   new.
       ।  㭪 operator new()   ࠧ饭
    ᠬ ⮩  㭪権  த.  ਢ
   㣮 ਬ 㭪樨 ࠧ饭, 뤥饩   ன
    :

          class Arena {
             // ...
             virtual void* alloc(size_t) = 0;
             virtual void free(void*) = 0;
          };

          void operator new(size_t sz, Arena* a)
          {
            return a.alloc(sz);
          }

     ⢮   ꥪ⮢ ந ⨯ 
   ࠧ ⥩ (Arena):

          extern Arena* Persistent; // ﭭ 
          extern Arena* Shared;     // ࠧ塞 

          void g(int i)
          {
            X* p = new(Persistent) X(i); // X  ﭭ 
            X* q = new(Shared) X(i);     // X  ࠧ塞 
            // ...
          }

   ᫨  頥 ꥪ   ,  ।⢥
    ࠢ ⠭묨 㭪ﬨ । ᢮
   ,     ࠢ쭮 㭨⮦ ꥪ.
   ᭮ ।⢮    맮 :

          void h(X* p)
          {
            p->~X();             // 맮 
            Persistent->free(p); // ᢮ 
          }

   ⨬,   맮 ஢,    㭪権
   ࠧ饭 ᯥ樠쭮 祭, ᫥,  ,
   . 뢠 砨,     㤭, 
   箪  ਦ 㬠, ० 祬 ᯮ짮 
   맮 ,   ᭠砫 ᮢ⮢   
   .

   6.8 ࠦ

   1. (*1)   

           class base {
           public:
              virtual void iam() { cout << "base\n"; }
           };

      ।  ந  base     ।
      㭪 iam(), 뤠  ᢮ .  ꥪ
       ᮢ  맮 iam()  . ᢮  ꥪ⮢
      ந ᮢ 㪠⥫ ⨯ base*  맮 iam() 
        㪠⥫.
   2. (*2)  ਬ⨢ ࠢ ࠭ ($$6.4.1) ࠧ㬭
       襩 ⥬ ࠧ.
   3. (*2) ।  triangle (㣮쭨)  circle
      (㦭).
   4. (*2) । 㭪,  १ אַ, ᮥ騩
       䨣. 砫   ᠬ 訥 窨 䨣, 
      ⥬ ᮥ .
   5. (*2)  ਬ  ᮬ shape ⠪, ⮡ line 뫮
      ந ᮬ  rectangle,  .
   6. (*2)   

           class char_vec {
              int sz;
              char element [1];
           public:
              static new_char_vec(int s);
              char& operator[] (int i) { return element[i]; }
              // ...
           };

       । 㭪 new_char_vec()  ⢥ 뢭
       ⪠   ꥪ⮢ char_vec ⠪, ⮡  
       뫮 ஢  ᨢ element[].   砥 
       㭪 맮 쥧 㤭?
    7. (*1)    ,  㦭 
       ਬ  ᮬ shape  $$6.4,  ᭨,  
       믮 㠫 맮.
    8. (*1.5)   ,  㦭  ਬ
        ᮬ satellite  $$6.5,  ᭨,   믮
       㠫 맮.
    9. (*2)   ,  㦭  ਬ 
       ᮬ window  $$6.5.3,  ᭨,   믮
       㠫 맮.
    10. (*2)   ᪨ ꥪ⮢  ஬ 
       権,  㤥 騬   ⥪ ᪨
       ꥪ⮢. ᫥ - ᪨ ⥪,
       ⮡ ,  樨 㦭. ।  ꥪ⮢
          ஬  権,  㤥
       騬  ᮬ ꥪ⮢, ࠭  ᫥⥫쭮
         . ᫥ -  , ⮡
       ,  樨 㦭. । ꥪ ᪮
        , ᯮ   ᯮ ⢥
       ᫥. 㤨 ⭮⥫    
       襭.
    11. (*2)  ਠ 㭪樨 clone()  $$6.7.1,  ஬
       ࠧ ꥪ     Arena
       ($$6.7.2), ।  ࠬ.  ⮩
        Arena  ந  Arena.
    12. (*2)    Circle (㦭), Square () 
       Triangle (㣮쭨), ந   shape. ।
       㭪 intersect()   ࠬࠬ ⨯ Shape*, 
       뢠 室 㭪, ⮡ ᭨, ᥪ 
         䨣.  ⮣  㪠  㦭
       । ᮮ⢥騥 㠫 㭪樨.  
       ᨫ  㭪,  ⢨⥫쭮 ⠭, 
       䨣 ᥪ,  ⮫쪮 ࠢ쭮
       ᫥⥫쭮 맮 㭪権.
    13. (*5) ࠡ⠩  ॠ ⥪  ஢,
       ࠢ塞 ᮡﬨ. ᪠: ᯮ <task.h>.
        㦥 ॢ訥 㭪樨    . 
         task (). ꥪ task  㬥 ࠭
       ᢮ ﭨ  ⠭  ( ⮣ 
       । 㭪樨 task::save()  task::restore())  ⮣
         ⢮  ᮯணࠬ. 樠 
        ।  ꥪ ᮢ, ந  task.
       ணࠬ,  믮 , । 
       㠫 㭪.    ।
       ࠬ    ࠬ   
       ࠬ.   ᯥ,  ॠ
       ⨥ 㠫쭮 ६. । 㭪
       task::delay(long),  㤥 "ꥤ" 㠫쭮
       ६.   ࠧࠡ⪨:  
       ᯥ   task,     ᨬ?
           饭   㣮.
        ⮩ 楫 ࠧࠡ⠩  queue (।). ਤ㬠
       ᯮᮡ, ⮡    室 ⮪  ᪮쪨
       ।.  ᪨ 訡  ࠡ뢠
       ࠧ.  ࣠ ⫠ ணࠬ, ᠭ
         ⠪ ⥪?

