IA010: Principles of Programming Languages · This notion is used (in the context of programming...

34
IA010: Principles of Programming Languages 7. Abstract Data Types Jan Obdržálek obdrzalek@fi.muni.cz Faculty of Informatics, Masaryk University, Brno IA010 7. Abstract Data Types 1

Transcript of IA010: Principles of Programming Languages · This notion is used (in the context of programming...

Page 1: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

IA010: Principles of ProgrammingLanguages

7. Abstract Data Types

Jan Obdržálek [email protected]

Faculty of Informatics, Masaryk University, Brno

IA010 7. Abstract Data Types 1

Page 2: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Abstraction

A view or representation of an entity which retains only themost significant attributes.

IA010 7. Abstract Data Types 2

Page 3: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Abstraction

A view or representation of an entity which retains only themost significant attributes.

• bird vs. carrion crow vs. chiffchaff• which attributed have birds in common?• common attributes of birds can be abstracted away

(in the descriptions of crow/chiffchaff)• different levels of abstraction

(c.Aves, o.Passeriformes, f.Corvidae, g.Corvus)• why use abstraction in programming?

• increases readability• hides (at the given time) unimportant details⇒ decreases complexity

• two main kinds:• process abstraction (subroutines)• data abstraction (ADTs)

IA010 7. Abstract Data Types 3

Page 4: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Outline

Introduction to ADTs

ADTs in Ada

ADTs in C++

Parameterized ADTs

Encapsulation topics

IA010 7. Abstract Data Types 4

Page 5: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Introduction to ADTs

IA010 7. Abstract Data Types 5

Page 6: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Abstract data types (ADTs)Definition

An abstract data type (ADT) is a mathematical model of a datastructure which is defined by its operations.

vs

ADT is a data type with the following properties:• The representation of objects of the type is hidden from

program units using the type. hiding• Declarations of the type and the protocols of the operations

on objects of the type (type’s interface) are contained in asingle syntactic unit. The interface is independent of theimplementation, and all other units (the clients) can createvariables of this type. encapsulation

IA010 7. Abstract Data Types 6

Page 7: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

ADT example: a stack

operations (the interface)

• create(stack)

• destroy(stack)

• empty(stack)

• push(stack, element)

• pop(stack)

• top(stack)

IA010 7. Abstract Data Types 7

Page 8: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Origins of data abstraction

Built-in types as ADTs?• All built-in types are, in a way, abstract data types.• Example: float

• the inner representation (significand + exponent) is hiddenfrom the user

• the only operations available are those provided by thelanguage (the user cannot create new operations)

• portability: the program will work for any innerrepresentation (IEEE 754 is from 1985)

First ADTs• records in COBOL (data structure—in the form of a

record—plus subroutines manipulating it)

Object-oriented programming (OOP)• ADT != OOP (Even though our examples will be OO.)• OOP is the logical next step beyond data abstraction• data abstraction is one of the main ingredients of OOP

IA010 7. Abstract Data Types 8

Page 9: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Encapsulation

This notion is used (in the context of programming languages)in two meanings (or their combinations):

1 a mechanism for restricting access to some fields of anobject⇒ information hiding

2 a language construction for associating data withoperations over these data

IA010 7. Abstract Data Types 9

Page 10: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Benefits of information hiding

• increases reliability• clients cannot directly manipulate the inner representation⇒ helps to guarantee the integrity of objects

• reduces the amount of code the programmer must beaware of

• the code is easier to understand• makes errors easier to find

• decreases the potential for name conflicts• the inner representation can be changed without affecting

the rest of the program(as long as the interface is preserved)

Note: It is necessary to have a mechanism which makes visiblefrom the outside only some of parts (typically operationdeclarations) of a unit.IA010 7. Abstract Data Types 10

Page 11: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Access to object’s data

• sometimes “direct” access to data members is needed• possible solutions

• make data visible to the outside• using accessor methods (setters/getters)

• advantages of using accessor methods• read-only access can be provided (just a getter)• constraints can be enforced in setters• possible to change inner implementation any time

Both solutions violate the ADT principles!

IA010 7. Abstract Data Types 11

Page 12: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Further topics

Generic operations(in addition to the operation defining the ADT)

• all types• assignment (usually built into the language)• test for (in)equality

• some types• constructors and destructors• iterators• accessors

ADT design issues• type and interface representation• can ADTs be parameterized?• access control mechanism?• is the specification of a type separate from its

implementation?IA010 7. Abstract Data Types 12

Page 13: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

ADTs in Ada

IA010 7. Abstract Data Types 13

Page 14: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

ADTs in Ada

• the first commercial language to offer a full support forADTs (together with Modula-2)

• ADTs are implemented using the encapsulation construct(provided by packages)

packages• two parts, sharing the same name

1 package specification (the interface)2 package body (the implementation)

• not all packages have the body part(if encapsulating only types and constants)

• both parts can be compiled separately(provided the specification is compiled first)

• for compiling client code, only the specification is needed

IA010 7. Abstract Data Types 14

Page 15: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Information hiding in Ada

• by separating the specification into two parts:• the private part

• starts by the private keyword• always at the end• visible to the compiler, but not to client units

• the public part

• type representation must be given in the packagespecification(so the compiler can compute the size of type’s objects)

• two variants of private types:• private types

• assignment and (in)equality built-in• limited private types

• declared as limited private in the public part• no built-in operations (e.g. for stacks)

IA010 7. Abstract Data Types 15

Page 16: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Ada stack specification

package Stack_Pack is-- The visible entities, or public interfacetype Stack_Type is limited private;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 part that is hidden from clientsprivatetype List_Type is array (1..Max_Size) of Integer;type Stack_Type isrecordList : List_Type;Topsub : Integer range 0..Max_Size := 0;end record;

end Stack_Pack;

IA010 7. Abstract Data Types 16

Page 17: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Ada stack implementation

with Ada.Text_IO; use Ada.Text_IO;package body Stack_Pack is

function Empty(Stk: in Stack_Type) return Boolean isbeginreturn Stk.Topsub = 0;end Empty;

procedure Push(Stk : in out Stack_Type; Element : in Integer) isbeginif Stk.Topsub >= Max_Size thenPut_Line("ERROR - Stack overflow");

elseStk.Topsub := Stk.Topsub + 1;Stk.List(Topsub) := Element;

end if;end Push;

...

IA010 7. Abstract Data Types 17

Page 18: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

ADTs in C++

IA010 7. Abstract Data Types 18

Page 19: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

ADTs in C++

• C++ was created by adding features to C• the first (and the most important one) was OOP• basic construct providing ADT functionality is the class• ADTs are also provided by the struct• classes are types• terminology:

• data: data members• operations: member functions• class members vs instance members

• access to class members only through some object (aninstance) of this class

• all instances share member functions, but not data

IA010 7. Abstract Data Types 19

Page 20: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Information hiding in C++

• Method definitions and declarations• definitions in the definition file of the class (.cpp)• declarations in the header file (.h)• if both declaration and definition of a method are in the

header file, then the method is implicitly inline

• Information hiding – two clauses• private – hidden entities• public – public entities (the interface)• (protected – third possibility, OOP related)

IA010 7. Abstract Data Types 20

Page 21: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Constructors and destructors

• Constructor• initializes data members of newly created objects• can allocate space on the heap• called implicitly when a new object of the class is created• same name as the class• does not return a value• can be overloaded

• Destructor• implicitly called at the end of object’s lifetime• typically used for deallocating space on heap• name: ‘~’ + class name• useful for debugging (printing debug info)• does not return a value

IA010 7. Abstract Data Types 21

Page 22: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

C++ stack specification

// Stack.h - the header file for the Stack class#include <iostream.h>class Stack {private: //** These members are visible only to other

//** members and friendsint *stackPtr;int maxLen;int topSub;

public: //** These members are visible to clientsStack(); //** A constructor~Stack(); //** A destructorvoid push(int);void pop();int top();int empty();

}

IA010 7. Abstract Data Types 22

Page 23: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

C++ stack implementation

// Stack.cpp - the implementation file for the Stack class#include <iostream.h>#include "Stack.h"using std::cout;Stack::Stack() { //** A constructorstackPtr = new int [100];maxLen = 99;topSub = -1;

}

