Abstract Data Type: A Definition Of A Data Type Which Defines The Behavior Of The Data Type And Its Associated Operations
Data Type Representation: A Way To Represent The Elements Of A Data Type
Data Type Implementation: A Way To Implement The Operations Of A Data Type Which Is Consistent With Its Abstract Definition
1. Domain 2. Operations (Addition, Subtraction Etc.) 3. Axioms (Peano Axioms)
Literal Representations
Roman Or Arabic Numerals
Internal Machine Representations
Natural, BCD Or ASCII
Determined By The Arithmetic Logic Unit Of The Central Processing Unit Of The Computer
Encapsulation: The Physical Packaging Of A Type Definition And Operations For An Abstract Data Type
Information Hiding: Limiting Visibility Of Type And Variable Declarations To Prevent Incorrect Usage
Loosely Coupled Systems: Systems Subdivided Into Components With Minimal Interdependence
Reusable Software: General Software That Can Be Used By Many Programs
Ada Packages Are An Encapsulation Device, Type Definitions And Their Operations Can Be Encapsulated In A Package
Function Names Can Be Operators, Operations Can Be Implemented In A Natural Way
The Private Part Of The Package Specification And The Package Body Are Not Visible, Their Contents Are Hidden
Loose Coupling Can Be Achieved By Limiting The Contents Of The Visible Part Of The Package Specification, Excluding Variable Declarations In Particular
Generic Units Enable General Reusable Software To Be Written
package_specification ::=
PACKAGE identifier IS
{basic_declarative_item}
[PRIVATE
{basic_declarative_item}]
END [package_simple_name];
package_body ::=
PACKAGE BODY package_simple_name IS [declarative_part] [BEGIN sequence_of_statements] END [package_simple_name];
The Abstract Data Type Is Defined Here
1. A Private Type Definition Names The Type
2. Subprogram Definitions Define The Operations
3. Comments Can Define The Axioms
The Representation Is Defined By A Type Definition
The Implementation Is Defined By The Subprogram Bodies
private_type_declaration ::=
TYPE identifier IS [LIMITED] PRIVATE;
Assignment
Assignment Of Private Types Is Permitted Provided That The Types Match
Relational Operators = /=
The Equal Operator Is Defined As Identical, The Not Equal Operator Is Defined As Not Identical
1. The Remaining Relational Operators Are Not Predefined For Private Types And No Arithmetic Or Logical Operators Are Predefined
2. Limited Private Types Have No Predefined Operations Outside Their Package, Not Even Assignment Or Testing For Equality
3. Types Created From Dynamically Allocated Memory, Can Not Use Predefined Assignment, They Require A Copy Procedure
Ada Is Designed For Embedded Systems, Graceful Recovery From Anomalous Conditions Is Important
Code To Handle Exceptional Conditions Should Not Obscure The Main Code
Declaring An Exception: Naming A User Defined Exception
Raising An Exception: The Determination That An Anomalous Condition Has Occurred
Handling An Exception: The Action Taken In Response To An Exception Being Raised
exception_declaration ::=
identifier_list : EXCEPTION;
raise_statement ::=
RAISE [exception_name];
1. Exception Declarations Appear In The Visible Part Of The Package Specification
2. Exceptions Are Raised In The Package Body
3. Exceptions Are Handled In The User's Program
PACKAGE Rational_Package IS
TYPE Rationals IS PRIVATE;
FUNCTION "+"(X,Y: Rationals)
RETURN Rationals;
FUNCTION "-"(X,Y: Rationals)
RETURN Rationals;
FUNCTION "*"(X,Y: Rationals)
RETURN Rationals;
FUNCTION "/"(X,Y: Rationals)
RETURN Rationals;
--Constructor Function
FUNCTION "/"(X: Integer; Y: Positive)
RETURN Rationals;
FUNCTION "="(X,Y: Rationals)
RETURN Boolean;
Division_By_Zero: EXCEPTION;
PRIVATE
TYPE Rationals IS
RECORD
Numerator: Integer;
Denominator: Positive;
END RECORD;
END Rational_Package;
PACKAGE BODY Rational_Package IS
FUNCTION "/"(X,Y: Rationals)
RETURN Rationals IS
Numerator, Denominator: Integer;
BEGIN
Numerator := X.Numerator *
Y.Denominator;
Denominator := X.Denominator *
Y.Numerator;
IF Denominator > 0 THEN
RETURN (Numerator,Denominator);
ELSIF Denominator < 0 THEN
RETURN (-Numerator,-Denominator);
ELSE
RAISE Division_By_Zero;
END IF;
END "/";
FUNCTION "="(X,Y: Rationals)
RETURN Boolean IS
BEGIN
RETURN X.Numerator * Y.Denominator =
X.Denominator * Y.Numerator;
END "=";
END Rational_Package;
Global Variable: A Variable Declared In The Package Body Outside Of Any Subprogram
Lifetime: Time Period During Program Execution During Which A Variable Exists
1. The Names In Any Package Specification Are Visible To The Corresponding Package Body
2. The Names In A Package Specification Are Visible By Selection To Any Unit Which Names It In Its With Clause
3. A Use Clause Makes The Names In A Package Specification Directly Visible In The Scope Of The Use
1. The Names Inside The Package Body Are Hidden
2. The Scope Of Global Variables Is From Their Declaration To The End Of The Package Body
1. Global Variables In Separately Compiled Packages Have A Program Lifetime
2. Global Variables In Nested Packages Have The Same Lifetime As The Package, Nesting Is Not Recommended
Child Package: A Package Whose Name Is Its Parent's Name Followed By A Dot Followed By Another Name
1. A Child Package Can See The Private Part Of The Package Specification Of Its Parent
PACKAGE Rational_Package.IO IS PROCEDURE Get(Rational: OUT Rationals); PROCEDURE Put(Rational: IN Rationals); END Rational_Package.IO;
WITH Text_IO; USE Text_IO;
PACKAGE BODY Rational_Package.IO IS
PROCEDURE Put(Rational: IN Rationals) IS
BEGIN
Put(Rational.Numerator'Image);
Put(" / ");
Put(Rational.Denominator'Image);
END Rational_Package.IO;
PACKAGE Running_Average IS PROCEDURE Record_Event; PROCEDURE Next_Period; FUNCTION Average RETURN Float; END Running_Average;
Running Average Package
1. Type And Variable Declarations Which Define The Object Are Encapsulated In The Package Body
2. The Object Is Not A Parameter In Any Of The Subprogram Specifications
Rational Number Package
1. Type Declarations Are In The Package Specification
2. Variable Declarations Are In The Unit Which Uses The Package
3. Subprogram Specifications Contain Parameters Which Belong To The Class Of Defined Objects
PACKAGE BODY Running_Average IS
Total: CONSTANT := 20;
SUBTYPE Table_Range IS Integer RANGE
0..Total - 1;
Table: ARRAY(Table_Range) OF Integer;
Current: Table_Range;
PROCEDURE Record_Event IS
BEGIN
Table(Current) := Table(Current) + 1;
END Record_Event;
PROCEDURE Next_Period IS
BEGIN
Current := (Current + 1) MOD Total;
Table(Current) := 0;
END Next_Period;
FUNCTION Average RETURN Float IS
Total: Integer := 0;
Period: Integer := Current;
BEGIN
FOR Count IN 1..5 LOOP
Period := (Period - 1) MOD Total;
Total := Total + Table(Period);
END LOOP;
RETURN Float(Total)/ 5.0;
END Average;
END Running_Average;
body_stub ::=
subprogram_specification IS SEPARATE; | PACKAGE BODY package_simple_name IS SEPARATE;
subunit ::=
SEPARATE (parent_unit_name) proper_body
1. Scope Rule:
A Subprogram Which Is A Subunit Can See Any Identifiers Which It Would Have Been Able To See If It Were Physically Nested In The Parent Unit
PROCEDURE Main IS PROCEDURE Subprogram IS SEPARATE; BEGIN ... END Main; SEPARATE (Main) PROCEDURE Subprogram IS ... END Subprogram;
/ Top of Page /