Previous topic | Ada Home Page | Index

Example using access types

Package defining a record

-- Culwin p. 468

package pers_pack is

    min_weight   : constant := 0.0;
    max_weight   : constant := 250.0;
    min_height   : constant := 0;
    max_height   : constant := 300;
    max_name_len : constant := 15;

    space        : constant character := ' ';

    subtype weights is FLOAT   range min_weight..max_weight;
    subtype heights is INTEGER range min_height..max_height;
    subtype name_strings is string (1..max_name_len);

    type person_records is record
        f_name : name_strings := (others => space);
        s_name : name_strings := (others => space);
        weight : weights      := 0.0;
        height : heights      := 0;
    end record;

    procedure get_person (a_person  : out person_records);

    procedure show_person (a_person : in  person_records);

    function name_less_than
      (this_pers,that_pers : person_records) return boolean;

    function name_equal
      (this_pers,that_pers : person_records) return boolean;

end pers_pack;


Example program - without access types

with pers_pack; use pers_pack;
procedure main is

    person_1 : person_records;
    person_2 : person_records;

    procedure swap_people
        (a_person, another_person : in out person_records) 
    is
        temp : person_records;
    begin
        temp           := a_person;
        a_person       := another_person;
        another_person := temp;
    end;

begin
    -- input two records from the user
    get_person (person_1);
    get_person (person_2);

    -- swap if necessary
    if name_less_than (person_1, person_2) then
        swap_people (person_1, person_2);
    end_if;

    -- print out the sorted records
    show_person (person_1);
    show_person (person_2);

end main;


Compare - same program, using access types

-- Culwin p. 469

with pers_pack; use pers_pack;
procedure main is

    type person_ptr is access pers_pack.person_records;

    person_1 : person_ptr := new pers_pack.person_records;
    person_2 : person_ptr := new pers_pack.person_records;

    procedure swap_people
        (a_person, another_person : in out person_ptr) 
    is
        temp : person_ptr;
    begin
        temp           := a_person;
        a_person       := another_person;
        another_person := temp;
    end;

begin
    -- input two records from the user
    get_person (person_1.all);
    get_person (person_2.all);

    -- swap if necessary
    if name_less_than (person_1.all, person_2.all) then
        swap_people (person_1, person_2);
    end_if;

    -- print out the sorted records
    show_person (person_1.all);
    show_person (person_2.all);

end main;


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 27 Feb 96