Upload
varian
View
52
Download
5
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
CS112 Intro to CS II with C++
Introduction
04/22/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
WhatDesign spec
How
04/22/23 Gene Itkis; cs112 3
“Your world”
cin cout
var x
04/22/23 Gene Itkis; cs112 4
“Your world”: example (p.4)
cin cout
var x var yvar sum
04/22/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/22/23 Gene Itkis; cs112 6
“Your world”: Creation - new from old
Combining Objects“Simple glue”:
arraysstructexpressions
Objects from objects:classes
04/22/23 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 createdOtherwise: Memory leaks
04/22/23 Gene Itkis; cs112 8
ExampleExample from G:pg.4
On blackboard
04/22/23 Gene Itkis; cs112 9
Objects: from Outside and InsideTop-down approach
From outside: InterfaceDefine/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/22/23 Gene Itkis; cs112 10
Independence and StructureEasy maintenance
If 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/22/23 Gene Itkis; cs112 11
Everything is an object (example)Object: operator “+”
Integer additionIn:
int a, bOut:
int c=a+b
04/22/23 Gene Itkis; cs112 12
Implementation obliviousYou 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/22/23 Gene Itkis; cs112 13
Continued example – extendingReal numbers additionIn:
Float x, yOut:
Float z=x+y
Looks simple…
04/22/23 Gene Itkis; cs112 14
Extending example further…Character strings concatenation
In:string a, b
Out:string c= a||bc=a+b
04/22/23 Gene Itkis; cs112 16
Another example – bottom upAtomic objects
Numbers, characters, strings, etc.Pair (in Lisp: CONS)Using Pair, build
Link-listStackTreeGraph
04/22/23 Gene Itkis; cs112 17
Link-listLink-list = Pair of an element
and a (smaller) link-list
04/22/23 Gene Itkis; cs112 18
Link-list (cont.)
RecursiveTermination – nil-object
“Inside view”!
04/22/23 Gene Itkis; cs112 19
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
04/22/23 Gene Itkis; cs112 20
Stack implementationLink-list
Empty? ( S ) : S = Push (e, S): S Pair (e, S)Pop (S):
Let S = Pair (x, S¯)Set S S¯; Return x
04/22/23 Gene Itkis; cs112 21
Life of a Stack
PopPush (3 boxes)
Create empty stack (!!!)
04/22/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/22/23 Gene Itkis; cs112 23
Simple ListInterface:
Create empty listInsert elementsDelete elementsEmpty?
More complex…Concatenate lists, split, etc.
04/22/23 Gene Itkis; cs112 24
TreeBinary Tree = Pair ( left sub-tree, right sub-tree )Internal structure
04/22/23 Gene Itkis; cs112 25
GraphGraph = List of nodesNode = Pair (node info;
List of adjacent nodes)
04/22/23 Gene Itkis; cs112 26
Generic objectObject = 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/22/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