Lecture #9
Programming Languages MISHRA 95
---Slide 1---
Data Abstraction
Encapsulation and Inheritance
---Slide 2---
Data Abstraction and Encapsulation of
Programmer Defined Data Types
Abstract Data Types
\
---Slide 3---
Stack Example in Ada
package STACK_PACKAGE is procedure PUSH(C: in Character); procedure POP; function TOP return Character; function EMPTY return Boolean; function FULL return Boolean; procedure INIT; end STACK_PACKAGE; package body STACK_PACKAGE is MAX_LEN: constant Integer := 1000; TOP: Integer := -1 ELEMENTS: array (0..MAX_LEN -1) of Character; ...
---Slide 4---
STACK_PACKAGE
Continued
procedure PUSH(C: in Character) is begin if TOP < MAX_LEN then TOP := TOP + 1; ELEMENTS(TOP) := C; end if; end PUSH; procedure POP is begin if TOP > -1 then TOP := TOP - 1; end if; end POP; function TOP return Character is begin return ELEMENTS(TOP); end; function EMPTY return Boolean is begin return TOP = -1; end; function FULL return Boolean is begin return TOP = MAX_LEN - 1; end; procedure INIT is begin null; end; end STACK_PACKAGE;
---Slide 5---
STACK
in C++
#define MAX_LEN 1000 struct STACK { char ELEMENTS[MAX_LEN]; int TOP = -1; enum {EMPTY = -1; FULL = MAX_LEN -1}; void PUSH(char C){TOP++; ELEMENTS[TOP] = C;} void POP(){TOP--;} char TOP(){return (ELEMENT[TOP]);} boolean EMPTY(){return (boolean)(TOP == EMPTY);} boolean FULL(){return (boolean)(TOP == FULL);} void INIT(){} };
---Slide 6---
Abstraction Facilities
Set of Modules:
Each module performs a limited set of operations on a limited amount of data.
---Slide 7---
Modules & Classes
---Slide 8---
Example in C++
const int max_len = 255; class string { char *s; int len; \\ PRIVATE public: \\ PUBLIC form here string(){s = new char[max_len]; len = max_len - 1;} string(char *p){len = strlen(p); s = new char[len+1]; strcpy(s,p);} \\ CONSTRUCTORS ~string() {delete s;} \\ DESTRUCTOR void operator = (string&); void operator += (string&); string operator + (string&); void assign(char* st){strcpy(s,st); len=strlen(st);} void print(){cout << s << "\nLength" << len << "\n";} };
---Slide 9---
string
continued
void string::operator = (string& a) {strcpy(s,a.s); len = a.len;} void string::operator += (string& a) {strcpy(s+len,a.s); len += a.len;} string string::operator + (string& a) { string temp(len+a.len); strcpy(temp.s,s); strcpy(temp.s+len, a.s); return(temp); }
---Slide 10---
string
Usage
main() { string one("Bud_Why_Err"), two("Why ask Why"), both, four; both = one; both.print(); both += two; both.print(); both = both + both; both.print(); four.assign("This Bud's for you"); both = four + two; both.print(); }
---Slide 11---
Implementation Issues
Indirect Encapsulation
---Slide 12---
Implementation Issues
Direct Encapsulation
---Slide 13---
Direct vs. Indirect Encapsulation in Ada
package A is type MyStack is private; procedure NewStack(S: out MyStack); ... private type MyStackRep; -- Hidden type MyStack is access MyStack Rep; end;
package A is type MyStack is private; procedure NewStack(S: out MyStack); ... private type MyStack is record -- also hidden TOP: integer; A: array (1..100) of integer; end record; end;
---Slide 14---
Generic
allows certain attribute of the type to be specified separately so that one base type definition may be given, with the attributes as parameters, and then several specialized types derived from the same base type may be created.
---Slide 15---
Use of generic
in Ada
package QUEUE_PACKAGE is procedure APPEND(C: in Character); procedure REMOVE(C: out Character); function IS_EMPTY return Boolean; function IS_FULL return Boolean; procedure INIT_QUEUE; procedure DESTROY_QUEUE; end;
---Slide 16---
Generic QUEUE_PACKAGE
generic type Elem is private; MAXELTS: in Natural; package QUEUE_PACKAGE is type Queue is limited private; procedure APPEND(Q: in out Queue; C: in Elem); procedure REMOVE(Q: in out Queue; C: out Elem); function IS_EMPTY(Q: in Queue) return Boolean; function IS_FULL(Q: in Queue) return Boolean; procedure INIT_QUEUE(Q: in out Queue); procedure DESTROY_QUEUE(Q: in out Queue); FULL_QUEUE, EMPTY_QUEUE: exceptions; private type Queue is record FIRSTELT, LASTELT: Integer range 0..MAXELTS - 1 := 0; CURSIZE : Integer range 0..MAXELTS := 0; ELEMENTS: array (0..MAXELTS - 1) of Elem; end record; end QUEUE_PACKAGE;
---Slide 17---
Generic Formal Parameters
generic MAX_LEN : in Natural; --Local Constants
generic type Item is private; type Sort_Array is array (positive range <>) of Item; with function "<"(u,v: Item) return Boolean is <>; function HEAP_SORT(A: Sort_Array) return Sort_Array is ... end HEAP_SORT;
---Slide 18---
Generic Subprogram Parameters
must be provided.
---Slide 19---
Inheritance
Information passed implicitly among program components
---Slide 20---
Single & Multiple Inheritance
---Slide 21---
Example: C++
class elem { public: elem(){v = 0} void ToElem(int b){v = b;} int FromElem(){ return v;} private: int v;} class ElemStack: elem { public: ElemStack() { size=0;} void push(elem i) {storage[++size] = i;} void pop() {return storage[size--];} void MyType() {printf("I am type ElemStack\n")} protected: int size; elem storage[100];}
---Slide 22---
Derived Class and Method Inheritance
class NewStack: ElemStack { public: int peek(){ return storage[size].FromElem()} } . . . { elem x; ElemStack y; int i; read(i); x.ToElem(i); // Coercion to elem type y.push(x); ... }
---Slide 23---
Mixin Inheritance
deltaclass StackMod{ int peek(){return storage[size].FromElem();} }// Does not exist in C++ class NewStack = class ElemStack + deltaclass StackMod;
---Slide 24---
Abstraction Concepts
Specialization & Decomposition
\
NewStack
is a specialization of
ElemStack
---Slide 25---
Abstraction Concepts
Instantiation & Individualization
---Slide 26---
Polymorphism
Polymorphism means the ability of a single operator or a subprogram name to refer to any number of functions, depending on the data types of the arguments and results.
+
in 1 + 2 and 1.1 + 2.1 is
overloaded, signifying polymorphism.
---Slide 27---
Polymorphism in ML
ML and Smalltalk allow for the most general form of polymorphism
fun ident(x) = x; ----> val ident = fn: 'a->'a ident(3) ----> val it=3 : int ident("abc") ----> val it="abc" : string ident([1,2,3]) ----> val it=[1,2,3] : int list
---Last Slide---
Functional Polymorphism
fun length(nil)= 0 | length(a::y)= 1+length(y); fun printit(nil)= print("\n") | printit(a::y)= (print(a); printit(y)); fun process(f, l)= f l; process(printit, [1,2,3]); ----> 123 process(length, [1,2,3]); ----> 3
[End of Lecture #9]