chapter 3 data abstraction: the walls. © 2005 pearson addison-wesley. all rights reserved3-2 3.1...

65
Chapter 3 Data Abstraction: The Walls

Upload: kristopher-morrison

Post on 13-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

Chapter 3

Data Abstraction: The Walls

Page 2: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-2

3.1 Abstract Data Types (抽象資料型態 )

• Modularity– Keeps the complexity of a large program

manageable by systematically controlling the interaction of its components

– Isolates errors

Page 3: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-3

Abstract Data Types

• Modularity (Continued)– Eliminates redundancies– A modular program is

• Easier to write

• Easier to read

• Easier to modify

Page 4: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-4

Abstract Data Types

• Procedural abstraction– Separates the purpose and use of a module from

its implementation– A module’s specifications should

• Detail how the module behaves

• Identify details that can be hidden within the module

Page 5: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-5

Abstract Data Types

• Information hiding– Hides certain implementation details within a

module– Makes these details inaccessible from outside

the module

Page 6: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-6

Abstract Data Types

Figure 3.1

Isolated tasks: the implementation of task T does not affect task Q

Q 是應用程式, Q 不用知道工作 T 是怎樣完成的,但 Q 必須知道這個工作的內容以及如何啟動 T 這個工作 ( 就是呼叫 T )

Page 7: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-7

Abstract Data Types

• The isolation of modules is not total– Functions’ specifications ( 規格 ), or contracts, govern how they interact

with each other

Figure 3.2 A slit in the wall

Page 8: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-8

Abstract Data Types

• Typical operations on data– Add data to a data collection– Remove data from a data collection– Ask questions about the data in a data

collection

Page 9: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-9

Abstract Data Types

• Data abstraction– Asks you to think what you can do to a

collection of data independently of how you do it

– Allows you to develop each data structure in relative isolation from the rest of the solution

– A natural extension of procedural abstraction

Page 10: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-10

Abstract Data Types

• Abstract data type (ADT)– An ADT is composed of

• A collection of data• A set of operations on that data

– Specifications of an ADT indicate• What the ADT operations do, not how to implement

them

– Implementation of an ADT• Includes choosing a particular data structure

Page 11: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-11

範例

• 如果要設計可以儲存一群員工姓名及薪資的資料結構const int MAX_NUMBER = 500;string names[MAX_NUMBER];double salaries[MAX_NUMBER];– 在此第 i 個員工的姓名為 names[i] ,薪資為

salaries[i] , names 及 salaries 這兩個陣列就形成一個資料結構, C++ 並沒有資料型態 (data type) 可以直接表示這樣的資料結構

• 當程式需要執行一些程式語言並不直接支援的資料相關操作,我們就要先設計一個抽象資料型態 (ADT) ,定義其相關操作的內容 (what) ,接著才去實做這些相關操作

Page 12: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-12

Abstract Data Types

Figure 3.4

A wall of ADT operations isolates a data structure from the program that uses it範例:製冰機、販賣機

Page 13: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-13

製冰機

Page 14: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-14

3.2 The ADT List

• List contains items of the same type• Except for the first and last items, each item has a

unique predecessor ( 前一項 ) and a unique successor ( 後一項 )

• Head ( 序列頭 ) or front ( 序列最前線 ) do not have a predecessor

• Tail ( 序列尾巴 ) or end ( 序列最後 ) do not have a successor

• The items on a list, together with operations that you can perform on the items, form an ADT

Page 15: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-15

超市購物單序列

Page 16: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-16

The ADT List

• Items ( 項目 ) are referenced ( 參考 ) by their position ( 位置 ) within the list

• Specifications ( 規格 ) of the ADT operations– Define the contract ( 合約、規格 ) for the ADT list– Do not specify how to store the list or how to perform

the operations

• ADT operations can be used in an application without the knowledge of how the operations will be implemented

Page 17: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-17

The ADT List

• ADT List operations– Create an empty list– Determine whether a list is empty– Determine the number of items in a list– Add an item at a given position in the list– Remove the item at a given position in the list– Remove all the items from the list– Retrieve (get) item at a given position in the list

