Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 403 - Programming Languages Class 22 November 14, 2000.

Similar presentations


Presentation on theme: "1 CS 403 - Programming Languages Class 22 November 14, 2000."— Presentation transcript:

1 1 CS 403 - Programming Languages Class 22 November 14, 2000

2 CS 403, Class 22 Slide # 2 Today’s Agenda Start Chapter 10 Assignment: Read chapter 10 for today Announcement:. Programming Assignment, due next week

3 CS 403, Class 22 Slide # 3 Abstraction The concept of abstraction is fundamental in programming Nearly all programming languages support process abstraction with subprograms Nearly all programming languages designed since 1980 have supported data abstraction with some kind of module

4 CS 403, Class 22 Slide # 4 Encapsulation Original motivation: Large programs have two special needs: Some means of organization, other than simply division into subprograms 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 These are called encapsulations

5 CS 403, Class 22 Slide # 5 Examples of Encapsulation Mechanisms 1. Nested subprograms in some ALGOL-like languages (e.g., Pascal) 2. FORTRAN 77 and C - Files containing one or more subprograms can be independently compiled 3. FORTRAN 90 and Ada - separately compilable modules What is the difference between independent and separate compilation?

6 CS 403, Class 22 Slide # 6 Abstract Data Type (ADT) Def: An abstract data type is a user-defined data type that satisfies the following two conditions: 1. The representation of and operations on objects of the type are defined in a single syntactic unit; also, other units can create objects of the type. 2. 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. Let’s write that down...

7 CS 403, Class 22 Slide # 7 ADT Definition Rationale What are the advantages of each restriction? Advantage of Restriction 1: Same as those for encapsulation: program organization, modifiability (everything associated with a data structure is together), and separate compilation Advantage of Restriction 2: Reliability--by hiding the data representations, user code cannot directly access objects of the type. User code cannot depend on the representation, allowing the representation to be changed without affecting user code.

8 CS 403, Class 22 Slide # 8 ADT examples Built-in types are abstract data types e.g. int type in C  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

9 CS 403, Class 22 Slide # 9 Language Requirements for Data Abstraction: 1. A syntactic unit in which to encapsulate the type definition. 2. A method of making type names and subprogram headers visible to clients, while hiding actual definitions. 3. Some primitive operations must be built into the language processor (usually just assignment and comparisons for equality and inequality) Some operations are commonly needed, but must be defined by the type designer e.g., iterators, constructors, destructors

10 CS 403, Class 22 Slide # 10 Language Design Issues 1. Encapsulate a single type, or something more? 2. What types can be abstract? 3. Can abstract types be parameterized? 4. What access controls are provided?

11 CS 403, Class 22 Slide # 11 Language Examples 1. Simula 67 Provided encapsulation, but no information hiding 2. Ada The encapsulation construct is the package Packages usually have two parts:  1. Specification package (the interface)  2. Body package (implementation of the entities named in the specification Any type can be exported Information Hiding Hidden types are named in the spec package as in:  type NODE_TYPE is private;

12 CS 403, Class 22 Slide # 12 Language Examples, Ada (cont.) Representation of an exported hidden type is specified in a special invisible (to clients) part of the spec package (the private clause), as in: package … is type NODE_TYPE is private; … type NODE_TYPE is record … end record; …

13 CS 403, Class 22 Slide # 13 Language Examples, Ada (cont.) A spec package can also define unhidden types simply by providing the representation outside a private clause The reasons for the two-part type definition are:  1. The compiler must be able to see the representation after seeing only the spec package (the compiler can see the private clause)  2. Clients must see the type name, but not the representation (clients cannot see the private clause)

14 CS 403, Class 22 Slide # 14 Language Examples, Ada (cont.) Private types have built-in operations for ssignment and comparison with = and /= Limited private types have no built-in operations ---> SHOW specification and body packages (pp. 422-423) and the procedure that uses them (p. 423)

15 CS 403, Class 22 Slide # 15 Evaluation of Ada Abstract Data Types 1. Lack of restriction to pointers is better Cost is recompilation of clients when the representation is changed 2. Cannot import specific entities from other packages

16 CS 403, Class 22 Slide # 16 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

17 CS 403, Class 22 Slide # 17 Language Examples, C++, Information Hiding Private clause for hidden entities Public clause for interface entities Protected clause - for inheritance (see Ch. 11) 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

18 CS 403, Class 22 Slide # 18 Language Examples, C++, Information Hiding 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 tilda (~) ---> SHOW class definition for stack (p. 425-426 and the example program that uses it (p. 426) Friend functions or classes - to provide access to private members to some unrelated units or functions (NECESSARY in C++)

19 CS 403, Class 22 Slide # 19 Evaluation of C++ Support for ADTs Classes are similar to Ada packages for providing abstract data type Difference: packages are encapsulations, whereas classes are types

20 CS 403, Class 22 Slide # 20 A Related Language: Java Similar to C++, except: All user-defined types are classes All objects are allocated from the heap and accessed through reference variables Individual entities in classes have access control modifiers (private or public), rather than clauses Java has a second scoping mechanism, package scope, which can be used in place of friends All entities in all classes in a package that do not have access control modifiers are visible throughout the package --> SHOW Java class definition for stacks (p. 428) and the class that uses it (p. 429)

21 CS 403, Class 22 Slide # 21 Parameterized Abstract Data Types 1. Ada Generic Packages Make the stack type more flexible by making the element type and the size of the stack generic ---> SHOW GENERIC_STACK package and two instantiations (p. 430) 2. C++ Templated Classes Classes can be somewhat generic by writing parameterized constructor functions

22 CS 403, Class 22 Slide # 22 Example of C++ Parameterized Constructor Stack (int size) { stk_ptr = new int [size]; max_len = size - 1; top = -1; } Stack(100) stk;

23 CS 403, Class 22 Slide # 23 Parameterized Classes in C++ The Vector element type can be parameterized by making the class a templated class Example from the STL ( http://www.sgi.com/Technology/STL/index.html ) : int main() { stack S; S.push(8); S.push(7); S.push(4); assert(S.size() == 3); assert(S.top() == 4); S.pop(); assert(S.top() == 7); S.pop(); assert(S.top() == 8); S.pop(); assert(S.empty()); }

24 CS 403, Class 22 Slide # 24 Parameterized Classes in Java Java does not support generic abstract data types But some extensions do; Generic Java (GJ)


Download ppt "1 CS 403 - Programming Languages Class 22 November 14, 2000."

Similar presentations


Ads by Google