Chapter 11 Abstract Data Types and Encapsulation Concepts PDF
Document Details
Uploaded by UnmatchedMandolin
2018
Robert W. Sebesta
Tags
Summary
This document is a chapter on abstract data types (ADTs) and encapsulation concepts, focusing on their importance in programming language design. It discusses the benefits of abstraction and encapsulation, including improved reliability and code organization. It also covers different language examples and implementation details in C++, Java, and C#.
Full Transcript
Chapter 11 Abstract Data Types and Encapsulation Concepts ISBN 0-321-49362-1 Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language Examples Parameterized Abstract Data Types Encapsulati...
Chapter 11 Abstract Data Types and Encapsulation Concepts ISBN 0-321-49362-1 Chapter 11 Topics The Concept of Abstraction Introduction to Data Abstraction Design Issues for Abstract Data Types Language Examples Parameterized Abstract Data Types Encapsulation Constructs Naming Encapsulations Copyright © 2018 Pearson. All rights reserved. 1-2 The Concept of Abstraction An abstraction is a view or representation of an entity that includes only the most significant attributes The concept of abstraction is fundamental in programming (and computer science) Nearly all programming languages support process abstraction with subprograms Nearly all programming languages designed since 1980 support data abstraction Copyright © 2018 Pearson. All rights reserved. 1-3 Introduction to Data Abstraction An abstract data type is a user-defined data type that satisfies the following two conditions: – The representation of objects of the type is hidden from the program units that use these objects, so the only operations possible are those provided in the type's definition – The declarations of the type and the protocols of the operations on objects of the type are contained in a single syntactic unit. Other program units are allowed to create variables of the defined type. Copyright © 2018 Pearson. All rights reserved. 1-4 Advantages of Data Abstraction Advantages the first condition – Reliability--by hiding the data representations, user code cannot directly access objects of the type or depend on the representation, allowing the representation to be changed without affecting user code – Reduces the range of code and variables of which the programmer must be aware – Name conflicts are less likely Advantages of the second condition – Provides a method of program organization – Aids modifiability (everything associated with a data structure is together) – Separate compilation Copyright © 2018 Pearson. All rights reserved. 1-5 Language Requirements for ADTs A syntactic unit in which to encapsulate the type definition A method of making type names and subprogram headers visible to clients, while hiding actual definitions Some primitive operations must be built into the language processor Copyright © 2018 Pearson. All rights reserved. 1-6 Design Issues Can abstract types be parameterized? What access controls are provided? Is the specification of the type physically separate from its implementation? Copyright © 2018 Pearson. All rights reserved. 1-7 Language Examples: C++ Based on C struct type and Simula 67 classes The class is the encapsulation device A class is a type 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 Copyright © 2018 Pearson. All rights reserved. 1-8 Language Examples: C++ (continued) Information Hiding – Private clause for hidden entities – Public clause for interface entities – Protected clause for inheritance (Chapter 12) Copyright © 2018 Pearson. All rights reserved. 1-9 Language Examples: C++ (continued) Constructors: – Functions to initialize the data members of instances (they do not create the objects) – 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 Copyright © 2018 Pearson. All rights reserved. 1-10 Language Examples: C++ (continued) 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 (~) Copyright © 2018 Pearson. All rights reserved. 1-11 An Example in C++ class Stack { private: int *stackPtr, maxLen, topPtr; public: Stack() { // a constructor stackPtr = new int ; maxLen = 99; topPtr = -1; }; ~Stack () {delete [] stackPtr;}; void push (int number) { if (topSub == maxLen) cerr