ITEC 320 Lecture 16 Packages (1)
Review Questions? –HW –Exam Nested records –Benefits –Downsides
Packages(1) Objectives Packages –OO in Ada
Packages(1) Definitions Client –Uses the class and its methods –Routine or package that uses the types and routines defined in package P Examples –With ada.text_io; use ada.text_io; Which one is Java? Which one is Ada?
Packages(1) Ada “Classes” Records store information Procedures work with records / other data Put the two together and you have the Ada version of classes Contain –Procedures –Types
Packages(1) Java class Pair { int x, y; int distanceToOrigin() // How far to origin { return Math.abs(x) + Math.abs(y); } } // Client for class Pair class PairClient { public static void main(String[] args) { Pair p; p = new Pair(); p.x = 1; p.y = 2; Sop(p.distanceToOrigin()); }
Packages(1) Ada 2 pieces –Record for a pair –Function to calculate the distance to the origin File structure –Specification –Body
Packages(1) Specificatio n Tells the compiler what the package does, but not how to do it Store in a.ads file package PairPkg is type Pair is record x, y: Integer; end record function distanceToOrigin(p: Pair) return Integer; end PairPkg;
Packages(1) Body Contains the code for a certain package’s procedures / functions package body PairPkg is function distanceToOrigin(p: Pair) return Integer is begin return abs p.x + abs p.y; end distanceToOrigin; end PairPkg;
Packages(1) Ada / Java Java classes (1 file) Ada method (2 files) What are the benefits / downsides of each approach? How does Java sort of provide the same idea that Ada does?
Packages(1) Rules The specification must be followed to the letter All functions / procedures must be implemented All return / parameter types must match
Packages(1) Use Without the use statement, you have to fully-qualify the package name with pairpkg; procedure pairclient is p: pairpkg.Pair; begin p.x := 1; p.y := 2; put(pairpkg.distanceToOrigin(p)); end pairclient;
Packages(1) Specificatio n Tells the compiler what the package does, but not how to do it Store in a.ads file package PairPkg is type Pair is record x, y: Integer; end record function distanceToOrigin(p: Pair) return Integer; end PairPkg;
Packages(1) Body Contains the code for a certain package’s procedures / functions package body PairPkg is function distanceToOrigin(p: Pair) return Integer is begin return abs p.x + abs p.y; end distanceToOrigin; end PairPkg;
Packages(1) Compilation Rule of thumb –Keep it all in the same directory –Some way of pointing where to look for specifications / bodies Compiler does the work for you –Automatically pulls in packages thanks to the with statement Take a look at the files produced and think about why they are there…
Packages(1) Privacy Java has private variables Why ? What do they provide? How are they used?
Packages(1) Ada package PairPkg is -- Public section of package type Pair is private; -- Only type name is public function newPair(x, y: Integer) return Pair; function distanceToOrigin(p: Pair) return Integer; -- Private section of package private -- Code below is NOT visible to client type Pair is record -- Type implementation is private x, y: Integer; end record end PairPkg; What can you do with the
Packages(1) Package package body PairPkg is function newPair(x, y: Integer) return Pair is temp: Pair := (x, y); begin return temp; end newPair; function distanceToOrigin(p: Pair) return Integer is begin return p.x + p.y; end distanceToOrigin; end PairPkg; What does this look like?
Packages(1) Client with ada.integer_text_io; use ada.integer_text_io; with pairpkg; use pairpkg; procedure pairclient is p: Pair; begin p := newPair(1, 2); put(distanceToOrigin(p)); p.x := 3; -- Syntax error end pairclient;
Packages(1) Specificatio n Tells the compiler what the package does, but not how to do it Store in a.ads file package PairPkg is type Pair is record x, y: Integer; end record function distanceToOrigin(p: Pair) return Integer; end PairPkg;
Packages(1) Body Contains the code for a certain package’s procedures / functions package body PairPkg is function distanceToOrigin(p: Pair) return Integer is begin return abs p.x + abs p.y; end distanceToOrigin; end PairPkg;
Packages(1) Summary Packages in Ada –Specifications –Body