1 c++ plus data structures nell dale david teague chapter 1 software engineering principles slides...

41
1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus

Upload: liliana-tate

Post on 05-Jan-2016

270 views

Category:

Documents


12 download

TRANSCRIPT

Page 1: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

1

C++ Plus Data Structures

Nell Dale

David Teague

Chapter 1

Software Engineering Principles

Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus

Page 2: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

2

Programming Life Cycle Activities• Problem analysis understand the problem

• Requirements definition specify what program will do

• High- and low-level design how it meets requirements

• Implementation of design code it

• Testing and verification detect errors, show correct

• Delivery turn over to customer

• Operation use the program

• Maintenance change the program

Page 3: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

3

Software Engineering

• A disciplined approach to the design, production, and maintenance of computer programs

• that are developed on time and within cost estimates,

• using tools that help to manage the size and complexity of the resulting software products.

Page 4: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

4

An Algorithm Is . . .

• A logical sequence of discrete steps that describes a complete solution to a given problem computable in a finite amount of time.

Page 5: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

5

Goals of Quality Software

• It works.

• It can be read and understood.

• It can be modified.

• It is completed on time and within budget.

Page 6: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

6

Detailed Program Specification

• Tells what the program must do, but not how it does it.

• Is written documentation about the program.

Page 7: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

7

Detailed Program Specification Includes

• Inputs

• Outputs

• Processing requirements

• Assumptions

Page 8: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

8

Abstraction

• A model of a complex system that includes only the details essential to the perspective of the viewer of the system.

Page 9: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

9

Information Hiding

• Hiding the details of a function or data structure with the goal of controlling access to the details of a module or structure.

PURPOSE: To prevent high-level designs from depending on low-level design details that may be changed.

Page 10: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

10

Two Approaches to Building Manageable Modules

Divides the problem into more easily handled subtasks, until the functional modules (subproblems) can be coded.

Identifies various objects composed of data and operations, that can be used together to solve the problem.

FUNCTIONALDECOMPOSITION

OBJECT-ORIENTED DESIGN

FOCUS ON: processes FOCUS ON: data objects

Page 11: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

11

FindWeighted Average

PrintWeighted Average

Functional Design Modules

Main

Print Data

Print Heading

Get DataPrepare File for Reading

Page 12: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

12

Object-Oriented DesignA technique for developing a program in which

the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data.

Private data

<<

setf...

Private data

>>

get...

ignore

cin cout

Page 13: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

13

More about OOD

Languages supporting OOD include: C++, Java, Smalltalk, Eiffel, and Object-Pascal.

A class is a programmer-defined data type and objects are variables of that type.

In C++, cin is an object of a data type (class) named istream, and cout is an object of a class ostream. Header files iostream and fstream contain definitions of stream classes.

Page 14: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

14

Procedural vs. Object-Oriented Code

“Read the specification of the software you want to build. Underline the verbs if you are after procedural code, the nouns if you aim for an object-oriented program.”

Grady Booch, “What is and Isn’t Object Oriented Design,”

1989.

Page 15: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

15

• Program Verification is the process of determining the degree to which a software product fulfills its specifications.

Program Verification

PROGRAM

SPECIFICATIONS

Inputs

Outputs

Processing Requirements

Assumptions

Page 16: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

16

Verification vs. Validation

Program verification asks,

“Are we doing the job right?”

Program validation asks,

“Are we doing the right job?”

B. W. Boehm, Software Engineering Economics, 1981.

Page 17: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

17

Program Testing

• Testing is the process of executing a program with various data sets designed to discover errors.

DATA SET 1

DATA SET 2

DATA SET 3

DATA SET 4

. . .

Page 18: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

18

Keyboard and Screen I/O #include <iostream>

using namespace std;

cin

(of type istream)

cout

(of type ostream)

Keyboard Screenexecutingprogram

input data output data

Page 19: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

19

namespace

• In slides that follow, assume the statement:

using namespace std;

• We explain namespace in Chapter 2

Page 20: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

20

<iostream> is header file

• for a library that defines 3 objects

• an istream object named cin (keyboard)

• an ostream object named cout (screen)

• an ostream object named cerr (screen)

Page 21: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

21

Insertion Operator ( << )

• The insertion operator << takes 2 operands.

• The left operand is a stream expression, such as cout.

• The right operand is an expression describing what to insert into the output stream. It may be of simple type, or a string, or a manipulator (like endl).

Page 22: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

22

Extraction Operator ( >> )• Variable cin is predefined to denote an input

stream from the standard input device ( the keyboard ).

• The extraction operator >> called “get from” takes 2 operands. The left operand is a stream expression, such as cin. The right operand is a variable of simple type.

• Operator >> attempts to extract the next item from the input stream and store its value in the right operand variable.

Page 23: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

23

Extraction Operator >>

“skips” (reads but does not store anywhere) leading whitespace characters (blank, tab, line feed, form feed, carriage return) before extracting the input value from the

stream (keyboard or file). To avoid skipping, use function get to read the

next character in the input stream.

cin.get(inputChar);

Page 24: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

#include <iostream>using namespace std;int main( ){ // USES KEYBOARD AND SCREEN I/O

int partNumber;float unitPrice;

cout << “Enter part number followed by return : “ << endl ; // prompt

cin >> partNumber ;cout << “Enter unit price followed by return : “

