1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.

Slides:



Advertisements
Similar presentations
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Advertisements

C Language.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Computer Science 1620 Programming & Problem Solving.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Classes and Data Structures Jeffrey S. Childs
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 14 Introduction to Sorting Algorithms Jeffrey S. Childs Clarion University of PA © 2008, Prentice.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
CSCI 383 Object-Oriented Programming & Design Lecture 6 Martin van Bommel.
CPS120: Introduction to Computer Science Functions.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Classes and Data Structures Jeffrey S. Childs
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
1 Introduction to Object Oriented Programming Chapter 10.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CONSTRUCTOR AND DESTRUCTOR. Topics to be discussed……………….. 1. Introduction Introduction 2. FeaturesFeatures 3. Types of ConstructorTypes of Constructor.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
1 C++ Classes and Data Structures Course link…..
Variables A piece of memory set aside to store data
group work #hifiTeam
DATA STRUCTURE : DAT JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)
C++ Classes and Data Structures Jeffrey S. Childs
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
slides created by Ethan Apter
CMSC 202 Templates.
Functions.
slides created by Ethan Apter
slides created by Ethan Apter and Marty Stepp
Lists CMSC 202, Version 4/02.
Templates CMSC 202, Version 4/02.
Presentation transcript:

1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

2 The const Specifier When added to the end of a function heading, it tells the compiler that no changes should be made to any private members during the execution of that function The use of const specifiers aids in debugging programs: the compiler will catch the error if ( numDependents = 3 )

3 The const Specifier (cont.) const can also be used for parameters Objects, or parameters that can be objects (DataType parameters) are often passed by reference for speed –in pass by value, it can take a long time to copy –in pass by reference, only the address is copied The use of const here specifies that the parameter should not change – called passing by const reference

4 Rules for Passing Objects Pass objects by value when the function will change them and you don’t want the change to be reflected to the caller Pass objects by reference when you want changes to be reflected to the caller Pass objects by const reference for speed when objects won’t be changed – the compiler will catch mistaken changes

5 Constructors Constructors are special class functions that have unusual features: –No return type –Name must be the same as the class name –Called automatically when an object is declared Constructors are commonly used to initialize data members

6 Constructors (cont.) More than one constructor can be written for a class; constructors differ by the number and types of parameters A constructor with no parameters is called a default constructor If a constructor is not written by the programmer, the compiler supplies a default constructor which does nothing

