home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / OOPTUT34.ZIP / INHERIT.PAS < prev    next >
Pascal/Delphi Source File  |  1992-11-10  |  4KB  |  121 lines

  1. program object_inherit;
  2.  
  3. {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  4. { Program to illustrate object inheritance, showing that descendant  }
  5. { object types inherit the data fields and the methods of their      }
  6. { ancestor type, but that instances of these object types do not     }
  7. { automatically inherit the values in these data fields of an        }
  8. { ancestor instance. This is only to be expected, as there may be    }
  9. { many instances and the program has no way of determining which     }
  10. { instance is appropriate. The programmer can, of course, write the  }
  11. { necessary code to transfer values as required and this is greatly  }
  12. { facilitated by the use of the 'dot' notation, as shown below.      }
  13. {                                                                    }
  14. { INHERIT.PAS        R Shaw        23.9.91                           }
  15. {____________________________________________________________________}
  16.  
  17. Uses  Crt;
  18.  
  19. Type
  20.  
  21. TAncestor = object
  22.    a,b,c  : integer;
  23.    constructor init(aa,bb,cc : integer);
  24.    end;
  25.  
  26. TDescendant = object(TAncestor)
  27.    d : integer;
  28.    constructor init(ad,bd,cd,dd : integer);
  29.    end;
  30.  
  31. TNewDescendant = object(TAncestor)
  32.    d : integer;
  33.    constructor init(dd : integer);
  34.    procedure Inherit(Anc : TAncestor);
  35.    end;
  36.  
  37. TNewAncestor = object(TDescendant)
  38.    procedure Inherit(ADesc : TDescendant);
  39.    end;
  40.  
  41.  
  42. constructor TAncestor.Init(aa,bb,cc : integer);
  43. begin
  44.    a := aa;
  45.    b := bb;
  46.    c := cc;
  47. end;
  48.  
  49. constructor TDescendant.Init(ad,bd,cd,dd : integer);
  50. begin
  51.    a := ad;
  52.    b := bd;
  53.    c := cd;
  54.    d := dd;
  55. end;
  56.  
  57. constructor TNewDescendant.Init(dd : integer);
  58. begin
  59.    d := dd;
  60. end;
  61.  
  62. procedure TNewDescendant.Inherit(Anc : TAncestor);
  63. begin
  64.    a := Anc.a;
  65.    b := Anc.b;
  66.    c := Anc.c;
  67. end;
  68.  
  69. procedure TNewAncestor.Inherit(ADesc : TDescendant);
  70. begin
  71.    a := ADesc.a;
  72.    b := ADesc.b;
  73.    c := ADesc.c;
  74. end;
  75.  
  76. {Main}
  77.  
  78. Var
  79.    Ancestor : TAncestor;
  80.    NewAncestor : TNewAncestor;
  81.    ADescendant : TDescendant;
  82.    NewDescendant : TNewDescendant;
  83.  
  84. begin
  85.    ClrScr;
  86.    Ancestor.Init(1,2,3);
  87.    ADescendant.Init(6,7,8,9);
  88.    NewDescendant.Inherit(Ancestor);
  89.    NewDescendant.Init(4);
  90.    NewAncestor.Inherit(ADescendant);
  91.    Writeln('Ancestor.a = ',Ancestor.a,'...b = ',Ancestor.b,'...c = ',Ancestor.c);
  92.    writeln;
  93.    writeln('ADescendant.a = ',ADescendant.a,'...b = ',ADescendant.b,'...c = '
  94.            ,ADescendant.c,'...d = ',ADescendant.d);
  95.    writeln;
  96.    writeln('ADescendant has data fields of Ancestor but its own initialized values');
  97.    writeln('and also a new field d and value');
  98.    writeln('             --------------- ');
  99.    writeln;
  100.    writeln('NewDescendant.a = ',NewDescendant.a,'...b = ',NewDescendant.b,
  101.             '...c = ',NewDescendant.c,'...d = ',NewDescendant.d);
  102.    writeln;
  103.    writeln('NewDescendant has same fields a,b,c and values as Ancestor');
  104.    writeln('but new field and value d in addition');
  105.    writeln('             --------------- ');
  106.    writeln;
  107.    writeln('NewAncestor.a = ',NewAncestor.a,'...b = ',NewAncestor.b,
  108.             '...c = ',NewAncestor.c);
  109.    writeln;
  110.    writeln('NewAncestor has same fields as Ancestor but values changed ');
  111.    writeln('to those of NewDescendant');
  112.    writeln('             --------------- ');
  113.    writeln;
  114.    Ancestor := NewAncestor;
  115.    writeln('Ancestor.a = ',Ancestor.a,'...b = ',Ancestor.b,'...c = ',Ancestor.c);
  116.    writeln;
  117.    writeln('Ancestor field values are now changed to those of NewAncestor');
  118.    repeat until keypressed;
  119. end.
  120.  
  121.