Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS112 Intro to CS II with C++ Introduction. 6/25/2015Gene Itkis; cs1122 Problems and Programs Program helps articulate structure of Problem, and maybe.

Similar presentations


Presentation on theme: "CS112 Intro to CS II with C++ Introduction. 6/25/2015Gene Itkis; cs1122 Problems and Programs Program helps articulate structure of Problem, and maybe."— Presentation transcript:

1 CS112 Intro to CS II with C++ Introduction

2 6/25/2015Gene Itkis; cs1122 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

3 6/25/2015Gene Itkis; cs1123 “Your world” cin cout var x

4 6/25/2015Gene Itkis; cs1124 “Your world”: example (p.4) cin cout var x y sum

5 6/25/2015Gene Itkis; cs1125 “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 ; … ;…

6 6/25/2015Gene Itkis; cs1126 “Your world”: Creation - new from old Combining Objects “ Simple glue ” : arrays struct expressions Objects from objects: classes

7 6/25/2015Gene Itkis; cs1127 “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

8 6/25/2015Gene Itkis; cs1128 Example Example from G:pg.4 On blackboard

9 6/25/2015Gene Itkis; cs1129 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

10 6/25/2015Gene Itkis; cs11210 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 ” )

11 6/25/2015Gene Itkis; cs11211 Everything is an object (example) Object: operator “ + ” Integer addition In: int a, b Out: int c=a+b

12 6/25/2015Gene Itkis; cs11212 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

13 6/25/2015Gene Itkis; cs11213 Continued example – extending Real numbers addition In: Float x, y Out: Float z=x+y Looks simple …

14 6/25/2015Gene Itkis; cs11214 Extending example further… Character strings concatenation In: string a, b Out: string c= a||b c=a+b

15 6/25/2015Gene Itkis; cs11215 A different addition In: int a, b, m Out: int c=(a+b) mod m c=(a+b)%m

16 6/25/2015Gene Itkis; cs11216 Another example – bottom up Atomic objects Numbers, characters, strings, etc. Pair (in Lisp: CONS) Using Pair, build Link-list Stack Tree Graph

17 6/25/2015Gene Itkis; cs11217 Link-list Link-list = Pair of an element and a (smaller) link-list

18 6/25/2015Gene Itkis; cs11218 Link-list (cont.) Recursive Termination – nil-object “ Inside view ” !

19 6/25/2015Gene Itkis; cs11219 Stack Collection of elements Can add, remove elements Last in – first out (LIFO) Interface (access methods): Push( element e, Stack S ) Pop ( Stack S )  element e Empty? (Stack S )  bool empty

20 6/25/2015Gene Itkis; cs11220 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

21 6/25/2015Gene Itkis; cs11221 Life of a Stack Pop Push (3 boxes) Create empty stack (!!!)

22 6/25/2015Gene Itkis; cs11222 Stack Collection of elements Can add, remove elements Last in – first out (LIFO) Interface (access methods): Push( element e, Stack S ) Pop ( Stack S )  element e Empty? (Stack S )  bool empty Create  empty stack

23 6/25/2015Gene Itkis; cs11223 Simple List Interface: Create empty list Insert elements Delete elements Empty? More complex … Concatenate lists, split, etc.

24 6/25/2015Gene Itkis; cs11224 Tree Binary Tree = Pair ( left sub-tree, right sub-tree ) Internal structure

25 6/25/2015Gene Itkis; cs11225 Graph Graph = List of nodes Node = Pair (node info; List of adjacent nodes)

26 6/25/2015Gene Itkis; cs11226 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), … ) )

27 6/25/2015Gene Itkis; cs11227 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


Download ppt "CS112 Intro to CS II with C++ Introduction. 6/25/2015Gene Itkis; cs1122 Problems and Programs Program helps articulate structure of Problem, and maybe."

Similar presentations


Ads by Google