cs112 intro to cs ii with c++ introduction. 6/25/2015gene itkis; cs1122 problems and programs...
Post on 21-Dec-2015
214 views
TRANSCRIPT
04/18/23 Gene Itkis; cs112 2
Problems and Programs
Program helps articulate structure of Problem, and maybe even solve it“Model the World”Hence “objects”Functional spec
What
Design specHow
04/18/23 Gene Itkis; cs112 5
“Your world”: Primitive Objects“Built-in”:
Numbers int; long; float; double
CharactersOperators:
+; -; /; *“boxes”: variablesStreams: I/O
cin; coutBring them in: Libraries
#include <stdio.h>; … <iostream.h>;…
04/18/23 Gene Itkis; cs112 6
“Your world”: Creation - new from old
Combining Objects“Simple glue”:
arraysstruct
expressions
Objects from objects:classes
04/18/23 Gene Itkis; cs112 7
“Your world”: Creation ‘ex nihilo’
ConstructorsCreate objects of defined kind (class)
DestructorsClean up after yourself – remove the objects you createdOtherwise: Memory leaks
04/18/23 Gene Itkis; cs112 9
Objects: from Outside and Inside
Top-down approachFrom outside: Interface
Define/design interfaces first and wellInterface defines the objectInterface and its use makes no assumptions about implementation of object
From Inside: Object implementationDepends only on interface, not on how the object will be used
04/18/23 Gene Itkis; cs112 10
Independence and Structure
Easy maintenanceIf object implementation changes (without change of interface) the rest of the program will continue to workObjects can be re-used in ways not originally anticipated (as long as interface is same)
Easy designClarity (“Pictorial”)
04/18/23 Gene Itkis; cs112 11
Everything is an object (example)
Object: operator “+”Integer addition
In:int a, b
Out:int c=a+b
04/18/23 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
04/18/23 Gene Itkis; cs112 13
Continued example – extending
Real numbers addition
In:Float x, y
Out:Float z=x+y
Looks simple…
04/18/23 Gene Itkis; cs112 14
Extending example further…
Character strings concatenation
In:string a, b
Out:string c= a||bc=a+b
04/18/23 Gene Itkis; cs112 16
Another example – bottom up
Atomic objectsNumbers, characters, strings, etc.
Pair (in Lisp: CONS)Using Pair, build
Link-listStackTreeGraph
04/18/23 Gene Itkis; cs112 19
Stack
Collection of elementsCan add, remove elementsLast in – first out (LIFO)
Interface (access methods):Push ( element e, Stack S )Pop ( Stack S ) element eEmpty? (Stack S ) bool empty
04/18/23 Gene Itkis; cs112 20
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
04/18/23 Gene Itkis; cs112 22
StackCollection of elements
Can add, remove elementsLast in – first out (LIFO)
Interface (access methods):Push ( element e, Stack S )Pop ( Stack S ) element eEmpty? (Stack S ) bool empty
Create empty stack
04/18/23 Gene Itkis; cs112 23
Simple List
Interface:Create empty listInsert elementsDelete elementsEmpty?
More complex…Concatenate lists, split, etc.
04/18/23 Gene Itkis; cs112 24
Tree
Binary Tree = Pair ( left sub-tree, right sub-tree )
Internal structure
04/18/23 Gene Itkis; cs112 25
Graph
Graph = List of nodesNode = Pair (node info;
List of adjacent nodes)
04/18/23 Gene Itkis; cs112 26
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), … ) )
04/18/23 Gene Itkis; cs112 27
In and out once againImplementation techniques vs. Objects/Data StructuresObjects & Data Structures
Clear interfaceHidden implementation detailsExamples: Stack, Simple List
Implementation techniquesExamples: Link-list, Tree, Graph, Generic Object
Object from one perspective can be an implementation detail from another