CSCI 431 Lecture Notes - Abstraction

Abstraction

Process abstraction

Encapsulation

Data abstraction

An Example

Consider implementing an a stack as an ADT

Implementation in Ada


The specification package:

package stack is 
-- The visible entities, or the public interface
   type STACKTYPE is limited private;
   MAX_SIZE : constant := 100;
   function EMPTY (STK : in STACKTYPE) return BOOLEAN;
   procedure PUSH (STK : in out STACKTYPE; ELEMENT : in INTEGER);
   procedure POP(STK : in out STACKTYPE);
   function TOP(STK : in STACKTYPE);
-- The part that is hidden
   private
      type LIST_TYPE is array (1..MAX_SIZE) of INTEGER;
      type STACKTYPE is
         record
	 LIST : LIST_TYPE;
	 TOPSUB : INTEGER range 0..MAX_SIZE := 0;
	 end record;
end STACKTYPE;

The body package:

with TEXT_IO; use TEXT_IO;
package body STACKPACK is
   function EMPTY(STK : in STACKTYPE) return BOOLEAN is
      begin
      return STK.TOPSUB = 0;
      end EMPTY;

   procedure PUSH (STK : in out STACKTYPE; ELEMENT : in INTEGER) is
      begin
      if STK.TOPSUB > = MAX_SIZE then
         PUT_LINE("Error - Stack Overflow");
      else
         STK.TOPSUB := STK.TOPSUB + 1;
	 STK.LIST(TOPSUB) := ELEMENT;
      end if;
      end PUSH;

   procedure POP(STK : in out STACKTYPE) is
      begin
      if STK.TOPSUB = 0
         then PUT_LINE("Error - Stack Underflow"); 
	 else STK.TOPSUB := STK.TOPSUB - 1;
      end if;
      end POP;
 
   function TOP(STK : in STACKTYPE) return INTEGER is
      begin
      if STK.TOPSUB = 0
         then PUT_LINE("Error - Stack is Empty");
	 else return STK.LIST(STK.TOPSUB);
      end if;
      end TOP;
end STACKPACK;

A driver program

with STACKPACK, TEXT_IO; 
use STACKPACK, TEXT_IO;
procedure USE_STACKS is
   TOPONE : INTEGER;
   STACK : STACKTYPE; -- Creates a new STACKTYPE object
   begin
   PUSH(STACK, 42);
   PUSH(STACK, 17);
   TOPONE := TOP(STACK);
   POP(STACK);
   ...
   end USE_STACKS;

Procedural abstraction

Parameters and parameter transmission

Methods for transmitting parameters

Call by value

Call by reference

Call by name

Call by result

Call by value-result

Call by constant value

Question:

What happens when you pass an expression by reference, such as in the call sub(&(a+b), &b); ?

Subprograms as parameters

Free variables in subprograms passed as parameters

Implementation of Issues

Example program


#include < stdio.h>

int avg8(int a, int b, int c, int d,
		 int e, int f, int g, int h)
{
	return((a+b+c+d+e+f+g+h+4)/8) ;
}

int avg3(int a, int b, int c)
{
	return((a+b+c+2)/3) ;
}

void main()
{
	int i, v[8] ;

	for (i=0; i < 8; ++i)
		scanf("%d", &v[i]) ;

	printf("%d %d\n",
		avg8(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]),
		avg3(v[0], v[1], v[2])) ;

}

Calling conventions in Windows 95 on an Intel PC

Calling conventions in Digital Unix on Alpha

Calling conventions in Solaris on Sun SPARC


	ld	[%fp-36],%l0		;; load v[0]
	ld	[%fp-32],%l1		;; load v[1]
	ld	[%fp-28],%l2		;; load v[2]
	ld	[%fp-24],%l3		;; load v[3]
	ld	[%fp-20],%l5		;; load v[4]
	ld	[%fp-16],%l6		;; load v[5]
	ld	[%fp-12],%l7		;; load v[6]
	ld	[%fp-8],%l4		;; load v[7]
	mov	%l0,%o0			;; move local register to out resister
	mov	%l1,%o1			;; move local register to out resister
	mov	%l2,%o2			;; move local register to out resister
	mov	%l3,%o3			;; move local register to out resister
	mov	%l5,%o4			;; move local register to out resister
	mov	%l6,%o5			;; move local register to out resister
	st	%l7,[%sp+92]		;; store on stack
	st	%l4,[%sp+96]		;; store on stack
	call	avg8