c++ for business programming - gbv

9
C++ for Business Programming John C. Molluzzo Pace University Prentice Hall Upper Saddle River, New Jersey 07458

Upload: others

Post on 08-Jan-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ for Business Programming - GBV

C++ for Business Programming

John C. Molluzzo Pace University

Prentice Hall Upper Saddle River, New Jersey 07458

Page 2: C++ for Business Programming - GBV

Contents

PART I Basic C++ 1

Chapter 1 Introduction to C++ 3 Objectives 3

Why Study C++? 3

1.1 Our First C++Program 5 The Program Development Cycle • Displaying a Two-Line Message—dem01-1.cpp • Comments • The Function main () • The Output Stream cout • The r e t u r n Statement • The Preprocessor Directive ^ i nc l ude • The us ing Directive • Execution ofdem0l-1.cpp • About the Exercises, Experiments, and Programming Problems • Exercises 1.1 • Experiments 1.1 • Programming Problems 1.1

1.2 Integers and Ar i thmet ic 21 Identifiers • Variables • Using Integer Variables—dem01-2.cpp • Variable Declarations • Asking the User for Input—Prompts • The Input Stream e i n • The Input Buffer • The Assignment Operator and Integer Constants • The Arithmetic Operators • Operator Precedence Rules • The Object cout Revisited • Exercises 1.2 • Experiments 1.2 • Programming Problems 1.2

1.3 Solving a Problem w i th Integers 34 Problem 1.1 • Discussion of Problem 1.1 • The Program prb01-1.cpp • Header Files • Defined Constants • Declaration of Variables • The Prompts and Calculations • The Output—I/O Manipulators • Exercises 1.3 • Programming Problems 1.3

1.4 Other Integer Data Types 39 Unsigned, Long, and Short Integers • Mixing Data Types • Using the Other Integer Types—Problem 1.2 • Discussion of Problem 1.2 • Exercises 1.4 • Experiments 1.4 • Programming Problems 1.4

Chapter Review 44 Terminology • Summary • Review Exercises

Chapter 2 Real Numbers 47 Objectives 47

2.1 Real Number Variables and Constants 48 Input and Output of Real Numbers • Three Difficulties When Displaying Decimal Numbers with cout • Calculating Sales Tax—dem02-1.cpp • Exercises 2.1 • Experiments 2.1 • Programming Problems 2.1

iv

Page 3: C++ for Business Programming - GBV

Contents v

2.2 Solving Problems w i th Real Numbers 57 Simulating a Cash Register—Problem 2.1 • Program Design • The Program prb02-1.cpp • Discussion of prb02-1. cpp • Averaging Quiz Grades—Problem 2.2 • Program Design—Mixed Typesand Type Casts • The Program prb02~2.cpp • Metric Conversion— Problem 2.3 • Program Design • The Program prb02-3. cpp • Exercises 2.2 • Experiments 2.2 • Programming Problems 2.2

2.3 More On Ar i thmet ic 70 The Value of an Expression • Multiple Assignment Statements • The Compound Assignment Operators • Increment and Decrement Operators • Exercises 2.3 • Experiments 2.3 • Programming Problems 2.3

Chapter Review 81 Terminology • Summary • Review Exercises

Chapter 3 Iteration 83 Objectives 83

3.1 Relation Conditions 84 Exercises 3.1 • Experiments 3.1

3.2 Indefinite I terat ion: The w h i l e and do Statements 87 The wh i l e Statement • An Example of Indefinite Iteration and the char data type • The Member Function e i n . get () • The Program dem03-1.cpp • Embedded Assignment—Recoding dem03-1 .cpp • The do -wh i l e Loop • Exercises 3.2 • Experiments 3.2

3.3 Solving a Problem w i t h Indef ini te I terat ion 100 Problem 3.1 • Program Design • The Program prb03-1 .cpp • Discussion of prb03-1.cpp • More on Control Expressions • Experiments 3.3 • Programming Problems 3.3

3.4 Definite Iteration 109 The f o r Statement • An Example of Definite Iteration • Calculating Interest on a CD— Problem 3.2 • Exercises 3.4 • Experiments 3.4 • Programming Problems 3.4

3.5 Nested Loops 123 Nested wh i l e Loops • Nested f o r Loops • Experiments 3.5 • Programming Problems 3.5

Chapter Review 132 Terminology • Summary • Review Exercises

Chapter 4 Decision Making 134 Objectives 134

4.1 Basic Decision Making 135 The i f Statement • Calculating a Payroll with Overtime—prb04-1.cpp • Exercises 4.1 • Experiments 4.1 • Programming Problems 4.1

