chapter 9 high-level programming languages: c++. chapter goals describe the expectations of high...

27
Chapter 9 High-Level Programming Languages: C++

Upload: arabella-dawson

Post on 15-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

Chapter 9

High-Level Programming Languages: C++

Page 2: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

Chapter Goals• Describe the expectations of high level languages

• Distinguish between functional design and object-oriented design

• Describe the stages of the object-oriented design process

• Apply the object-oriented design process

• Understand how the constructs of top-down and object-oriented design are implemented in programming languages

2

Page 3: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

3

Meeting Expectations

• C++ meets the four expectations for a high-level programming language

• Expectations

– Programmer need not manage details of the movement of data items within memory, nor pay any attention to where they are stored

Page 4: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

4

Meeting Expectations (continued)

• Expectations (continued)

– Programmer can take a macroscopic view of tasks, thinking at a higher level of problem-solving

– Programs written in high-level languages will be portable rather than machine-specific

– Programming statements in a high-level language

• Will be closer to standard English

• Will use standard mathematical notation

Page 5: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

8-5

Functionality of Imperative Languages

• Sequence Executing statements in sequence until an instruction is encountered that changes this sequencing

• Selection Deciding which action to take

• Iteration (looping) Repeating an action

Both selection and iteration require the use of a Boolean expression

Top-Down Approach: a technique that attempts to solve a general problem in a hierarchical way by decomposing it in sub-problems. Each sub-problem is then refined by using a similar decomposition approach until it cannot be further decomposed.s

Page 6: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

6

Object-Oriented Design

Object-oriented Design

A problem-solving methodology that produces a solution to a problem in terms of self-contained entities called objects

Object

A thing or entity that makes sense within the context of the problem

For example, a student, a car, time, date

Page 7: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

7

Object-Oriented Design

World View of OOD

Problems are solved by – isolating the objects in a problem, – determining their properties and actions

(responsibilities), and – letting the objects collaborate to solve a

problem

What? Say again!

Page 8: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

8

Object-Oriented Design

An analogy: You and your friend fix dinner

Objects: you, friend, dinner

Class: you and friend are people

People have name, eye color, …

People can shop, cook, …

Instance of a class: you and friend are instances of class People, you each have your own name and eye color, you each can shop and cook

You collaborate to fix dinner

Page 9: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

9

Object-Oriented Design

Class (or object class)

A description of a group of similar objects

Object (instance of a class)

A concrete example of the class

Classes contain fields that represent the properties (name, eye color) and behaviors (responsibilities) (shop, cook) of the class

Method

A named algorithm that defines behavior (shop, cook)

Page 10: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

10

Object-Oriented Design

Top-Down Design

decomposes problems into tasks

Object-Oriented Design

decomposes problems into

collaborating objects

Yes, but how?

Page 11: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

11

Object-Oriented Design

Steps – isolate the real-world objects in the

problem– abstract the objects with like properties

into groups (classes)– determine the responsibilities of the group

in interacting with other groups

Page 12: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

12

Object-Oriented Design

Think of design as a mapping from real world objects to classes of objects

birth date

marriagedate

dog's birth date

Date class

Objects Classes of objects

Page 13: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

13

Object-Oriented Design

Program World simulates these groups

class Date

dogBirthdate

birthdate

marriageDate

Description Instances

Page 14: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

14

Object-Oriented Design

Date'sActions inreal world

?

We call an object's interactions with other objects its

responsibilities

Create itselfKnow the state of its fields

Compare itself to another dateReturn a date a number of days hence

Page 15: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

15

Object-Oriented Design

Responsibilities become methods in the Program World

class DategetMonthgetDaygetYear

dogBirthdate

birthdate

marriageDate

Page 16: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

16

Class Definition

A class encapsulates both data and actions

public class Person // Name the class

// Declare Class variables

String name

String address

String telephone

String email

Encapsulation The bundling of data and actions in such a way that the logical properties of the data and actions are separated from the implementation details

Data of the class

Page 17: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

17

Class Definition

// Declare Class Methods

Initialize() // Code for Initialize

public Print() // Code for Print

public String GetName()

RETURN name

public String GetAddress()

RETURN address

public String GetEmail()

RETURN email

public String GetTelephone()

RETURN telephone

17

Actions of the class

Page 18: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

18

Class Definition

Figure 9.4 The Class person

18

Example of Object declaration

// Create an object of the class Person

Person person1; // The object is called person1

The class encapsulates both the data

and the actions

Page 19: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

19

Object-Oriented Design Methodology

Four stages to the decomposition process

– Brainstorming to locate possible classes

– Filtering the classes to find duplicates or remove unnecessary ones

– Scenarios are tried to be sure we understand collaborations

– Responsibility algorithms are designed for all actions that classes must exhibit

Page 20: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

20

Scenarios and Responsibilities

Assign responsibilities to each class

There are two types of responsibilities– What a class must know about itself

(knowledge responsibilities)• Knowledge responsibilities usually just return the

contents of one of an object’s variables

– What a class must be able to do (behavior responsibilities)

• Action responsibilities are a little more complicated, often involving calculations

Page 21: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

21

Computer Example

Let’s examine the problem-solving process for creating an address list

Brainstorming and filtering– Circling the nouns and underlining the verbs

is a good way to begin

Page 22: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

22

Responsibility AlgorithmsExample: 2 Responsibility of the Person Class

Initialize

name.initialize()Write "Enter the first and last name; press return.“Get the nameWrite "Enter phone number; press return."Get telephone numberWrite "Enter email address; press return."Get email address

Print

name.print() Write “Name: " + name Write "Telephone number: " + telephoneNumberWrite "Email address: " + emailAddress

Tells name to initialize itself

Tells name to print itself

Page 23: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

23

Functionality of OOPsObject class (problem-solving phase)

An entity or thing that is relevant in the context of a problem

Object class (class) (problem-solving phase)

A description of a group of objects with similar properties and behaviors

Class (implementation phase) A pattern for an object

Object ( implementation phase) An instance of a class

23

Page 24: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

24

Functionality of OOPsEncapsulation

A language feature that enforces information hiding

Classes

Different meanings in different places (See next slide)

Inheritance

A property that allows a class to inherit the data and actions of another class

Polymorphism

A ability to handle the ambiguity of duplicate names

Page 25: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

25

Who am I?

I am best known for structured programming.Can you defineit?I am also knownfor my wit. Canyou recall someof my witty sayings?

Page 26: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

26

Ethical Issues

Gambling on the Internet

Have you ever visited an Internet gambling site?

Should Internet gambling be outlawed?

Should Internet gambling be legalized and regulated?

Should Internet gambling be taxed?

Page 27: Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design

27

Do you know?

How are computers used in tennis tournaments?

What predated functionality of Bytecode?

What does the word "paradigm" mean? Howhas its meaning changed over time?

How many definitions can you think of for "bow"?