Compound data type: Difference between revisions

→‎[[Ada]]: Updated to meet the new expanded requirements
No edit summary
(→‎[[Ada]]: Updated to meet the new expanded requirements)
Line 40:
==[[Ada]]==
[[Category:Ada]]
'''Compiler:''' GNAT GPL 2006
===TaggedTask Type1===
Ada tagged types are extensible through inheritance. The reserved word ''tagged'' causes the compiler to create a tag for the type. The tag identifies the position of the type in an inheritance hierarchy.
type Point is tagged record
X : Integer := 0;
Y : Integer := 0;
end record;
===Record=Task Type2====
 
type Unsigned_8 is mod 2**8;
===Record Type===
type Unsigned_16 is mod 2**16;
Ada record types are not extensible through inheritance. Without the reserved word ''tagged'' the record does not belong to an inheritance hierarchy.
type PointUnsigned_32 is recordmod 2**32;
type Unsigned_64 is mod 2**64;
X : Integer := 0;
type Signed_8 is range -128..127;
Y : Integer := 0;
type Signed_16 is range -(2**15)..(2**15 - 1);
type Signed_32 is range -(2**31)..(2**31 - 1);
type Signed_64 is range -(2**63)..(2**63 - 1);
type My_Data is record
M_sch : Signed_8;
M_uch : Unsigned_8;
M_ss : Signed_16;
M_us : Unsigned_16;
M_sl : Signed_32;
M_ul : Unsigned_32;
M_sll : Signed_64;
M_ull : Unsigned_63;
M_si : Integer;
M_ui : Unsigned_32;
WeightM_f : Float;
M_d : Long_Float;
M_ld : Long_Long_Float;
M_Str : String(1..80);
M_Wstr : Wide_String(1..80);
end record;
The Ada ''pragma Pack'' will compress the data, but may re-arrange the fields in memory. The only way to ensure a particular memory layout is to provide a representation clause. The representation clause allows the storage specification down to the bit level.
====Parameterized Types====
Word : constant := 4; -- storage element is byte, four elements per word
An Ada record type can contain a discriminant. The discriminant is used to choose between internal structural representations. Parameterized types were introduced to Ada before tagged types. Inheritance is generally a cleaner solution to multiple representations than is a parameterized type.
typefor PersonMy_Data (Gender : Gender_Type) isuse record
NameM_sch at 0 :* Name_StringWord range 0..7;
AgeM_uch at 0 * :Word Naturalrange 8..15;
M_ss at 0 * Word range 16..31;
Weight : Float;
M_us at 1 * Word range 0..15;
Case Gender is
M_sl at when1 Male* =>Word range 16..47;
M_ul at 2 * Word Beard_Lengthrange : Float16..47;
M_sll at 3 when* FemaleWord =>range 16..79;
M_ull at 5 * Word range null16..79;
M_si at 7 * Word range 16..47;
end case;
M_ui at 8 * Word range 16..47;
M_F at 9 * Word range 16..49;
M_D at 10 * Word range 18..81;
M_Ld at 12 * Word range 18..113; -- 96 bytes minimum
M_Str at 15 * Word range 18..657;
M_Wstr at 35 * Word range 18..1297;
end record;
In this case every person will have the attributes of gender, name, age, and weight. A person with a Male gender will also have a beard length.
 
==[[BASIC]]==
Anonymous user