4.2 Compound Conditions—The Logical Operators 145 The not Operator, ! • The and Operator, && • Testing for a Digit—dem04-1.cpp • The or Operator, | | • The Program dem04-2.cpp • Precedence Rules • Exercises4.2 • Experiments 4.2 • Programming Problems 4.2

Page 4: C++ for Business Programming - GBV

vi Contents

4.3 Nested i f Statements 153 Coding a Nested i f Statement • Calculating a Payroll—Problem 4.2 • Program Design • Character Input • The Program prb04-2.cpp • Exercises 4.3 • Experiments 4.3 • Programming Problems 4.3

4.4 The s w i t c h Statement 168 The Case Structure and the sw i tch Statement • Calculating Real Estate Commission— Problem 4.3 • Program Design • The Program prb04-3.cpp • More on break • The cont inue Statement • Exercises 4.4 » Experiments 4.4 • Programming Problems 4.4

Chapter Review 180 Terminology • Summary • Review Exercises

Chapter 5 Functions 182 Objectives 182

5.1 The Function Concept 183 How Functions Work • A Function with No Arguments and No Return Value • The Function Prototype and Definition • A Function with Arguments and No Return Value • Defining and Using a Function That Has Arguments • Passing Arguments by Value • Exercises 5.1 • Experiments 5.1 • Programming Problems 5.1

5.2 User-Defined Functions That Return a Value 199 Using a Function to Calculate Grades • Calculating Grades—The Program dem05-4.cpp • Variable Attributes • Scope • Duration—Storage Class • Exercises 5.2 • Experiments 5.2 • Programming Problems 5.2

5.3 Programs That Use Functions 211 Problem 5.1 Moving Costs • Program Design • The Program prb05-1.cpp * Problem 5.2 Calculating Simple Interest • Program Design • The Program prb05-2.cpp • Discussion of the Program * Testing the Program • Problem 5.3—Calculating Commissions • Program Design • The Program prb05-3.cpp • Discussion of the Program • Experiments 5.3 • Programming Problems 5.3

5.4 Macros and Inline Functions 236 Symbolic Constants Using #def i ne • Macro Definitions * An Example Using Macros— The Conditional Operator • Functions versus Macros • Inline Functions • Exercises 5.4 Experiments 5.4 • Programming Problems 5.4

Chapter Review 246 Terminology • Summary • Review Exercises

Chapter 6 Arrays 249 Objectives 249

6.1 Basic Concepts 250 Definition of an Array • Declaring an Array • Referencing and Initializing Array Elements • Accessing Array Elements by Subscript • Exercises 6.1

6.2 Processing an Array: f o r Loops 257 Using f o r Loops • Searching an Array • Exercises 6.2 • Experiments 6.2 • Programming Problems 6.2

Page 5: C++ for Business Programming - GBV

Contents vii

6.3 Sorting an Array 266 Sorting • A Sort Program—dem06-4.cpp • Exercises 6.3 • Experiments 6.3 • Programming Problems 6.3

6.4 Mult idimensional Arrays 271 Declaring Multidimensional Arrays • Processing a Two-Dimensional Array • Finding Student Averages—dem06-5.cpp • Exercises 6.4 • Experiments 6.4 • Programming Problems 6.4

Chapter Review 280 Terminology • Summary • Review Exercises

Chapter 7 Pointers and Strings 283 Objectives 283

7.1 Pointers 284 Declaring and Initializing Pointers • The Indirection Operator • Pointers and Arrays • Exercises 7.1 • Experiments 7.1

7.2 Strings 294 Defining and Initializing a String • String Input and Output • String Constants as Pointers • Counting Characters in a String • Displaying a String in Reverse • Counting Words in a String • Exercises 7.2 • Experiments 7.2 • Programming Problems 7.2

7.3 Arrays of Strings and Pointers 308 Defining an Array of Strings • Using an Array of Strings • Using an Array of Pointers to Store Strings • Exercises 7.3 • Experiments 7.3 • Programming Problems 7.3

Chapter Review 316 Terminology • Summary • Review Exercises

Chapter 8 Pointers, Arrays, and Functions 318 Objectives 318

8.1 Pointers, Reference Variables, and Functions 319 Call By Address—Pointers as Function Arguments • Swapping Variable Values—An Example of Call by Address • Reference Variables and Call by Reference • Exercises 8.1 • Experiments 8.1 • Programming Problems 8.1

8.2 Arrays and Functions 329 Passing an Array to a Function • Sorting an Array • Exercises 8.2 • Experiments 8.2 • Programming Problems 8.2

