Chapter 2 Data Design and Implementation Outline Different Views - - PowerPoint PPT Presentation

chapter 2 data design and implementation outline
SMART_READER_LITE
LIVE PREVIEW

Chapter 2 Data Design and Implementation Outline Different Views - - PowerPoint PPT Presentation

Chapter 2 Data Design and Implementation Outline Different Views of Data: logical, application and implementation ADT C++ built-in types and Class type, OOP review Exception Management Algorithms comparison 2 Data


slide-1
SLIDE 1

Chapter 2
 Data Design and Implementation

slide-2
SLIDE 2

2

Outline

  • Different Views of Data: logical,

application and implementation

  • ADT
  • C++ built-in types and Class type, OOP

review

  • Exception Management
  • Algorithms comparison
slide-3
SLIDE 3
  • Representation of information in a

manner suitable for communication or analysis by humans or machines

  • Data are nouns of programming world:
  • The objects that are manipulated
  • The information that is processed

Data

slide-4
SLIDE 4

Abstract Data Type (ADT)

  • A data type whose (logical) properties (domain

and operations) are specified independently of any particular implementation.

  • Different views of ADT:
  • Application (or user) level: modeling real-life

data in a specific context.

  • Logical (or ADT) level: abstract view of the

domain and operations. WHAT

  • Implementation level: specific representation to

hold the data items, and implementation of for

  • perations. HOW
slide-5
SLIDE 5

Example: ADT List

  • (Application or user level) modeling real-life

list, a homogeneous collection of elements with a linear relation.

  • there is one first element,
  • every element except first one has a unique

predecessor

  • every element except last one has a unique

successor

  • (Logical level) operations supported: PutItem,

GetItem, DeleteItem, GetNext, …

  • (Implementation level) implemented using

array, linked list, or other; codes for

  • perations
slide-6
SLIDE 6

Basic ADT Operations

  • Constructor -- creates a new instance (object)
  • f an ADT.
  • Transformer -- changes state of one or more of

the data values of an instance.

  • Observer -- allows us to observe the state of
  • ne or more of the data values without

changing them.

  • Iterator -- allows us to process all the

components in a data structure sequentially.

slide-7
SLIDE 7

C++ Data Types

Composite

array struct union class

Address

pointer reference

Simple

Integral Floating char short int long enum float double long double

slide-8
SLIDE 8

C++ Data Type int

Value range: INT_MIN . . INT_MAX Operations: + prefix

  • prefix

+ infix

  • infix

* infix / infix % infix Relational Operators infix

TYPE int

encapsulated inside the box is implementation details Representation of int as 16 bits two’s complement and Implementation of Operations

slide-9
SLIDE 9

Communication between Application Level and Implementation Level

  • Application (or user) level:

Library of Congress, or Baltimore County Public Library.

  • Logical (or ADT) level:

domain is a collection of books; operations include: check book out, check book in, pay fine, reserve a book.

  • Implementation level:

representation of the structure to hold “books”, and the coding for

  • perations.
slide-10
SLIDE 10

10

Outline

  • Different Views of Data: logical,

application and implementation

  • ADT
  • Review: C++ built-in types and Class

type, OOP review

  • Exception Management
  • Algorithms comparison
slide-11
SLIDE 11

C++ Data Types

Composite

array struct union class

Address

pointer reference

Simple

Integral Floating char short int long enum float double long double

slide-12
SLIDE 12

Composite Data Types

Components are not

  • rganized with respect to
  • ne another.

e.g., struct and classes

Organization of components

determines method used to access individual components. e.g., arrays

UNSTRUCTURED STRUCTURED

stores a collection of individual data components under one variable name, and allows individual components to be accessed.

slide-13
SLIDE 13

Records (struct in C++)

  • A record: a composite data type made up of a

finite collection of often times heterogeneous elements, called members or fields.

  • to access individual member:

