27
Abstract data types & object-oriented paradigm

Abstract data types & object-oriented paradigm

  • Upload
    kiet

  • View
    67

  • Download
    1

Embed Size (px)

DESCRIPTION

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) - PowerPoint PPT Presentation

Citation preview

Page 1: Abstract data types & object-oriented paradigm

Abstract data types & object-oriented paradigm

Page 2: Abstract data types & object-oriented paradigm

Abstraction• Abstraction: a view of an entity that includes o

nly 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 s

orting algorithm is changed– used for a long time

Page 3: Abstract data types & object-oriented paradigm

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

Page 4: Abstract data types & object-oriented paradigm

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

Page 5: Abstract data types & object-oriented paradigm

Abstract data type example

interfaces:create(stack)destroy(stack)empty(stack)push(stack, ite

m)pop(stack)top(stack)

code example: …create(stk1);push(stk1, 34);push(stk1, 20);if ( !empty(stk1) )

temp = top(stk1);

• stack implementation can be changed from array to list w/o affecting the code

Page 6: Abstract data types & object-oriented paradigm

Encapsulation

• Encapsulation: a grouping of subprograms and the data they manipulate

• Hiding the implementation details of the abstract interface

• Protect internal data

• Reliability, maintenance

Page 7: Abstract data types & object-oriented paradigm

Language Examples-C++• Based on C struct type and Simula 67 clas

ses• The class is the encapsulation device• All of the class instances of a class share a si

ngle copy of the member functions• Each instance of a class has its own copy of t

he class data members• Instances can be static, stack dynamic, or he

ap dynamic

Page 8: Abstract data types & object-oriented paradigm

At first glance#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; }

void push(int item) {if (topPtr == maxLen) cerr << “Stack full\n”;else stackPtr[++topPtr] = it

em; } void pop() {

if (topPtr == -1) cerr << “Stack empty\n”;else topPtr--;

} int top() {

return ( stackPtr[topPtr] ); } int empty() {

return ( topPtr == -1); }} // end class stack

Data members

Member functions

Page 9: Abstract data types & object-oriented paradigm

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

Page 10: Abstract data types & object-oriented paradigm

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( )

Page 11: Abstract data types & object-oriented paradigm

Example in C++#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; }

void push(int item) {if (topPtr == maxLen) cerr << “Stack full\n”;else stackPtr[++topPtr] = it

em; } void pop() {

if (topPtr == -1) cerr << “Stack empty\n”;else topPtr--;

} int top() {

return ( stackPtr[topPtr] ); } int empty() {

return ( topPtr == -1); }} // end class stack

Page 12: Abstract data types & object-oriented paradigm

Class usage in C++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

}

• stack stk constructor is called to initialize the instance

• at the end of the main, stk’s destructor is called

Page 13: Abstract data types & object-oriented paradigm

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

Page 14: Abstract data types & object-oriented paradigm

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 (~)

Page 15: Abstract data types & object-oriented paradigm

Examples in Java

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();

}

• Java uses implicit garbage collection (no destructor) and reference variable (not pointer)

Page 16: Abstract data types & object-oriented paradigm

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

Page 17: Abstract data types & object-oriented paradigm

Example:Stack in Javaimport java.io.*;class Stack_class {

private int [ ] stack_ref;private int max_len,

top_index; public Stack_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;

}

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

Page 18: Abstract data types & object-oriented paradigm

Parameterized Abstract Data Types- C++

• Templated Classes– Classes can be somewhat generic by writin

g parameterized constructor functions, e.g.

stack (int size) { stk_ptr = new int [size]; max_len = size - 1; top = -1; } stack stk(100);

Page 19: Abstract data types & object-oriented paradigm

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

Page 20: Abstract data types & object-oriented paradigm

Parameterized Abstract Data in C++

#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; }

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

Usage: stack myStack(50);

Page 21: Abstract data types & object-oriented paradigm

Template abstract data type in C++

#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; }

• C++ example in leftstack<int> stk;stack<int> stk(150);

• C++ template classes are instantiated at compile time– code for new type is cre

ated when not existed

• Java does not support generic abstract data type as in left

Page 22: Abstract data types & object-oriented paradigm

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

Page 23: Abstract data types & object-oriented paradigm

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 header file– Problem: the linker does not check types betwee

n a header and associated implementation

• Encapsulation in C++ – Similar to C– Addition of friend functions that have access to pr

ivate members of the friend class

Page 24: Abstract data types & object-oriented paradigm

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

Page 25: Abstract data types & object-oriented paradigm

OO Key feature• Abstraction

– Well-defined interface

• Hierarchy – Composition– Derivation– inheritance

• Encapsulation– Hiding the detail

• Polymorphism

Page 26: Abstract data types & object-oriented paradigm

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];

}

Page 27: Abstract data types & object-oriented paradigm

Abstraction• 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);…

}