INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

13
INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY WILLIAM H. FORD WILLIAM R. TOPP University of the Pacific \An Alan R. Apt Books Prentice Hall, Upper Saddle River, NJ 07458

Transcript of INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

Page 1: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

I N T R O D U C T I O N T O

COMPUTING—C++-OBJECT TECHNOLOGY

WILLIAM H. FORD

WILLIAM R. TOPP

University of the Pacific

\An Alan R. Apt Books

Prentice Hall, Upper Saddle River, NJ 07458

Page 2: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

Contents 1 Introduction to Object Technology

1.1 Elements of Problem Solving 2 Problem Solving and Objects 3

1.2 Computers and Problem Solving 4 1.3 Describing Computer Objects 7

Initializing Object Attributes 9 Visual Model of Object Types 9

1.4 Programming with C++Objects 10 Language Identifiers 11 Declaring and Using Objects 11 A First C++ Program 12 The C++ Programming Environment 16

1.5 Exploring Rectangle Objects 17 1.6 Drawing Figures: Rectangles 20

1.7 The C++ Programming Language 23 Chapter 1 Summary 24

Object Types (Classes) in the Chapter 24

Key Terms 24 Review Exercises 25

Answers to Review Exercises 27 Written Exercises 28 Programming Exercises 31 Programming Project 33

2 Basic C++ Types and Programs

2.1 Integer Objects and Simple Expressions 36 Declaring Integer Objects 37

Basic Integer Operators 39 Assignment Operators 41

2.2 C++ Input and Output 42 Sending Output to the Screen 42 Formatting the Output 44 Reading from the Keyboard 46 Initializing Objects during Program Execution

Page 3: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

XVÜi C o n t e n t s

2.3 Character Objects 50 Escape Codes 51 Character I/O 52 Representing Character Data 54 Character Operations 56

2.4 Real Number Objects 58 C++ Real Number Types 59 Exploring Real Numbers in Linear Equations 62 Named Constants 64

2.5 Working with C++ Operators 66 Operator Precedence 66 Operator Associativity 67 Type Conversion 67

2.6 Extending Arithmetic Operators 71 Compound Assignment Operators 71 Increment Operators 72

2.7 The String Type 73 String Object Declaration 73

String Input/Output 74

String Operations 75 Chapter 2 Summary 78

Object Types (Classes) in the Chapter 79 Key Terms 79

Review Exercises 80

Answers to Review Exercises 82 Written Exercises 82 Programming Exercises 87 Programming Projects 90

3 Describing and Declaring Classes 92

3.1 Class Descriptions 93 Describing Operations As Functions 94 Constructor 95

The StudentAccount Class: Formal Description 95 3.2 Declaring and Using Objects 96

Calling Member Functions 97 Application: Student Account Transactions 98

3.3 Describing the Rectangle Class 101

3.4 A Visual Model of Classes 103

Page 4: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

C o n t e n t s

3.5 The C++ Class Declaration 104 Moving from Description to Declaration 107

3.6 Classes and Problem Solving 107 3.7 Function Prototypes with Default Values 111

The GradeRecord Class 113 3.8 Drawing Circle Figures 116 3.9 Free Functions 118

Pure Object-Oriented Design 118 Structured Design 119 Program Design Using C++ 120 Building C++ Programs with Objects and Free Functions 121 Exploring the Math Library 122 The Drawing Functions 124 Application: Using Graphics Functions and Objects 125

3.10 Defining Free Functions in a Main Program 127 Chapter 3 Summary 131

Classes in the Chapter 132 Key Terms 132

Review Exercises 133 Answers to Review Exercises 135

Written Exercises 137 Programming Exercises 141 Programming Project 144

Basic C++ Control Structures

4.1 Algorithms and Flow Control 146 Selection 147 Looping 149

4.2 Logical Expressions 150 Relational Operators 150 Logical Operators 152 Precedence of Arithmetic and Logical Operators 154 Short-Circuit Evaluation 154 Interpreting the OR Operation 155