recordVar.fieldIdentifier e.g., thisCar.year = 2008;

.year 2008 .maker ‘h’ ‘o’ ‘n’ ‘d’ ‘a’ ‘\0’ . . . .price 18678.92 thisCar

struct CarType { int year; char maker[10]; float price; }; CarType thisCar; //CarType variables

slide-14
SLIDE 14

Composite type variables

  • Can be assigned
  • each fields/members are assigned
  • thatCar = thisCar;
  • thatCar will be a bit-by-bit copy of thisCar
  • Can be passed as a parameter to a function (either by value or by

reference)

  • Can be returned from a function, e.g.,

//Prompts user to enter year, price, and maker of a car // return a CarType value CarType ReadUserInput();

struct CarType { int year; char maker[10]; float price; }; CarType thisCar; //CarType variables

CarType thatCar;

slide-15
SLIDE 15

Pass-by-value

CALLING BLOCK FUNCTION
 CALLED sends a copy

  • f the contents of

the actual argument SO, actual argument cannot be changed by the function.

slide-16
SLIDE 16

Pass struct type by value

bool LateModel(CarType car, int date) // Returns true if the car’s model year is later than // or equal to date; returns false otherwise. { return ( car.year >= date ) ; }; SAMPLE CALL myCar.year=2000; myCar.price = 12,000; if ( LateModel(myCar, 1995) ) std::cout << myCar.price << std::endl; Can we modify car in LateModel?

slide-17
SLIDE 17

Pass-by-reference

sends the location (memory address)

  • f the actual argument

function access actual argument itself (not a copy) CALLING BLOCK FUNCTION
 CALLED

slide-18
SLIDE 18

Using struct type 
 Reference Parameter to change a member

void AdjustForInflation(CarType& car, float perCent) // Increases price by the amount specified in perCent { car.price = car.price * perCent + car.price; }; SAMPLE CALL AdjustForInflation(myCar, 0.03);

slide-19
SLIDE 19

C++ Data Types

Composite

array struct union class

Address

pointer reference

Simple

Integral Floating char short int long enum float double long double

slide-20
SLIDE 20

One-Dimensional Array

  • A one-dimensional array is a structured composite data

type made up of a finite, fixed size collection of homogeneous (all of same data type) elements

  • “structured”: elements have relative positions (index)
  • There is direct access to each element

Array operations (creation, storing a value, retrieving a value) are performed using a declaration and indexes.

int a[20]; CarType carFleet[100]; a[0]=34;

slide-21
SLIDE 21

One-Dimensional Arrays in C++

  • True or False?
  • Arrays cannot be assigned one to another, and

cannot be the return type of a function.

slide-22
SLIDE 22

Array Implementation

C++ array elements are stored in a contiguous memory block with a base address (lowest address) and size = sizeOfElement*arrayLen;

float values[5]; // assume element size is 4 bytes

Base Address values[0] values[1] values[2] values[3] values[4] 7000 7004 7008 7012 7016 Indexes

slide-23
SLIDE 23

Array Implementation: accessing element

float values[5]; // assume element size is 4 bytes

Address of values[Index]? 
 Address(Index) = BaseAddress + Index * SizeOfElement What is address of values[0]? values[2]? Base Address values[0] values[1] values[2] values[3] values[4] 7000 7004 7008 7012 7016 Indexes

slide-24
SLIDE 24

Passing Arrays as Parameters

  • In C++, arrays are always passed by

reference, and & is not used with formal parameter type.

  • Whenever an array is passed as an argument,

its base address is sent to the called function.

  • This means function can modify array
  • you can protect array from unintentional

changes by using const in formal parameter list and function prototype.

slide-25
SLIDE 25

float SumValues (const float values[ ], 
 numOfValues) // Pre: values[ 0] through values[numOfValues-1] // have been assigned // Returns the sum of values[0] through // values[numOfValues-1] { float sum = 0; for ( int index = 0; index < numOfValues; index++ ) { sum = values [index] + sum; } return sum; }

