ebook img

C++ for business programming PDF

848 Pages·3.183 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C++ for business programming

C++ for Business Programming Second Edition John C.Molluzzo Pace University,New York Upper Saddle River,New Jersey 07458 Library of Congress Cataloging-in-Publication Data Molluzzo,John C. C++ for business programming / John C.Molluzzo.-- 2nd ed. p.cm. Includes bibliographical references and index. ISBN 0-13-046700-6 1. C++ (Computer program language) 2. Business--Computer programs. I.Title. HF5548.5.C125M65 2006 005.13'3--dc22 2005014365 Vice President and Editorial Director,ECS:Marcia J.Horton Senior Acquisitions Editor:Tracy Dunkelberger Editorial Assistant:Christianna Lee Executive Managing Editor:Vince O’Brien Managing Editor:Camille Trentacoste Director of Creative Services:Paul Belfanti Managing Editor,AV Management and Production:Patricia Burns Art Editor:Gregory Dulles Cover Design Director:Jayne Conte Cover Designer:Kiwi Designs Manufacturing Manager,ESM:Alexis Heydt-Long Manufacturing Buyer:Lisa McDowell Executive Marketing Manager:Pamela Shaffer Marketing Assistant:Barrie Reinhold © 2006 Pearson Education,Inc. Pearson Prentice Hall Pearson Education,Inc. Upper Saddle River,NJ 07458 All rights reserved.No part of this book may be reproduced in any form or by any means,without permission in writing from the publisher. Pearson Prentice Hall®is a trademark of Pearson Education,Inc. The author and publisher of this book have used their best efforts in preparing this book.These efforts include the development,research,and testing of the theories and programs to determine their effectiveness.The author and publisher make no warranty of any kind,expressed or implied, with regard to these programs or the documentation contained in this book.The author and publisher shall not be liable in any event for incidental or consequential damages in connection with,or arising out of,the furnishing,performance,or use of these programs. Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 ISBN: 0-13-046700-6 Pearson Education Ltd.,London Pearson Educación de Mexico,S.A.de C.V. Pearson Education Australia Pty.Ltd.,Sydney Pearson Education—Japan,Tokyo Pearson Education Singapore,Pte.Ltd. Pearson Education Malaysia,Pte.Ltd. Pearson Education North Asia Ltd.,Hong Kong Pearson Education,Inc.,Upper Saddle River,New Jersey Pearson Education Canada,Inc.,Toronto Contents PREFACE XV PART I C++ Basics 1 1. INTRODUCTION TO C++ 1 Objectives 1 Why Study C++? 1 1.1 Our First C++ Program 3 1.1.1 The Program Development Cycle 3 1.1.2 Errors in the Cycle 7 1.1.3 Displaying a Two-Line Message:dem01-1.cpp 8 1.1.4 Comments 9 1.1.5 The Function main() 10 1.1.6 The Output Stream cout 11 1.1.7 The returnStatement 13 1.1.8 The Preprocessor Directive #include 13 1.1.9 The usingDirective 14 1.1.10 Execution of dem01-1.cpp 15 1.1.11 Keywords,Multiline Comments,and Escape Sequences 15 1.1.12 About the Exercises,Experiments,and Programming Problems 16 1.2 Integers and Arithmetic 20 1.2.1 Identifiers 20 1.2.2 Variables 20 1.2.3 Using Integer Variables:dem01-2.cpp 22 1.2.4 Variable Declarations 23 1.2.5 Asking the User for Input:Prompts 24 1.2.6 The Input Stream cin 24 1.2.7 The Input Buffer 25 iii iv Contents 1.2.8 The Assignment Operator and Integer Constants 25 1.2.9 The Arithmetic Operators 26 1.2.10 Operator Precedence Rules 28 1.2.11 The Object coutRevisited 30 1.3 Solving a Problem with Integers 32 1.3.1 Problem 1.1 32 1.3.2 The Program prb01-1.cpp 33 1.3.3 Header Files 34 1.3.4 Defined Constants 34 1.3.5 Declaration of Variables 35 1.3.6 The Prompts and Calculations 35 1.3.7 The Output:I/O Manipulators 35 1.4 Other Integer Data Types 36 1.4.1 Unsigned,Long,and Short Integers 36 1.4.2 Mixing Data Types 37 1.4.3 Using the Other Integer Types:Problem 1.2 38 Chapter Review 42 Terminology 42 Summary 43 Review Exercises 43 2. REAL NUMBERS 45 Objectives 45 2.1 Real Numbers 45 2.1.1 Real-Number Variables and Constants 45 2.1.2 Input and Output of Real Numbers 47 2.1.3 Outputting Decimal Numbers 47 2.1.4 Calculating Sales Tax—dem02-1.cpp 48 2.2 Solving Problems with Real Numbers 52 2.2.1 Simulating a Cash Register—Problem 2.1 52 2.2.2 Program Design 53 2.2.3 The Program prb02-1.cpp 53 2.2.4 Discussion of prb02-1.cpp 55 2.2.5 Averaging Quiz Grades—Problem 2.2 55 2.2.6 Program Design—Mixed Types and Type Casts 55 2.2.7 The Program prb02-2.cpp 57 2.2.8 Metric Conversion—Problem 2.3 58 2.2.9 Program Design 59 2.2.10 The Program prb02-3.cpp 61 2.3 More on Arithmetic 64 2.3.1 The Value of an Expression 64 2.3.2 Multiple Assignment Statements 65 2.3.3 The Compound Assignment Operators 66 2.3.4 Increment and Decrement Operators 69 Contents v 2.4 Three Difficulties When Displaying Decimal Numbers with cout 75 Chapter Review 78 Terminology 78 Summary 78 Review Exercises 79 3. ITERATION 80 Objectives 80 3.1 Relation Conditions 80 3.2 Indefinite Iteration:The whileanddoStatements 83 3.2.1 The whileStatement 83 3.2.2 An Example of Indefinite Iteration:The charData Type 85 3.2.3 Using cin.get() 87 3.2.4 The Program dem03-1.cpp 87 3.2.5 Embedded Assignment—Recoding dem03-1.cpp 90 3.2.6 The do-whileLoop 91 3.3 Solving a Problem with Indefinite Iteration 96 3.3.1 Problem 3.1 96 3.3.2 Program Design 96 3.3.3 The Program prb03-1.cpp 98 3.3.4 More on Control-Expressions 102 3.4 Definite Iteration 105 3.4.1 The forStatement 105 3.4.2 An Example of Definite Iteration 109 3.4.3 Calculating Interest on a CD—Problem 3.2 111 3.5 Nested Loops 118 3.5.1 Nested whileLoops 118 3.5.2 Nested forLoops 121 Chapter Review 126 Terminology 126 Summary 126 Review Exercises 127 4. DECISION MAKING 128 4.1 Basic Decision Making 128 4.1.1 The ifStatement 128 4.1.2 Calculating a Payroll with Overtime—Problem 4.1 131 4.2 Compound Conditions—The Logical Operators 139 4.2.1 The not Operator,! 139 4.2.2 The and Operator,&& 139 4.2.3 Testing for a Digit—dem04-1.cpp 140 4.2.4 The or Operator,|| 141 vi Contents 4.2.5 The Program dem04-2.cpp 141 4.2.6 Precedence Rules 142 4.3 Nested ifStatements 146 4.3.1 Coding a Nested ifStatement 146 4.3.2 Calculating a Payroll—Problem 4.2 150 4.3.3 Program Design 150 4.3.4 Character Input 151 4.3.5 The Program prb04-2.cpp 153 4.4 The switchStatement 161 4.4.1 Calculating Real Estate Commission—Problem 4.3 162 4.4.2 Program Design 162 4.4.3 The Program prb04-3.cpp 164 4.4.4 More on break 166 4.4.5 The continueStatement 168 Chapter Review 172 Terminology 172 Summary 172 Review Exercises 173 5. FUNCTIONS 174 Objectives 174 5.1 The Function Concept 175 5.1.1 How Functions Work 175 5.1.2 A Function with No Arguments and No Return Value 176 5.1.3 The Function Prototype and Definition 177 5.1.4 A Function with Arguments and No Return Value 181 5.1.5 Defining and Using a Function that Has Arguments 184 5.1.6 Passing Arguments by Value 186 5.2 User-Defined Functions that Return a Value 191 5.2.1 Calculating Grades Using a Function 191 5.2.2 Calculating Grades—The Program dem05-4.cpp 192 5.2.3 Variable Attributes 194 5.2.4 Scope 195 5.2.5 Duration—Storage Class 199 5.3 Programs that Use Functions 203 5.3.1 Problem 5.1—Moving Costs 203 5.3.2 Program Design 203 5.3.3 The Program prb05-1.cpp 204 5.3.4 Problem 5.2—Calculating Simple Interest 206 5.3.5 Program Design 207 5.3.6 The Program prb05-2.cpp: 208 5.3.7 Discussion of the Program 212 Contents vii 5.3.8 Testing the Program 212 5.3.9 Problem 5.3—Calculating Commissions:Data Validation 214 5.3.10 Program Design 215 5.3.11 The Program prb05-3.cpp 217 5.3.12 Discussion of the Program 222 5.4 The C++ Math Library Functions 227 5.4.1 The Library Function pow() 227 5.4.2 The Library Function sqrt() 228 Chapter Review 231 Terminology 231 Summary 231 Review Exercises 232 6. ARRAYS 234 Objectives 234 6.1 Basic Concepts 234 6.1.1 Definition of an Array 235 6.1.2 Declaring an Array 236 6.1.3 Referencing and Initializing Array Elements 236 6.1.4 Accessing Array Elements by Subscript 238 6.2 Processing an Array:forLoops 242 6.2.1 Using forLoops 242 6.2.2 Searching an Array 245 6.3 Sorting an Array 250 6.3.1 Sorting 250 6.3.2 A Sort Program—dem06-4.cpp 252 6.4 Multidimensional Arrays 256 6.4.1 Declaring Multidimensional Arrays 256 6.4.2 Processing a Two-Dimensional Array 258 6.4.3 Finding Student Averages—dem06-5.cpp 259 Chapter Review 264 Terminology 264 Summary 264 Review Exercises 265 7. POINTERS AND C-STRINGS 266 Objectives 266 7.1 Pointers 266 7.1.1 Declaring and Initializing Pointers 266 7.1.2 The Indirection Operator 270 7.1.3 Pointers and Arrays 272 viii Contents 7.2 C-Strings 277 7.2.1 Defining and Initializing a String 277 7.2.2 String Input and Output 278 7.2.3 String Constants as Pointers 280 7.2.4 Counting Characters in a String 281 7.2.5 Displaying a String in Reverse 282 7.2.6 Counting Words in a String 285 7.3 Arrays of Strings and Pointers 290 7.3.1 Defining an Array of Strings 290 7.3.2 Using an Array of Strings 291 7.3.3 Using an Array of Pointers to Store Strings 294 Chapter Review 297 Terminology 297 Summary 298 Review Exercises 298 8. POINTERS, ARRAYS, AND FUNCTIONS 299 Objectives 299 8.1 Pointers,Reference Variables,and Functions 299 8.1.1 Call By Address—Pointers as Function Arguments 300 8.1.2 Swapping Variable Values—An Example of Call by Address 302 8.1.3 Reference Variables and Call by Reference 303 8.2 Arrays and Functions 309 8.2.1 Passing an Array to a Function 309 8.2.2 Sorting an Array 312 8.3 Strings and Functions 318 8.3.1 Using a Function to Count the Characters in a String 318 8.3.2 Using a Function to Reverse a String in Place 320 8.3.3 A Function that Returns a Pointer 321 8.4 The Standard Library String Functions 326 8.4.1 The Length of a String—The Function strlen() 326 8.4.2 String Assignment—The Function strcpy() 327 8.4.3 Comparing Strings—The Function strcmp() 329 8.4.4 Pasting Strings Together—The Function strcat() 331 8.4.5 Using the String Functions—dem08-9.cpp 332 8.5 Character Classification and Conversion Functions 336 8.5.1 The Character Classification Functions 336 8.5.2 The Character Conversion Functions 338 8.5.3 Putting the Functions to Work—Testing for a Palindrome 340 8.5.4 Numeric Conversion Functions and Numeric Validation 346 8.6 Dynamic Memory Allocation 351 8.6.1 The Heap 351 8.6.2 Static,Automatic,and Heap Memory 354 Contents ix 8.6.3 The Program dem08-15.cpp 355 8.6.4 A Second Example 356 Chapter Review 360 Terminology 360 Summary 361 Review Exercises 361 9. USER-DEFINED DATA TYPES AND TABLES 363 Objectives 363 9.1 The typedefandenumStatements 364 9.1.1 The typedefStatement 364 9.1.2 The enumStatement 366 9.1.3 An Example Using typedefandenum 367 9.2 Structures 371 9.2.1 Defining a Structure 371 9.2.2 Accessing Structure Members 372 9.2.3 Initializing a Structure Variable 375 9.2.4 More Complicated Structures 375 9.2.5 Assigning One Structure Variable to Another 377 9.3 Arrays of Structures:Tables 379 9.3.1 Defining a Table Using Structures 380 9.3.2 Loading Table Values 381 9.3.3 Sorting a Table 385 9.3.4 Searching a Table 390 9.3.5 Sequential Search 392 9.3.6 Binary Search 397 9.4 Structures,Functions,and Pointers 407 9.4.1 Functions and Structures 407 9.4.2 Pointers to Structures 413 9.4.3 Structure References 418 Chapter Review 424 Terminology 424 Summary 424 Review Exercises 425 PART II Object-Oriented Programming 426 10. THE stringCLASS: AN INTRODUCTION TO CLASSES AND OBJECTS 426 Objectives 426 10.1 Objects,Classes,and Object-Oriented Systems 427 10.1.1 Familiar Classes and Objects 428

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.