L3a_-_ADT_(4_on_1) - Lecture 3a Abstract Data Type 2--- 34...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 3a Abstract Data Type 2--- 34 [CS1102C AY0809S2 Lecture 3a] Lecture Overview ¡ Abstraction in Programs ¡ Abstraction Data Type ¢ Definition ¢ Benefits ¡ Abstraction Data Type Examples 3--- 34 [CS1102C AY0809S2 Lecture 3a] Abstraction ¡ Abstraction: ¢ The process of isolating implementation details and extracting only essential property from an entity ¡ Program = data + algorithms ¡ Abstraction involving a program: ¢ Data abstraction ¡ What operations are needed by the data ¢ Functional abstraction ¡ What is the purpose of a function (algorithm) 4--- 34 [CS1102C AY0809S2 Lecture 3a] Abstract Data Type (ADT) ¡ Abstract Data Type (ADT): ¢ End result of data abstraction ¢ A collection of data together with a set of operations on that data ¢ ADT = Data + Operations ¡ ADT is a language independent concept ¢ Different language supports ADT in different ways ¢ In C++, the class construct is the best match ¡ Important Properties of ADT: ¢ Specification: ¡ The supported operations of the ADT ¢ Implementation: ¡ Data structures and actual coding to meet the specification 5--- 34 [CS1102C AY0809S2 Lecture 3a] ADT : Specification and Implementation ¡ Specification and implementation are disjointed: ¢ One specification ¢ One or more implementations ¡ Using different data structure ¡ Using different algorithm ¡ Users of ADT: ¢ Aware of the specification only ¡ Usage only base on the specified operations ¢ Do not care / Need not know about the actual implementation ¡ i.e. Different implementation do not affect the user 6--- 34 [CS1102C AY0809S2 Lecture 3a] Abstraction as Wall : Illustration ¡ main() needs to know ¢ factorial() ’s purpose ¢ Its parameters and return value ¡ main() does not need to know ¢ factorial() internal coding ¡ Different factorial() coding ¢ Does not affect its users! ¡ We can build a wall to shield factorial() from main() ! int factorial(int n) { if (n == 0) return 1; return n * factorial(n-1); } int factorial(int n) { int i, result = 1; for (i = 2; i <= n; i++) result *= i; return result; } int main() { int fac5; fac5 = factorial(5); ... ... } Implementation 1 Implementation 2 User of factorial( ) 7--- 34 [CS1102C AY0809S2 Lecture 3a] Specification as Slit in the Wall ¡ User only depends on specification int factorial(int n) { ... ... ... } int main() { int fac5; fac5 = factorial(5); ... ... } Implementation User of factorial( ) Request of operation factorial(5) Result of operation 5! = 120 8--- 34 [CS1102C AY0809S2 Lecture 3a] A wall of ADT operations ¡ ADT operations provides: ¢ Interface to data structure ¢ Secure access 9--- 34 [CS1102C AY0809S2 Lecture 3a] Violating the Abstraction ¡ User programs should not : ¢ Use the underlying data structure directly ¢ Depend on implementation details 10--- 34 [CS1102C AY0809S2 Lecture 3a] When do we need ADT?...
View Full Document

Page1 / 9

L3a_-_ADT_(4_on_1) - Lecture 3a Abstract Data Type 2--- 34...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online