Presentation is loading. Please wait.

Presentation is loading. Please wait.

package STACK is procedure PUSH (x : INTEGER);

Similar presentations


Presentation on theme: "package STACK is procedure PUSH (x : INTEGER);"— Presentation transcript:

1 package STACK is procedure PUSH (x : INTEGER); function POP return INTEGER; end STACK;

2 package body STACK is MAX: constant := 100; S : array (1..MAX) of INTEGER; TOP : INTEGER range 0..MAX; procedure PUSH (x : INTEGER) is begin TOP := TOP + 1; S(TOP) := x; end PUSH; function POP return integer is TOP := TOP - 1; return S(TOP + 1); end POP; TOP := 0; end STACK;

3 with STACK; use STACK; procedure myStackExample is I, O : integer; begin push(i); O := pop; end myStackExample;

4 Procedure not a function?
package STACKS is type STACK is private; procedure PUSH (s : in out STACK; i : in integer); procedure POP (s : in out STACK; o : out integer); private MAX: constant := 100; type STACK is record ST : array (1..MAX) of INTEGER; TOP : INTEGER range 0..MAX := 0; end record; end STACKS; Procedure not a function?

5 package body STACKS is procedure PUSH (S: in out STACK; i : in INTEGER) is begin S.TOP := S.TOP + 1; S.ST(S.TOP) := i; end PUSH; procedure POP (S: in out STACK; o : out INTEGER) is o := S.ST(S.TOP); S.TOP := S.TOP - 1; end POP; end STACK;

6 declare use STACKS; myStack : STACK; begin push(myStack, inValue); pop(myStack, outValue); end;

7 package STACKS is overFlow, underFlow : exception; type STACK is private; procedure PUSH (s : in out STACK; i : in integer); procedure POP (s : in out STACK; o : out integer); private MAX: constant := 100; type STACK is record ST : array (1..MAX) of INTEGER; TOP : INTEGER range 0..MAX := 0; end record; end STACKS;

8 package body STACKS is procedure PUSH (S: in out STACK; i : in INTEGER) is begin if S.TOP = MAX then raise overFlow; end if; S.TOP := S.TOP + 1; S.ST(S.TOP) := i; end PUSH; procedure POP (S: in out STACK; o : out INTEGER) is if S.TOP = 0 then raise underFlow; o := S.ST(S.TOP); S.TOP := S.TOP - 1; end POP; end STACK;

9 declare use STACKS; myStack : STACK; begin push(myStack, inValue); pop(myStack, outValue); exception when overFlow => cleanUpOverFlow; when underFlow => cleanUpUnderFlow; end;

10 Object Orientation – The Ada Way
Ada provides tools and constructs for extending types through inheritance In many object oriented languages the concept of a class is overloaded It is both the unit of encapsulation and a type definition Ada separates these two concepts Packages are used for encapsulation Tagged types are used to define extensible types

11 Tagged Type type Person is tagged record Name : String(1..20);
Age : Natural; end record; Such a type definition can be performed in a package Immediately following the type definition must be the procedures and functions comprising the primitive operations defined for this type  Primitive operations must have one of its parameters be of the tagged type, or for functions the return value can be an instance of the tagged type This allows you to overload functions based upon their return type Note that C++ and Java do not allow you to overload, based upon the return type of a function

12 Extending Tagged Types
The tagged type is extended by making a new tagged record based upon the original type type Employee is new Person with record Employee_Id : Positive; Salary : Float; end record; Type Employee inherits all the fields and primitive operations of type Person

13 Generics Allow parameterization of subprograms and packages with parameters which can be types and subprograms as well as values and objects

14 Generics generic MAX : positive; type item is private;
package STACK is procedure PUSH (x : item); function POP return item; end STACK;

15 package body STACK is S : array (1..MAX) of item; TOP : INTEGER range 0..MAX; procedure PUSH (x : item) is begin end PUSH; function POP return item is end POP; TOP := 0; end STACK;

16 declare package myStack is new STACK(100, integer); use myStack; begin push (i); O := pop; end;

17 Concurrency The support for concurrency in Ada is provided by Tasking
It initiates several parallel activities which cooperate as needed Syntax is similar to packages

18 Concurrency task thisTask is …-- interfaces end thisTask;
task body thisTask is end thisTask; task simpleTask; -- this task does not provide an -- interface to other tasks

19 Concurrency procedure COOKING is begin cookMeat; cookRice;
cookPudding; end COOKING;

20 Concurrency procedure COOKING is task cookMeat; task body cookMeat is
begin -- follow instructions for cooking meat end cookMeat; task cookRice; task body cookRice is -- follow instructions for cooking rice end cookRice; cookPudding; end COOKING;

21 Ada: The Software Engineering Approach
In general, software development time is: 10% design, 10% coding, 60% debug and 20% test Last 80% of the project is spent trying to find and eliminate mistakes made in the first 20% of the project

22 Ada: The Software Engineering Approach
Ada Promise: 20% design, 60% coding, 10% debug and 10% test Cutting the entire cycle time in half! Ada produces these benefits because it is designed to support software engineering as a human activity!

23 When Roman engineers built a bridge, they had to stand under it while the first legion marched across. If programmers today worked under similar ground rules, they might find themselves getting much more interested in Ada. - Robert Dewar


Download ppt "package STACK is procedure PUSH (x : INTEGER);"

Similar presentations


Ads by Google