7 1 template 2 class Checkbook 3 { 4 public: 5Checkbook( ); 6Checkbook( float initBalance ); 7void setBalance( float amount ); 8bool writeCheck( const DataType & amount ); 9void deposit( float amount ); 10float getBalance( ) const; 11DataType getLastCheck( ) const; 12float getLastDeposit( ) const; 13 private:. Example

8 1 // checkbook.cpp -- The function definitions of the class 2 // template for the Checkbook 3 4 template 5 Checkbook ::Checkbook( ) 6 { 7 } 8 9 template 10 Checkbook ::Checkbook( float initBalance ) 11 { 12balance = initBalance; 13 } Constructor Definitions

9 1 int main( ) 2 { 3float bal; 4cout << "Enter your initial balance: "; 5cin >> bal; 6 7Checkbook cbook( bal ); 8 9cbook.deposit( );. Use of Second Constructor

10 Modifying Classes The default constructor and the setBalance function now seem removable BUT … the resulting class would not be backward compatible with older clients’ code if we did remove them

11 Modifying Classes (cont.) When changing a class, always make sure it will still work with older programs. If the clients need to make minor changes to their programs, they can still compile them with the new class. The next example shows a further modification of the Checkbook class, so that it stores an array of checks.

12 Check Array 1 // comments for overloaded operators go here 2 3 const int CAPACITY = 5; 4 5 // The templates for an associated CheckInfo and 6 // Checkbook must match in DataType 7 template 8 struct CheckInfo { 9int numChecks; 10DataType checks[ CAPACITY ]; 11 };

13 12 template 13 class Checkbook 14 { 15 public: 16Checkbook( ); 17Checkbook( float initBalance ); 18void setBalance( float amount ); 19bool writeCheck( const DataType & amount ); 20void deposit( float amount ); Check Array (cont.) public section continued

14 21float getBalance( ) const; 22DataType getLastCheck( ) const; 23// getLastChecks returns up to CAPACITY checks in a 24// CheckInfo struct. The number of checks is also in the 25// CheckInfo struct. Checks in the checks array are stored 26// in order with the latest check first 27CheckInfo getLastChecks( ) const; 28float getLastDeposit( ) const; Check Array (cont.)

15 29 private: 30float balance; 31int numChecks; // number of checks in array 32int lastIndex; // the index of the last check that 33 // was written 34DataType lastChecks[ CAPACITY ]; // saves 35 // checks 36float lastDeposit; 37 }; #include "checkbook.cpp" Check Array (cont.)

16 Checkbook.cpp 40 template 41 Checkbook ::Checkbook( ) 42 { 43lastIndex = -1; 44 } template 47 Checkbook ::Checkbook( float initBalance ) 48 { 49balance = initBalance; 50 lastIndex = -1; 51 }

17 Checkbook.cpp (cont.) The CAPACITY of the lastChecks array is set to 5 In the writeCheck function, when the array is filled, new checks are added to the beginning of the array again The array will contain the latest 5 checks This type of array is known as a circular array

18 Checkbook.cpp (cont.) $225$250 $100 $ lastIndex numChecks: 4 new check: $175

19 Checkbook.cpp (cont.) $225$250 $100 $ lastIndex numChecks: 4 new check: $175

20 Checkbook.cpp (cont.) $225$250 $100 $125$ lastIndex numChecks: 5 new check: $175

21 Checkbook.cpp (cont.) $225$250 $100 $125$ lastIndex numChecks: 5 new check: $50

22 Checkbook.cpp (cont.) $225$250 $100 $125$ numChecks: 5 new check: $50 lastIndex

23 Checkbook.cpp (cont.) $50$250 $100 $125$ lastIndex numChecks: 5 new check: $50

24 Checkbook.cpp (cont.) $50$250 $100 $125$ lastIndex numChecks: 5 new check: $75

25 Checkbook.cpp (cont.) $50$250 $100 $125$ lastIndex numChecks: 5 new check: $75

26 Checkbook.cpp (cont.) $50$75 $100 $125$ lastIndex numChecks: 5 new check: $75

27 Checkbook.cpp (cont.) $50$75 $100 $125$ lastIndex numChecks: 5

28 52 template 53 bool Checkbook ::writeCheck( 54const DataType & amount ) 55 { 56if ( amount > balance ) 57return false; 58balance -= amount; 59 60lastIndex++; 61if ( lastIndex == CAPACITY ) 62lastIndex = 0; 63lastChecks[ lastIndex ] = amount; 64if ( numChecks != CAPACITY ) 65numChecks++; 66 67return true; 68 } Checkbook.cpp (cont.) shown next…

29 52 template 53 bool Checkbook ::writeCheck( 54const DataType & amount ) 55 { 56if ( amount > balance ) 57return false; 58balance -= amount; 59 60lastIndex++; 61if ( lastIndex == CAPACITY ) 62lastIndex = 0; 63lastChecks[ lastIndex ] = amount; 64if ( numChecks != CAPACITY ) 65numChecks++; 66 67return true; 68 } Checkbook.cpp (cont.)

30 52 template 53 bool Checkbook ::writeCheck( 54const DataType & amount ) 55 { 56if ( amount > balance ) 57return false; 58balance -= amount; 59 60lastIndex++; 61if ( lastIndex == CAPACITY ) 62lastIndex = 0; 63lastChecks[ lastIndex ] = amount; 64if ( numChecks != CAPACITY ) 65numChecks++; 66 67return true; 68 } Checkbook.cpp (cont.)

31 69 template 70 DataType Checkbook ::getLastCheck( ) const 71 { 72return lastChecks[ lastIndex ]; 73 } Checkbook.cpp (cont.) The getLastCheck function must be written differently, but the old clients’ code which use it will not be affected.

32 74 template 75 CheckInfo Checkbook :: 76getLastChecks( ) const 77 { 78CheckInfo info; 79info.numChecks = numChecks; 80 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} 86 87return info; 88 } Checkbook.cpp (cont.)

33 74 template 75 CheckInfo Checkbook :: 76getLastChecks( ) const 77 { 78CheckInfo info; 79info.numChecks = numChecks; 80 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} 86 87return info; 88 } Checkbook.cpp (cont.) Let’s take a look at how this loop works.

34 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) Let’s take a look at how this loop works.

35 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ lastIndex lastChecks info.checks We want checks from latest to oldest: $75, $50, $175, $125, $100 numChecks : 5

36 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ lastIndex lastChecks info.checks numChecks : 5

37 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks info.checks i numChecks : 5

38 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks info.checks i numChecks : 5

39 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks info.checks i numChecks : 5

40 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks info.checks i numChecks : 5

41 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks info.checks i numChecks : 5

42 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i numChecks : 5

43 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i numChecks : 5

44 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i numChecks : 5

45 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i numChecks : 5

