Chapter 11. Abstract Data Types and Encapsulation Concepts

Similar documents
Chapter 11. Abstract Data Types and Encapsulation Concepts

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Abstract Data Types and Encapsulation Concepts

Abstract data types &

Abstract Data Types & Object-Oriented Programming

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑 ISBN

OBJECT ORIENTED PROGRAMMING USING C++

CMSC 4023 Chapter 11

Chapter 11. Abstract Data Types and Encapsulation Concepts

Implementing Subprograms

COSC252: Programming Languages: Abstraction and OOP. Jeremy Bolton, PhD Asst Teaching Professor. Copyright 2015 Pearson. All rights reserved.

10. Abstract Data Types

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Evolution of Programming Languages

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

CPS 506 Comparative Programming Languages. Programming Language

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Where do we go from here?

Chapter 5. Names, Bindings, and Scopes

An Introduction to C++

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

Introduction to Programming Using Java (98-388)

CS304 Object Oriented Programming Final Term

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

G Programming Languages - Fall 2012

Fundamentals of Programming Languages

Cpt S 122 Data Structures. Introduction to C++ Part II

Data types. Definitions Aggregate constructors User-defined type definitions Types and storage Types and subtypes Type systems and type checking

Chapter 4: Writing Classes

Chapter 9 Subprograms

Chapter 6 Introduction to Defining Classes

COMP 2355 Introduction to Systems Programming

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Chapter 5 Names, Binding, Type Checking and Scopes

Object-Oriented Programming

Data Abstraction. Hwansoo Han

Object Oriented Design

Introduction to the C programming language

Introduction to the C programming language


Chapter 9. Subprograms

Programming II (CS300)

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Lecture 18 Tao Wang 1

Organization of Programming Languages CS 3200/5200N. Lecture 09

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

C++ Important Questions with Answers

CSE 307: Principles of Programming Languages

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Fast Introduction to Object Oriented Programming and C++

Programming Languages Third Edition. Chapter 7 Basic Semantics

CS 231 Data Structures and Algorithms, Fall 2016

Object Oriented Design

Binding and Variables

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Concepts of Programming Languages

STRUCTURING OF PROGRAM

G52CPP C++ Programming Lecture 9

Interview Questions of C++

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

CSC 1214: Object-Oriented Programming

Object Oriented Programming. Solved MCQs - Part 2

Chapter 9. Subprograms

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

CSCE 314 Programming Languages. Type System

Programming overview

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Introduction to C++ Friends, Nesting, Static Members, and Templates Topic #7

Programming in C/C Lecture 3

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Programming in C and C++

September 10,

Absolute C++ Walter Savitch

Lecture Notes on Programming Languages

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Today s lecture. CS 314 fall 01 C++ 1, page 1

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Constructors for classes

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Data Structures (list, dictionary, tuples, sets, strings)

C++ Templates. David Camp

Names, Bindings, Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes

Object Oriented Paradigm

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

Object Oriented Software Design II

Overview of C++: Part 1

Introductory ios Development

CA341 - Comparative Programming Languages

G Programming Languages - Fall 2012

Transcription:

Chapter 11 Abstract Data Types and Encapsulation Concepts

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 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 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. 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 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 1-6

Design Issues What is the form of the container for the interface to the type? Can abstract types be parameterized? What access controls are provided? Is the specification of the type physically separate from its implementation? 1-7

Language Examples: Ada The encapsulation construct is called a package Specification package (the interface) Body package (implementation of the entities named in the specification) Information Hiding The spec package has two parts, public and private The name of the abstract type appears in the public part of the specification package. This part may also include representations of unhidden types The representation of the abstract type appears in a part of the specification called the private part More restricted form with limited private types Private types have built-in operations for assignment and comparison Limited private types have NO built-in operations 1-8

Language Examples: Ada (continued) Reasons for the public/private spec package: 1. The compiler must be able to see the representation after seeing only the spec package (it cannot see the private part) 2. Clients must see the type name, but not the representation (they also cannot see the private part) 1-9