Page 18: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-18

UML Diagram for ADT List

要先定義好這些操作對於 List 物件的影響,請自己試試看加上參數並說明其工作內容

Page 19: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-19

應用程式

• 假設有一 aList ,以下每一動作執行完後, aList 的內容會變成什麼樣子 ??aList.createList( );aList.insert(1, milk, success);aList.insert(2, eggs, success);aList.insert(3, butter, success);aList.insert(4, apples, success);aList.insert(5, bread, success);aList.insert(6, chicken, success);aList.insert(4, nuts, success);aList.remove(5, success);

我們不用知道 List 是怎麼實做的就可以知道 aList 在這些動作後的結果,這就是 ADT 的優點success 是一

個 boolean 參數

Page 20: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-20

應用程式

• displayList(in aList:List)

for (position = 1 through aList.getlength( ) )

{aList.retrieve(position, dataItem, success)

cout << dataItem << endl;}

Page 21: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-21

ADT 操作的牆壁

Page 22: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-22

應用程式

• replace(in aList: List, in i:integer, in newItem:ListItemType,

out success:boolean)

aList.remove(i, success);

if (success)aList.insert(i, newItem, success);

Page 23: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-23

The ADT List

• The ADT sorted list– Maintains items in sorted order– Inserts and deletes items by their values, not

their positions

Page 24: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-24

The ADT sorted list

• OperationscreateSortedList()destroySortedList( )sortedIsEmpty( ): boolean {query}sortedGetLength( ): integer {query}sortedInsert(in newItem:ListItemType, out success:boolean)sortedRemove(in anItem:ListItemType, out success: boolean)sortedRetrieve(in index:integer, out dataItem:

ListItemType, out success: boolean) {query} locatePosition(in anItem: ListItemType, out isPresent: boolean): integer

{query}

Page 25: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-25

Designing an ADT

• The design of an ADT should evolve naturally during the problem-solving process

• Questions to ask when designing an ADT– What data does a problem require?– What operations does a problem require?

放假日範例會議簿範例

Page 26: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-26

範例

• listHolidays(in year: integer) 列出每年所有放假日演算法如下

date = date of the first day of year while ( date is before the first day of year+1) { if (date is a holiday) cout << date << “is a holiday” <<

endl; date = next day of date } // end of while

要有哪些資料?有

哪些操作 ?

Page 27: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-27

範例

