1 / 27

Abstract data types & object-oriented paradigm

Abstract data types & object-oriented paradigm. Abstraction. Abstraction: a view of an entity that includes only the attributes of significance in a particular context. Two fundamental abstractions: process abstraction & data abstraction Process abstraction: sortInt(list, list_len)

kiet
Download Presentation

Abstract data types & object-oriented paradigm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Abstract data types & object-oriented paradigm

  2. Abstraction • Abstraction: a view of an entity that includes only the attributes of significance in a particular context. • Two fundamental abstractions: process abstraction & data abstraction • Process abstraction: sortInt(list, list_len) • sorting algorithms is not revealed • user code remains same even implementation of sorting algorithm is changed • used for a long time

  3. Abstract data type • Abstract Data Type: an encapsulation that includes only the data representation and the subprograms that provides operations for that type. • reliability: user cannot directly access objects • readability: without implementation details

  4. Introduction to Data Abstraction • Built-in types are abstract data types e.g. int type in Java • The representation is hidden • Operations are all built-in • User programs can define objects of int type • User-defined abstract data types must have the same characteristics as built-in abstract data types • To declare variables of that type • A set of ops

  5. Abstract data type example code example: … create(stk1); push(stk1, 34); push(stk1, 20); if ( !empty(stk1) ) temp = top(stk1); interfaces: create(stack) destroy(stack) empty(stack) push(stack, item) pop(stack) top(stack) • stack implementation can be changed from array to list w/o affecting the code

  6. Encapsulation • Encapsulation: a grouping of subprograms and the data they manipulate • Hiding the implementation details of the abstract interface • Protect internal data • Reliability, maintenance

  7. Language Examples-C++ • Based on C struct type and Simula 67 classes • The class is the encapsulation device • All of the class instances of a class share a single copy of the member functions • Each instance of a class has its own copy of the class data members • Instances can be static, stack dynamic, or heap dynamic

  8. At first glance void push(int item) { if (topPtr == maxLen) cerr << “Stack full\n”; else stackPtr[++topPtr] = item; } void pop() { if (topPtr == -1) cerr << “Stack empty\n”; else topPtr--; } int top() { return ( stackPtr[topPtr] ); } int empty() { return ( topPtr == -1); } } // end class stack #include <iostream.h> class stack { private: int *stackPtr; int maxLen; int topPtr; public: stack() { stackPtr = new int [100]; maxLen = 99; topPtr = -1; } ~stack() { delete [ ] stackPtr; } Data members Member functions

  9. C++ Class • data members: data defined in a class • *stack_ptr, max_len, top_ptr • member functions: functions defined in a class • also called access interfaces • push( ), pop( ), top( ), empty( ) • private: entities (data or member functions) that are to be hidden from outside the class • public: entities (data or member functions) that are visible outside the class

  10. C++ class (con’t) • constructor: initialize the data members of newly created objects • stack( ) • implicitly called when an object of the class type is created • can have one or more constructor for a class • destructor: implicitly called when the lifetime of an instance of the class ends • ~stack( )

  11. Example in C++ void push(int item) { if (topPtr == maxLen) cerr << “Stack full\n”; else stackPtr[++topPtr] = item; } void pop() { if (topPtr == -1) cerr << “Stack empty\n”; else topPtr--; } int top() { return ( stackPtr[topPtr] ); } int empty() { return ( topPtr == -1); } } // end class stack #include <iostream.h> class stack { private: int *stackPtr; int maxLen; int topPtr; public: stack() { stackPtr = new int [100]; maxLen = 99; topPtr = -1; } ~stack() { delete [ ] stackPtr; }

  12. Class usage in C++ • stack stk constructor is called to initialize the instance • at the end of the main, stk’s destructor is called Code example in C++: void main() { int topOne; stack stk; // stack-dynamic stk.push(42); stk.push(20); topOne = stk.top(); stk.pop(); … // stk being freed }

  13. Language Examples • Constructors: • Functions to initialize the data members of instances • May also allocate storage if part of the object is heap-dynamic • Can include parameters to provide parameterization of the objects • Implicitly called when an instance is created • Can be explicitly called • Name is the same as the class name

  14. Language Examples • Destructors • Functions to cleanup after an instance is destroyed; usually just to reclaim heap storage • Implicitly called when the object’s lifetime ends • Can be explicitly called • Name is the class name, preceded by a tilde (~)

  15. Examples in Java • Java uses implicit garbage collection (no destructor) and reference variable (not pointer) Code example in C++: void main() { int topOne; stack stk; // stack-dynamic stk.push(42); stk.push(20); topOne = stk.top(); stk.pop(); … } Code example in Java: public class TestStack { public static void main(…) { INTEGER topOne; StackClass myStack = new StackClass(); myStack.push(42); myStack.push(20); topOne = myStack.top(); myStack.pop(); … }

  16. Java features • Stack_class does not have destructor • implicit garbage collector • In main( ), myStack does not get freed • implicit garbage collector • The use of reference variables • Java does not support pointer

  17. Example:Stack in Java public void pop( ) { if (top_index == -1) System.out.println( “Error Stack empty”); else –top_index; } public int top( ) { return (stack_ref[top_index]); } public boolean empty( ) { return (top_index == -1); } } // end class stack import java.io.*; class Stack_class { private int [ ] stack_ref; private int max_len, top_index; publicStack_class( ) { stack_ref = new int [100]; max_len = 99; top_index = -1; } public void push (int number) { if (top_index = max_len) System.out.println( “Error stack full”); else stack_ref[++top_index] = number; }

  18. Parameterized Abstract Data Types- C++ • Templated Classes • Classes can be somewhat generic by writing parameterized constructor functions, e.g. stack (int size) { stk_ptr = new int [size]; max_len = size - 1; top = -1; } stack stk(100);

  19. Parameterized Abstract Data Types • The stack element type can be parameterized by making the class a templated class • Java does not support generic abstract data types

  20. Parameterized Abstract Data in C++ void push(int item) { if (topPtr == maxLen) cerr << “Stack full\n”; else stackPtr[++topPtr] = item; } void pop() { if (topPtr == -1) cerr << “Stack empty\n”; else topPtr--; } int top() { return ( stack[topPtr] ); } int empty() { return ( topPtr == -1); } } // end class stack #include <iostream.h> class stack { private: int *stackPtr; int maxLen; int topPtr; public: stack(int size) { stackPtr = new int [size]; maxLen = size -1; topPtr = -1; } ~stack() { delete [ ] stackPtr; } Usage: stack myStack(50);

  21. Template abstract data type in C++ • C++ example in left stack<int> stk; stack<int> stk(150); • C++ template classes are instantiated at compile time • code for new type is created when not existed • Java does not support generic abstract data type as in left #include <iostream.h> template <class Type> class stack { private: Type *stackPtr; public: stack() : stackPtr (new Type [100]), maxLen (99), topPtr(-1) { } stack(int size) { stackPtr = new Type [size]; maxLen = size – 1; topPtr= -1; }

  22. Encapsulation Constructs • Original motivation: • Large programs have two special needs: 1. Some means of organization, other than simply division into subprograms 2. Some means of partial compilation (compilation units that are smaller than the whole program) • Obvious solution: a grouping of subprograms that are logically related into a unit that can be separately compiled (compilation units) • These are called encapsulations

  23. Encapsulation Constructs • Nested subprograms in Ada and Fortran 95 • Encapsulation in C • Files containing one or more subprograms can be independently compiled • The interface is placed in a headerfile • Problem: the linker does not check types between a header and associated implementation • Encapsulation in C++ • Similar to C • Addition of friend functions that have access to private members of the friend class

  24. Naming Encapsulations • Large programs define many global names; need a way to divide into logical groupings • A naming encapsulation is used to create a new scope for names • C++ Namespaces • Can place each library in its own namespace and qualify names used outside with the namespace • C# also includes namespaces

  25. OO Key feature • Abstraction • Well-defined interface • Hierarchy • Composition • Derivation • inheritance • Encapsulation • Hiding the detail • Polymorphism

  26. From C to C++ • Without abstraction main(){ int stack_items[STACKSIZE], stack_top =0, x; /*push x into stack*/ stack_item[stack_top++] = x; /*pop stack to x*/ x = stack_item[--stack_top]; }

  27. Abstraction: void init(stack *s; viod push(stack *s, int i); int pop(stack *s); void cleanup(stack *s); typedef struct{ int items[stacksize]; Int top } stack; Void init(stack *s) {s->top = 0;} Void push (stack *s, int i) {s-> items[s->top++] =I;} Int pop(stack *s){return s->items[--s->top];} … main(){ int x; stack stack1; init(&stack1); push(&stack1, x); … } Abstraction

More Related