1 / 26

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. x. var. “Your world”. cin. A. cout. +. -. 3.14. 5. 1. b. 0.

varian
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. x var “Your world” cin A cout + - 3.14 5 1 b 0 Gene Itkis; cs112

  4. x y sum var var var “Your world”: example (p.4) 6 cin cout 6 1 5 + Gene Itkis; cs112

  5. “Your world”: Primitive Objects • “Built-in”: • Numbers • int; long; float; double • Characters • Operators: • +; -; /; * • “boxes”: variables • Streams: I/O • cin; cout • Bring them in: Libraries • #include <stdio.h>; … <iostream.h>;… Gene Itkis; cs112

  6. “Your world”: Creation - new from old Combining Objects • “Simple glue”: • arrays • struct • expressions • Objects from objects: • classes Gene Itkis; cs112

  7. “Your world”: Creation ‘ex nihilo’ • Constructors • Create objects of defined kind (class) • Destructors • Clean up after yourself – remove the objects you created • Otherwise: Memory leaks Gene Itkis; cs112

  8. Example • Example from G:pg.4 • On blackboard Gene Itkis; cs112

  9. Objects: from Outside and Inside Top-down approach • 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  25. Generic object • Object = Pair ( ID, List of attribute-value pairs ) • Example • Instructor = (bu.cas.cs112.a1.2003.fall, ( (name, “Gene Itkis”), (phone, 353-5285), (office, mcs284), (course, cs112), … ) ) Gene Itkis; cs112

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