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

  1.                                      (* Chapter 14 - Program 3 *)
  2. program Inheritance_1;
  3.  
  4. { ******************** class definitions **********************}
  5.  
  6. type
  7.    Vehicle = object
  8.       Wheels : integer;
  9.       Weight : real;
  10.       constructor Init(In_Wheels : integer; In_Weight : real);
  11.       function Get_Wheels : integer;
  12.       function Get_Weight : real;
  13.       function Wheel_loading : real;
  14.    end;
  15.  
  16.  
  17.    Car = object(Vehicle)
  18.       Passenger_Load : integer;
  19.       constructor Init(In_Wheels : integer;
  20.                        In_Weight : real;
  21.                        People    : integer);
  22.       function Passengers : integer;
  23.    end;
  24.  
  25.  
  26.    Truck = Object(Vehicle)
  27.       Passenger_Load : integer;
  28.       Payload        : real;
  29.       constructor Init(People : integer;
  30.                        Max_Load : real;
  31.                        In_Wheels : integer;
  32.                        In_Weight : real);
  33.       function Efficiency : real;
  34.       function Wheel_Loading :real;
  35.    end;
  36.  
  37. { ********************* class implementations ******************** }
  38.  
  39.    constructor Vehicle.Init(In_Wheels : integer; In_Weight : real);
  40.    begin
  41.       Wheels := In_Wheels;
  42.       Weight := In_Weight;
  43.    end;
  44.  
  45.    function Vehicle.Get_Wheels : integer;
  46.    begin
  47.       Get_Wheels := Wheels;
  48.    end;
  49.  
  50.    function Vehicle.Get_Weight : real;
  51.    begin
  52.       Get_Weight := Weight;
  53.    end;
  54.  
  55.    function Vehicle.Wheel_loading : real;
  56.    begin
  57.       Wheel_Loading := Weight/Wheels;
  58.    end;
  59.  
  60.    constructor Car.Init(In_Wheels : integer;
  61.                         In_Weight : real;
  62.                         People    : integer);
  63.    begin
  64.       Wheels := In_Wheels;
  65.       Weight := In_Weight;
  66.       Passenger_Load := People;
  67.    end;
  68.  
  69.    function Car.Passengers : integer;
  70.    begin
  71.       Passengers := Passenger_Load;
  72.    end;
  73.  
  74.    constructor Truck.Init(People : integer;
  75.                           Max_Load : real;
  76.                           In_Wheels : integer;
  77.                           In_Weight : real);
  78.    begin
  79.       Passenger_Load := People;
  80.       Payload := Max_Load;
  81.       Vehicle.Init(In_Wheels, In_Weight);
  82.    end;
  83.  
  84.    function Truck.Efficiency : real;
  85.    begin
  86.       Efficiency := 100.0 * Payload / (Payload + Weight);
  87.    end;
  88.  
  89.    function Truck.Wheel_Loading : real;
  90.    begin
  91.       Wheel_Loading := (Weight + Payload)/Wheels;
  92.    end;
  93.  
  94. { ************************ main program ************************** }
  95.  
  96. var Unicycle : Vehicle;
  97.     Sedan    : Car;
  98.     Semi     : Truck;
  99.  
  100. begin
  101.  
  102.    Unicycle.Init(1, 12.0);
  103.    Sedan.Init(4, 2100.0, 5);
  104.    Semi.Init(1, 25000.0, 18, 5000.0);
  105.  
  106.    WriteLn('The unicycle weighs ', Unicycle.Get_Weight:5:1,
  107.            ' pounds, and has ', Unicycle.Get_Wheels, ' wheel.');
  108.  
  109.    WriteLn('The car weighs ', Sedan.Get_Weight:7:1,
  110.            ' pounds, and carries ', Sedan.Passengers,
  111.            ' passengers.');
  112.  
  113.    WriteLn('The semi has a wheel loading of ', Semi.Wheel_Loading:8:1,
  114.            ' pounds per tire,');
  115.    WriteLn(' and has an efficiency of ', Semi.Efficiency:5:1,
  116.            ' percent.');
  117.  
  118.    with Semi do
  119.    begin
  120.       WriteLn('The semi has a wheel loading of ', Wheel_Loading:8:1,
  121.               ' pounds per tire,');
  122.       WriteLn(' and has an efficiency of ', Efficiency:5:1,
  123.               ' percent.');
  124.    end;
  125. end.
  126.  
  127.  
  128.  
  129.  
  130. { Result of execution
  131.  
  132. The unicycle weighs  12.0 pounds, and has 1 wheel.
  133. The car weighs  2100.0 pounds, and carries 5 passengers.
  134. The semi has a wheel loading of   1666.7 pounds per tire,
  135.  and has an efficiency of 83.3 percent.
  136. The semi has a wheel loading of   1666.7 pounds per tire,
  137.  and has an efficiency of 83.3 percent.
  138.  
  139. }