home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / privat2.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  2.7 KB  |  112 lines

  1.                                        -- Chapter 21 - Program 3
  2.  
  3. -- This package uses a data structure composed of three INTEGER
  4. -- variables.  It allow the user to add two structures, component
  5. -- by component, or subtract component by component.  Provision is
  6. -- also made to build a structure from three numbers, or decompose
  7. -- a structure into its components.
  8.  
  9. package Three is
  10. type DATA_STRUCTURE is private;
  11. function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE;
  12. function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE;
  13. function Build_Structure(Val1, Val2, Val3 : INTEGER) return
  14.                                                    DATA_STRUCTURE;
  15. procedure Decompose(Data1 : DATA_STRUCTURE;
  16.                     Val1, Val2, Val3 : out INTEGER);
  17.  
  18. private
  19.    type DATA_STRUCTURE is array(1..3) of INTEGER;
  20. end Three;
  21.  
  22.  
  23.  
  24. package body Three is
  25.  
  26. function "+"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
  27. Temp : DATA_STRUCTURE;
  28. begin
  29.    Temp(1) := Data1(1) + Data2(1);
  30.    Temp(2) := Data1(2) + Data2(2);
  31.    Temp(3) := Data1(3) + Data2(3);
  32.    return Temp;
  33. end "+";
  34.  
  35.  
  36. function "-"(Data1, Data2 : DATA_STRUCTURE) return DATA_STRUCTURE is
  37. Temp : DATA_STRUCTURE;
  38. begin
  39.    Temp(1) := Data1(1) - Data2(1);
  40.    Temp(2) := Data1(2) - Data2(2);
  41.    Temp(3) := Data1(3) - Data2(3);
  42.    return Temp;
  43. end "-";
  44.  
  45.  
  46. function Build_Structure(Val1, Val2, Val3 : INTEGER) return
  47.                                                    DATA_STRUCTURE is
  48. Temp : DATA_STRUCTURE;
  49. begin
  50.    Temp(1) := Val1;
  51.    Temp(2) := Val2;
  52.    Temp(3) := Val3;
  53.    return Temp;
  54. end Build_Structure;
  55.  
  56.  
  57. procedure Decompose(Data1 : DATA_STRUCTURE;
  58.                     Val1, Val2, Val3 : out INTEGER) is
  59. begin
  60.    Val1 := Data1(1);
  61.    Val2 := Data1(2);
  62.    Val3 := Data1(3);
  63. end Decompose;
  64.  
  65. end Three;
  66.  
  67.  
  68.  
  69.  
  70. -- This program exercises the package Three as an illustration.
  71.  
  72. with Text_IO;   use Text_IO;
  73. with Three;     use Three;
  74.  
  75. procedure Privat2 is
  76.  
  77.    My_Data, Extra_Data : DATA_STRUCTURE;
  78.    Temp                : DATA_STRUCTURE;
  79.  
  80. begin
  81.  
  82.    My_Data := Build_Structure(3,7,13);
  83.    Extra_Data := Build_Structure(-4,77,0);
  84.    My_Data := My_Data + Extra_Data;
  85.  
  86.    if My_Data /= Extra_Data then
  87.       Put_Line("The two structures are not equal.");
  88.    end if;
  89.  
  90.    My_Data := Extra_Data;
  91.  
  92.    if My_Data = Extra_Data then
  93.       Put_Line("The two structures are equal now.");
  94.    end if;
  95.  
  96. --       The following line is illegal with the private type.
  97. -- My_Data.Value1 := My_Data.Value1 + 13;
  98.  
  99.    Temp := Build_Structure(13,0,0);
  100.    My_Data := My_Data + Temp;
  101.  
  102. end Privat2;
  103.  
  104.  
  105.  
  106.  
  107. -- Result of execution
  108.  
  109. -- The two structures are not equal.
  110. -- The two structures are equal now.
  111.  
  112.