26
CS112 Intro to CS II with C++ Introduction

CS112 Intro to CS II with C++

  • 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

Page 1: CS112 Intro to CS II  with C++

CS112 Intro to CS II with C++

Introduction

Page 2: CS112 Intro to CS II  with C++

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

Page 3: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 3

“Your world”

cin cout

var x

Page 4: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 4

“Your world”: example (p.4)

cin cout

var x var yvar sum

Page 5: CS112 Intro to CS II  with C++

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>;…

Page 6: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 6

“Your world”: Creation - new from old

Combining Objects“Simple glue”:

arraysstructexpressions

Objects from objects:classes

Page 7: CS112 Intro to CS II  with C++

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

Page 8: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 8

ExampleExample from G:pg.4

On blackboard

Page 9: CS112 Intro to CS II  with C++

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

Page 10: CS112 Intro to CS II  with C++

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”)

Page 11: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 11

Everything is an object (example)Object: operator “+”

Integer additionIn:

int a, bOut:

int c=a+b

Page 12: CS112 Intro to CS II  with C++

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

Page 13: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 13

Continued example – extendingReal numbers additionIn:

Float x, yOut:

Float z=x+y

Looks simple…

Page 14: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 14

Extending example further…Character strings concatenation

In:string a, b

Out:string c= a||bc=a+b

Page 15: CS112 Intro to CS II  with C++

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

Page 16: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 17

Link-listLink-list = Pair of an element

and a (smaller) link-list

Page 17: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 18

Link-list (cont.)

RecursiveTermination – nil-object

“Inside view”!

Page 18: CS112 Intro to CS II  with C++

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

Page 19: CS112 Intro to CS II  with C++

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

Page 20: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 21

Life of a Stack

PopPush (3 boxes)

Create empty stack (!!!)

Page 21: CS112 Intro to CS II  with C++

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

Page 22: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 23

Simple ListInterface:

Create empty listInsert elementsDelete elementsEmpty?

More complex…Concatenate lists, split, etc.

Page 23: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 24

TreeBinary Tree = Pair ( left sub-tree, right sub-tree )Internal structure

Page 24: CS112 Intro to CS II  with C++

04/22/23 Gene Itkis; cs112 25

GraphGraph = List of nodesNode = Pair (node info;

List of adjacent nodes)

Page 25: CS112 Intro to CS II  with C++

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), … ) )

Page 26: CS112 Intro to CS II  with C++

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