4.3 Selection Statements 155 One-way Selection Using the if Statement 155 Grouping Statements in a Block 156 Two-Way Selection Using the if /else Statement 158 Multiple Selection Using Nested if/else 161

Page 5: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

C o n t e n t s

The Roots of a Quadratic Equation 163 4.4 The Boolean Type in C++ 165

Checking Graduation Requirements 167 Integers As Logical Values 169

4.5 Loop Structures 170 The while Statement 171 Counter and Event-Controlled Loops 173

The do/while Statement 179 Loops and Numeric Palindromes 180

4.6 Animation with Loops 185 Jack in the Box Animation 186

Chapter 4 Summary 190

Classes in the Chapter 190 Key Terms 190

Review Exercises 192

Answers to Review Exercises 193 Written Exercises 194 Programming Exercises 201 Programming Project 203

Developing Your Own Classes

5.1 Implementing Classes 204

Illustrating Member Function Implementations 205 Implementing the Constructor 206 Implementing the Rectangle Class 208

5.2 Organizing Program Source Code 210 Building A Class Header File 210 Building the Main Program 211

Building and Using the Accumulator Class 213 Alternative Source Code Design (Optional) 215

5.3 Error Checking in Class and Program Design 217

5.4 Private Member Functions 219 The Time24 Class 219

Time24 Class Declaration and UML 221 Implementation of the Time24 Class 223

5.5 Member Functions with Loops: the Loan Class 225 The Loan Class 226

Implementing the Loan Class 228

5.6 Inline Code 230 The Circle Class Using Inline Code 230

Page 6: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

C o n t e n t s XXI

Implementing Inline Code 232 Application: Enclosing Maximum Area 232 Inline Code for Free Functions (Optional) 234

Chapter 5 Summary 235 Classes in the Chapter 236 Key Terms 236

Review Exercises 237 Answers to Review Exercises 238

Written Exercises 240 Programming Exercises 246 Programming Project 248

6 Additional C++ Control Structures 250

6.1 Multiple Selection: Nested If Statements 251 The "Dangling" Else 253

6.2 Multiple Selection: Switch Statement 255 6.3 Enumeration Types 261

Properties of Enumeration Types 263 6.4 Building an Enumeration Class 265

Implementation of the Days class 267 6.5 The Date Class 269

Date Class Implementation 272 6.6 The For Loop 275 6.7 For Loop Applications 278

Number Theory: Exploring Factors 279 Approximating a Circle 280

6.8 Advanced Loop Concepts 283 Designing Nested Loops 283 Generalized For Loops 286 Loop Break and Continue Statements 288

6.9 Argument Passing 292 Pass by Value 292 Pass by Reference 294

6.10 Constant Function Arguments 302 6.11 Conditional Expressions (OPTIONAL) 303 6.12 Text File I/O 306

C++ Stream Classes 307 Creating File Stream Objects 308 Reading and Writing File Data 310 File Error Checking 311

Page 7: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

XXÜ C o n t e n t s

Streams as Function Arguments (OPTIONAL) 316 Stream States and Errors (OPTIONAL) 319

Chapter 6 Summary 319 Classes in the Chapter 320 Key Terms 320

Review Exercises 323 Answers to Review Exercises 324

Written Exercises 326 Programming Exercises 334 Programming Projects 337

7 Arrays 339

7.1 Introducing Arrays 340 Application: Weather Statistics 344

7.2 Array Storage 346 Array Bounds Checking 349

7.3 Initializing Arrays 349 Array Initialization in the Date Class 352

7.4 Arrays as Arguments 353 The Statistics Class 357

7.5 Arrays Of Objects 361 Default Constructor 361 Application: Painter's Job Schedule 364

7.6 Random Numbers 367 Estimating TT 372

7.7 Sequential Array Algorithms 374 Sequential Search 374 Removing a List Element 376 Application: Removing Duplicates 377

