Ada Example : Object

This example shows how Ada language fully integrates object programming. It handles object as python does with "this" object as the first parameter. More info here


with Ada.Text_IO; use Ada.Text_IO;
with vector;
procedure Main is
 
   V : vector.vector2d := vector.get_vect (2.0, 5.0);
 
   computed_range : vector.range_type;
 
begin
 
   Ada.Text_IO.Put_Line ("V.X :" & V.X'Img);
   Ada.Text_IO.Put_Line ("V.Y :" & V.Y'Img);
   computed_range := V.get_range;
   Ada.Text_IO.Put_Line ("computed range  :" & computed_range'Img);
 
end Main;
 
package vector is
 
   type vector2d is tagged record
      X : Float;
      Y : Float;
   end record;
 
   type range_type is digits 6 range 0.0 .. Float'Last;
 
 
   procedure set_X (V : in out vector2d; X : in Float);
   procedure set_Y (V : in out vector2d; Y : in Float);
   function get_vect (X : in Float; Y : in Float) return vector2d;
   function get_range (V : in vector2d) return range_type;
 
end vector;
 
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Generic_Elementary_Functions;
package body vector is
 
   package Math is new Ada.Numerics.Generic_Elementary_Functions (Float);
 
   function get_range (V : in vector2d) return range_type is
   begin
      return range_type(Math.Sqrt (V.X * V.X + V.Y * V.Y));
   end get_range;
 
   function get_vect (X : in Float; Y : in Float) return vector2d is
      V : vector.vector2d;
   begin
      V.X := X;
      V.Y := Y;
      return V;
   end get_vect;
 
   procedure set_X (V : in out vector2d; X : in Float) is
   begin
      V.X := X;
   end set_X;
   procedure set_Y (V : in out vector2d; Y : in Float) is
   begin
      V.Y := Y;
   end set_Y;
 
end vector;