8.3 Strings and Functions 338 Using a Function to Count the Characters in a String • Using a Function to Reverse a String in Place • A Function that Returns a Pointer • Exercises 8.3 • Programming Problems 8.3

8.4 The Standard Library String Functions 346 The Length of a String—The Function s t r l e n () • String Assignment—The Function s t r c p y O • Comparing Strings—The Function strcmp () • Pasting Strings Together— The Function s t r c a t () • Using the String Functions—dem08-9.cpp • Experiments 8.4 • Programming Problems 8.4

Page 6: C++ for Business Programming - GBV

viii Contents

8.5 Character Classification and Conversion Functions 357 The Character Classification Functions • The Character Conversion Functions • Putting the Functions to Work—Testing for a Palindrome • Numeric Conversion Functions and Numeric Validation • Exercises 8.5 • Experiments 8.5 • Programming Problems 8.5

8.6 Dynamic Memory Al locat ion 372 The Heap • Static, Automatic, and Heap Memory • The Program dem08-15.cpp • A Second Example • Exercises 8.6 • Experiments 8.6 • Programming Problems 8.6

Chapter Review 381 Terminology • Summary • Review Exercises

Chapter 9 User-Defined Data Types and Tables 384 Objectives 384

9.1 The t y p e d e f and enum Statements 385 The typedef Statement • The enum Statement • An Example Using typedef and enum • Exercises 9.1 • Experiments 9.1

9.2 Structures 392 Defining a Structure • Accessing Structure Members • Initializing a Structure Variable • More Complicated Structures • Assigning One Structure Variable to Another • Exercises 9.2 • Experiments 9.2

9.3 Arrays of Structures: Tables 400 Using Structures to Define a Table • Loading Table Values • Sorting a Table • Searching a Table • Sequential Search • Binary Search • Exercises 9.3 • Experiments 9.3 • Programming Problems 9.3

9.4 Structures, Functions, and Pointers 431 Functions and Structures • Pointers to Structures • Structure References • Exercises 9.4 • Experiments 9.4 • Programming Problems 9.4

Chapter Review 449

Terminology • Summary • Review Exercises.

PART II Object-Oriented Programming 451

Chapter 10 Classes and Objects 453 Objectives 453 Introduct ion 453

The Object-Oriented View of a System • Familiar Classes and Objects 10.1 Objects and Classes 456

Defining a Class • Using a Class • The Program deml 0-1. cpp • Discussion of deml 0-1.cpp • The Relation Between class and struct • A More Useful Class—Accessor and Mutator Functions • Exercises 10.1 • Experiments 10.1 • Programming Problems 10.1

10.2 Constructors and Destructors 476 Constructors • A Constructor for Class Account • Destructors • Assigning One Object to Another • Exercises 10.2 • Experiments 10.2 • Programming Problems 10.2

Page 7: C++ for Business Programming - GBV

Contents ix

10.3 Overloading, Default Arguments, and Function Templates 489 Overloading Constructors • De fault Arguments • General Function Overloading • Function Templates • Exercises 10.3 • Experiments 10.3 • Programming Problems 10.3

Chapter Review 503 Terminology • Summary • Review Exercises

Chapter 11 Manipulating Objects 506 Objectives 506

11.1 Using Arrays, Pointers, and Dynamic Memory Al locat ion 507 Array and Pointer Class Members • Experiments 11.1 • Programming Problems 11.1

11.2 The Copy Constructor 515 The Copy Constructor—A Simple Example • A Copy Constructor for Account • Exercises 11.2 • Experiments 11.2 • Programming Problems 11.2

11.3 Using c o n s t W i th Classes 527 Constant Objects and Functions • const in Pointer Declarations • Mixing Constant and Non-Constant Pointers • Constant Arguments in Functions • Constant Argumentsand the Copy Constructor • Accessor Functions that Return Constant Pointers • Exercises 11.3 • Experiments 11.3 • Programming Problems 11.3

11.4 Objects, Functions, and Pointers 545 Functions that Return an Object • Passing an Object by Value • Pointers to Objects • Passing an Object by Pointer • References to Objects • Passing on Object by Reference • Exercises 11.4 • Experiments 11.4 • Programming Problems 11.4

11.5 Dynamic Al locat ion of Objects 572 Using new and de le te With Objects • Exercises 11.5 • Experiments 11.5 • Programming Problems 11.5

11.6 Static Data Members and Functions 580 Static Data Members • Static Member Functions • Experiments 11.6 • Programming Problems 11.6

