Chapter 2 Data Design and Implementation

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

1 Techniques of Programming CSCI 131 Lecture 24 Structs.
Chapter 2 Data Design and Implementation 1. Lecture 4.
Chapter 10.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 C++ Plus Data Structures Nell Dale Chapter 2 Data Design and Implementation Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
1 C++ Plus Data Structures Nell Dale Chapter 2 Data Design and Implementation Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 8 Arrays and Strings
Dynamic Structures & Arrays.
1 Chapter 12 Arrays Dale/Weems/Headington. 2 Chapter 12 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument.
1 Lecture 22 Chapter 12 Arrays Dale/Weems/Headington.
1 Chapter 9 Scope, Lifetime, and More on Functions.
C++ Plus Data Structures Data Design and Implementation
Chapter 2 Data Design and Implementation. Data The representation of information in a manner suitable for communication or analysis by humans or machines.
CISC Data Structures C/C++ Review Fall
1 C++ Plus Data Structures Nell Dale David Teague Chapter 2 Data Design and Implementation Slides by Sylvia Sorkin, Community College of Baltimore County.
1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.
1 Chapter 2 Data Design and Implementation. 2 Data The representation of information in a manner suitable for communication or analysis by humans or machines.
Chapter 8 Arrays and Strings
1 C++ Syntax and Semantics, and the Program Development Process.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 2 Data Design and Implementation Chapter 2 Data Design and Implementation.
1 Chapter 12-3 Arrays Dale/Weems. 2 Specification of Time class Time // “Time.h” { public : // 7 function members void Set (int hours, int minutes, int.
C++ Classes and Data Structures Jeffrey S. Childs
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 11 Arrays. 2 Chapter 11 Topics l Declaring and Using a One-Dimensional Array l Passing an Array as a Function Argument Using const in Function.
1 C++ Plus Data Structures Nell Dale Lecture 2 Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta Barneva.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Fall 1999CS Composite Data Types. Fall 1999CS Topics to be Covered l Abstract data types l One-dimensional arrays l Two-dimensional arrays.
Fall 2013CISC2200 Yanjun Li1 Review. Fall 2013CISC2200 Yanjun Li2 Outline Array Pointer Object-Oriented Programming.
Chapter 1 Data Abstraction: The Walls CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Structures and Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Introduction to C++ (Extensions to C)
Programming Logic and Design Seventh Edition
Objectives In this chapter, you will:
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Computer Programming BCT 1113
Chapter 13 Applied Arrays: Lists and Strings
Basic Elements of C++.
Chapter 1 Data Abstraction: The Walls
Review: Two Programming Paradigms
Dale/Weems/Headington
About the Presentations
Chapter Structured Types, Data Abstraction and Classes
Multi-Dimension Arrays
Object Oriented Programming COP3330 / CGS5409
C++ Data Types Simple Structured Address Integral Floating
Basic Elements of C++ Chapter 2.
Chapter 9 Scope, Lifetime, and More on Functions
Classes and Data Abstraction
7 Arrays.
Andy Wang Object Oriented Programming in C++ COP 3330
CPS120: Introduction to Computer Science
7 Arrays.
Introduction to Data Structure
CPS120: Introduction to Computer Science
Chapter 9 Introduction To Classes
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Chapter 2 Data Design and Implementation

Data The representation of information in a manner suitable for communication or analysis by humans or machines Data are the nouns of the programming world: The objects that are manipulated The information that is processed

Data Abstraction Separation of a data type’s logical properties from its implementation. LOGICAL PROPERTIES IMPLEMENTATION What are the possible values? How can this be done in C++? What operations will be needed? How can data types be used?

Data Encapsulation is the separation of the representation of data from the applications that use the data at a logical level; a programming language feature that enforces information hiding. APPLICATION 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 REPRESENTATION int y; y = 25;

Encapsulated C++ Data Type int Representation of int as 16 bits two’s complement + Implementation of Operations Value range: INT_MIN . . INT_MAX Operations: + prefix - prefix + infix - infix * infix / infix % infix Relational Operators infix (inside)

