home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / PASANS.ZIP / CH14_1.PAS < prev    next >
Pascal/Delphi Source File  |  1991-02-04  |  3KB  |  117 lines

  1.                         (* Chapter 15 - Programming exercise 1 *)
  2. program Encapsulation_2;
  3.  
  4. type
  5.    Box = object
  6.       length : integer;
  7.       width  : integer;
  8.       constructor Init(len, wid : integer);
  9.       procedure Set_Data(len, wid : integer);
  10.       function Get_Area : integer;
  11.    end;
  12.  
  13.    Pole = object
  14.       height : integer;
  15.       depth  : integer;
  16.       constructor Init(hei, dep : integer);
  17.       procedure Set_Data(hei, dep : integer);
  18.       function Get_Total_Length : integer;
  19.    end;
  20.  
  21.    constructor Box.Init(len, wid : integer);
  22.    begin
  23.       length := len;
  24.       width := wid;
  25.    end;
  26.  
  27.    constructor Pole.Init(hei, dep : integer);
  28.    begin
  29.       height := hei;
  30.       depth := dep;
  31.    end;
  32.  
  33.    procedure Box.Set_Data(len, wid : integer);
  34.    begin
  35.       length := len;
  36.       width := wid;
  37.    end;
  38.  
  39.    function Box.Get_Area : integer;
  40.    begin
  41.       Get_Area := length * width;
  42.    end;
  43.  
  44.    procedure Pole.Set_Data(hei, dep : integer);
  45.    begin
  46.       height := hei;
  47.       depth := dep;
  48.    end;
  49.  
  50.    function Pole.Get_Total_length : integer;
  51.    begin
  52.       Get_Total_length := height + depth;
  53.    end;
  54.  
  55. var Small, Medium, Large : Box;
  56.     Short, Average, Tall : Pole;
  57.     Dumb                 : integer;
  58.  
  59. begin
  60.  
  61.    Small.Init(8,8);
  62.    Medium.Init(10,12);
  63.    Large.Init(15,20);
  64.    Short.Init(8,2);
  65.    Average.Init(12,3);
  66.    Tall.Init(17,4);
  67.  
  68.    Dumb := Short.height * Medium.length;
  69.    Writeln('The dumb product is ',Dumb);
  70.  
  71.    WriteLn('The area of the small box is ',Small.Get_Area);
  72.    WriteLn('The area of the medium box is ',Medium.Get_Area);
  73.    WriteLn('The area of the large box is ',Large.Get_Area);
  74.    WriteLn('The overall length of the short pole is ',
  75.                                           Short.Get_Total_Length);
  76.    WriteLn('The overall length of the average pole is ',
  77.                                         Average.Get_Total_Length);
  78.    WriteLn('The overall length of the tall pole is ',
  79.                                            Tall.Get_Total_Length);
  80.    WriteLn;
  81.  
  82.    Small.Set_Data(6,7);
  83.    Short.Set_Data(6,1);
  84.    Average.Set_Data(11,2);
  85.    WriteLn('The area of the small box is ',Small.Get_Area);
  86.    WriteLn('The area of the medium box is ',Medium.Get_Area);
  87.    WriteLn('The area of the large box is ',Large.Get_Area);
  88.    WriteLn('The overall length of the short pole is ',
  89.                                           Short.Get_Total_Length);
  90.    WriteLn('The overall length of the average pole is ',
  91.                                         Average.Get_Total_Length);
  92.    WriteLn('The overall length of the tall pole is ',
  93.                                            Tall.Get_Total_Length);
  94.  
  95. end.
  96.  
  97.  
  98.  
  99.  
  100. { Result of execution
  101.  
  102. The dumb product is 80
  103. The area of the small box is 64
  104. The area of the medium box is 120
  105. The area of the large box is 300
  106. The overall length of the short pole is 10
  107. The overall length of the average pole is 15
  108. The overall length of the tall pole is 21
  109.  
  110. The area of the small box is 42
  111. The area of the medium box is 120
  112. The area of the large box is 300
  113. The overall length of the short pole is 7
  114. The overall length of the average pole is 13
  115. The overall length of the tall pole is 21
  116.  
  117. }