Chapter Review 591 Terminology • Summary • Review Exercises

Chapter 12 Friends and Operator Overloading 594 Objectives 594

12.1 f r i e n d Functions 595 Defining a f r i e n d Function • Exercises 12.1 • Experiments 12.1 • Programming Problems 12.1

12.2 Overloading Basic Ar i thmet ic Operators 603 Adding Two Objects • Adding an Object and an Integer • Operator Overloading Rules • Exercises 12.2 • Experiments 12.2 • Programming Problems 12.2

12.3 Overloading Assignment and the t h i s Pointer 611 Overloading Assignment • The t h i s Pointer • Exercises 12.3 • Experiments 12.3 • Programming Problems 12.3

Page 8: C++ for Business Programming - GBV

Contents

12.4 Overloading the Insertion and Extraction Operators 620 Overloading the Insertion Operator • Overloading the Extraction Operator • Exercises 12.4 • Experiments 12.4 • Programming Problems 12.4

12.5 Overloading Compound Assignment Operators 631 Overloading += and - = • Exercises 12.5 • Programming Problems 12.5

12.6 Overloading Relational Operators 637 Overloading == • Exercises 12.6 • Programming Problems 12.6.

12.7 Overloading Unary Operators 644 Overloading++ • Overloading the Post-increment++ • Exercises 12.7 • Experiments 12.7 • Programming Problems 12.7

Chapter Review 649 Terminology • Summary • Review Exercises

Chapter 13 Inheritance 652 Objectives 652 Introduct ion 653

13.1 Inheritance 653 Examples of Inheritance and Basic Terminology • Defining Derived Classes • Constructors in a Derived Class • The Destructor in a Derived Class • The Copy Constructor in a Derived Class • Exercises 13.1 • Experiments 13.1 • Programming Problems 13.1

13.2 Functions in Class Hierarchies 671 Inheriting a Function • Function Overriding • Using the Scope Resolution Operator • Exercises 13.2 • Experiments 13.2 • Programming Problems 13.2

13.3 Polymorphism 683 The Class Hierarchy • Defining the Classes • Pointers in a Class Hierarchy • v i r t u a l Functions and Polymorphism • v i r t u a l Destructors • Putting Polymorphism to Work • Exercises 13.3 • Experiments 13.3 • Programming Problems 13.3

13.4 Abstract Base Classes 703 Pure v i r t u a l Functions and Abstract Base Classes • The Loan Class • Exercises 13.4 • Experiments 13.4 • Programming Problems 13.4

Chapter Review 716 Terminology • Summary • Review Exercises

Chapter 14 Files 720 Objectives 720

14.1 Input /Output Streams 721 The Standard I/O Streams: Revisiting e in and с out • Text Files • User-Declared Files • Declaring the File Object • Opening the File • Processing the File • Closing the File • Checking for Errors When Opening a File • Creating a Text File of Records • File Input • Processing a Text File of Records • File Input/Output • Exercises 14.1 • Experiments 14.1 • Programming Problems 14.1

Page 9: C++ for Business Programming - GBV

Contents xi

14.2 Processing a File One Character at a Time 723 The get () and put () Member Functions • Output to the Printer • Exercises 14.2 • Experiments 14.2 • Programming Problems 14.2

14.3 Random Access Files 749 Text and Binary Files • Random Access Member Functions • Exercises 14.3 • Experiments 14.3 • Programming Problems 14.3

14.4 Binary Sequential File Processing 757 Creating a Binary Sequential File of Records • Reading a Sequential File of Records • Exercises 14.4 • Experiments 14.4 • Programming Problems 14.4

14.5 Random Record Input /Output 767 Random Access File Update • Program Design • Program Code • Experiments 14.5 • Programming Problems 14.5

Chapter Review 785 Terminology • Summary • Review Exercises

Appendix A Computers and Data 789 A.1 A model Computer System 789

Main Memory • The Control Unit • The Registers and the Arithmetic/Logic Unit • The Operating System • Input and Output Ports

A.2 Data Representation—Characters 792 The ASCII Code • The Binary Number System and Characters • The Hexadecimal Number System and Characters

A.3 Data Representation—Integers 796 Binary Integers • Negative Integers

A.4 Data Representation—Real Numbers 796

Exercises Appendix A 797

Appendix В Program Control 799 B.1 Sequence 799

B.2 Selection 800 The i f Statement • Nested i f Statements • The Case Structure

B.3 Iteration 804 The wh i l e Loop • The do Loop • Definite Iteration • Infinite Loops • Improper Loop Exits

B.4 Combinations 808

Index 811