Chapter 18 C Legacy Code Topics. Chapter 19 Class string and String Stream
Processing. Chapter 20 Standard Template Library (STL). Chapter 21 ANSI/ISO ...
C++ HOW TO PROGRAM SECOND EDITION
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Chapter 19 Chapter 20 Chapter 21
Introduction to Computers and C++ Programming Control Structures Functions Arrays Pointers and Strings Classes and Data Abstraction Classes: Part II Operator Overloading Inheritance Virtual Functions and Polymorphism C++ Stream Input/Output Templates Exception Handling File Processing Data Structures Bits, Characters, Strings, and Structures The Preprocessor C Legacy Code Topics Class string and String Stream Processing Standard Template Library (STL) ANSI/ISO C++ Standard Language Additions
© Copyright 1998 by Prentice Hall. All Rights Reserved. For use only by instructors in courses for which C++ How to Program, Second Editon is the required textbook.
CHAPTER 1
INTRODUCTION TO COMPUTERS AND C++ PROGRAMMING
Illustrations List .
Fig. 1.1 Fig. 1.2 Fig. 1.3 Fig. 1.4 Fig. 1.5 Fig. 1.6 Fig. 1.7 Fig. 1.8 Fig. 1.9 Fig. 1.10 Fig. 1.11 Fig. 1.12 Fig. 1.13 Fig. 1.14 Fig. 1.15 Fig. 1.16
(Main Page)
A typical C++ environment. Text printing program. Some common escape sequences. Printing on one line with separate statements using cout. Printing on multiple lines with a single statement using cout. An addition program. A memory location showing the name and value of a variable. Memory locations after values for two variables have been input. Memory locations after a calculation. Arithmetic operators. Precedence of arithmetic operators. Order in which a second-degree polynomial is evaluated. Equality and relational operators. Using equality and relational operators. Precedence and associativity of the operators discussed so far. Using new-style header files.
© Copyright 1998 by Prentice Hall. All Rights Reserved. For use only by instructors in courses for which C++ How to Program, Second Editon is the required textbook.
1
CHAPTER 1
INTRODUCTION TO COMPUTERS AND C++ PROGRAMMING
Editor
Disk
Program is created in the editor and stored on disk.
Preprocessor
Disk
Preprocessor program processes the code.
Disk
Compiler creates object code and stores it on disk.
Disk
Linker links the object code with the libraries, creates a.out and stores it on disk
Compiler
Linker
Primary Memory Loader
Loader puts program in memory. Disk
. . .
Primary Memory CPU
. . .
Fig. 1.1
CPU takes each instruction and executes it, possibly storing new data values as the program executes.
A typical C++ environment.
© Copyright 1998 by Prentice Hall. All Rights Reserved. For use only by instructors in courses for which C++ How to Program, Second Editon is the required textbook.
2
CHAPTER 1
1 2 3 4 5 6 7 8 9 10
INTRODUCTION TO COMPUTERS AND C++ PROGRAMMING
// Fig. 1.2: fig01_02.cpp // A first program in C++ #include int main() { cout 0 ) sales[ month ] = amount; else cout 0 ? s : 0; } // Get the Boss's pay double Boss::earnings() const { return weeklySalary; } // Print the Boss's name void Boss::print() const { cout 0 ? c : 0; } // Set CommissionWorker's quantity sold void CommissionWorker::setQuantity( int q ) { quantity = q > 0 ? q : 0; } // Determine CommissionWorker's earnings double CommissionWorker::earnings() const { return salary + commission * quantity; } // Print the CommissionWorker's name void CommissionWorker::print() const { cout 0 ? w : 0; } // Set the number of items output void PieceWorker::setQuantity( int q ) { quantity = q > 0 ? q : 0; } // Determine the PieceWorker's earnings double PieceWorker::earnings() const { return quantity * wagePerPiece; }
Fig. 10.1
Demonstrating polymorphism with the Employee class hierarchy (part 8 of 13).
218 219 // Print the PieceWorker's name 220 void PieceWorker::print() const 221 { 222 cout 0 ? w : 0; } // Set the hours worked void HourlyWorker::setHours( double h ) { hours = h >= 0 && h < 168 ? h : 0; } // Get the HourlyWorker's pay double HourlyWorker::earnings() const { if ( hours transaction; // should validate client.balance += transaction; outputLine( cout, client ); updateFile.seekp( ( account-1 ) * sizeof( clientData ) ); updateFile.write( reinterpret_cast( &client ), sizeof( clientData ) );
} else cerr > client.balance; client.accountNumber = account; insertInFile.seekp( ( account - 1 ) * sizeof( clientData ) ); insertInFile.write( reinterpret_cast( &client ), sizeof( clientData ) ); } else cerr ::ListNode( const NODETYPE &info ) : data( info ), nextPtr( 0 ) { } // Return a copy of the data in the node template< class NODETYPE > NODETYPE ListNode< NODETYPE >::getData() const { return data; } #endif
Fig. 15.3 29 30 31 32 33 34 35 36 37 38 39
...
D
// Fig. 15.3: listnd.h // ListNode template definition #ifndef LISTND_H #define LISTND_H
Fig. 15.3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
lastPtr
Manipulating a linked list (part 2 of 8).
// Fig. 15.3: list.h // Template List class definition #ifndef LIST_H #define LIST_H #include #include #include "listnd.h" template< class NODETYPE > class List {
© Copyright 1998 by Prentice Hall. All Rights Reserved. For use only by instructors in courses for which C++ How to Program, Second Editon is the required textbook.
2
CHAPTER 15
40 41 42 43 44 45 46 47 48 49 50 51
public: List(); // constructor ~List(); // destructor void insertAtFront( const NODETYPE & ); void insertAtBack( const NODETYPE & ); bool removeFromFront( NODETYPE & ); bool removeFromBack( NODETYPE & ); bool isEmpty() const; void print() const; private: ListNode< NODETYPE > *firstPtr; // pointer to first node ListNode< NODETYPE > *lastPtr; // pointer to last node
Fig. 15.3 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
DATA STRUCTURES
Manipulating a linked list (part 3 of 8). // Utility function to allocate a new node ListNode< NODETYPE > *getNewNode( const NODETYPE & );
}; // Default constructor template< class NODETYPE > List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) { } // Destructor template< class NODETYPE > List< NODETYPE >::~List() { if ( !isEmpty() ) { // List is not empty cout *currentPtr = firstPtr, *tempPtr; while ( currentPtr != 0 ) { // delete remaining nodes tempPtr = currentPtr; cout data nextPtr; delete tempPtr; } } cout void List< NODETYPE >::insertAtFront( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty newPtr->nextPtr = firstPtr; firstPtr = newPtr; } } // Insert a node at the back of the list template< class NODETYPE > void List< NODETYPE >::insertAtBack( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value );
© Copyright 1998 by Prentice Hall. All Rights Reserved. For use only by instructors in courses for which C++ How to Program, Second Editon is the required textbook.
3
CHAPTER 15
101 102
if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr;
Fig. 15.3 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
DATA STRUCTURES
Manipulating a linked list (part 4 of 8). else { // List is not empty lastPtr->nextPtr = newPtr; lastPtr = newPtr; }
} // Delete a node from the front of the list template< class NODETYPE > bool List< NODETYPE >::removeFromFront( NODETYPE &value ) { if ( isEmpty() ) // List is empty return false; // delete unsuccessful else { ListNode< NODETYPE > *tempPtr = firstPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else firstPtr = firstPtr->nextPtr; value = tempPtr->data; delete tempPtr; return true;
// data being removed // delete successful
} } // Delete a node from the back of the list template< class NODETYPE > bool List< NODETYPE >::removeFromBack( NODETYPE &value ) { if ( isEmpty() ) return false; // delete unsuccessful else { ListNode< NODETYPE > *tempPtr = lastPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else { ListNode< NODETYPE > *currentPtr = firstPtr; while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; } value = tempPtr->data; delete tempPtr;
Fig. 15.3
Manipulating a linked list (part 5 of 8).
return true; // delete successful 152 153 } 154 } 155 156 // Is the List empty?
© Copyright 1998 by Prentice Hall. All Rights Reserved. For use only by instructors in courses for which C++ How to Program, Second Editon is the required textbook.
4
CHAPTER 15
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
template< class NODETYPE > bool List< NODETYPE >::isEmpty() const { return firstPtr == 0; } // Return a pointer to a newly allocated node template< class NODETYPE > ListNode< NODETYPE > *List< NODETYPE >::getNewNode( const NODETYPE &value ) { ListNode< NODETYPE > *ptr = new ListNode< NODETYPE >( value ); assert( ptr != 0 ); return ptr; } // Display the contents of the List template< class NODETYPE > void List< NODETYPE >::print() const { if ( isEmpty() ) { cout *currentPtr = firstPtr; cout &listObject, const char *type ) { cout data ) insertNodeHelper( &( ( *ptr )->rightPtr ), value ); else cout void Tree< NODETYPE >::preOrderHelper( TreeNode< NODETYPE > *ptr ) const { if ( ptr != 0 ) { cout data leftPtr ); preOrderHelper( ptr->rightPtr ); } }
© Copyright 1998 by Prentice Hall. All Rights Reserved. For use only by instructors in courses for which C++ How to Program, Second Editon is the required textbook.
17
CHAPTER 15
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
template< class NODETYPE > void Tree< NODETYPE >::inOrderTraversal() const { inOrderHelper( rootPtr ); } template< class NODETYPE > void Tree< NODETYPE >::inOrderHelper( TreeNode< NODETYPE > *ptr ) const { if ( ptr != 0 ) { inOrderHelper( ptr->leftPtr ); cout data rightPtr ); } }
Fig. 15.16 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
Creating and traversing a binary tree (part 3 of 6).
template< class NODETYPE > void Tree< NODETYPE >::postOrderTraversal() const { postOrderHelper( rootPtr ); } template< class NODETYPE > void Tree< NODETYPE >::postOrderHelper( TreeNode< NODETYPE > *ptr ) const { if ( ptr != 0 ) { postOrderHelper( ptr->leftPtr ); postOrderHelper( ptr->rightPtr ); cout data intTree; int intVal; cout > intVal; intTree.insertNode( intVal ); } cout z1\n"; else // s1 < z1 cout