L09 Abstract

  • Upload
    habtamu

  • View
    225

  • Download
    0

Embed Size (px)

Citation preview

  • 8/10/2019 L09 Abstract

    1/15

    Programming Languages

    Abstract Data Types and

    Design Issues

  • 8/10/2019 L09 Abstract

    2/15

    Outline

    The Concept of Abstraction Introduction to Data AbstractionDesign IssuesLanguage Examples

  • 8/10/2019 L09 Abstract

    3/15

    Abstraction

    Two types of abstractions:Process abstraction: subprogramsData abstraction

    Floatingpoint data type as data abstractionThe programming language will pro!ide "#$ a

    way of creating !ariables of the %oatingpointdata type& and "'$ a set of operators formanipulating !ariables

    Abstract away and hide the information of howthe %oatingpoint number is presented andstored

  • 8/10/2019 L09 Abstract

    4/15

    Abstraction Data Type

    Abstract data type: a userde(ned data typeDeclaration of the type and protocol of

    operations on ob)ects of the type& i*e*& type+s

    interface& are de(ned in a syntactic unit,interface indep* of implementation

    -epresentation of ob)ects of the type is hiddenfrom program units that use these ob)ects, onlypossible operations are those pro!ided in type.s

    de(nitionclass data type int

    ob)ect !ariable i& )& /

    method operators 0& & 1& 2

    y 3 stac/#*top"$04, !s y 3 "x$ 0 4,

  • 8/10/2019 L09 Abstract

    5/15

    Advantages of Data Abstraction

    Ad!antage of ha!ing interfaceindependent of ob)ect representation orimplementation of operations:

    5rogram organi6ation& modi(abilityAd!antage of 'nd condition "info* hiding$-eliability: 7y hiding data representations& user

    code cannot directly access ob)ects of the type

    or depend on the representation& allowing therepresentation to be changed without a8ectinguser code

  • 8/10/2019 L09 Abstract

    6/15

    cont.....

    Design Issues and Language Examples

  • 8/10/2019 L09 Abstract

    7/15

    Language Requirements for ADTs

    A syntactic unit to encapsulate type de(nitionA method of ma/ing type names and

    subprogram headers !isible to clients& while

    hiding actual de(nitions9ome primiti!e operations that are built into

    the language processorExample: an abstract data type for stac/

    create"stac/$& destroy"stac/$& empty"stac/$&push"stac/& element$& pop"stac/$& top"stac/$

    9tac/ may be implemented with array& lin/edlist& ***

  • 8/10/2019 L09 Abstract

    8/15

    Abstract Data Types in C++

    The classis the encapsulation de!iceAll of the class instances of a class share a single

    copy of the member functionsEach instance has own copy of class data membersInstances can be static& stac/ dynamic& heap

    dynamic

    Information hidingPrivateclause for hidden entitiesPublicclause for interface entitiesProtectedclause for inheritance

  • 8/10/2019 L09 Abstract

    9/15

    ember !unctions Defined in Class

    class Stack {private:

    int *stackPtr, maxLen, topPtr;public:

    Stack() { // a constructorstackPtr = new int !""#;maxLen = $$; topPtr = %!; &;

    'Stack () {elete # stackPtr;&;voi pus (int num) {&;

    voi pop () {&;int top () {&;int empt+ () {&;

    &

  • 8/10/2019 L09 Abstract

    10/15

    Language "#amples$ C++ %cont.&

    Constructors:Functions to initiali6e the data members of

    instances "they do not create the ob)ects$

    ay also allocate storage if part of the ob)ectis heapdynamicImplicitly called when an instance is createdCan be explicitly called

    ;ame is the same as the class name

  • 8/10/2019 L09 Abstract

    11/15

    Language "#amples$ C++ %cont.&

    DestructorsFunctions to clean up after an instance is

    destroyed, usually )ust to reclaim heap storage

    Implicitly called when the ob)ect+s lifetimeendsCan be explicitly called;ame is the class name& preceded by a tilde

    "

  • 8/10/2019 L09 Abstract

    12/15

    'ses of t(e )tac* Class

    voi main()

    {

    int topne;

    Stack stk; //create an instance o-te Stack class

    stk.pus(0); // c.-., stk 1= 0

    stk.pus(!2);

    topne = stk.top(); // c.-., 3stkstk.pop();

    ...

    &

  • 8/10/2019 L09 Abstract

    13/15

    Abstract Data Types in ava

    9imilar to C00& except:All userde(ned types are classesAll ob)ects are allocated from the heap and

    accessed through reference !ariablesethods must be de(ned completely in a classIndi!idual entities in classes ha!e access

    control modi(ers "pri!ate or public$& ratherthan clauses

    ;o destructor implicit garbage collection

  • 8/10/2019 L09 Abstract

    14/15

    An "#ample in ava

    class Stack4lass {

    private int # stack5e-;

    private int maxLen, top6nex;

    public Stack4lass() { // a constructorstack5e- = new int !""#;

    maxLen = $$; topPtr = %!;&;

    public voi pus (int num) {&;

    public voi pop () {&;public int top () {&;

    public boolean empt+ () {&;

    &

  • 8/10/2019 L09 Abstract

    15/15

    An "#ample in ava

    public class 7stStack {

    public static voi main(Strin8# ar8s) {

    Stack4lass m+Stack = new Stack4lass();

    m+Stack.pus(0);m+Stack.pus(0$);

    S+stem.out.println(9:91m+Stack.top());

    m+Stack.pop();

    m+Stack.empt+();&

    &