Stack::~Stack() {delete [] stackPtr;}; //** A destructor

int Stack::empty() {return (topSub == -1);}

void Stack::push(int number) {if (topSub == maxLen)cerr << "Error in push--stack is full\n";else stackPtr[++topSub] = number;

}...IA010 7. Abstract Data Types 23

Page 24: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

ADTs in Java

• similar to C++• differences:

• all objects allocated on the heap• each ADT defined and declared in a single file• each method is declared public/private separately

(third possibility - package access )• no destructors

IA010 7. Abstract Data Types 24

Page 25: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Java stack example

class StackClass {private int [] stackRef;private int maxLen, topIndex;

public StackClass() { // A constructorstackRef = new int [100];maxLen = 99;topIndex = -1;

}

public boolean empty() {return (topIndex == -1);}

public void push(int number) {if (topIndex == maxLen)System.out.println("Error in push - stack is full");

else stackRef[++topIndex] = number;}

...

IA010 7. Abstract Data Types 25

Page 26: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Parameterized ADTs

IA010 7. Abstract Data Types 26

Page 27: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Parameterized ADTs in Adamodified examplegenericMax_Size : Positive; -- generic stack sizetype Element_Type is private; -- generic element type

package Generic_Stack is-- The visible entities, or public interfacetype Stack_Type is limited private;function Empty(Stk : in Stack_Type) return Boolean;procedure Push(Stk : in out Stack_Type; Element : in Element_Type);procedure Pop(Stk : in out Stack_Type);function Top(Stk : in Stack_Type) return Element_Type;

-- The part that is hidden from clientsprivatetype List_Type is array (1..Max_Size) of Element_Type;

...

new instancepackage Integer_Stack is new Generic_Stack(100, Integer);

IA010 7. Abstract Data Types 27

Page 28: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Parameterized ADTs in Java 5.0

• generic parameters must be classes(see generic methods in Java 5.0)

• a generic class definition:public class MyClass<T> { ... }

Collections (before Java 5.0)• store instances of the Object type• disadvantages

1 need to type cast element after reading from a collection2 no type checking for insertion3 primitive types cannot be stored

(e.g. int needs to be encapsulated in Integer)

Generic collections (Java ≥ 5.0)ArrayList <Integer> myArray = new ArrayList <Integer>();

IA010 7. Abstract Data Types 28

Page 29: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Java stack using generic collections

import java.util.*;public class Stack<T> {private ArrayList<T> stackRef;private int maxLen;

public Stack() { // A constructorstackRef = new ArrayList<T> ();maxLen = 99;

}

public boolean empty() {return (stackRef.isEmpty());}

public void push(T newValue) {if (stackRef.size() == maxLen)System.out.println("Error in push - stack is full");

elsestackRef.add(newValue);

}...

IA010 7. Abstract Data Types 29

Page 30: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Encapsulation topics

IA010 7. Abstract Data Types 30

Page 31: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Encapsulation – multiple types

the problem with large programs• “flat” colection of subroutines and ADTs is difficult to

intellectually handle• time-demanding recompilation

solution• organize the program into logically related collections of

code and data, which can be compiled separately(typical example: libraries)

• variants1 nested subroutines (unsatisfactory)2 ADTs (just one type per file)3 general collections (e.g. Ada packages)

(basically separate compilation in C (make))

IA010 7. Abstract Data Types 31

Page 32: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Encapsulation – names

the problem with names• larger programs are today developed by (independent)

teams of programmers• massive use of third-party code⇒ real possibility of name collisions

solution: naming encapsulations• new name scopes are defined• e.g. each library has its own naming encapsulation• multiples collections of code can be put into the same

namespace (even if they reside in different files)

• languages: C++, Java, Ada, Ruby, . . .

IA010 7. Abstract Data Types 32

Page 33: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Example: namespace in C++

• all required declarations are put into a common namespaceblock

namespace myNameSpace { // declarations go hereint foo()...

}

Access (3 variants)

1 myNameSpace:foostate the package on access

2 using myNameSpace::foo;foo is now visible in the unit

3 using namespace myNameSpace;all names of myNameSpace are now visible in the unit

IA010 7. Abstract Data Types 33

Page 34: IA010: Principles of Programming Languages · This notion is used (in the context of programming languages) in two meanings (or their combinations): 1 a mechanism for restricting

Example: Java packages

• packages can contain multiple types(in this case classes and interfaces)

• if no access policy is specified, then visible for all othertypes in the same package: package scope

• specifying the package for a compilation unit:

package myPackage;

• access to package elements:• import myPackage.foo;• import myPackage.*;

IA010 7. Abstract Data Types 34