7.8 Arrays as Class Data Members 380 Const Member Functions 382 Application: Using the FinanceCenter Class 383 Implementing the FinanceCenter Class 385

7.9 Graphing Dice Tosses 387 Graphing Dice Probabilities 391

7.10 Array Sorting Algorithm 395 7.11 String Objects 398

The Index Operator [] 399 String I/O 399

Page 8: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

C o n t e n t s

7.12 Additional String Member Functions (OPTIONAL) 404 String Search Functions 405 Copying Substrings 406 Modifying a String 407 Analyzing File Names 407

7.13 Multidimensional Arrays 409 Two-dimensional Array Arguments 413 The SqMatrix Class 414 The Storage of Two Dimensional Arrays (OPTIONAL) 421

Chapter 7 Summary 422 Classes in the Chapter 423 Key Terms 423

Review Exercises 425 Answers to Review Exercises 428

Written Exercises 429 Programming Exercises 436 Programming Projects 439

Program Design and Algorithms

8.1 Object Composition 442 The Employee Class 443 UML Representation for Composition 446 The Constructor in Composition 446 The Triangle Class 448

8.2 Object and Program Design Principles 451 Object-Oriented Analysis 451 Object-Oriented Design 452 Object-Oriented Programming 453

8.3 Creating a Full Year Calendar 455 Designing the Calendar Class 456 Declaring the Calendar Class 459 Implementing the Member Functions 461 The Calendar Application 465

8.4 Accessing and Storing Objects 466 Scope 466 Storage Class 471

8.5 Recursive Algorithms 475 Finding Recursive Algorithms 477 Developing Recursive Functions 478

Page 9: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

XXIV C o n t e n t s

Tracing a Recursive Function 481 8.6 Recursion and Problem Solving 482

The Tower of Hanoi Puzzle 482 Traversing a Maze 486

Comparing Recursive and Iterative Algorithms 494 Chapter 8 Summary 495

Classes in the Chapter 496

Key Terms 496 Review Exercises 498

Answers to Review Exercises 500 Written Exercises 503 Programming Exercises 508 Programming Projects 509

9 Operator Overloading and Templates 511

9.1 Function Overloading 512 Overloading Class Member Functions 515

9.2 The Rational Number System 518 Defining Rational Numbers 518 Rational Number Arithmetic 519 Number Systems (OPTIONAL) 519 The Rational Class 520

9.3 Operator Overloading 522 Defining Overloaded Operators 523 The Rational Class (Extended) 526 Overloading the Stream I/O Operators 527

9.4 Member Function Overloading 530

9.5 Converting Rational Numbers 532 Mixed Numbers 537

9.6 Template Functions 540 Template Syntax 542 Template Expansion 542 Template-based Selection Sort 544

9.7 Generalized Searching 546 Binary Search 548 Comparing Search Algorithms (OPTIONAL) 552

9.8 Template Classes 555 Constructing a Template Class 556 Declaring Template Class Objects 558

Page 10: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

C o n t e n t s

Chapter 9 Summary 559 Classes in the Chapter 560 Key Terms 560

Review Exercises 562 Answers to Review Exercises 565

Written Exercises 566 Programming Exercises 572 Programming Projects 573

Pointers and Dynamic Memory

10.1 C++Pointers 577 Declaring Pointer Objects 578 Assigning Values to Pointers 579 Accessing Data with Pointers 580

10.2 Arrays and Pointers 583 Pointer Arithmetic 583 Arrays As Pointer Arguments 585 Pointers and Class Types 588

10.3 Dynamic Memory 590 The Memory Allocation Operator new 590 Dynamic Array Allocation 592 Dynamically Allocated Class Objects 593 The Memory Deallocation Operator delete 595

10.4 Classes Using Dynamic Memory 599 The DynamicDemo Class 600

10.5 The Assignment Operator and Copy Constructor 605 The Assignment Operator for DynamicDemo Objects 606 The Class this Pointer 610 The Copy Constructor for DynamicDemo Objects 611

