230 likes | 252 Views
CS112 Intro to CS II with C++. Introduction. Problems and Programs. Program helps articulate structure of Problem, and maybe even solve it “ Model the World ” Hence “ objects ” Functional spec What Design spec How. Your world. cin. cout. A. +. 3.14. 1. b. 0.
E N D
CS112 Intro to CS II with C++ Introduction
Problems and Programs • Program helps articulate structure of Problem, and maybe even solve it • “Model the World” • Hence “objects” • Functional spec • What • Design spec • How Gene Itkis; cs112
Your world cin cout A + 3.14 1 b 0 Gene Itkis; cs112
Objects: from Outside and Inside • From outside: Interface • Define/design interfaces first and well • Interface defines the object • Interface and its use makes no assumptions about implementation of object • From Inside: Object implementation • Depends only on interface, not on how the object will be used Gene Itkis; cs112
Independence and Structure • Easy maintenance • If object implementation changes (without change of interface) the rest of the program will continue to work • Objects can be re-used in ways not originally anticipated (as long as interface is same) • Easy design • Clarity (“Pictorial”) Gene Itkis; cs112
Everything is an object (example) • Object: operator “+” • Integer addition • In: • int a, b • Out: • int c=a+b Gene Itkis; cs112
Implementation oblivious • You can use “+” without knowing how it is implemented! • E.g. suppose it was implemented as repetitive incrementing (the way children count on their fingers) • When a new implementation based on grade school arithmetic is developed you see only speed improvement Gene Itkis; cs112
Continued example – extending Real numbers addition • In: • Float x, y • Out: • Float z=x+y • Looks simple… Gene Itkis; cs112
Extending example further… • Character strings concatenation • In: • string a, b • Out: • stringc= a||b Gene Itkis; cs112
A different addition • In: • int a, b, m • Out: • int c=(a+b) mod m Gene Itkis; cs112
Another example – bottom up • Atomic objects • Numbers, characters, strings, etc. • Pair(in Lisp: CONS) • Using Pair, build • Link-list • Stack • Tree • Graph Gene Itkis; cs112
Link-list • Link-list = Pair of an element and a (smaller) link-list Gene Itkis; cs112
Link-list (cont.) • Recursive • Termination – nil-object • “Inside view”! Gene Itkis; cs112
Stack • Collection of elements • Can add, remove elements • Last in – first out (LIFO) • Interface (access methods): • Push ( elemente, StackS ) • Pop ( StackS ) elemente • Empty? (StackS) boolempty Gene Itkis; cs112
Stack implementation • Link-list • Empty? ( S ) : S = • Push (e, S): S Pair (e, S) • Pop (S): • Let S = Pair (x, S¯) • Set S S¯; Return x Gene Itkis; cs112
Life of a Stack • Pop • Push (3 boxes) • Create empty stack (!!!) Gene Itkis; cs112
Stack • Collection of elements • Can add, remove elements • Last in – first out (LIFO) • Interface (access methods): • Push ( elemente, StackS ) • Pop ( StackS ) elemente • Empty? (StackS) boolempty • Create empty stack Gene Itkis; cs112
Simple List Interface: • Create empty list • Insert elements • Delete elements • Empty? More complex… • Concatenate lists, split, etc. Gene Itkis; cs112
Tree • Binary Tree = Pair ( left sub-tree, right sub-tree ) • Internal structure Gene Itkis; cs112
Graph • Graph = List of nodes • Node = Pair (node info; List of adjacent nodes) Gene Itkis; cs112
Generic object • Object = Pair ( ID, List of attribute-value pairs ) • Example • Instructor = (cas.cs113.2000.2, ( (name, “Gene Itkis”), (phone, 353-5285), (office, mcs284), … ) ) Gene Itkis; cs112
In and out once again • Implementation techniques vs. Objects/Data Structures • Objects & Data Structures • Clear interface • Hidden implementation details • Examples: Stack, Simple List • Implementation techniques • Examples: Link-list, Tree, Graph, Generic Object • Object from one perspective can be an implementation detail from another Gene Itkis; cs112