46 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i numChecks : 5

47 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i numChecks : 5

48 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i numChecks : 5

49 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i $50 numChecks : 5

50 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i $50 numChecks : 5

51 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j : -1 lastChecks $ info.checks i $50 numChecks : 5

52 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j : -1 lastChecks $ info.checks i $50 numChecks : 5

53 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ lastChecks $ info.checks i $50 numChecks : 5 j : -1

54 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ lastChecks $ info.checks i $50 numChecks : 5 j : -1

55 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

56 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i $50 numChecks : 5

57 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

58 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

59 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$ info.checks i $50 numChecks : 5

60 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

61 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

62 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

63 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$ info.checks i $50 numChecks : 5

64 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

65 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

66 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$ info.checks i $50 numChecks : 5

67 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

68 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

69 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

70 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$ info.checks i $50 numChecks : 5

71 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

72 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$125$ info.checks i $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

73 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$125$ info.checks i $50 numChecks : 5

74 Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$125$ info.checks i : 5 $50 numChecks : 5 81for ( int i = 0, j = lastIndex; i < numChecks ; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85}

75 81for ( int i = 0, j = lastIndex ; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$125$ info.checks i : 5 $50 numChecks : 5

76 81for ( int i = 0, j = lastIndex; i < numChecks; i++, j-- ) { 82if ( j == -1 ) 83j = CAPACITY - 1; 84info.checks[ i ] = lastChecks[ j ]; 85} Checkbook.cpp (cont.) $50$75 $100 $125$ j lastChecks $75$175$125$ info.checks i : 5 $50 DONE! numChecks : 5

77 Data Translation The class interface (public section) is sometimes involved in doing this kind of data translation The data is translated from a form convenient for the class programmer to a form convenient for the client –another reason it is good that the client doesn’t have access to private data

78 Client Program 89 #include 90 #include 91 #include "checkbook.h" using namespace std; int menu( ); const int CHECK = 1, DEPOSIT = 2, BALANCE = 3, QUIT = 4; int main( ) 100 {

79 99 int main( ) 100 { 101float balance; 102cout << "Enter the initial balance: $"; 103cin >> balance; 104Checkbook cb( balance ); 105float amount; 106int choice; 107bool checkAccepted = false; cout << fixed << showpoint << setprecision( 2 ); 110choice = menu( ); Client Program (cont.)

80 111while ( choice != QUIT ) { 112 if ( choice == CHECK ) { 113 cout << "Enter check amount: $"; 114 cin >> amount; 115 if ( cb.writeCheck( amount ) ) { 116cout << "Check accepted." << endl; 117checkAccepted = true; 118} 119 else 120cout << "Your balance is not high " << 121 "enough for that check." << endl; 122 }.. // other choices are written here. 123choice = menu( ) 124} Client Program (cont.)

81 111while ( choice != QUIT ) { 112 if ( choice == CHECK ) { 113 cout << "Enter check amount: $"; 114 cin >> amount; 115 if ( cb.writeCheck( amount ) ) { 116cout << "Check accepted." << endl; 117checkAccepted = true; 118} 119 else 120cout << "Your balance is not high " << 121 "enough for that check." << endl; 122 }.. // other choices are written here. 123choice = menu( ) 124} Client Program (cont.)

82 111while ( choice != QUIT ) { 112 if ( choice == CHECK ) { 113 cout << "Enter check amount: $"; 114 cin >> amount; 115 if ( cb.writeCheck( amount ) ) { 116cout << "Check accepted." << endl; 117checkAccepted = true; 118} 119 else 120cout << "Your balance is not high " << 121 "enough for that check." << endl; 122 }.. // other choices are written here. 123choice = menu( ) 124} Client Program (cont.)

83 111while ( choice != QUIT ) { 112 if ( choice == CHECK ) { 113 cout << "Enter check amount: $"; 114 cin >> amount; 115 if ( cb.writeCheck( amount ) ) { 116cout << "Check accepted." << endl; 117checkAccepted = true; 118} 119 else 120cout << "Your balance is not high " << 121 "enough for that check." << endl; 122 }.. // other choices are written here. 123choice = menu( ) 124} Client Program (cont.)

84 125if ( checkAccepted ) { 126 cout << "Your last check was: $" 127<< cb.getLastCheck( ) << endl; 128 CheckInfo ci; 129 ci = cb.getLastChecks( ); 130 if ( ci.numChecks > 1 ) { 131cout << "Your last checks are:" << endl; 132for ( int i = 0; i < ci.numChecks; i++ ) 133cout << "$" << ci.checks[ i ] << endl; 134} 135 }. Client Program (cont.)