Abstract Data Type (ADT) A data type whose properties (domain and operations) are specified independently of any particular implementation.

Collection ordered in different ways

Data from 3 different levels 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 of the structure to hold the data items, and the coding for operations. HOW

Communication between the Application Level and Implementation Level

Viewing a library from 3 different levels 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 the “books”, and the coding for operations.

Composite Data Type A composite data type is a type which stores a collection of individual data components under one variable name, and allows the individual data components to be accessed.

4 Basic Kinds of ADT Operations Constructor -- creates a new instance (object) of an ADT. Transformer -- changes the state of one or more of the data values of an instance. Observer -- allows us to observe the state of one or more of the data values without changing them. Iterator -- allows us to process all the components in a data structure sequentially.

Two Forms of Composite Data Types UNSTRUCTURED STRUCTURED Components are not organized with respect to one another. The organization determines method used to access individual data components. EXAMPLES: EXAMPLES: arrays classes and structs

C++ Built-In Data Types Simple Composite Integral Floating array struct union class char short int long enum Address pointer reference float double long double

Records A record is a composite data type made up of a finite collection of not necessarily homogeneous elements called members or fields. For example . . . .year 2008 .maker ‘h’ ‘o’ ‘n’ ‘d’ ‘a’ ‘\0’ . . . .price 18678.92 thisCar at Base Address 6000

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

Accessing struct members The member selection operator (period . ) is used between the variable name and the member identifier to access individual members of a record (struct or class) type variable. EXAMPLES myCar.year thisCar.maker[4]

Valid struct operations Operations valid on an entire struct type variable: assignment to another struct variable of same type, pass as a parameter to a function (either by value or by reference), return as the value of a function.

Pass-by-value sends a copy of the contents of the actual parameter CALLING BLOCK FUNCTION CALLED SO, the actual parameter cannot be changed by the function.

Pass-by-reference sends the location (memory address) of the actual parameter CALLING BLOCK FUNCTION CALLED can change value of actual parameter

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

Using struct type Value Parameter to examine a member 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 if ( LateModel(myCar, 1995) ) std::cout << myCar.price << std::endl;

One-Dimensional Array at the Logical Level A one-dimensional array is a structured composite data type made up of a finite, fixed size (known at compile time) collection of homogeneous (all of the same data type) elements having relative positions and to which there is direct access (any element can be accessed immediately). Array operations (creation, storing a value, retrieving a value) are performed using a declaration and indexes.

Implementation Example This ACCESSING FUNCTION gives position of values[Index] Address(Index) = BaseAddress + Index * SizeOfElement float values[5]; // assume element size is 4 bytes Base Address 7000 7004 7008 7012 7016 values[0] values[1] values[2] values[3] values[4] Indexes

One-Dimensional Arrays in C++ The index must be of an integral type (char, short, int, long, or enum). The index range is always 0 through the array size minus 1. Arrays cannot be assigned one to another, and cannot be the return type of a function.

Another Example char name[10]; // assume element size is 1 byte This ACCESSING FUNCTION gives position of name[Index] Address(Index) = BaseAddress + Index * SizeOfElement Base Address 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 name[0] name[1] name[2] name[3] name[4] . . . . . name[9]

Passing Arrays as Parameters In C++, arrays are always passed by reference, and & is not used with the formal parameter type. Whenever an array is passed as a parameter, its base address is sent to the called function.

const array parameter FOR EXAMPLE . . . Because arrays are always passed as reference parameters, you can protect the actual parameter from unintentional changes by using const in formal parameter list and function prototype. FOR EXAMPLE . . . // prototype float SumValues( const float values[ ], int numOfValues );

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;

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 and to which there is direct access. Array operations (creation, storing a value, retrieving a value) are performed using a declaration and a pair of indexes (called row and column) representing the component’s position in each dimension.

