1 / 22

CS112 Intro to CS II with C++

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.

Download Presentation

CS112 Intro to CS II with C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS112 Intro to CS II with C++ Introduction

  2. 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

  3. Your world cin cout A + 3.14 1 b 0 Gene Itkis; cs112

  4. 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

  5. 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

  6. Everything is an object (example) • Object: operator “+” • Integer addition • In: • int a, b • Out: • int c=a+b Gene Itkis; cs112

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

  8. Continued example – extending Real numbers addition • In: • Float x, y • Out: • Float z=x+y • Looks simple… Gene Itkis; cs112

  9. Extending example further… • Character strings concatenation • In: • string a, b • Out: • stringc= a||b Gene Itkis; cs112

  10. A different addition • In: • int a, b, m • Out: • int c=(a+b) mod m Gene Itkis; cs112

  11. 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

  12. Link-list • Link-list = Pair of an element and a (smaller) link-list Gene Itkis; cs112

  13. Link-list (cont.) • Recursive • Termination – nil-object • “Inside view”! Gene Itkis; cs112

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

  15. 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

  16. Life of a Stack • Pop • Push (3 boxes) • Create empty stack (!!!) Gene Itkis; cs112

  17. 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

  18. Simple List Interface: • Create empty list • Insert elements • Delete elements • Empty? More complex… • Concatenate lists, split, etc. Gene Itkis; cs112

  19. Tree • Binary Tree = Pair ( left sub-tree, right sub-tree ) • Internal structure Gene Itkis; cs112

  20. Graph • Graph = List of nodes • Node = Pair (node info; List of adjacent nodes) Gene Itkis; cs112

  21. 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

  22. 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

More Related