slide-26
SLIDE 26

Two-Dimensional Array 
 at the Logical Level

  • A two-dimensional array is a structured composite

data type made up of a finite, fixed size collection of homogeneous elements having relative positions given by a pair of indexes and to which there is direct access.

  • Array operations (creation, storing a value, retrieving a

value) are performed using a declaration and a pair

  • f indexes (called row and column) representing the

component’s position in each dimension.

slide-27
SLIDE 27

EXAMPLE -- To keep monthly high temperatures for 50 states in a two-dimensional array.

const int NUM_STATES = 50 ; const int NUM_MONTHS = 12 ; int stateHighs [ NUM_STATES ] [ NUM_MONTHS ] ; [ 0 ] [ 1 ] [ 2 ] . . stateHighs [2] [7] . [ 48 ] [ 49 ]

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

66 64 72 78 85 90 99 115 98 90 88 80 row 2, col 7 might be Arizona’s high for August

How to Find the average high temperature for Arizona?

slide-28
SLIDE 28

const int NUM_STATES = 50 ;
 const int NUM_MONTHS = 12 ;
 int stateHighs [ NUM_STATES ] [ NUM_MONTHS ] ;

  • In memory, C++ stores 2D arrays in row order:

the first row is followed by the second row, etc.

12 highs for state 0 12 highs for state 1 etc. Alabama Alaska first row second row 8000 8024 8048 Base Address

STORAGE

. . . rows columns

slide-29
SLIDE 29

Implementation Level View

stateHighs[ 0 ] [ 0 ] stateHighs[ 0 ] [ 1 ] stateHighs[ 0 ] [ 2 ] stateHighs[ 0 ] [ 3 ] stateHighs[ 0 ] [ 4 ] stateHighs[ 0 ] [ 5 ] stateHighs[ 0 ] [ 6 ] stateHighs[ 0 ] [ 7 ] stateHighs[ 0 ] [ 8 ] stateHighs[ 0 ] [ 9 ] stateHighs[ 0 ] [10 ] stateHighs[ 0 ] [11 ] stateHighs[ 1 ] [ 0 ] stateHighs[ 1 ] [ 1 ] stateHighs[ 1 ] [ 2 ] stateHighs[ 1 ] [ 3 ] . . .

To locate an element such as stateHighs [ 2 ] [ 7] the compiler needs to know that there are 12 columns in this two-dimensional array. At what address will stateHighs [ 2 ] [ 7 ] be found? baseAddress+(2*12+7)*2 There are 2*12+7 elements before it

Assume 2 bytes for type int. Base Address 8000

slide-30
SLIDE 30

Two-Dimensional Array Parameters

  • Just as with a one-dimensional array, when a

two- (or higher) dimensional array is passed as a parameter, base address of array is sent to the function.

  • size of all dimensions except the first must be

included in function heading and prototype.

  • sizes of those dimensions for the formal

parameter must be exactly the same as in the actual array.

slide-31
SLIDE 31

void findAverages (const int stateHighs [ ] [ NUM_MONTHS], 
 int stateAverages [ ]) // Pre: stateHighs[ 0..NUM_STATES-1] [ 0..NUM_MONTHS-1] // assigned // Post: stateAverages[ 0..NUM_STATES-1 ] contains rounded // high temperature for each state { int state; int month; float total; for ( state = 0 ; state < NUM_STATES; state++ ) { total = 0.0; for ( month = 0 ; month < NUM_MONTHS ; month++ ) total = stateHighs [ state ][ month ] + total; stateAverages [ state ] = total / 12.0 + 0.5; } } Use the two-dimensional stateHighs array to fill a 


  • ne-dimensional stateAverages array
slide-32
SLIDE 32

Using typedef with arrays

helps eliminate chances of size mismatches between formal and actual parameters.