10.6 Building a Vector Class 616 Declaring the Vector Class 617 Applications Using Vectors 620 Implementing the Vector Class 624

10.7 C++ Strings 629 C++ String I/O 631 C++ String Handling Functions 634 Implementing the String Class 636

Chapter 10 Summary 638 Classes in the Chapter 639

Page 11: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

XXVI C o n t e n t s

Key Terms 639 Review Exercises 640

Answers to Review Exercises 643 Written Exercises 645 Programming Exercises 650 Programming Projects 651

/ / Containers and Linked Lists

11.1 The List Container 655 Creating List Objects 656

Accessing General List Elements 659 General Insert and Erase Operations 663 Application: Removing Duplicates 665

Iterators 668 11.2 Linked Lists 667

Describing a Linked List 668 Computer Representation of a Linked List 669 The Node Class 670 Building a Linked List 674

11.3 Building Linked Lists with Nodes 675 Creating a Node 676 Inserting at the Front of a List 676 Deleting at the Front of a List 677 Inserting a Node at the Rear of a List 678 Clearing a List 679 Removing a Target Node 681

11.4 Implementing the List Class 684 UML Representation for the List Class 684 List Class Private Members 685 List Iterators 687 Selected Member Functions 690

The General List Insertion Member Function 693 Chapter 11 Summary 695

Classes in the Chapter 696 Key Terms 696

Review Exercises 697 Answers to Review Exercises 700

Written Exercises 702

Page 12: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

C o n t e n t s

Programming Exercises 707 Programming Project 709

12 Inheritance and Virtual Functions

12.1 Inheritance in C++ 713 Declaring Derived Classes 715 Delcaring the Graphics Hierarchy 717 Constructors And Derived Classes 720

12.2 An Employee Hierarchy 721 12.3 Ordered Lists 726

Application: List Insertion Sort 728 Ordered List Class Implementation 730

12.4 Polymorphism and Virtual Functions 730 Virtual Functions in an Inheritance Hierarchy 732 Application: Painting Houses using Polymorphism 734

12.7 Geometric Figures And Virtual Functions 737 Implementation of the PolyShape Class 739 Application: Building a Kaleidoscope 742

12.8 Advanced Inheritance Topics 746 Virtual Functions And The Destructor 746 Abstract Base Classes 749

Chapter 12 Summary 751 Classes in the Chapter 752 Key Terms 752

Review Exercises 753 Answers to Review Exercises 756

Written Exercises 757 Programming Exercises 765 Programming Project 766

Appendix A Computer Data Storage

Binary Numbers A-l Converting Binary and Decimal Numbers A-2 Storing and Retrieving Numbers in a Computer A-3 Primitive Data Types A-5

The Object Type char A-5 The Object Type short A-5

The Object Type long A-5

Page 13: INTRODUCTION TO COMPUTING—C++- OBJECT TECHNOLOGY

XXVIII C o n t e n t s

The Storage of Real Numbers A-5 The C++ size of Operator A-7

Appendix B Character Representations B

ASCII Character Set B-l

Control Codes B-2 Printable Characters B-2

EBCDIC Character Set B-3 Unicode Character Set B-4

Appendix C C++ Operator Summary C

Appendix D Stream Formatting D

Format Flags D-l Handling Format Flags D-2

One Argument Form of setf() D-3 Two Argument Form of setf() D-3

Clearing Flags with unsef () D-5 Accessing Flag Values D-5

Adjusting the Width of Output D-6 Adjusting the Fill Character D-6 Setting Decimal Point Precision D-6

I/O Manipulators D-7

Width Manipulator: setw(n) D-8

Precision Manipulator: setprecision(n) D-8

Appendix E The C++ Preprocessor. f

#include Directive E-l #define Directive E-2 #Conditional Compilation E-4

Appendix F Namespaces and the ANSI/ISO C++ Standard Library F

The Keyword using F-2 The C++ Standard Library F-3