date = firstDay(year) while ( isBefore(date, firstDay(year+1) ) { if (date.isHoliday()) cout << date << “is a holiday”

<< endl; date = date.nextDay() } // end of while

Page 28: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-28

範例

• 會議簿:– 假設你希望可以紀錄上班日自上午 8 點至

下午 5 點各整點及 30 分的各項會議主題– 要有哪些資料?有哪些操作?

• 設定會議日期、時間、主題• 取消某日期、時間的會議• 查詢某個日期時間是否有會議要開?• 如果有會議,主題為何?• 細節請參考課本 p. 128

Page 29: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-29

範例

• OperationscreateAppointmentBook( )isAppointment( in appDate:aDate, in appTime:Time):

boolean {query}makeAppointment(in appDate:Date, inappTime:Time, in purpose:string): booleancancelAppoint(in appDate:aDate, in appTime:Time):

booleancheckAppointment(in appDate:aDate, in appTime:Time,

out purpose:string): boolean {query}

• 如何運用以上操作 , 寫一個修改會議的日期時間的程式?

Page 30: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-30

ADTs that suggests other ADTs

• 假設你要設計一個食譜資料庫– Data Items 是食譜– 對此資料庫的操作包括:

insertRecipe(in aRecipe: Recipe, out: success: boolean)

deleteRecipe(in aRecipe: Recipe, out: success: boolean)

retrieveRecipe(in name: string, out: a Recipe: recipe, out success: boolean) {query}

Page 31: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-31

ADTs that suggests other ADTs

• 現在要將食譜擴充可服務人數:假設原食譜可服務 n 個人現在要服務 m 個人

• 這個問題建議我們為食譜物件再增加一個 ADT :份量 (measurement)– Operations

getMeasure(): Measurement {query}setMeasure(in m: Measurement)scaleMeasure(out newMeasure:Measurement, in

scaleFactor: float)convertMeasure(in oldUnits: MeasureUnit, out

newMeasure: Measurement, in newUnits: MeasureUnit) {query}

Page 32: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-32

ADTs that suggests other ADTs

• 假設份量 (Measurement) 又要細到可以做分數的運算

• 這個問題建議我們為份量物件再增加一個 ADT :分數 (fraction)– Operations

• 加、減、乘、除

Page 33: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-33

Designing an ADT

• For complex abstract data types, the behavior of the operations must be specified using axioms– Axiom: A mathematical rule– Ex. : (aList.createList()).size() = 0– 透過這些數學定理,證明比較嚴謹,但需紮

實數學基礎• Axioms 部分跳過 (p. 131 – 133 上半 )

Page 34: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-34

3.3 Implementing ADTs

• Choosing the data structure to represent the ADT’s data is a part of implementation– Choice of a data structure depends on

• Details of the ADT’s operations

• Context in which the operations will be used

• Writing functions that access the data structure in accordance with the ADT operations– In general, you should refine the ADT operations

through successive ( 連續的 ) levels of abstraction. 也就是要用 top-down 的方式來設計各個 operation 的演算法

Page 35: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-35

Implementing ADTs

• Implementation details should be hidden behind a wall of ADT operations– A program would only be able to access the

data structure using the ADT operations• 傳統非物件導向的方式無法禁止應用程式違反這

項原則,例如:如果以陣列 items 來儲存 ADT List 的各個項目時,無法防止應用程式以下列方式讀取資料:

firstItem = items[0];

當以其他方式來實做 ADT List 時,這些程式要跟著修改

Page 36: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-36

Implementing ADTs

Figure 3.8

ADT operations provide access to a data structure

Page 37: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-37

Implementing ADTs

Figure 3.9 Violating the wall of ADT operations

Page 38: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-38

C++ Classes

• Encapsulation combines an ADT’s data with its operations to form an object – An object is an instance of a class

– A class contains data members and member functions

• By default, all members in a class are private• An ADT’s operations become an object’s

behaviors

Page 39: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-39

C++ Classes

• You could use struct when you only want to group data of different types

• Each class definition is placed in a header file– Classname.h

• The implementation of a class’s member functions are placed in an implementation file– Classname.cpp

Page 40: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-40

C++ Classes

Figure 3.10

An object’s data and methods

are encapsulated

Page 41: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-41

C++ Classes (Example)

• Sphere ( 球體 ) 相關檔案– header file: Sphere.h– Implementation file: Sphere.cpp– 應用程式 : SphereDemo.cpp

Page 42: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-42

Class Constructors and Destructors

• Constructors ( 建構子 )– Create and initialize new instances of a class– Have the same name as the class– Have no return type, not even void

Page 43: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-43

Class Constructors and Destructors

• A class can have several constructors– A default constructor has no arguments– Initializers can set data members to initial

values– The compiler will generate a default constructor

if you omit all constructors for your class• 但如果有其他需要參數的建構子,卻沒有預設建

構子,那麼 compiler 就不會自動產生預設建構子,也就不能做如下宣告:

Sphere defaultSphere;

Page 44: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-44

Class Constructors and Destructors

• The implementation of a constructor (or any member function) is qualified with the scope resolution operator ::

Sphere::Sphere(double initialRadius) :

theRadius(initialRadius)

Page 45: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-45

Class Constructors and Destructors

• Destructors– Destroy an instance of an object when the object’s

lifetime ends

• Each class has one destructor– For many classes, the destructor can be omitted

– 如果成員變數裡包含有動態陣列,就不能省略,否則程式執行一段時間後會當掉

– The compiler will generate a default destructor if one is omitted

Page 46: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-46

Inheritance in C++

• A derived class or subclass inherits any of the publicly defined functions or data members of a base class or superclass

• 這部份 3 年級再上 ( 課本 p. 143 – p.144先跳過 )

Page 47: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-47

Inheritance in C++

• An instance of a derived class can invoke public methods of the base class:#include “Sphere.h”enum Color {RED, BLUE, GREEN, YELLOW}class ColoredSphere: public Sphere{public:…Color getColor() const;

Page 48: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-48

C++ Namespaces

• A mechanism for logically grouping declarations and definitions into a common declarative region

Page 49: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-49

C++ Namespaces

• The contents of the namespace can be accessed by code inside or outside the namespace– Use the scope resolution operator to access

elements from outside the namespace– Alternatively, the using declaration allows

the names of the elements to be used directly

Page 50: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-50

C++ Namespaces

• Creating a namespacenamespace smallNamespace{ int count = 0; void abc(); } //end smallNamespace

• Using a namespaceusing namespace smallNamespace;count +=1;abc();

Page 51: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-51

C++ Namespaces

• Items declared in the C++ Standard Library are declared in the std namespace

• C++ include files for several functions are in the std namespace – To include input and output functions from the

C++ library, write include <iostream>

using namespace std;

Page 52: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-52

An Array-Based ADT List

• A list’s items are stored in an array items• Both an array and a list identify their items by

number• 相關檔案: ListA.h, ListA.cpp, ListDemo.cpp

Page 53: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-53

An Array-Based ADT List

• A list’s kth item will be stored in items[k-1]

Figure 3.11 An array-based implementation of the ADT list

Page 54: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-54

An Array-Based ADT List

Page 55: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-55

An Array-Based ADT List

Page 56: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-56

An Array-Based ADT List

Page 57: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-57

C++ Exceptions

• Exception– A mechanism for handling an error during

execution– A function indicates that an error has occurred

by throwing an exception– The code that deals with the exception is said to

catch or handle it

Page 58: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-58

C++ Exceptions

• Throwing exceptions– A throw statement is used to throw an

exceptionthrow ExceptionClass (stringArgument);

Page 59: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-59

C++ Exceptions

• try block– A statement that might throw an exception is

placed within a try block– Syntax

try {

statement(s);

} // end try

Page 60: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-60

C++ Exceptions

• catch block– Used to catch an exception and deal with the

error condition– Syntax

catch (exceptionClass identifier) {

statement(s);

} // end catch

Page 61: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-61

C++ Exceptions

• A programmer-define ListException class #include <exception>#include <string>using namespace std;class ListException: public exception{ public: ListException (const string & message = “”)

: exception(message.c_str()) {}}; // end ListException

Page 62: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-62

C++ Exceptions

• List with Exception handling– 相關檔案:

• ListAexcep.h, • insert.cpp, ( 還不完整 ), • ListException.h,• ListIndexOutOfRangeException.h,

• ListDemo.cpp ( 但要修一下 include 的檔案 )

Page 63: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-63

Summary

• Data abstraction controls the interaction between a program and its data structures

• Abstract data type (ADT): a set of data-management operations together with the data values upon which they operate

• Mathematical study of ADTs uses axioms to specify the behavior of ADT operations

• An ADT should be fully defined before any implementation decisions

Page 64: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-64

Summary

• Hide an ADT’s implementation by defining the ADT as a C++ class

• An object encapsulates both data and operations

• A class contains at least one constructor and a destructor

• The compiler generates default constructor and destructor if none are provided

Page 65: Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 3.1 Abstract Data Types ( 抽象資料型態 ) Modularity –Keeps the

© 2005 Pearson Addison-Wesley. All rights reserved 3-65

Summary

• Members of a class are private by default– Data members are typically private– Public functions can be provided to access them

• Define and implement a class within header and implementation files

• Namespace: a mechanism to group classes, functions, variables, types, and constants

• Use exception handling to throw, catch, and handle errors during program execution