typedef int StateHighsType [ NUM_STATES ][ NUM_MONTHS ]; typedef float StateAveragesType [ NUM_STATES ]; void findAverages( const StateHighsType stateHighs, StateAveragesType stateAverages ) { . . . }

slide-33
SLIDE 33

Declaring Multidimensional Arrays

EXAMPLE USING TYPEDEF

const int NUM_DEPTS = 5; // mens, womens, childrens, electronics, linens const int NUM_MONTHS = 12 ; const int NUM_STORES = 3 ; // White Marsh, Owings Mills, Towson typedef long MonthlySalesType [NUM_DEPTS] [NUM_MONTHS] [NUM_STORES]; MonthlySalesType monthlySales;

slide-34
SLIDE 34

41 41

const int NUM_DEPTS = 5; // mens, womens, childrens, electronics, linens const int NUM_MONTHS = 12 ; const int NUM_STORES = 3 ; // White Marsh, Owings Mills, Towson typedef long MonthlySalesType [NUM_DEPTS] [NUM_MONTHS] [NUM_STORES] ; MonthlySalesType monthlySales;

monthlySales[3][7][0]

sales for electronics in August at White Marsh 12 MONTHS columns

5 DEPTS rows 3 S T O R E S s h e e t s

slide-35
SLIDE 35

C++ Data Types

Composite

array struct union class

Address

pointer reference

Simple

Integral Floating char short int long enum float double long double

slide-36
SLIDE 36

C++ class data type

  • A class is an unstructured type that

encapsulates a fixed number of data components (data members) with the functions (called member functions) that manipulate them.

  • predefined operations on an instance of a

class are whole assignment and component access.

slide-37
SLIDE 37

class DateType Specification

// SPECIFICATION FILE ( datetype.h ) class DateType // declares a class data type { public : // 4 public member functions void Initialize (int newMonth, int newDay, int newYear ) ; int GetYear( ) const ; // returns year int GetMonth( ) const ; // returns month int GetDay( ) const ; // returns day private : // 3 private data members int year ; int month ; int day ; };

; must be there!!!

slide-38
SLIDE 38

Use of C++ data type class

  • Variables of a class type are called objects (or

instances) of that particular class.

  • Software that declares and uses objects of the

class is called a client.

  • Client code uses public member functions (called

methods in OOP) to handle its class objects.

  • - means calling a public member function.
slide-39
SLIDE 39

Client Code Using DateType

#include “datetype” // includes specification of the class using namespace std; int main ( void ) { DateType startDate; // declares 2 objects of DateType DateType endDate; bool retired = false; startDate.Initialize ( 6, 30, 1998 ); endDate.Initialize ( 10, 31, 2002 ); cout << startDate.MonthIs( ) << “/” << startDate.DayIs( ) << “/” << startDate.YearIs( ) << endl; while ( ! retired ) { // finishSomeTask } }

slide-40
SLIDE 40

2 separate files generally 
 used for class type

// SPECIFICATION FILE ( datetype .h ) // Specifies the data and function members. class DateType

{

public:

. . .

private: . . .

} ;

// IMPLEMENTATION FILE ( datetype.cpp ) // Implements the DateType member functions.

. . .

slide-41
SLIDE 41

DateType Class Instance Diagrams

Initialize GetYear GetMonth GetDay

startDate endDate

Private data: year month day

2002 10 31 Initialize GeytYear GetMonth GetDay 1998 6 30

Private data: year month day

slide-42
SLIDE 42

Implementation of DateType member functions

// IMPLEMENTATION FILE (datetype.cpp) #include “datetype.h” // also must appear in client code void DateType :: Initialize ( int newMonth, int newDay, int newYear ) // Post: year is set to newYear. // month is set to newMonth. // day is set to newDay. { year = newYear; month = newMonth; day = newDay; }

slide-43
SLIDE 43

49