<< endl ;cin >> unitPrice ;cout << “Part # “ << partNumber // echo << “at Unit Cost: $ “ << unitPrice << endl ;

return 0;} 24

Page 25: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

25

Disk files for I/O

your variable

(of type ifstream)

your variable

(of type ofstream)

disk file“A:\myInfile.dat”

disk file“A:\myOut.dat”

executingprogram

input data output data

#include <fstream>

Page 26: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

26

For File I/O• use #include <fstream>• choose valid variable identifiers for your files and

declare them

• open the files and associate them with disk names

• use your variable identifiers with >> and <<

• close the files

Page 27: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

27

Statements for using file I/O

#include <fstream>

using namespace std;

ifstream myInfile; // declarations

ofstream myOutfile;

myInfile.open(“A:\\myIn.dat”); // open files

myOutfile.open(“A:\\myOut.dat”);

myInfile.close( ); // close files

myOutfile.close( );

Page 28: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

28

What does opening a file do?

• associates the C++ identifier for your file with the physical (disk) name for the file

• if the input file does not exist on disk, open is not successful

• if the output file does not exist on disk, a new file with that name is created

• if the output file already exists, it is erased

• places a file reading marker at the very beginning of the file, pointing to the first character in it

Page 29: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

#include <fstream>using namespace std;int main( ){ // USES FILE I/O

int partNumber;float unitPrice;ifstream inFile; // declare file variablesofstream outFile;

inFile.open(“input.dat”); //open filesoutFile.open(“output.dat”);

inFile >> partNumber ;inFile >> unitPrice ;outFile << “Part # “ << partNumber // echo << “at Unit Cost: $ “ << unitPrice << endl ;

return 0;}

29

Page 30: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

30

Stream Failure

• When a stream enters the fail state, further I/O operations using that stream are ignored. But the computer does not automatically halt the program or give any error message.

• Possible reasons for entering fail state include: • invalid input data (often the wrong type),• opening an input file that doesn’t exist,• opening an output file on a diskette that is already

full or is write-protected.

Page 31: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

#include <fstream>#include <iostream>using namespace std;int main( ){ // CHECKS FOR STREAM FAIL

STATE

ifstream inFile;inFile.open(“input.dat”); // try to open fileif ( !inFile ){

cout << “File input.dat could not be opened.”; return 1;}

. . . return 0;}

31

Page 32: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

32

Various Types of Errors

• Design errors occur when specifications are wrong

• Compile errors occur when syntax is wrong

• Run-time errors result from incorrect assumptions, incomplete understanding of the programming language, or unanticipated user errors.

Page 33: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

33

Robustness

• Robustness is the ability of a program to recover following an error; the ability of a program to continue to operate within its environment.

Page 34: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

34

An Assertion

• Is a logical proposition that is either true or false (not necessarily in C++ code).

EXAMPLES

studentCount is greater than 0

sum is assigned && count > 0

response has value ‘y’ or ‘n’

partNumber == 5467

Page 35: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

35

Preconditions and Postconditions

• The precondition is an assertion describing what a function requires to be true before beginning execution.

• The postcondition describes what must be true at the moment the function finishes execution.

• The caller is responsible for ensuring the precondition, and the function code must ensure the postcondition. FOR EXAMPLE . . .

Page 36: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

void PrintList ( ofstream& dataFile, UnsortedType list)// Pre: list has been initialized.// dataFile is open for writing.// Post: Each component in list has been written to dataFile.// dataFile is still open.{ using namespace std;

int length;ItemType item;

list.ResetList();length = list.LengthIs();for (int counter = 1; counter <= length; counter++){

list.GetNextItem(item);item.Print(dataFile);

}}

36

Page 37: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

37

Another Examplevoid Getroots (float a, float b, float c,

float& root1, float& root2 )// Pre: a, b, and c are assigned.// a is non-zero, b*b - 4*a*c is non-zero.// Post: root1 and root2 are assigned // root1 and root2 are roots of quadratic with coefficients a, b,

c{

using namespace std; float temp; temp = b * b - 4.0 * a * c;

root1 = (-b + sqrt(temp) ) / ( 2.0 * a );

root2 = (-b - sqrt(temp) ) / ( 2.0 * a ); return;

}

Page 38: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

38

A Walk-Through

• Is a verification method using a team to perform a manual simulation of the program or design, using sample test inputs, and keeping track of the program’s data by hand.

• Its purpose is to stimulate discussion about the programmer’s design or implementation .

Page 39: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

39

Tasks within each test case:

• determine inputs that demonstrate the goal.

• determine the expected behavior for the input.

• run the program and observe results.

• compare expected behavior and actual behavior. If they differ, we begin debugging.

Page 40: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

40

Integration Testing

• Is performed to integrate program modules that have already been independently unit tested.

FindWeighted Average

PrintWeighted Average

Main

Print Data

Print Heading

Get DataPrepare File for Reading

Page 41: 1 C++ Plus Data Structures Nell Dale David Teague Chapter 1 Software Engineering Principles Slides by Sylvia Sorkin, Community College of Baltimore County

41

Integration Testing Approaches

Ensures correct overall design logic.

Ensures individual moduleswork together correctly, beginning with the lowest level.

TOP-DOWN BOTTOM-UP

USES: placeholder USES: a test driver to callmodule “stubs” to test the functions being tested.the order of calls.