Ada: Understanding private types and understanding packaging

I’m trying to learn how to use private ads in Ada, as well as understand the packaging. I tried to keep my codes as short as possible.

Let's start with the main file rectangular_Form.ads

with Rectangular_Method_1;
package Rectangular_Form renames Rectangular_Method_1;
-- with Rectangular_Method_2;
-- package Rectangular_Form renames Rectangular_Method_2;

This tells us that we can have two implementations and what is currently selected Rectangular_Method_1.

Then we have a specification file Rectangular_Method_1.ads:

package Rectangular_Method_1 is
   type Rectangular is private;

   procedure Vector_Basis_r (A : in Long_Float; D : out Rectangular);
   procedure Set_Horz (R : in out Rectangular; H : Long_Float);
   function Get_Horz (R : Rectangular) return Long_Float;

 private
   type Rectangular is
        record
             Horz, Vert: Long_Float;
        end record;
end Rectangular_Method_1;

and his body Rectangular_Method_1.adb:

with Ada.Numerics.Long_Elementary_Functions;
use  Ada.Numerics.Long_Elementary_Functions;

package body Rectangular_Method_1 is
procedure Vector_Basis_r (A : in Long_Float; D : out Rectangular) is
begin
  D.Horz := Cos (A, Cycle => 360.0);
  D.Vert := Sin (A, Cycle => 360.0);
end Vector_Basis_r;

procedure Set_Horz (R : in out Rectangular; H : Long_Float) is
begin
  R.Horz := H;
end Set_Horz;

function Get_Horz (R : Rectangular) return Long_Float is
begin
  return R.Horz;
end Get_Horz;

end Rectangular_Method_1;

and finally a test script test_rectangular_form.adb::

with Ada.Long_Float_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
with Rectangular_Form;
use type Rectangular_Form.Rectangular;

procedure Test_Rectangular_Form is
Component_Horz, Component_Vert, Theta  : Long_Float;
Basis_r                                : Rectangular_Form.Rectangular;

begin
  Ada.Text_IO.Put("Enter the angle ");
  Ada.Long_Float_Text_IO.Get (Item => theta);

  --Vector basis
  Rectangular_Form.Vector_Basis_r (A => Theta, D => Basis_r);
  Ada.Text_IO.New_Line;

  Ada.Text_IO.Put("rhat_min_theta = ");
  Ada.Long_Float_Text_IO.Put (Item => Rectangular_Form.Get_Horz (Basis_r), Fore => 3, Aft  => 5, Exp  => 0);
  Ada.Text_IO.Put(" ihat + ");
  Ada.Long_Float_Text_IO.Put (Item => Rectangular_Form.Get_Vert (Basis_r), Fore => 3, Aft  => 5, Exp  => 0);
  Ada.Text_IO.Put (" jhat ");
end Test_Rectangular_Form;

Question (applies to test_rectangular_form.adb):

I get components A.Horzand A.Vertdirectly using

    Rectangular_Form.Vector_Basis_r (A => Theta, D => Basis_r);

and then access the horizontal and vertical components just by using

    Rectangular_Form.Get_Horz (Basis_r)

and

    Rectangular_Form.Get_Vert (Basis_r)

, Get_Horz Get_Vert . Theta ,

    Rectangular_Form.Vector_Basis_r (A => Theta, D => Basis_r);

A.Horz A.Vert , , set_Horz(A) set_Vert(A) before

    Rectangular_Form.Vector_Basis_r (A => Theta, D => Basis_r);

PS: Set_Vert Get_Vert , .

...

+2
1

set_Horz (A) set_Vert (A)

, :

Rectangular_Form.Vector_Basis_r (A = > Theta, D = > Basis_r);

( Vector_Basis_r.)

-

Basis_r Test_Rectangular_Form, "". Horz Vert , ( ).

Rectangular_Form.Vector_Basis_r, . , : , , . , Vector_Basis_r ( Set_Vert Set_Horz).

+4

All Articles