int DateType :: GetMonth ( ) const // Accessor function for data member month { return month; } int DateType :: GetYear ( ) const // Accessor function for data member year { return year; } int DateType :: GetDay ( ) const // Accessor function for data member day { return day; }

slide-44
SLIDE 44

Familiar Class Instances 
 and Member Functions

  • member selection operator ( . ) selects either data

members or member functions.

  • Header files iostream and fstream declare istream,
  • stream,and ifstream, ofstream I/O classes.
  • Both cin and cout are class objects and get and ignore are

member functions.

cin.get (someChar); cin.ignore (100, ‘\n’);

  • These statements declare myInfile as an instance of class

ifstream and invoke member function open.

ifstream myInfile ; myInfile.open ( “mydata.dat” );

slide-45
SLIDE 45

Scope Resolution Operator ( :: )

  • C++ programs typically use several class types.
  • Different classes can have member functions with the

same identifier, like Write( ).

  • Member selection operator is used to determine the

class whose member function Write( ) is invoked.

currentDate.Write( ) ; // class DateType numberZ.Write( ) ; // class ComplexNumberType

  • In implementation file, scope resolution operator is used

in heading before function’s name to specify its class.

void DateType :: Write ( ) const

{ . . . }

slide-46
SLIDE 46

Inheritance

slide-47
SLIDE 47

Object-Oriented Programming

  • Three inter-related constructs: classes,
  • bjects, and inheritance
  • Objects are basic run-time entities in an
  • bject-oriented system.
  • A class defines the structure of its
  • bjects.
  • Classes are organized in an “is-a”

hierarchy defined by inheritance.

slide-48
SLIDE 48

Inheritance

  • Allows programmers to create a new

class that is a specialization of an existing class.

  • new class is called a derived class of the

existing class

  • the existing class is the base class of the

new class.

slide-49
SLIDE 49

Inheritance

  • Inheritance fosters reuse by allowing an

application to take an already-tested class and derive a class from it that inherits the properties the application needs

  • Polymorphism: the ability of a language to

have duplicate method names in an inheritance hierarchy and to apply the method that is appropriate for the object to which the method is applied

slide-50
SLIDE 50

Inheritance

  • Inheritance and

polymorphism combined allow the programmer to build useful hierarchies of classes that can be reused in different applications

  • Mapping of problem

into solution

slide-51
SLIDE 51

# include <string> class MoneyType { public: void Initialize(long, long); long DollarsAre( ) const; long CentsAre( ) const; private: long dollars; long cents; };

slide-52
SLIDE 52

class ExtMoneyType:public MoneyType { public: string CurrencyIs( ); void Initialize(long, long, const string); private: string currency; }; ExtMoneyType extMoney; void ExtMoneyType::Initialize (long newDollars, long newCents, string newCurrency) { currency = newCurrency; MoneyType::Initialize(newDollars, newCents); } String ExtMoneyType::CurrencyIs() const { return currency; }

slide-53
SLIDE 53

53

Outline

  • Different Views of Data: logical,

application and implementation

  • data abstraction, data encapsulation
  • data structure
  • ADT
  • C++ built-in types and Class type, OOP

review

  • Exception management
  • Algorithms comparison
slide-54
SLIDE 54

Exceptions

  • An exception is an unusual situation that
  • ccurs when the program is running.
  • Exception Management
  • Define the error condition
  • Enclose code containing possible error (try).
  • Alert the system if error occurs (throw).
  • Handle error if it is thrown (catch).
slide-55
SLIDE 55

try, catch, and throw

