290 likes | 392 Vues
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.
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
x var “Your world” cin A cout + - 3.14 5 1 b 0 Gene Itkis; cs112
x y sum var var var “Your world”: example (p.4) 6 cin cout 6 1 5 + Gene Itkis; cs112
“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
“Your world”: Creation - new from old Combining Objects • “Simple glue”: • arrays • struct • expressions • Objects from objects: • classes Gene Itkis; cs112
“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
Example • Example from G:pg.4 • On blackboard Gene Itkis; cs112
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
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 • c=a+b 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 = (bu.cas.cs112.a1.2003.fall, ( (name, “Gene Itkis”), (phone, 353-5285), (office, mcs284), (course, cs112), … ) ) 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