EXAMPLE -- To keep monthly high temperatures for 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

Finding the average high temperature for Arizona float total = 0; int month; float average; for ( month = 0; month < NUM_MONTHS; month ++ ) total = total + stateHighs [ 2 ][ month ]; average = ( total / 12.0 + 0.5 );

const int NUM_STATES = 50 ; const int NUM_MONTHS = 12 ; int stateHighs [ NUM_STATES ] [ NUM_MONTHS ] ; STORAGE rows columns In memory, C++ stores arrays in row order. The first row is followed by the second row, etc. Base Address 8000 8024 8048 . . . 12 highs for state 0 12 highs for state 1 etc. Alabama Alaska first row second row

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 ] . Base Address 8000 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? Assume 2 bytes for type int.

Example of a 2-dimensional object

Two-Dimensional Array Parameters Just as with a one-dimensional array, when a two- (or higher) dimensional array is passed as a parameter, the base address of the actual array is sent to the function. The size of all dimensions except the first must be included in the function heading and prototype. The sizes of those dimensions for the formal parameter must be exactly the same as in the actual array.

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] Use the two-dimensional stateHighs array to fill a one-dimensional stateAverages array const int NUM_STATES = 50 ; const int NUM_MONTHS = 12 ; int stateHighs [ NUM_STATES ] [ NUM_MONTHS ] ; float stateAverages [ NUM_STATES ] ; [ 0 ] ? [ 1 ] ? [ 2 ] . [ 48 ] [ 49 ] [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] Alaska Arizona 43 42 50 55 60 78 79 80 77 72 63 40 66 64 72 78 85 90 99 115 98 90 88 80

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

Using typedef with arrays helps eliminate the chances of size mismatches between formal and actual parameters. FOR EXAMPLE, typedef int StateHighsType [ NUM_STATES ][ NUM_MONTHS ]; typedef float StateAveragesType [ NUM_STATES ]; void findAverages( const StateHighsType stateHighs, StateAveragesType stateAverages ) { . }

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;

monthlySales[3][7][0] 3 STORES sheets 5 DEPTS rows 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 3 STORES sheets 5 DEPTS rows 41 12 MONTHS columns

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. The predefined operations on an instance of a class are whole assignment and component access.

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!!!

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.

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 }

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.

DateType Class Instance Diagrams startDate endDate Private data: year month day 2002 10 31 Private data: year month day Initialize GetYear GetMonth GetDay Initialize GeytYear GetMonth GetDay 1998 6 30

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

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

Familiar Class Instances and Member Functions The member selection operator ( . ) selects either data members or member functions. Header files iostream and fstream declare the istream, ostream,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” );

Scope Resolution Operator ( :: ) C++ programs typically use several class types. Different classes can have member functions with the same identifer, 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 the implementation file, the scope resolution operator is used in the heading before the member function’s name to specify its class. void DateType :: Write ( ) const { . . . }

Inheritance

A Short Review of Object-Oriented Programming Three inter-related constructs: classes, objects, and inheritance Objects are the basic run-time entities in an object-oriented system. A class defines the structure of its objects. Classes are organized in an “is-a” hierarchy defined by inheritance.

Inheritance Allows programmers to create a new class that is a specialization of an existing class. The new class is called a derived class of the existing class; the existing class is the base class of the new class.

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

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

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

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;

Exceptions An exception is an unusual situation that occurs 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).

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;

infile >> value; do if (value < 0) Exits loop and goes to catch 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;

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

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.

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.

Map to Joe’s Diner

Which Cost More to Feed?

Order of Magnitude of a Function The order of magnitude, or Big-O notation, of a function expresses the computing time of a problem as the term in a function that increases most rapidly relative to the size of a problem.

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

N log2N N*log2N N2 2N 1 0 0 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

Comparison of Two Algorithms

Find “John Smith”

Big-O Comparison of List Operations 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) OPERATION UnsortedList SortedList GetItem O(N) O(N) linear search O(log2N) binary search