Try { // code that contains a possible error … throw string(“An error has occurred in function …”); } Catch (string message) { std::cout << message << std::endl; return 1; }

slide-56
SLIDE 56

try { infile >> value; do { if (value < 0) throw string("Negative value"); sum = sum + value; } while (infile); } catch (string message) // Parameter of the catch is type string { // Code that handles the exception cout << message << " found in file. Program aborted." return 1; } // Code to continue processing if exception not thrown cout << "Sum of values on the file: " << sum;

Exits loop and goes to catch

slide-57
SLIDE 57

Namespace

namespace mySpace { // All variables and // functions within this // block must be accessed // using scope // resolution operator (::). }

Purpose: Avoid namespace pollution.

slide-58
SLIDE 58

Three Ways to Access Members within a Namespace

  • Qualify each reference:

mySpace::name with every reference.

  • Using declaration:

using mySpace::name;

All future references to name refer to mySpace::name.

  • Using directive:

using namespace mySpace;

All members of mySpace can be referenced without qualification.

slide-59
SLIDE 59

Rules for Use of Namespace std


(within text)

  • Qualify names in prototypes and/or

function definitions.

  • If name used more than once in a

function block, use a using declaration.

  • If more than one name is used from a

namespace, use a using directive.

slide-60
SLIDE 60

60

Outline

  • Different Views of Data: logical,

application and implementation

  • data abstraction, data encapsulation
  • data structure
  • ADT
  • C++ built-in types and Class type, OOP

review

  • Exception Management
  • Algorithms comparison
slide-61
SLIDE 61

Two routes to Joe’s Diner

  • Which route is

better?

  • It depends…
  • If you are really

hungry,

  • If you enjoy

nature and not hungry et, …

  • Similar for multiple algorithms to same

problem?

  • It depends on requirement…
  • time efficiency (running time)? Space

requirement? Simplicity (for maintenance)

slide-62
SLIDE 62

62

One Problem & Two Algorithms

  • Problem: Calculate the sum of the integers

from 1 to N, i.e., 1+2+3+…+(N-1) + N

  • Alg #1

sum = 0; for (count=1; count<=N; count++) sum = sum + count;

  • Alg #2

sum = (1+N)*N/2; Can you see that both are correct?

slide-63
SLIDE 63

Comparison of Two Algorithms

Which is faster?

slide-64
SLIDE 64

64

How to compare Algorithms

  • 1. Compare the actual running time on a

computer

  • 2. Compare the number of instructions/

statements executed

  • varies with languages used and

programmer’s style

  • Count the number of passes through a critical

loop in algorithm

  • 3. Count representative operations performed in

the algorithm

slide-65
SLIDE 65

65

One Problem & Two Algorithms

  • Problem: Calculate the sum of the integers from 1 to N, i.e.,

1+2+3+…+(N-1) + N

  • Alg #1

sum = 0; for (count=1; count<=N; count++) sum = sum + count; performs N addition/assignment operations

  • Alg #2

sum = (1+N)*N/2; Performs one addition, one multiplication and one division

slide-66
SLIDE 66

Order of Magnitude of a Function

The order of magnitude, or Big-O notation,

  • f a function expresses the computing time
  • f a problem as the term in a function that

increases most rapidly relative to the size

  • f a problem.
slide-67
SLIDE 67

Names of Orders of Magnitude

O(1) bounded (by a constant) time O(log2N) logarithmic time O(N) linear time O(N*log2N) N*log2N time O(N2) quadratic time O( 2N ) exponential time

slide-68
SLIDE 68

N log2N N*log2N N2 2N

1 1 2 2 1 2 4 4 4 2 8 16 16 8 3 24 64 256 16 4 64 256 65,536 32 5 160 1024 4,294,967,296 64 6 384 4096 128 7 896 16,384

slide-69
SLIDE 69

Find “John Smith” in phonebook

slide-70
SLIDE 70

Big-O Comparison of List Operations

OPERATION UnsortedList SortedList GetItem O(N) O(N) linear search O(log2N) binary search PutItem Find O(1) O(N) search Put O(1) O(N) moving down Combined O(1) O(N) DeleteItem Find O(N) O(N) search Put O(1) swap O(N) moving up Combined O(N) O(N)