Professional Documents
Culture Documents
Modula-2 ADTs
To define a module:
definition module: the interface containing a partial or
complete type definition (data structure) and the subprogram
headers and parameters
implementation module: the portion of the data structure that
is to be hidden, along with all operation subprograms
ADTs in Ada
The encapsulation construct is the package
Packages consist of two parts:
specification package (the public or interface part)
body package (the hidden or private part)
Example Part I
package Stack_Pack is
The specification package
type Stack_Type is limited private;
for a stack ADT see the next
slide for the body package
Max_Size : constant := 100;
function Empty(Stk : in Stack_Type) return Boolean;
procedure Push(Stk : in out Stack_Type; Element : in Integer);
procedure Pop(Stk : in out Stack_Type);
function Top(Stk : in Stack_Type) return Integer;
The actual ADT
private
definition must either
type List_Type is array (1..Max_Size) of Integer;
appear in the open
type Stack_Type is
section (e.g., the public
record
part) or in the private
List : List_Type;
section
Topsub : Integer range 0..Max_Size := 0;
end record;
end Stack_Pack;
An alternative implementation to this approach is to
define a pointer in the private section of this package and
define the actual Stack_Type ADT in the body package.
This is discussed in more detail in the notes section of this
slide.
Example Part II
with Ada.Text_IO; use Ada.Text_IO;
package body Stack_Pack is
function Empty(Stk : in Stack_Type) return Boolean is
begin
return Stk.Topsub = 0;
end Empty;
procedure Push(Stk : in out Stack_Type; 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 Stack_Type) is
begin end Pop;
function Top(Stk : in Stack_Type) return Integer is
begin end Top;
The rest of the implementation
is omitted
end Stack_Pack;
C++ ADTs
C++ offers two mechanisms for building data structures:
the struct and the class
because the struct does not have a mechanism for information
hiding, it can only offer encapsulation (and encapsulation is
not enforced when using structs, merely available), so for a
true ADT, we must use C++s object
C++ classes contain both visible (public) and hidden (private)
components (as well as protected for inheritance)
C++ instances can be static, heap-dynamic and stack-dynamic
the lifetime of an instance ends when it reaches the end of the scope of
where it was declared
a stack-dynamic object may have heap-dynamic data so that parts of the
object may continue even though the instant is deallocated
C++ Example
Unlike the Ada example, in C++, the
#include <iostream.h>
entire definition is encapsulated in one
class stack {
location
private:
int *stackPtr;
Information hiding is preserved through
int max;
the use of a private part with the interface
int topPtr;
being defined in the public part
public:
Any methods that are to be defined in this
stack( ) {
// constructor
class but not accessible outside of the
stackPtr = new int [100];
class would also be defined in the private
max = 99;
section
topPtr = -1;
}
~stack( ) {delete [ ] stackPtr;} // destructor
void push(int number) {}
// details omitted
void pop( ) {}
int top( ) {}
int empty( ) {}
C# borrows from both C++ and Java but primarily from Java,
where all objects are heap dynamic, modifiers are private,
public, protected, but C# also offers
internal and protected internal modifiers which are used for assemblies
(cross-platform objects), and methods that can serve as both accessors
and mutators (see the example in section 11.4)
Parameterized ADTs
The ability to define an ADT
where the type and/or size is
specified generically so that a
specific version can be
generated later
a stack defined without
specifying the element type
(integer vs. string vs. float, etc)
a stack defined without a
restriction on the size of the stack
Ada, C++, Java and C# all have
this capability
In ADA:
generic
Max_Size : positive;
type Element_Type is private;
rest of ADT as before except that
Element_Type replaces Integer
and Max_Size as a constant is
removed
now we instantiate our ADT:
package Integer_Stack is new
Generic_Stack(100, Integer);
Encapsulation Constructs
For large programs, to avoid having to recompile all
code when one section changes
code can be grouped into logically related chunks called
encapsulations
using nested subprograms we can place logically related
subprograms inside of the subprograms that commonly call them
approach not available in C-languages