An Example in Ada - Specification package Stack_Pack is type stack_type is limited private; max_size: constant := 100; function empty(stk: in stack_type) return Boolean; procedure push(stk: in out stack_type; elem: in Integer); procedure pop(stk: in out stack_type); function top(stk: in stack_type) return Integer; private -- hidden from clients type list_type is array (1..max_size) of Integer; type stack_type is record list: list_type; topsub: Integer range 0..max_size) := 0; end record; end Stack_Pack 1-10

An Example in Ada - Body with Ada.Text_IO; use Ada.Text_IO; package body Stack_Pack is function Empty(Stk : in Stack_Type) return Boolean is begin return Stk.Topsub = 0; end Empty; procedure Push(Stk: in out Stack_Type; Element : in Integer) is begin if Stk.Topsub >= Max_Size then else Put_Line( ERROR Stack overflow ); Stk.Topsub := Stk.Topsub + 1; Stk.List(Topsub) := Element; end if; end Push;... end Stack_Pack; 1-11

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 1-12

Language Examples: C++ (continued) Information Hiding Private clause for hidden entities Public clause for interface entities Protected clause for inheritance (Chapter 12) 1-13

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 1-14

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

An Example in C++ class Stack { private: int *stackptr, maxlen, topptr; public: Stack() { // a constructor stackptr = new int [100]; maxlen = 99; topptr = -1; }; ~Stack () {delete [] stackptr;}; void push (int number) { if (topsub == maxlen) cerr << Error in push - stack is full\n ; else stackptr[++topsub] = number; }; void pop () { }; int top () { }; int empty () { }; } 1-16

A Stack class header file // Stack.h - the header file for the Stack class #include <iostream.h> class Stack { private: //** These members are visible only to other //** members and friends (see Section 11.6.4) int *stackptr; int maxlen; int topptr; public: //** These members are visible to clients Stack(); //** A constructor ~Stack(); //** A destructor void push(int); void pop(); int top(); int empty(); } 1-17

The code file for Stack // Stack.cpp - the implementation file for the Stack class #include <iostream.h> #include "Stack.h" using std::cout; Stack::Stack() { //** A constructor stackptr = new int [100]; maxlen = 99; topptr = -1; } Stack::~Stack() {delete [] stackptr;}; //** A destructor void Stack::push(int number) { if (topptr == maxlen) cerr << "Error in push--stack is full\n"; else stackptr[++topptr] = number; }... 1-18

Language Examples: C++ (continued) Friend functions or classes - to provide access to private members to some unrelated units or functions Necessary in C++ 1-19

Language Examples Objective-C Interface containers @interface class-name: parent-class { instance variable declarations } method prototypes @end Implementation containers @implementation class-name method definitions @end Classes are types 1-20

Language Examples Objective-C (continued) Method prototypes form (+ -) (return-type) method-name [: (formal-parameters)]; - Plus indicates a class method - Minus indicates an instance method - The colon and the parentheses are not included when there are no parameters - Parameter list format is different - If there is one parameter (name is meth1:) -(void) meth1: (int) x; - For two parameters -(int) meth2: (int) x second: (float) y; - The name of the method is meth2:: 1-21

Language Examples Objective-C (continued) Method call syntax [object-name method-name]; Examples: [myadder add1: 7]; [myadder add1: 7: 5: 3]; - For the method: -(int) meth2: (int) x second: (float) y; the call would be like the following: [myobject meth2: 7 second: 3.2]; 1-22

Language Examples Objective-C (continued) Constructors are called initializers all they do is initialize variables Initializers can have any name they are always called explicitly Initializers always return self Objects are created by calling alloc and the constructor Adder *myadder = [[Adder alloc] init]; All class instances are heap dynamic 1-23

Language Examples Objective-C (continued) To import standard prototypes (e.g., i/o) #import <Foundation/Foundation.h> The first thing a program must do is allocate and initialize a pool of storage for its data (pool s variable is pool in this case) NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; At the end of the program, the pool is released with: [pool drain]; 1-24

Language Examples Objective-C (continued) Information Hiding The directives @private and @public are used to specify the access of instance variables. The default access is protected (private in C++) There is no way to restrict access to methods The name of a getter method is always the name of the instance variable The name of a setter method is always the word set with the capitalized variable s name attached If the getter and setter for a variable does not impose any constraints, they can be implicitly generated (called properties) 1-25

Language Examples Objective-C (continued) // stack.m interface and implementation for a simple stack #import <Foundation/Foundation.h> @interface Stack: NSObject { } @end int stackarray[100], stackptr,maxlen, topsub; -(void) push: (int) number; -(void) pop; -(int) top; -(int) empty; @implementation Stack } -(Stack *) initwith { maxlen = 100; topsub = -1; stackptr = stackarray; return self; 1-26

Language Examples Objective-C (continued) // stack.m continued -(void) push: (int) number { if (topsub == maxlen) NSLog(@ Error in push stack is full ); else stackptr[++topsub] = number;... } 1-27

Language Examples Objective-C (continued) An example use of stack.m Placed in the @implementation of stack.m int main (int argc, char *argv[]) { int temp; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Stack *mystack = [[Stack alloc] initwith]; [mystack push: 5]; [mystack push: 3]; temp = [mystack top]; NSLog(@ Top element is: %i, temp); [mystack pop]; temp = [mystack top]; NSLog(@ Top element is: %i, temp); temp = [mystack top]; mystack pop]; [mystack release]; [pool drain]; return 0; } 1-28

Language Examples: 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 1-29

An Example in Java class StackClass { private: private int [] *stackref; private int [] maxlen, topindex; public StackClass() { // a constructor stackref = new int [100]; maxlen = 99; topptr = -1; }; public void push (int num) { }; public void pop () { }; public int top () { }; public boolean empty () { }; } 1-30

Language Examples: C# Based on C++ and Java Adds two access modifiers, internal and protected internal All class instances are heap dynamic Default constructors are available for all classes Garbage collection is used for most heap objects, so destructors are rarely used structs are lightweight classes that do not support inheritance 1-31

Language Examples: C# (continued) Common solution to need for access to data members: accessor methods (getter and setter) C# provides properties as a way of implementing getters and setters without requiring explicit method calls 1-32

C# Property Example public class Weather { public int DegreeDays { //** DegreeDays is a property get {return degreedays;} set { if (value < 0 value > 30) Console.WriteLine( "Value is out of range: {0}", value); else degreedays = value;} } private int degreedays;... }... Weather w = new Weather(); int degreedaystoday, olddegreedays;... w.degreedays = degreedaystoday;... olddegreedays = w.degreedays; 1-33

Abstract Data Types in Ruby Encapsulation construct is the class Local variables have normal names Instance variable names begin with at signs (@) Class variable names begin with two at signs (@@) Instance methods have the syntax of Ruby functions (def end) Constructors are named initialize (only one per class) implicitly called when new is called If more constructors are needed, they must have different names and they must explicitly call new Class members can be marked private or public, with public being the default Classes are dynamic 1-34

Abstract Data Types in Ruby (continued) class StackClass { def initialize @stackref = Array.new @maxlen = 100 @topindex = -1 end def push(number) if @topindex == @maxlen puts " Error in push stack is full" else @topindex = @topindex + 1 @stackref[@topindex] = number end end def pop end def top end def empty end end 1-35

Parameterized Abstract Data Types Parameterized ADTs allow designing an ADT that can store any type elements only an issue for static typed languages Also known as generic classes C++, Ada, Java 5.0, and C# 2005 provide support for parameterized ADTs 1-36

Parameterized ADTs in Ada Ada Generic Packages Make the stack type more flexible by making the element type and the size of the stack generic generic Max_Size: Positive; type Elem_Type is private; package Generic_Stack is type Stack_Type is limited private; function Empty(Stk : in Stack_Type) return Boolean; function Top(Stk: in out StackType) return Elem_type;... private type List_Type is array (1..Max_Size) of Element_Type; type Stack_Type is record List : List_Type; Topsub : Integer range 0.. Max_Size := 0; end record; end Generic_Stack; 1-37

Parameterized ADTs in Ada (continued) Instantiations of the generic stack package Integer_Stack is new Generic_Stack(100,Integer); package Float_Stack is new Generic_Stack(100,Float); 1-38

Parameterized ADTs in C++ The stack element type can be parameterized by making the class a templated class template <class Type> class Stack { private: Type *stackptr; const int maxlen; int topptr; public: Stack() { // Constructor for 100 elements stackptr = new Type[100]; maxlen = 99; topptr = -1; } Stack(int size) { // Constructor for a given number stackptr = new Type[size]; maxlen = size 1; topsub = -1; }... } - Instantiation: Stack<int> myintstack; 1-39

Parameterized Classes in Java 5.0 Generic parameters must be classes Most common generic types are the collection types, such as LinkedList and ArrayList Eliminate the need to cast objects that are removed Eliminate the problem of having multiple types in a structure Users can define generic classes Generic collection classes cannot store primitives Indexing is not supported Example of the use of a predefined generic class: ArrayList <Integer> myarray = new ArrayList <Integer> (); myarray.add(0, 47); // Put an element with subscript 0 in it 1-40

Parameterized Classes in Java 5.0 (continued) import java.util.*; public class Stack2<T> { } private ArrayList<T> stackref; private int maxlen; public Stack2)( { } stackref = new ArrayList<T> (); maxlen = 99; public void push(t newvalue) {... if (stackref.size() == maxlen) else System.out.println(" Error in push stack is full"); stackref.add(newvalue); - Instantiation: Stack2<string> mystack = new Stack2<string> (); 1-41

Parameterized Classes in C# 2005 Similar to those of Java 5.0, except no wildcard classes Predefined for Array, List, Stack, Queue, and Dictionary Elements of parameterized structures can be accessed through indexing 1-42

Encapsulation Constructs 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 (compilation units) Such collections are called encapsulation 1-43

Nested Subprograms Organizing programs by nesting subprogram definitions inside the logically larger subprograms that use them Nested subprograms are supported in Ada, Fortran 95+, Python, JavaScript, and Ruby 1-44

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 between a header and associated implementation #include preprocessor specification used to include header files in applications 1-45

Encapsulation in C++ Can define header and code files, similar to those of C Or, classes can be used for encapsulation The class is used as the interface (prototypes) The member definitions are defined in a separate file Friends provide a way to grant access to private members of a class 1-46

Ada Packages Ada specification packages can include any number of data and subprogram declarations Ada packages can be compiled separately A package s specification and body parts can be compiled separately 1-47

C# Assemblies A collection of files that appears to application programs to be a single dynamic link library or executable Each file contains a module that can be separately compiled A DLL is a collection of classes and methods that are individually linked to an executing program C# has an access modifier called internal; an internal member of a class is visible to all classes in the assembly in which it appears 1-48

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 1-49

Naming Encapsulations (continued) Java Packages Packages can contain more than one class definition; classes in a package are partial friends Clients of a package can use fully qualified name or use the import declaration Ada Packages Packages are defined in hierarchies which correspond to file hierarchies Visibility from a program unit is gained with the with clause 1-50

Summary The concept of ADTs and their use in program design was a milestone in the development of languages Two primary features of ADTs are the packaging of data with their associated operations and information hiding Ada provides packages that simulate ADTs C++ data abstraction is provided by classes Java s data abstraction is similar to C++ Ada, C++, Java 5.0, and C# 2005 support parameterized ADTs C++, C#, Java, Ada, and Ruby provide naming encapsulations 1-51