Lost your password? | Sorjonen HDTV 720p AC3 5.1 | Ernie Dingo
Cpphtp8e_final:Layout 1

1/19/11

10:30 AM

Page 2

Deitel® How to Program Series Cover Theme The cover theme for the DEITEL® HOW TO PROGRAM SERIES emphasizes social consciousness issues such as going green, clean energy, recycling, sustainability and more. Within the text, in addition to conventional programming exercises, we’ve included our Making a Difference exercise set to raise awareness of issues such as global warming, population growth, affordable healthcare, accessibility, privacy of electronic records and more. In this book, you’ll use C++ to program applications that relate to these issues. We hope that what you learn in C++ How to Program, 8/e will help you to make a difference.

Continued from Back Cover

❝ I really like the Making a Difference programming exercises. The game programming [in the Functions chapter] gets students excited.❞—Virginia Bailey, Jackson State University ❝ It’s great that the text introduces object-oriented programming early. The car analogy was well-thought out. An

extremely meticulous treatment of control structures. The virtual function figure and corresponding explanation in the Polymorphism chapter is thorough and truly commendable .❞—Gregory Dai, eBay, Inc. ❝ An excellent ‘objects first’ coverage of C++ that remains accessible to beginners. The example-driven presentation is enriched by the optional OO design case study that contextualizes the material in a software engineering project.❞

—Gavin Osborne, Saskatchewan Institute of Applied Science and Technology

Rainforests The world’s rainforests are often referred to as the “Earth’s lungs,” the “jewels of the Earth” and the “world’s largest pharmacy.” Approximately 50% of the world’s tropical rainforests are in Central and South America, over 33% are in Asia and Oceania (which consists of Australia, New Zealand and various South Pacific Islands), and 15% are in Africa. Rainforests absorb from the atmosphere vast amounts of carbon dioxide—a gas that many scientists blame for global warming—and they provide approximately 40% of the world’s oxygen. They regulate water flow to surrounding areas preventing mudslides and crop loss. Rainforests also support the livelihoods of 1.6 billion people, providing food, fresh water, medicines and more. Approximately 25% of Western medicines used to treat infections, viruses, cancer and more are derived from plants found in rainforests. The U.S. National Cancer Institute has found about 2100 rainforest plant species that are effective against cancer cells. Fewer than one percent of rainforest plant species have been tested for medical use. Rainforests are being deforested at an alarming rate. According to a March 2010 report by the United Nations Food and Agriculture Organization, deforestation has slowed over the last 10 years, but more than 30 million acres of forests are still lost annually, and they’re not easily renewed. The United Nations Environment Programme Plant for the Planet: Billion Tree Campaign is one of many reforestation initiatives. To learn more about how you can make a difference, visit www.unep.org/billiontreecampaign/index.asp. For further information visit: www.rain-tree.com/facts.htm www.savetherainforest.org/savetherainforest_007.htm en.wikipedia.org/wiki/Rainforest www.rainforestfoundation.org/

About Deitel & Associates, Inc. Deitel & Associates, Inc., is an internationally recognized authoring and corporate training organization. The company offers instructor-led courses delivered at client sites worldwide on programming languages and other software topics such as C++, Visual C++®, C, Java™, C#®, Visual Basic®, Objective-C®, XML®, Python®, JavaScript, object technology, Internet and web programming, and Android and iPhone app development. The company’s clients include many of the world’s largest companies, as well as government agencies, branches of the military and academic institutions. To learn more about Deitel Pearson Higher Education publications and Dive Into® Series corporate training, e-mail [email protected] or visit www.deitel.com/training/. Follow Deitel on Facebook® at www.deitel.com/deitelfan/ and on Twitter® @deitel.

❝ It is excellent that [the authors] use the STL and standard libraries early.❞—John Dibling, SpryWare ❝ Provides a complete basis of fundamental instruction in all core aspects of C++. Examples provide a solid grounding in the construction of C++ programs. A solid overview of C++ Stream I/O.❞

—Peter DePasquale, The College of New Jersey ❝ Great discussion about the mistakes resulted from using = for == and vice versa! ❞

—Wing-Ning Li, University of Arkansas ❝ Thorough and detailed coverage of exceptions from an object-oriented point of view.❞

—Dean Mathias, Utah State University ❝ Chapter 20 (Data Structures) is very good. The examples are accessible to CS, IT, software engineering and business students.❞—Thomas J. Borrelli, Rochester Institute of Technology ❝ The Simpletron exercises are brilliant. The Polymorphism chapter explains one of the hardest topics to understand

in OOP in a clear manner. Great job! The writing is excellent, the examples are well developed and the exercises are interesting.❞— José Antonio González Seco, Parliament of Andalusia, Spain ❝ Introducing the UML early is a great idea.❞—Raymond Stephenson, Microsoft ❝ Good use of diagrams, especially of the activation call stack and recursive functions.❞

—Amar Raheja, California State Polytechnic University, Pomona ❝ Terrific discussion of pointers—the best I have seen.❞—Anne B. Horton, Lockheed Martin ❝ Great coverage of polymorphism and how the compiler implements polymorphism ‘under the hood.’ I wish I had such a clear presentation of data structures when I was a student.❞—Ed James-Beckham, Borland ❝ A nice introduction to searching and sorting, and Big-O.❞—Robert Myers, Florida State University ❝ Will get you up and running quickly with the memory management and regular expression libraries.❞

—Ed Brey, Kohler Co. ❝ Excellent introduction to the Standard Template Library (STL). The best book on C++ programming for the serious student! ❞—Richard Albright, Goldey-Beacom College ❝ Each code example is completely reviewed. This is a critical step for students to learn good programming practices.❞

—Jack R. Hagemeister, Washington State University ❝ The most thorough C++ treatment I’ve seen. Replete with real-world case studies covering the full software development lifecycle. Code examples are extraordinary! ❞—Terrell Hull, Logicalis Integration Solutions

D e i te l ® Se r i es P a ge How To Program Series

C++ How to Program, 8/E C How to Program, 6/E Java™ How to Program, 9/E Java™ How to Program, Late Objects Version, 8/E Internet & World Wide Web How to Program, 4/E Visual C++® 2008 How to Program, 2/E Visual Basic® 2010 How to Program Visual C#® 2010 How to Program, 3/E Small Java™ How to Program, 6/E Small C++ How to Program, 5/E

Simply Series Simply C++: An App-Driven Tutorial Approach Simply Java™ Programming: An App-Driven Tutorial Approach Simply C#: An App-Driven Tutorial Approach Simply Visual Basic® 2008, 3/E: An App-Driven Tutorial Approach

CourseSmart Web Books www.deitel.com/books/CourseSmart/

C++ How to Program, 5/E, 6/E, 7/E & 8/E Simply C++: An App-Driven Tutorial Approach Java™ How to Program, 6/E, 7/E, 8/E & 9/E (continued next column)

(continued) Simply Visual Basic 2008: An App-Driven Tutorial Approach, 3/E Visual Basic® 2010 How to Program Visual Basic® 2008 How to Program Visual C#® 2010 How to Program, 4/E Visual C#® 2008 How to Program, 3/E

Deitel® Developer Series C++ for Programmers AJAX, Rich Internet Applications and Web Development for Programmers Android for Programmers: An App-Driven Approach C# 2010 for Programmers, 3/E iPhone for Programmers: An App-Driven Approach Java™ for Programmers JavaScript for Programmers

LiveLessons Video Learning Products www.deitel.com/books/LiveLessons/

C++ Fundamentals Java™ Fundamentals C# Fundamentals iPhone® App Development Fundamentals JavaScript Fundamentals Visual Basic Fundamentals

To receive updates on Deitel publications, Resource Centers, training courses, partner offers and more, please register for the free Deitel® Buzz Online e-mail newsletter at: www.deitel.com/newsletter/subscribe.html

follow us on Twitter® @deitel

and become a Deitel & Associates fan on Facebook® www.deitel.com/deitelfan/

To communicate with the authors, send e-mail to: [email protected]

For information on government and corporate Dive-Into® Series on-site seminars offered by Deitel & Associates, Inc. worldwide, visit: www.deitel.com/training/

or write to [email protected]

For continuing updates on Prentice Hall/Deitel publications visit: www.deitel.com www.pearsonhighered.com/deitel/

Check out our Resource Centers for valuable web resources that will help you master C++, other important programming languages, software, and Internet- and web-related topics: www.deitel.com/ResourceCenters.html

Paul Deitel

Deitel & Associates, Inc.

Harvey Deitel

Deitel & Associates, Inc.

Vice President and Editorial Director: Marcia J. Horton Editor-in-Chief: Michael Hirsch Associate Editor: Carole Snyder Vice President, Marketing: Patrice Jones Marketing Manager: Yezan Alayan Senior Marketing Coordinator: Kathryn Ferranti Vice President, Production: Vince O’Brien Managing Editor: Jeff Holcomb Associate Managing Editor: Robert Engelhardt Operations Specialist: Lisa McDowell Art Director: Linda Knowle Cover Design: Abbey S. Deitel, Harvey M. Deitel, Marta Samsel Cover Photo Credit: © James Hardy/PhotoAlto/Getty Images Media Editor: Daniel Sandin Media Project Manager: Wanda Rockwell Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on page vi. The authors 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 authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in this book. The authors 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. Copyright © 2012, 2008, 2005, 2003, 2001 Pearson Education, Inc., publishing as Prentice Hall. All rights reserved. Manufactured in the United States of America. This publication is protected by Copyright, and permission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission(s) to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, 501 Boylston Street, Suite 900, Boston, Massachusetts 02116. Many of the designations by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed in initial caps or all caps. Library of Congress Cataloging-in-Publication Data Deitel, Paul J. C++ : how to program / P.J. Deitel, H.M. Deitel. -- 8th ed. p. cm. Includes index. ISBN 978-0-13-266236-9 1. C++ (Computer program language) I. Deitel, Harvey M. II. Title. QA76.73.C153D45 2012 005.13'3--dc22 2011000245

10 9 8 7 6 5 4 3 2 1 ISBN-10: 0-13-266236-1 ISBN-13: 978-0-13-266236-9

In memory of Ken Olsen, Founder of Digital Equipment Corporation (DEC): We are deeply grateful for the opportunities DEC extended to us, enabling us to form and grow Deitel & Associates, Inc. Paul and Harvey Deitel

Trademarks DEITEL, the double-thumbs-up bug and DIVE INTO are registered trademarks of Deitel and Associates, Inc. Microsoft and the Windows logo are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. UNIX is a registered trademark of The Open Group. Throughout this book, trademarks are used. Rather than put a trademark symbol in every occurrence of a trademarked name, we state that we are using the names in an editorial fashion only and to the benefit of the trademark owner, with no intention of infringement of the trademark.

Contents Chapters 25–26 and Appendices F–I are PDF documents posted online at the book’s Companion Website, which is accessible from www.pearsonhighered.com/deitel.

Preface

1

1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 1.12 1.13

Introduction to Computers and C++

xxi

1.14 1.15

Introduction Computers: Hardware and Software Data Hierarchy Computer Organization Machine Languages, Assembly Languages and High-Level Languages Introduction to Object Technology Operating Systems Programming Languages C++ and a Typical C++ Development Environment Test-Driving a C++ Application Web 2.0: Going Social Software Technologies Future of C++: TR1, the New C++ Standard and the Open Source Boost Libraries Keeping Up-to-Date with Information Technologies Wrap-Up

2

Introduction to C++ Programming

2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8

Introduction First Program in C++: Printing a Line of Text Modifying Our First C++ Program Another C++ Program: Adding Integers Memory Concepts Arithmetic Decision Making: Equality and Relational Operators Wrap-Up

1

2 5 6 7 9 10 13 15 17 21 27 29 31 32 32

37 38 38 42 43 47 48 51 55

viii

3

Contents

Introduction to Classes, Objects and Strings

64

3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9

Introduction Defining a Class with a Member Function Defining a Member Function with a Parameter Data Members, set Functions and get Functions Initializing Objects with Constructors Placing a Class in a Separate File for Reusability Separating Interface from Implementation Validating Data with set Functions Wrap-Up

4

Control Statements: Part 1

101

5

Control Statements: Part 2

152

4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 4.12 4.13

5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11

Introduction Algorithms Pseudocode Control Structures if Selection Statement if…else Double-Selection Statement while Repetition Statement Formulating Algorithms: Counter-Controlled Repetition Formulating Algorithms: Sentinel-Controlled Repetition Formulating Algorithms: Nested Control Statements Assignment Operators Increment and Decrement Operators Wrap-Up

Introduction Essentials of Counter-Controlled Repetition for Repetition Statement Examples Using the for Statement do…while Repetition Statement switch Multiple-Selection Statement break and continue Statements Logical Operators Confusing the Equality (==) and Assignment (=) Operators Structured Programming Summary Wrap-Up

65 65 68 71 77 81 84 90 95

102 102 103 104 107 108 113 114 120 130 134 135 138

153 153 155 158 162 164 173 174 179 180 185

Contents

6

ix

6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10 6.11 6.12 6.13 6.14 6.15 6.16 6.17 6.18 6.19 6.20 6.21 6.22

Functions and an Introduction to Recursion

Introduction Program Components in C++ Math Library Functions Function Definitions with Multiple Parameters Function Prototypes and Argument Coercion C++ Standard Library Headers Case Study: Random Number Generation Case Study: Game of Chance; Introducing enum Storage Classes Scope Rules Function Call Stack and Activation Records Functions with Empty Parameter Lists Inline Functions References and Reference Parameters Default Arguments Unary Scope Resolution Operator Function Overloading Function Templates Recursion Example Using Recursion: Fibonacci Series Recursion vs. Iteration Wrap-Up

194

7

Arrays and Vectors

267

7.1 7.2 7.3 7.4

Introduction Arrays Declaring Arrays Examples Using Arrays 7.4.1 Declaring an Array and Using a Loop to Initialize the Array’s Elements 7.4.2 Initializing an Array in a Declaration with an Initializer List 7.4.3 Specifying an Array’s Size with a Constant Variable and Setting Array Elements with Calculations 7.4.4 Summing the Elements of an Array 7.4.5 Using Bar Charts to Display Array Data Graphically 7.4.6 Using the Elements of an Array as Counters 7.4.7 Using Arrays to Summarize Survey Results 7.4.8 Static Local Arrays and Automatic Local Arrays

195 196 197 198 203 205 207 212 215 218 221 225 225 227 231 232 234 236 239 242 245 248

268 269 270 271

271 272 273 275 276 277 278 281

x

Contents

7.5 7.6 7.7 7.8 7.9 7.10 7.11 7.12

Passing Arrays to Functions Case Study: Class GradeBook Using an Array to Store Grades Searching Arrays with Linear Search Sorting Arrays with Insertion Sort Multidimensional Arrays Case Study: Class GradeBook Using a Two-Dimensional Array Introduction to C++ Standard Library Class Template vector Wrap-Up

8

Pointers

330

9

Classes: A Deeper Look, Part 1

379

8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 8.10 8.11 8.12 8.13

9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9

Introduction Pointer Variable Declarations and Initialization Pointer Operators Pass-by-Reference with Pointers Using const with Pointers Selection Sort Using Pass-by-Reference sizeof Operator Pointer Expressions and Pointer Arithmetic Relationship Between Pointers and Arrays Pointer-Based String Processing Arrays of Pointers Function Pointers Wrap-Up

9.10 9.11

Introduction Time Class Case Study Class Scope and Accessing Class Members Separating Interface from Implementation Access Functions and Utility Functions Time Class Case Study: Constructors with Default Arguments Destructors When Constructors and Destructors Are Called Time Class Case Study: A Subtle Trap—Returning a Reference to a private Data Member Default Memberwise Assignment Wrap-Up

10

Classes: A Deeper Look, Part 2

10.1

Introduction

283 287 293 294 297 300 307 313

331 331 332 335 339 343 347 349 352 354 357 358 361

380 381 388 389 390 393 398 399 402 405 407

414 415

Contents

10.2 10.3 10.4 10.5 10.6 10.7 10.8

const (Constant) Objects and const Member Functions Composition: Objects as Members of Classes friend Functions and friend Classes Using the this Pointer static Class Members Proxy Classes Wrap-Up

11

Operator Overloading; Class string

11.1 11.2 11.3 11.4 11.5

11.11 11.12 11.13 11.14 11.15

Introduction Using the Overloaded Operators of Standard Library Class string Fundamentals of Operator Overloading Overloading Binary Operators Overloading the Binary Stream Insertion and Stream Extraction Operators Overloading Unary Operators Overloading the Unary Prefix and Postfix ++ and -- Operators Case Study: A Date Class Dynamic Memory Management Case Study: Array Class 11.10.1 Using the Array Class 11.10.2 Array Class Definition Operators as Member Functions vs. Non-Member Functions Converting between Types explicit Constructors Building a String Class Wrap-Up

12

Object-Oriented Programming: Inheritance

11.6 11.7 11.8 11.9 11.10

12.1 12.2 12.3 12.4

Introduction Base Classes and Derived Classes protected Members Relationship between Base Classes and Derived Classes 12.4.1 Creating and Using a CommissionEmployee Class 12.4.2 Creating a BasePlusCommissionEmployee Class Without Using Inheritance 12.4.3 Creating a CommissionEmployee– BasePlusCommissionEmployee Inheritance Hierarchy 12.4.4 CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using protected Data

xi

415 423 429 431 436 441 445

451 452 453 456 457 458 462 463 464 469 471 472 475 483 483 485 487 488

499 500 500 503 503 504

508 514 519

xii

Contents

12.4.5 12.5 12.6 12.7 12.8

CommissionEmployee–BasePlusCommissionEmployee Inheritance Hierarchy Using private Data Constructors and Destructors in Derived Classes public, protected and private Inheritance Software Engineering with Inheritance Wrap-Up

13

Object-Oriented Programming: Polymorphism 534

13.1 13.2 13.3

13.4 13.5 13.6

Introduction Introduction to Polymorphism: Polymorphic Video Game Relationships Among Objects in an Inheritance Hierarchy 13.3.1 Invoking Base-Class Functions from Derived-Class Objects 13.3.2 Aiming Derived-Class Pointers at Base-Class Objects 13.3.3 Derived-Class Member-Function Calls via Base-Class Pointers 13.3.4 Virtual Functions Type Fields and switch Statements Abstract Classes and Pure virtual Functions Case Study: Payroll System Using Polymorphism 13.6.1 Creating Abstract Base Class Employee 13.6.2 Creating Concrete Derived Class SalariedEmployee 13.6.3 Creating Concrete Derived Class CommissionEmployee 13.6.4 Creating Indirect Concrete Derived Class BasePlusCommissionEmployee

13.6.5 Demonstrating Polymorphic Processing (Optional) Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood” 13.8 Case Study: Payroll System Using Polymorphism and Runtime Type Information with Downcasting, dynamic_cast, typeid and type_info 13.9 Virtual Destructors 13.10 Wrap-Up

13.7

14 14.1 14.2 14.3 14.4

Templates

Introduction Function Templates Overloading Function Templates Class Templates

522 527 527 528 529

535 536 536 537 540

541 543 549 549 551 552 556 558 560 562 566 569 573 573

579 580 580 583 584

Contents

14.5 14.6

Nontype Parameters and Default Types for Class Templates Wrap-Up

15

Stream Input/Output

15.1 15.2

Introduction Streams 15.2.1 Classic Streams vs. Standard Streams 15.2.2 iostream Library Headers 15.2.3 Stream Input/Output Classes and Objects 15.3 Stream Output 15.3.1 Output of char * Variables 15.3.2 Character Output Using Member Function put 15.4 Stream Input 15.4.1 get and getline Member Functions 15.4.2 istream Member Functions peek, putback and ignore 15.4.3 Type-Safe I/O 15.5 Unformatted I/O Using read, write and gcount 15.6 Introduction to Stream Manipulators 15.6.1 Integral Stream Base: dec, oct, hex and setbase 15.6.2 Floating-Point Precision (precision, setprecision) 15.6.3 Field Width (width, setw) 15.6.4 User-Defined Output Stream Manipulators 15.7 Stream Format States and Stream Manipulators 15.7.1 Trailing Zeros and Decimal Points (showpoint) 15.7.2 Justification (left, right and internal) 15.7.3 Padding (fill, setfill) 15.7.4 Integral Stream Base (dec, oct, hex, showbase) 15.7.5 Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed) 15.7.6 Uppercase/Lowercase Control (uppercase) 15.7.7 Specifying Boolean Format (boolalpha) 15.7.8 Setting and Resetting the Format State via Member Function flags 15.8 Stream Error States 15.9 Tying an Output Stream to an Input Stream 15.10 Wrap-Up

16 16.1

Exception Handling: A Deeper Look Introduction

xiii

590 591

595 596 597 597 598 598 601 601 601 602 602 605 605 605 606 607 607 609 610 612 612 613 615 616 617 618 618 619 620 622 623

632 633

xiv

Contents

16.2 16.3 16.4 16.5 16.6 16.7 16.8 16.9 16.10 16.11 16.12 16.13

Example: Handling an Attempt to Divide by Zero When to Use Exception Handling Rethrowing an Exception Exception Specifications Processing Unexpected Exceptions Stack Unwinding Constructors, Destructors and Exception Handling Exceptions and Inheritance Processing new Failures Class unique_ptr and Dynamic Memory Allocation Standard Library Exception Hierarchy Wrap-Up

17

File Processing

658

18

Class string and String Stream Processing

696

17.1 17.2 17.3 17.4 17.5 17.6 17.7 17.8 17.9 17.10 17.11 17.12

18.1 18.2 18.3 18.4 18.5 18.6 18.7 18.8 18.9 18.10 18.11

Introduction Files and Streams Creating a Sequential File Reading Data from a Sequential File Updating Sequential Files Random-Access Files Creating a Random-Access File Writing Data Randomly to a Random-Access File Reading from a Random-Access File Sequentially Case Study: A Transaction-Processing Program Object Serialization Wrap-Up

Introduction string Assignment and Concatenation Comparing strings Substrings Swapping strings string Characteristics Finding Substrings and Characters in a string Replacing Characters in a string Inserting Characters into a string Conversion to C-Style Pointer-Based char * Strings Iterators

633 639 640 641 642 642 644 645 645 648 650 652

659 659 660 664 669 670 671 675 677 679 686 686

697 698 700 703 703 704 706 708 710 711 713

Contents

18.12 String Stream Processing 18.13 Wrap-Up

19

xv

714 717

Searching and Sorting

19.4

Introduction Searching Algorithms 19.2.1 Efficiency of Linear Search 19.2.2 Binary Search Sorting Algorithms 19.3.1 Efficiency of Selection Sort 19.3.2 Efficiency of Insertion Sort 19.3.3 Merge Sort (A Recursive Implementation) Wrap-Up

724

20

Custom Templatized Data Structures

746

21

Bits, Characters, C Strings and structs

791

19.1 19.2

19.3

20.1 20.2 20.3 20.4 20.5 20.6 20.7 20.8

21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.9 21.10 21.11 21.12

Introduction Self-Referential Classes Dynamic Memory Allocation and Data Structures Linked Lists Stacks Queues Trees Wrap-Up

Introduction Structure Definitions typedef

Example: Card Shuffling and Dealing Simulation Bitwise Operators Bit Fields Character-Handling Library Pointer-Based String Manipulation Functions Pointer-Based String-Conversion Functions Search Functions of the Pointer-Based String-Handling Library Memory Functions of the Pointer-Based String-Handling Library Wrap-Up

725 725 726 727 732 733 733 733 740

747 748 749 749 764 768 772 780

792 792 794 794 797 806 810 815 822 827 831 835

xvi

Contents

22

Standard Template Library (STL)

23

Boost Libraries, Technical Report 1 and C++0x 936

22.1 22.2 22.3 22.4 22.5

850

Introduction to the Standard Template Library (STL) 851 Introduction to Containers 853 Introduction to Iterators 856 Introduction to Algorithms 861 Sequence Containers 863 22.5.1 vector Sequence Container 864 22.5.2 list Sequence Container 871 22.5.3 deque Sequence Container 875 22.6 Associative Containers 877 22.6.1 multiset Associative Container 877 22.6.2 set Associative Container 880 22.6.3 multimap Associative Container 881 22.6.4 map Associative Container 883 22.7 Container Adapters 885 22.7.1 stack Adapter 885 22.7.2 queue Adapter 887 22.7.3 priority_queue Adapter 888 22.8 Algorithms 890 22.8.1 fill, fill_n, generate and generate_n 890 22.8.2 equal, mismatch and lexicographical_compare 892 22.8.3 remove, remove_if, remove_copy and remove_copy_if 895 22.8.4 replace, replace_if, replace_copy and replace_copy_if 879 22.8.5 Mathematical Algorithms 900 22.8.6 Basic Searching and Sorting Algorithms 903 22.8.7 swap, iter_swap and swap_ranges 905 22.8.8 copy_backward, merge, unique and reverse 906 22.8.9 inplace_merge, unique_copy and reverse_copy 909 22.8.10 Set Operations 910 22.8.11 lower_bound, upper_bound and equal_range 913 22.8.12 Heapsort 915 22.8.13 min and max 918 22.8.14 STL Algorithms Not Covered in This Chapter 919 22.9 Class bitset 920 22.10 Function Objects 924 22.11 Wrap-Up 927

23.1

Introduction

937

Contents

23.2 23.3 23.4 23.5

Deitel Online C++ and Related Resource Centers Boost Libraries Boost Libraries Overview Regular Expressions with the regex Library 23.5.1 Regular Expression Example 23.5.2 Validating User Input with Regular Expressions 23.5.3 Replacing and Splitting Strings 23.6 Smart Pointers 23.6.1 Reference Counted shared_ptr 23.6.2 weak_ptr: shared_ptr Observer 23.7 Technical Report 1 23.8 C++0x 23.9 Core Language Changes 23.10 Wrap-Up

24 24.1 24.2 24.3 24.4 24.5 24.6 24.7 24.8 24.9

Other Topics

Introduction const_cast Operator mutable Class Members namespaces Operator Keywords Pointers to Class Members (.* and ->*) Multiple Inheritance Multiple Inheritance and virtual Base Classes Wrap-Up

xvii

937 937 938 941 942 944 947 950 950 954 960 961 962 967

974 975 975 977 979 982 984 986 991 996

Chapters on the Web

1001

A

Operator Precedence and Associativity

1002

B

ASCII Character Set

1004

C

Fundamental Types

1005

D

Number Systems

1007

D.1

Introduction

1008

xviii

Contents

D.2 D.3 D.4 D.5 D.6

Abbreviating Binary Numbers as Octal and Hexadecimal Numbers Converting Octal and Hexadecimal Numbers to Binary Numbers Converting from Binary, Octal or Hexadecimal to Decimal Converting from Decimal to Binary, Octal or Hexadecimal Negative Binary Numbers: Two’s Complement Notation

E

Preprocessor

1020

Appendices on the Web

1033

Index

1035

E.1 E.2 E.3 E.4 E.5 E.6 E.7 E.8 E.9 E.10

Introduction #include Preprocessor Directive #define Preprocessor Directive: Symbolic Constants #define Preprocessor Directive: Macros Conditional Compilation #error and #pragma Preprocessor Directives Operators # and ## Predefined Symbolic Constants Assertions Wrap-Up

1011 1012 1012 1013 1015

1021 1021 1022 1022 1024 1025 1026 1026 1027 1027

Chapters 25–26 and Appendices F–I are PDF documents posted online at the book’s Companion Website, which is accessible from www.pearsonhighered.com/deitel.

25 25.1 25.2 25.3 25.4 25.5 25.6 25.7 25.8 25.9

ATM Case Study, Part 1: Object-Oriented Design with the UML

Introduction Introduction to Object-Oriented Analysis and Design Examining the ATM Requirements Document Identifying the Classes in the ATM Requirements Document Identifying Class Attributes Identifying Objects’ States and Activities Identifying Class Operations Indicating Collaboration Among Objects Wrap-Up

25-1 25-2 25-2 25-3 25-10 25-17 25-21 25-25 25-32 25-39

Contents

26 26.1 26.2 26.3 26.4

ATM Case Study, Part 2: Implementing an Object-Oriented Design

26.5

Introduction Starting to Program the Classes of the ATM System Incorporating Inheritance into the ATM System ATM Case Study Implementation 26.4.1 Class ATM 26.4.2 Class Screen 26.4.3 Class Keypad 26.4.4 Class CashDispenser 26.4.5 Class DepositSlot 26.4.6 Class Account 26.4.7 Class BankDatabase 26.4.8 Class Transaction 26.4.9 Class BalanceInquiry 26.4.10 Class Withdrawal 26.4.11 Class Deposit 26.4.12 Test Program ATMCaseStudy.cpp Wrap-Up

F

C Legacy Code Topics

F.1 F.2

xix

26-1 26-2 26-2 26-8 26-15 26-16 26-23 26-25 26-26 26-28 26-29 26-31 26-35 26-37 26-39 26-44 26-47 26-47

F-1

F.3 F.4 F.5 F.6 F.7 F.8 F.9 F.10 F.11 F.12 F.13 F.14

Introduction Redirecting Input/Output on UNIX/Linux/Mac OS X and Windows Systems Variable-Length Argument Lists Using Command-Line Arguments Notes on Compiling Multiple-Source-File Programs Program Termination with exit and atexit Type Qualifier volatile Suffixes for Integer and Floating-Point Constants Signal Handling Dynamic Memory Allocation with calloc and realloc Unconditional Branch: goto Unions Linkage Specifications Wrap-Up

F-2 F-3 F-5 F-7 F-9 F-10 F-10 F-11 F-13 F-14 F-15 F-18 F-19

G

UML 2: Additional Diagram Types

G-1

G.1 G.2

Introduction Additional Diagram Types

F-2

G-1 G-2

xx

Contents

H

Using the Visual Studio Debugger

H.1 H.2 H.3 H.4 H.5 H.6

Introduction Breakpoints and the Continue Command Locals and Watch Windows Controlling Execution Using the Step Into, Step Over, Step Out and Continue Commands Autos Window Wrap-Up

I

Using the GNU C++ Debugger

I.2

Breakpoints and the run, stop, continue and print Commands

I.1 I.3 I.4 I.5 I.6

Introduction

and set Commands Controlling Execution Using the step, finish and next Commands watch Command Wrap-Up

print

H-1 H-2 H-2 H-8

H-11 H-13 H-14

I-1 I-2 I-2 I-8

I-10 I-13 I-15

Preface “The chief merit of language is clearness …” —Galen

For the Student Welcome to the C++ computer programming language and C++ How to Program, Eighth Edition! This book presents leading-edge computing technologies, and is particularly appropriate for inroductory course sequences based on the curriculum recommendations of two key professional organizations—the ACM and the IEEE. The new Chapter 1 presents intriguing facts and figures. Our goal is to get you excited about studying computers and programming. The chapter includes a table of some of the research made possible by computers; current technology trends and hardware discussions; the data hierarchy; social networking; a table of business and technology publications and websites that will help you stay up-to-date with the latest technology news, trends and career opportunities; additional Making a Difference exercises and more. We focus on software engineering best practices. At the heart of the book is our signature “live-code approach”—programming concepts are presented in the context of complete working programs, rather than in code snippets. Each C++ code example is accompanied by live sample executions, so you can see exactly what each program does when it’s run on a computer. All the source code is available at www.deitel.com/books/ cpphtp8/ and www.pearsonhighered.com/deitel/. Much of this Preface is addressed to instructors. Please be sure to read the sections entitled Pedagogic Features; Teaching Approach; Software Used in C++ How to Program, 8/e; C++ IDE Resource Kit and CourseSmart Web Books. We believe that this book and its support materials will give you an informative, interesting, challenging and entertaining C++ educational experience. As you read the book, if you have questions, send an e-mail to [email protected]—we’ll respond promptly. For updates on this book, visit www.deitel.com/books/cpphtp8/, follow us on Facebook (www.deitel.com/deitelfan) and Twitter (@deitel), and subscribe to the Deitel ® Buzz Online newsletter (www.deitel.com/newsletter/subscribe.html). Good luck!

New and Updated Features Here are the updates we’ve made for C++ How to Program, 8/e:

Impending New C++ Standard • Optional sections. We cover various features of the new standard (sometimes called C++0x and due late in 2011 or early in 2012) in optional modular sections and in Chapter 23. These are easy to include or omit. Popular compilers such as Microsoft Visual C++ 2010 and GNU C++ 4.5 already implement many of these features. To

xxii

Preface enable the new standard features in GNU C++, use the -std=C++0x flag when you compile the corresponding programs.



Boost C++ Libraries, Technical Report 1 (TR1) and C++0x. In Chapter 23, we introduce the Boost C++ Libraries, Technical Report 1 (TR1) and C++0x. The free Boost open source libraries are created by members of the C++ community. Technical Report 1 describes the proposed changes to the C++ Standard Library, many of which are based on current Boost libraries. The C++ Standards Committee is revising the C++ Standard—the main goals are to make C++ easier to learn, improve library building capabilities, and increase compatibility with the C programming language. The new standard will include many of the libraries in TR1 and changes to the core language. We overview the Boost libraries and provide code examples for the “regular expression” and “smart pointer” libraries. Regular expressions are used to match specific character patterns in text. They can be used, for example, to validate data to ensure that it’s in a particular format, to replace parts of one string with another, or to split a string. Many common bugs in C and C++ code are related to pointers, a powerful programming capability you’ll study in Chapter 8. Smart pointers help you avoid errors by providing additional functionality to standard pointers.



vs. auto_ptr. We replaced our auto_ptr example with the impending standard’s class unique_ptr, which fixes various problems that were associated with class auto_ptr. Use of auto_ptr is deprecated and unique_ptr is already implemented in many popular compilers, including Visual C++ 2010 and GNU C++ 4.5.



Initializer lists for user-defined types. These enable objects of your own types to be initialized using the same syntax as built-in arrays.



Range-based for statement. A version of the for statement that iterates over all the elements of an array or container (such as an object of the vector class).



Lambda expressions. These enable you to create anonymous functions that can be passed to other functions as arguments.



auto storage class specifier. The keyword auto can no longer be used as a storage class specifier.



auto.



nullptr.This



static_assert. This capability allows you to test certain aspects of the program at compile time.



New long long and unsigned long long types. These new types were introduced for use with 64-bit machines.

unique_ptr

This keyword now deduces the type of a variable from its initializer. keyword is a replacement for assigning zero to a null pointer.

Pedagogic Features • Enhanced Making a Difference exercises set. We encourage you to use computers and the Internet to research and solve significant social problems. These exercises are meant to increase awareness and discussion of important issues the world is facing. We hope you’ll approach them with your own values, politics and beliefs.

New and Updated Features

xxiii

Check out our new Making a Difference Resource Center at www.deitel.com/ for additional ideas you may want to investigate further.

MakingADifference



Page numbers for key terms in chapter summaries. For key terms that appear in the chapter summaries, we include the page number of each term’s defining occurrence in the chapter.



VideoNotes. The Companion Website includes 15+ hours of VideoNotes in which co-author Paul Deitel explains in detail most of the programs in the core chapters. Instructors have told us that their students find the VideoNotes valuable for preparing for and reviewing lectures.



Modular presentation. We’ve grouped the chapters into teaching modules. The Chapter Dependency Chart (later in this Preface) reflects the modularization.

Object Technology • Object-oriented programming and design. We introduce the basic concepts and terminology of object technology in Chapter 1. Students develop their first customized classes and objects in Chapter 3. Presenting objects and classes early gets students “thinking about objects” immediately and mastering these concepts more thoroughly. [For courses that require a late-objects approach, consider C++ How to Program, Late Objects Version, Seventh Edition, which begins with six chapters on programming fundamentals (including two on control statements) and continues with seven chapters that gradually introduce object-oriented programming concepts.] • Integrated case studies. We provide several case studies that span multiple sections and chapters. These include development of the GradeBook class in Chapters 3–7, the Time class in Chapters 9–10, the Employee class in Chapters 12–13, and the optional OOD/UML ATM case study in Chapters 25–26. • Integrated GradeBook case study. The GradeBook case study uses classes and objects in Chapters 3–7 to incrementally build a GradeBook class that represents an instructor’s grade book and performs various calculations based on a set of student grades, such as calculating the average grade, finding the maximum and minimum, and printing a bar chart. • Exception handling. We integrate basic exception handling early in the book. Instructors can easily pull more detailed material forward from Chapter 16, Exception Handling: A Deeper Look. • Prefer vectors to C arrays. C++ offers two types of arrays—vector class objects (which we start using in Chapter 7) and C-style, pointer-based arrays. As appropriate, we use class template vector instead of C arrays throughout the book. However, we begin by discussing C arrays in Chapter 7 to prepare you for working with legacy code and to use as a basis for building your own customized Array class in Chapter 11. • Prefer string objects to C strings. Similarly, C++ offers two types of strings— string class objects (which we use starting in Chapter 3) and C-style, pointerbased strings. We continue to include some early discussions of C strings to give

xxiv

Preface you practice with pointer manipulations, to illustrate dynamic memory allocation with new and delete and to prepare you for working with C strings in the legacy code that you’ll encounter in industry. In new development, you should favor string class objects. We’ve replaced most occurrences of C strings with instances of C++ class string to make programs more robust and eliminate many of the security problems that can be caused by using C strings.



Optional case study: Using the UML to develop an object-oriented design and C++ implementation of an ATM. The UML™ (Unified Modeling Language™) is the industry-standard graphical language for modeling object-oriented systems. Chapters 25–26 include an optional online case study on object-oriented design using the UML. We design and implement the software for a simple automated teller machine (ATM). We analyze a typical requirements document that specifies the system to be built. We determine the classes needed to implement that system, the attributes the classes need to have, the behaviors the classes need to exhibit and specify how the classes must interact with one another to meet the system requirements. From the design we produce a complete C++ implementation. Students often report having a “light-bulb moment”—the case study helps them “tie it all together” and really understand object orientation.



Standard Template Library (STL). This might be one of the most important topics in the book in terms of your appreciation of software reuse. The STL defines powerful, template-based, reusable components that implement many common data structures and algorithms used to process those data structures. Chapter 22 introduces the STL and discusses its three key components—containers, iterators and algorithms. The STL components provide tremendous expressive power, often reducing many lines of code to a single statement.

Other Features • Printed book contains core content; additional chapters are online. Several online chapters are included for more advanced courses and for professionals. These are available in searchable PDF format on the book’s password-protected Companion Website—see the access card in the front of this book. • Reorganized Chapter 11, Operator Overloading; Class string. We reorganized this chapter to begin with standard library class string so readers can see an elegant use of operator overloading before they implement their own. We also moved the section on proxy classes to the end of Chapter 10, where it’s a more natural fit. • Enhanced use of const. We increased the use of const book-wide to encourage better software engineering. • Software engineering concepts. Chapter 1 briefly introduces very current software engineering terminology, including agile software development, Web 2.0, Ajax, SaaS (Software as a Service), PaaS (Platform as a Service), cloud computing, web services, open source software, design patterns, refactoring, LAMP and more. • Compilation and linking process for multiple-source-file programs. Chapter 3 includes a detailed diagram and discussion of the compilation and linking process that produces an executable program.

Our Text + Digital Approach to Content

xxv



Function Call Stack Explanation. In Chapter 6, we provide a detailed discussion with illustrations of the function call stack and activation records to explain how C++ is able to keep track of which function is currently executing, how automatic variables of functions are maintained in memory and how a function knows where to return after it completes execution.



Tuned Treatment of Inheritance and Polymorphism. Chapters 12–13 have been carefully tuned using a concise Employee class hierarchy. We use this same treatment in our C++, Java, C# and Visual Basic books—one of our reviewers called it the best he had seen in 25 years as a trainer and consultant.



Discussion and illustration of how polymorphism works “under the hood.” Chapter 13 contains a detailed diagram and explanation of how C++ can implement polymorphism, virtual functions and dynamic binding internally. This gives students a solid understanding of how these capabilities work.



ISO/IEC C++ standard compliance. We’ve audited our presentation against the ISO/IEC C++ standard document.



Debugger appendices. We provide two Using the Debugger appendices on the book’s Companion Website—Appendix H, Using the Visual Studio Debugger, and Appendix I, Using the GNU C++ Debugger.



Code tested on multiple platforms. We tested the code examples on various popular C++ platforms including GNU C++ on Linux and Microsoft Windows, and Visual C++ on Windows. For the most part, the book’s examples port to popular standard-compliant compilers.



Game Programming. Because of limited interest, we’ve removed from the book Chapter 27, Game Programming with Ogre (which covers only Linux). For instructors who would like to continue using this material with C++ How to Program, 8/e, we’ve included the version from C++ How to Program, 7/e on the book’s Companion Website.

Our Text + Digital Approach to Content We surveyed hundreds of instructors teaching C++ courses and learned that most want a book with content focused on their introductory courses. With that in mind, we moved various advanced chapters to the web. Having this content in digital format makes it easily searchable, and gives us the ability to fix errata and add new content as appropriate. The book’s Companion Website, which is accessible at www.pearsonhighered.com/deitel/

(see the access card at the front of the book) contains the following chapters in searchable PDF format: • Chapter 25, ATM Case Study, Part 1: Object-Oriented Design with the UML • Chapter 26, ATM Case Study, Part 2: Implementing an Object-Oriented Design • Game Programming with Ogre (from C++ How to Program, 7/e) • Appendix F, C Legacy Code Topics

xxvi

Preface

• Appendix G, UML 2: Additional Diagram Types • Appendix H, Using the Visual Studio Debugger • Appendix I, Using the GNU C++ Debugger The Companion Website also includes: •

Extensive VideoNotes—watch and listen as co-author Paul Deitel discusses the key features of the code examples in Chapters 2–13 and portions of Chapters 16 and 17.



Two true/false questions per section with answers for self-review.



Solutions to approximately half of the solved exercises in the book.

The following materials are posted at the Companion Website and at www.deitel.com/

books/cpphtp8/:



An array of function pointers example and additional function pointer exercises (from Chapter 8).



String



Building Your Own Compiler exercise descriptions (from Chapter 20).

Class Operator Overloading Case Study (from Chapter 11).

Dependency Chart The chart on the next page shows the dependencies among the chapters to help instructors plan their syllabi. C++ How to Program, 8/e is appropriate for CS1 and CS2 courses.

Teaching Approach C++ How to Program, 8/e, contains a rich collection of examples. We stress program clarity and concentrate on building well-engineered software. Live-code approach. The book is loaded with “live-code” examples—most new concepts are presented in the context of complete working C++ applications, followed by one or more executions showing program inputs and outputs. In the few cases where we use a code snippet, we tested it in a complete working program, then copied and pasted it into the book. Syntax coloring. For readability, we syntax color all the C++ code, similar to the way most C++ integrated-development environments and code editors syntax color code. Our coloring conventions are as follows: comments appear like this keywords appear like this constants and literal values appear like this all other code appears in black

Code highlighting. We place light blue shaded rectangles around each program’s key code segments. Using fonts for emphasis. We place the key terms and the index’s page reference for each defining occurrence in bold blue text for easy reference. We emphasize on-screen components in the bold Helvetica font (e.g., the File menu) and C++ program text in the Lucida font (for example, int x = 5;).

Teaching Approach

Chapter Dependency Chart

xxvii

Introduction 1 Introduction to Computers and C++

[Note: Arrows pointing into a chapter indicate that chapter’s dependencies.]

Intro to Programming, Classes and Objects 2 Intro to C++ Programming 3 Intro to Classes and Objects

Control Statements, Methods and Arrays 4 Control Statements: Part 1 5 Control Statements: Part 2 6 Functions and an Intro to Recursion

Legacy C Topics 21 Bits, Characters, C-Strings and structs

7 Arrays and Vectors 8 Pointers

Object-Oriented Programming Object-Oriented Design with the UML 25 (Optional) Object-Oriented Design with the UML 26 (Optional) Implementing an Object-Oriented Design

9 Classes: A Deeper Look, Part 1 10 Classes: A Deeper Look, Part 2

Data Structures

11 Operator Overloading

6.19–6.21 Recursion

12 OOP: Inheritance

Streams, Files and Strings 15 Stream Input/Output1

17 File Processing

18 Class string and String Stream Processing

1.Most of Chapter 15 is readable after Chapter 7. A small portion requires Chapters 12 and 14.

19 Searching and Sorting

13 OOP: Polymorphism 14 Templates

20 Custom Templatized Data Structures

16 Exception Handling: A Deeper Look

22 Standard Template Library

Other Topics and the Future of C++ 23 Boost Libraries, Technical Report 1 and C++0x

24 Other Topics

xxviii

Preface

Objectives. The opening quotes are followed by a list of chapter objectives. Illustrations/ figures. Abundant tables, line drawings, UML diagrams, programs and program outputs are included. Programming tips. We include programming tips to help you focus on important aspects of program development. These tips and practices represent the best we’ve gleaned from a combined seven decades of programming and teaching experience.

Good Programming Practices

The Good Programming Practices call attention to techniques that will help you produce programs that are clearer, more understandable and more maintainable.

Common Programming Errors

Pointing out these Common Programming Errors reduces the likelihood that you’ll make them.

Error-Prevention Tips

These tips contain suggestions for exposing and removing bugs from your programs; many describe aspects of C++ that prevent bugs from getting into programs in the first place.

Performance Tips

These tips highlight opportunities for making your programs run faster or minimizing the amount of memory that they occupy.

Portability Tips

The Portability Tips help you write code that will run on a variety of platforms.

Software Engineering Observations

The Software Engineering Observations highlight architectural and design issues that affect the construction of software systems, especially large-scale systems.

Summary bullets. We present a section-by-section bullet-list summary of the chapter with the page references to the defining occurrence for many of the key terms in each section. Self-review exercises and answers. Extensive self-review exercises and answers are included for self study. All of the exercises in the optional ATM case study are fully solved. Exercises. Each chapter concludes with a substantial set of exercises including: •

simple recall of important terminology and concepts



What’s wrong with this code?



What does this code do?



writing individual statements and small portions of functions and classes



writing complete functions, classes and programs



major projects.

Please do not write to us requesting access to the Pearson Instructor’s Resource Center which contains the book’s instructor supplements, including the exercise solutions. Ac-

Software Used in C++ How to Program, 8/e

xxix

cess is limited strictly to college instructors teaching from the book. Instructors may obtain access only through their Pearson representatives. Solutions are not provided for “project” exercises. Check out our Programming Projects Resource Center for lots of additional exercise and project possibilities (www.deitel.com/ProgrammingProjects/). Index. We’ve included an extensive index. Defining occurrences of key terms are highlighted with a bold blue page number.

Software Used in C++ How to Program, 8/e We wrote C++ How to Program, 8/e using Microsoft’s free Visual C++ Express Edition (which is available free for download at www.microsoft.com/express/downloads/) and the free GNU C++ (gcc.gnu.org/install/binaries.html), which is already installed on most Linux systems and can be installed on Mac OS X and Windows systems. Apple includes GNU C++ in their Xcode development tools, which Mac OS X users can download from developer.apple.com/technologies/tools/xcode.html.

C++ IDE Resource Kit Your instructor may have ordered through your college bookstore a Value Pack edition of C++ How to Program, 8/e that comes bundled with the C++ IDE Resource Kit. This kit contains CD or DVD versions of: •

Microsoft® Visual Studio 2010 Express Edition (www.microsoft.com/express/)



Dev C++ (www.bloodshed.net/download.html)



NetBeans (netbeans.org/downloads/index.html)



Eclipse (eclipse.org/downloads/)



CodeLite (codelite.org/LiteEditor/Download)

You can download these software packages from the websites specified above. The C++ IDE Resource Kit also includes access to a Companion Website containing step-by-step written instructions and VideoNotes to help you get started with each development environment. If your book did not come with the C++ IDE Resource Kit, you can purchase access to the Resource Kit’s Companion Website from www.pearsonhighered.com/cppidekit/.

CourseSmart Web Books Today’s students and instructors have increasing demands on their time and money. Pearson has responded to that need by offering digital texts and course materials online through CourseSmart. CourseSmart allows faculty to review course materials online, saving time and costs. It offers students a high-quality digital version of the text for less than the cost of a print copy of the text. Students receive the same content offered in the print textbook enhanced by search, note-taking, and printing tools. For more information, visit www.coursesmart.com.

Instructor Supplements The following supplements are available to qualified instructors only through Pearson Education’s Instructor Resource Center (www.pearsonhighered.com/irc):

xxx

Preface



Solutions Manual with solutions to the vast majority of the end-of-chapter exercises and Lab Manual exercises. We’ve added dozens of Making a Difference exercises, most with solutions.



Test Item File of multiple-choice questions (approximately two per book section)



Customizable PowerPoint® slides containing all the code and figures in the text, plus bulleted items that summarize the key points in the text

If you’re not already a registered faculty member, contact your Pearson representative or visit www.pearsonhighered.com/educator/replocator/.

Acknowledgments2 We’d like to thank Abbey Deitel and Barbara Deitel of Deitel & Associates, Inc. for long hours devoted to this project. We’re fortunate to have worked with the dedicated team of publishing professionals at Pearson. We appreciate the guidance, savvy and energy of Michael Hirsch, Editor-in-Chief of Computer Science. Carole Snyder recruited the book’s reviewers and managed the review process. Bob Engelhardt managed the book’s production.

Reviewers We wish to acknowledge the efforts of our seventh and eighth edition reviewers. They scrutinized the text and the programs and provided countless suggestions for improving the presentation: Virginia Bailey (Jackson StateUniversity), Thomas J. Borrelli (Rochester Institute of Technology), Chris Cox (Adobe Systems), Gregory Dai (eBay), Peter J. DePasquale (The College of New Jersey), John Dibling (SpryWare), Susan Gauch (University of Arkansas), Doug Gregor (Apple, Inc.), Jack Hagemeister (Washington State University), Williams M. Higdon (University of Indiana), Wing-Ning Li (University of Arkansas), Dean Mathias (Utah State University), Robert A. McLain (Tidewater Community College), April Reagan (Microsoft), José Antonio González Seco (Parliament of Andalusia, Spain), Dave Topham (Ohlone College) and Anthony Williams (author and C++ Standards Committee member). Well, there you have it! As you read the book, we would sincerely appreciate your comments, criticisms, corrections and suggestions for improving the text. Please address all correspondence to: [email protected]

We’ll respond promptly. We hope you enjoy working with C++ How to Program, Eighth Edition as much as we enjoyed writing it! Paul and Harvey Deitel

About the Authors Paul J. Deitel, CEO and Chief Technical Officer of Deitel & Associates, Inc., is a graduate of MIT, where he studied Information Technology. Through Deitel & Associates, Inc., he has delivered hundreds of C++, Java, C#, Visual Basic, C and Internet programming courses to industry clients, including Cisco, IBM, Siemens, Sun Microsystems, Dell, Lu-

About Deitel & Associates, Inc.

xxxi

cent Technologies, Fidelity, NASA at the Kennedy Space Center, the National Severe Storm Laboratory, White Sands Missile Range, Rogue Wave Software, Boeing, SunGard Higher Education, Stratus, Cambridge Technology Partners, One Wave, Hyperion Software, Adra Systems, Entergy, CableData Systems, Nortel Networks, Puma, iRobot, Invensys and many more. He and his co-author, Dr. Harvey M. Deitel, are the world’s bestselling programming-language textbook authors. Dr. Harvey M. Deitel, Chairman and Chief Strategy Officer of Deitel & Associates, Inc., has 50 years of experience in the computer field. Dr. Deitel earned B.S. and M.S. degrees from MIT in Electrical Engineering and a Ph.D. in Mathematics from Boston University—at both he studied computing before separate computer science degree programs were created. He has extensive college teaching experience, including earning tenure and serving as the Chairman of the Computer Science Department at Boston College before founding Deitel & Associates, Inc., with his son, Paul J. Deitel. He and Paul are the co-authors of dozens of books and LiveLessons multimedia packages. With translations published in Japanese, German, Russian, Chinese, Spanish, Korean, French, Polish, Italian, Portuguese, Greek, Urdu and Turkish, the Deitels’ texts have earned international recognition. Dr. Deitel has delivered hundreds of professional programming language seminars to major corporations, academic institutions, government organizations and the military.

About Deitel & Associates, Inc. Deitel & Associates, Inc., is an internationally recognized corporate training and authoring organization specializing in computer programming languages, Internet and web software technology, object-technology and Android™ and iPhone® education and applications development. The company provides instructor-led courses delivered at client sites worldwide on major programming languages and platforms, such as C++, Visual C++®, C, Java™, Visual C#®, Visual Basic®, XML®, Python®, object technology, Internet and web programming, Android and iPhone app development, and a growing list of additional programming and software-development courses. The founders of Deitel & Associates, Inc., are Paul J. Deitel and Dr. Harvey M. Deitel. The company’s clients include many of the world’s largest corporations, government agencies, branches of the military, and academic institutions. Through its 35-year publishing partnership with Prentice Hall/ Pearson Higher Education, Deitel & Associates, Inc., publishes leading-edge programming textbooks, professional books, interactive multimedia Cyber Classrooms, and LiveLessons DVD-based and web-based video courses. Deitel & Associates, Inc., and the authors can be reached via e-mail at: [email protected]

To learn more about Deitel & Associates, Inc., its publications and its Dive Into® Series Corporate Training curriculum delivered at client locations worldwide, visit: www.deitel.com/training/

subscribe to the free Deitel® Buzz Online e-mail newsletter at: www.deitel.com/newsletter/subscribe.html

and follow the authors on Facebook (www.deitel.com/deitelfan) and Twitter (@deitel).

xxxii

Preface

Individuals wishing to purchase Deitel books, and LiveLessons DVD and web-based training courses can do so through www.deitel.com. Bulk orders by corporations, the government, the military and academic institutions should be placed directly with Pearson. For more information, visit www.pearsonhighered.com

1

Introduction to Computers and C++

Man is still the most extraordinary computer of all.

—John F. Kennedy

Good design is good business.

—Thomas J. Watson, Founder of IBM

How wonderful it is that nobody need wait a single moment before starting to improve the world.

—Anne Frank

Objectives In this chapter you’ll learn: ■



■ ■













Exciting recent developments in the computer field. Computer hardware, software and networking basics. The data hierarchy. The different types of programming languages. Basic object-technology concepts. The importance of the Internet and the web. A typical C++ programdevelopment environment. To test-drive a C++ application. Some key recent software technologies. How computers can help you make a difference.

2

Chapter 1 Introduction to Computers and C++ 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9

Introduction Computers: Hardware and Software Data Hierarchy Computer Organization Machine Languages, Assembly Languages and High-Level Languages Introduction to Object Technology Operating Systems Programming Languages C++ and a Typical C++ Development Environment

1.10 1.11 1.12 1.13

Test-Driving a C++ Application Web 2.0: Going Social Software Technologies Future of C++: TR1, the New C++ Standard and the Open Source Boost Libraries 1.14 Keeping Up-to-Date with Information Technologies 1.15 Wrap-Up

Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference | Making a Difference Resources

1.1 Introduction Welcome to C++—a powerful computer programming language that’s appropriate for technically oriented people with little or no programming experience, and for experienced programmers to use in building substantial information systems. You’re already familiar with the powerful tasks computers perform. Using this textbook, you’ll write instructions commanding computers to perform those kinds of tasks. Software (i.e., the instructions you write) controls hardware (i.e., computers). You’ll learn object-oriented programming—today’s key programming methodology. You’ll create and work with many software objects in this text. C++ is one of today’s most popular software development languages. This text provides an introduction to programming in the version of C++ standardized in the United States through the American National Standards Institute (ANSI) and worldwide through the efforts of the International Organization for Standardization (ISO). In use today are more than a billion general-purpose computers and billions more cell phones, smartphones and handheld devices (such as tablet computers). According to a study by eMarketer, the number of mobile Internet users will reach approximately 134 million by 2013.1 Other studies have projected smartphone sales to surpass personal computer sales in 20112 and tablet sales to account for over 20% of all personal computer sales by 2015.3 By 2014, the smartphone applications market is expected to exceed $40 billion,4 which is creating significant opportunities for programming mobile applications.

Computing in Industry and Research These are exciting times in the computer field. Many of the most influential and successful businesses of the last two decades are technology companies, including Apple, IBM, Hew1. 2. 3. 4.

www.circleid.com/posts/mobile_internet_users_to_reach_134_million_by_2013/. www.pcworld.com/article/171380/more_smartphones_than_desktop_pcs_by_2011.html. www.forrester.com/ER/Press/Release/0,1769,1340,00.html.

Inc., December 2010/January 2011, pages 116–123.

1.1 Introduction

3

lett Packard, Dell, Intel, Motorola, Cisco, Microsoft, Google, Amazon, Facebook, Twitter, Groupon, Foursquare, Yahoo!, eBay and many more—these are major employers of people who study computer science, information systems or related disciplines. At the time of this writing, Apple was the second most valuable company in the world and the most valuable technology company.5 Computers are also used extensively in academic and industrial research. Figure 1.1 provides just a few examples of exciting ways in which computers are used in research and industry. Name

Description

Internet

The Internet—a global network of computers—was made possible by the convergence of computing and communications. It has its roots in the 1960s, when research funding was supplied by the U.S. Department of Defense. Originally designed to connect the main computer systems of about a dozen universities and research organizations, the Internet today is accessible by billions of computers and computer-controlled devices worldwide. Computers break lengthy transmissions into packets at the sending end, route the packets to their intended receivers and ensure that those packets are received in sequence and without error at the receiving end. According to a study by Forrester Research, the average U.S. online consumer now spends as much time online as watching television (forrester.com/rb/Research/understanding_ changing_needs_of_us_online_consumer,/q/id/57861/t/2). The Human Genome Project was founded to identify and analyze the 20,000+ genes in human DNA . The project used computer programs to analyze complex genetic data, determine the sequences of the billions of chemical base pairs that make up human DNA and store the information in databases which have been made available to researchers in many fields. This research has led to tremendous innovation and growth in the biotechnology industry. World Community Grid (www.worldcommunitygrid.org) is a non-profit computing grid. People worldwide donate their unused computer processing power by installing a free secure software program that allows the World Community Grid to harness the excess power when the computers are idle. The computing power is used in place of supercomputers to conduct scientific research projects that are making a difference, including developing affordable solar energy, providing clean water to the developing world, fighting cancer, curing muscular dystrophy, finding influenza antiviral drugs, growing more nutritious rice for regions fighting hunger and more. X-ray computed tomography (CT) scans, also called CAT (computerized axial tomography) scans, take X-rays of the body from hundreds of different angles. Computers are used to adjust the intensity of the X-ray, optimizing the scan for each type of tissue, then to combine all of the information to create a 3D image.

Human Genome Project

World Community Grid

Medical imaging

Fig. 1.1 | A few uses for computers. (Part 1 of 3.) 5.

www.zdnet.com/blog/apple/apple-becomes-worlds-second-most-valuable-company/9047.

4

Chapter 1 Introduction to Computers and C++

Name

Description

GPS

Global Positioning System (GPS) devices use a network of satellites to retrieve location-based information. Multiple satellites send time-stamped signals to the device GPS device, which calculates the distance to each satellite based on the time the signal left the satellite and the time the signal was received. The location of each satellite and the distance to each are used to determine the exact location of the device. Based on your location, GPS devices can provide stepby-step directions, help you easily find nearby businesses (restaurants, gas stations, etc.) and points of interest, or help you find your friends.

Microsoft’s SYNC®

Many Ford cars now feature Microsoft’s SYNC technology, providing speechsynthesis (for reading text messages to you) and speech-recognition capabilities that allow you to use voice commands to browse music, request traffic alerts and more.

AMBER™ Alert

The AMBER (America’s Missing: Broadcast Emergency Response) Alert System is used to find abducted children. Law enforcement notifies TV and radio broadcasters and state transportation officials, who then broadcast alerts on TV, radio, computerized highway signs, the Internet and wireless devices. AMBER Alert recently partnered with Facebook. Facebook users can “Like” AMBER Alert pages by location to receive alerts in their news feeds.

Robots

Robots are computerized machines that can perform tasks (including physical tasks), respond to stimuli and more. They can be used for day-to-day tasks (e.g., iRobot’s Roomba vacuum), entertainment (such as robotic pets), military combat, space and deep sea exploration, manufacturing and more. In 2004, NASA’s remote-controlled Mars rover—which used Java technology— explored the surface to learn about the history of water on the planet.

One Laptop Per Child (OLPC)

One Laptop Per Child (OLPC) is providing low-power, inexpensive, Internet-enabled laptops to poor children worldwide—enabling learning and reducing the digital divide (one.laptop.org). By providing these educational resources, OLPC is increasing the opportunities for poor children to learn and make a difference in their communities.

Game programming

The computer game business is larger than the first-run movie business. The most sophisticated video games can cost as much as $100 million to develop. Activision’s Call of Duty 2: Modern Warfare, released in November 2009, earned $310 million in just one day in North America and the U.K. (news.cnet.com/8301-13772_3-10396593-52.html?tag=mncol;txt)! Online social gaming, which enables users worldwide to compete with one another, is growing rapidly. Zynga—creator of popular online games such as Farmville and Mafia Wars—was founded in 2007 and already has over 215 million monthly users. To accommodate the growth in traffic, Zynga is adding nearly 1,000 servers each week (techcrunch.com/2010/09/22/zynga-moves-1-petabyte-of-data-daily-adds-1000-servers-a-week/)! Video game consoles are also becoming increasingly sophisticated. The Wii Remote uses an accelerometer (to detect tilt and acceleration) and a sensor that determines where the device is pointing, allowing the device to respond to motion. By gesturing with the Wii Remote in hand, you can control the video game on the screen.

Fig. 1.1 | A few uses for computers. (Part 2 of 3.)

1.2 Computers: Hardware and Software

Name

Description

(cont.)

With Microsoft’s Kinect for Xbox 360, you—the player—become the controller. Kinect uses a camera, depth sensor and sophisticated software to follow your body movement, allowing you to control the game (en.wikipedia.org/wiki/Kinect). Kinect games include dancing, exercising, playing sports, training virtual animals and more. Internet TV set-top boxes (such as Apple TV and Google TV) give you access to content—such as games, news, movies, television shows and more—allowing you to access an enormous amount of content on demand; you no longer need to rely on cable or satellite television providers to get content.

Internet TV

5

Fig. 1.1 | A few uses for computers. (Part 3 of 3.)

1.2 Computers: Hardware and Software A computer is a device that can perform computations and make logical decisions phenomenally faster than human beings can. Many of today’s personal computers can perform billions of calculations in one second—more than a human can perform in a lifetime. Supercomputers are already performing thousands of trillions (quadrillions) of instructions per second! To put that in perspective, a quadrillion-instruction-per-second computer can perform in one second more than 100,000 calculations for every person on the planet! And—these “upper limits” are growing quickly! Computers process data under the control of sets of instructions called computer programs. These programs guide the computer through orderly sets of actions specified by people called computer programmers. The programs that run on a computer are referred to as software. In this book, you’ll learn today’s key programming methodology that’s enhancing programmer productivity, thereby reducing software-development costs— object-oriented programming. A computer consists of various devices referred to as hardware (e.g., the keyboard, screen, mouse, hard disks, memory, DVDs and processing units). Computing costs are dropping dramatically, owing to rapid developments in hardware and software technologies. Computers that might have filled large rooms and cost millions of dollars decades ago are now inscribed on silicon chips smaller than a fingernail, costing perhaps a few dollars each. Ironically, silicon is one of the most abundant materials—it’s an ingredient in common sand. Silicon-chip technology has made computing so economical that more than a billion general-purpose computers are in use worldwide, and this is expected to double in the next few years. Computer chips (microprocessors) control countless devices. These embedded systems include anti-lock brakes in cars, navigation systems, smart home appliances, home security systems, cell phones and smartphones, robots, intelligent traffic intersections, collision avoidance systems, video game controllers and more. The vast majority of the microprocessors produced each year are embedded in devices other than general-purpose computers.6 6.

www.eetimes.com/electronics-blogs/industrial-control-designline-blog/4027479/ Real-men-program-in-C?pageNumber=1.

6

Chapter 1 Introduction to Computers and C++

Moore’s Law Every year, you probably expect to pay at least a little more for most products and services. The opposite has been the case in the computer and communications fields, especially with regard to the costs of hardware supporting these technologies. For many decades, hardware costs have fallen rapidly. Every year or two, the capacities of computers have approximately doubled without any increase in price. This remarkable observation often is called Moore’s Law, named for the person who identified the trend, Gordon Moore, cofounder of Intel—a leading manufacturer of the processors in today’s computers and embedded systems. Moore’s Law and related observations are especially true in relation to the amount of memory that computers have for programs, the amount of secondary storage (such as disk storage) they have to hold programs and data over longer periods of time, and their processor speeds—the speeds at which computers execute their programs (i.e., do their work). Similar growth has occurred in the communications field, in which costs have plummeted as enormous demand for communications bandwidth (i.e., information-carrying capacity) has attracted intense competition. We know of no other fields in which technology improves so quickly and costs fall so rapidly. Such phenomenal improvement is truly fostering the Information Revolution.

1.3 Data Hierarchy Data items processed by computers form a data hierarchy that becomes larger and more complex in structure as we progress from bits to characters to fields, and so on. Figure 1.2 illustrates a portion of the data hierarchy. Figure 1.3 summarizes the data hierarchy’s levels.

Black

Tom

Blue

Judy

Green

Iris

Orange

Randy

Red

Judy

Green

J u d y

Field

01001010 1

Sally

Bit

Fig. 1.2 | Data hierarchy.

Byte (ASCII character J)

File

Record

1.4 Computer Organization

Level

Description

Bits

The smallest data item in a computer can assume the value 0 or the value 1. Such a data item is called a bit (short for “binary digit”—a digit that can assume one of two values). It’s remarkable that the impressive functions performed by computers involve only the simplest manipulations of 0s and 1s— examining a bit’s value, setting a bit’s value and reversing a bit’s value (from 1 to 0 or from 0 to 1).

Characters

It’s tedious for people to work with data in the low-level form of bits. Instead, they prefer to work with decimal digits (0–9), letters (A–Z and a–z), and special symbols (e.g., $, @, %, &, *, (, ), –, +, ", :, ? and / ). Digits, letters and special symbols are known as characters. The computer’s character set is the set of all the characters used to write programs and represent data items. Computers process only 1s and 0s, so a computer’s character set represents every character as a pattern of 1s and 0s. C++ uses the ASCII (American Standard Code for Information Interchange) character set (Appendix B).

Fields

Just as characters are composed of bits, fields are composed of characters or bytes. A field is a group of characters or bytes that conveys meaning. For example, a field consisting of uppercase and lowercase letters can be used to represent a person’s name, and a field consisting of decimal digits could represent a person’s age.

Records

Several related fields can be used to compose a record (implemented as a class in Java). In a payroll system, for example, the record for an employee might consist of the following fields (possible types for these fields are shown in parentheses): • Employee identification number (a whole number) • Name (a string of characters) • Address (a string of characters) • Hourly pay rate (a number with a decimal point) • Year-to-date earnings (a number with a decimal point) • Amount of taxes withheld (a number with a decimal point) Thus, a record is a group of related fields. In the preceding example, all the fields belong to the same employee. A company might have many employees and a payroll record for each one.

Files

7

A file is a group of related records. [Note: More generally, a file contains arbitrary data in arbitrary formats. In some operating systems, a file is viewed simply as a sequence of bytes—any organization of the bytes in a file, such as organizing the data into records, is a view created by the application programmer.] It’s not unusual for an organization to have many files, some containing billions, or even trillions, of characters of information.

Fig. 1.3 | Levels of the data hierarchy.

1.4 Computer Organization Regardless of differences in physical appearance, computers can be envisioned as divided into various logical units or sections (Fig. 1.4).

8

Chapter 1 Introduction to Computers and C++

Logical unit

Description

Input unit

This “receiving” section obtains information (data and computer programs) from input devices and places it at the disposal of the other units for processing. Most information is entered into computers through keyboards, touch screens and mouse devices. Other forms of input include speaking to your computer, scanning images and barcodes, reading from secondary storage devices (like hard drives, DVD drives, Blu-ray Disc™ drives and USB flash drives—also called “thumb drives” or “memory sticks”), receiving video from a webcam and having your computer receive information from the Internet (such as when you download videos from YouTube™ or e-books from Amazon). Newer forms of input include reading position data from a GPS device, and motion and orientation information from an accelerometer in a smartphone or game controller.

Output unit

This “shipping” section takes information that the computer has processed and places it on various output devices to make it available for use outside the computer. Most information that’s output from computers today is displayed on screens, printed on paper, played as audio or video on portable media players (such as Apple’s popular iPods) and giant screens in sports stadiums, transmitted over the Internet or used to control other devices, such as robots and “intelligent” appliances.

Memory unit

This rapid-access, relatively low-capacity “warehouse” section retains information that has been entered through the input unit, making it immediately available for processing when needed. The memory unit also retains processed information until it can be placed on output devices by the output unit. Information in the memory unit is volatile—it’s typically lost when the computer’s power is turned off. The memory unit is often called either memory or primary memory. Typical main memories on desktop and notebook computers contain between 1 GB and 8 GB (GB stands for gigabytes; a gigabyte is approximately one billion bytes).

Arithmetic and logic unit (ALU)

This “manufacturing” section performs calculations, such as addition, subtraction, multiplication and division. It also contains the decision mechanisms that allow the computer, for example, to compare two items from the memory unit to determine whether they’re equal. In today’s systems, the ALU is usually implemented as part of the next logical unit, the CPU.

Central processing unit (CPU)

This “administrative” section coordinates and supervises the operation of the other sections. The CPU tells the input unit when information should be read into the memory unit, tells the ALU when information from the memory unit should be used in calculations and tells the output unit when to send information from the memory unit to certain output devices. Many of today’s computers have multiple CPUs and, hence, can perform many operations simultaneously. A multi-core processor implements multiple processors on a single integrated-circuit chip—a dual-core processor has two CPUs and a quadcore processor has four CPUs. Today’s desktop computers have processors that can execute billions of instructions per second.

Secondary storage unit

This is the long-term, high-capacity “warehousing” section. Programs or data not actively being used by the other units normally are placed on secondary

Fig. 1.4 | Logical units of a computer. (Part 1 of 2.)

1.5 Machine Languages, Assembly Languages and High-Level Languages

Logical unit

Description

Secondary storage unit (cont.)

storage devices (e.g., your hard drive) until they’re again needed, possibly hours, days, months or even years later. Information on secondary storage devices is persistent—it’s preserved even when the computer’s power is turned off. Secondary storage information takes much longer to access than information in primary memory, but the cost per unit of secondary storage is much less than that of primary memory. Examples of secondary storage devices include CD drives, DVD drives and flash drives, some of which can hold up to 128 GB. Typical hard drives on desktop and notebook computers can hold up to 2 TB (TB stands for terabytes; a terabyte is approximately one trillion bytes).

9

Fig. 1.4 | Logical units of a computer. (Part 2 of 2.)

1.5 Machine Languages, Assembly Languages and HighLevel Languages Programmers write instructions in various programming languages, some directly understandable by computers and others requiring intermediate translation steps. Hundreds of such languages are in use today. These may be divided into three general types: 1. Machine languages 2. Assembly languages 3. High-level languages Any computer can directly understand only its own machine language, defined by its hardware design. Machine languages generally consist of strings of numbers (ultimately reduced to 1s and 0s) that instruct computers to perform their most elementary operations one at a time. Machine languages are machine dependent (a particular machine language can be used on only one type of computer). Such languages are cumbersome for humans. For example, here’s a section of an early machine-language program that adds overtime pay to base pay and stores the result in gross pay: +1300042774 +1400593419 +1200274027

Programming in machine language was simply too slow and tedious for most programmers. Instead of using the strings of numbers that computers could directly understand, programmers began using English-like abbreviations to represent elementary operations. These abbreviations formed the basis of assembly languages. Translator programs called assemblers were developed to convert early assembly-language programs to machine language at computer speeds. The following section of an assembly-language program also adds overtime pay to base pay and stores the result in gross pay: load add store

basepay overpay grosspay

10

Chapter 1

Introduction to Computers and C++

Although such code is clearer to humans, it’s incomprehensible to computers until translated to machine language. Computer usage increased rapidly with the advent of assembly languages, but programmers still had to use many instructions to accomplish even the simplest tasks. To speed the programming process, high-level languages were developed in which single statements could be written to accomplish substantial tasks. Translator programs called compilers convert high-level language programs into machine language. High-level languages allow you to write instructions that look almost like everyday English and contain commonly used mathematical notations. A payroll program written in a high-level language might contain a single statement such as grossPay = basePay + overTimePay

From the programmer’s standpoint, high-level languages are preferable to machine and assembly languages. C++, C, Microsoft’s .NET languages (e.g., Visual Basic, Visual C++ and Visual C#) and Java are among the most widely used high-level programming languages. Compiling a large high-level language program into machine language can take a considerable amount of computer time. Interpreter programs were developed to execute highlevel language programs directly (without the delay of compilation), although slower than compiled programs run.

1.6 Introduction to Object Technology Building software quickly, correctly and economically remains an elusive goal at a time when demands for new and more powerful software are soaring. Objects, or more precisely—as we’ll see in Chapter 3—the classes objects come from, are essentially reusable software components. There are date objects, time objects, audio objects, video objects, automobile objects, people objects, etc. Almost any noun can be reasonably represented as a software object in terms of attributes (e.g., name, color and size) and behaviors (e.g., calculating, moving and communicating). Software developers are discovering that using a modular, object-oriented design and implementation approach can make software-development groups much more productive than was possible with earlier popular techniques like “structured programming”—object-oriented programs are often easier to understand, correct and modify.

The Automobile as an Object To help you understand objects and their contents, let’s begin with a simple analogy. Suppose you want to drive a car and make it go faster by pressing its accelerator pedal. What must happen before you can do this? Well, before you can drive a car, someone has to design it. A car typically begins as engineering drawings, similar to the blueprints that describe the design of a house. These drawings include the design for an accelerator pedal. The pedal hides from the driver the complex mechanisms that actually make the car go faster, just as the brake pedal hides the mechanisms that slow the car, and the steering wheel “hides” the mechanisms that turn the car. This enables people with little or no knowledge of how engines, braking and steering mechanisms work to drive a car easily. Just as you cannot cook meals in the kitchen of a blueprint, you cannot drive a car’s engineering drawings. Before you can drive a car, it must be built from the engineering drawings that describe it. A completed car has an actual accelerator pedal to make the car

1.6 Introduction to Object Technology

11

go faster, but even that’s not enough—the car won’t accelerate on its own (hopefully!), so the driver must press the pedal to accelerate the car.

Member Functions and Classes Let’s use our car example to introduce some key object-oriented programming concepts. Performing a task in a program requires a member function, which houses the program statements that actually perform its task. The member function hides these statements from its user, just as the accelerator pedal of a car hides from the driver the mechanisms of making the car go faster. In C++, we create a program unit called a class to house the set of member functions that perform the class’s tasks. For example, a class that represents a bank account might contain one member function to deposit money to an account, another to withdraw money from an account and a third to inquire what the account’s current balance is. A class is similar in concept to a car’s engineering drawings, which house the design of an accelerator pedal, steering wheel, and so on. Instantiation Just as someone has to build a car from its engineering drawings before you can actually drive a car, you must build an object of a class before a program can perform the tasks that the class’s member functions define. The process of doing this is called instantiation. An object is then referred to as an instance of its class. Reuse Just as a car’s engineering drawings can be reused many times to build many cars, you can reuse a class many times to build many objects. Reuse of existing classes when building new classes and programs saves time and effort. Reuse also helps you build more reliable and effective systems, because existing classes and components often have gone through extensive testing, debugging and performance tuning. Just as the notion of interchangeable parts was crucial to the Industrial Revolution, reusable classes are crucial to the software revolution that has been spurred by object technology.

Software Engineering Observation 1.1

Use a building-block approach to creating your programs. Avoid reinventing the wheel— use existing pieces wherever possible. This software reuse is a key benefit of object-oriented programming.

Messages and Member Function Calls When you drive a car, pressing its gas pedal sends a message to the car to perform a task— that is, to go faster. Similarly, you send messages to an object. Each message is implemented as a member function call that tells a member function of the object to perform its task. For example, a program might call a particular bank account object’s deposit member function to increase the account’s balance. Attributes and Data Members A car, besides having capabilities to accomplish tasks, also has attributes, such as its color, its number of doors, the amount of gas in its tank, its current speed and its record of total miles driven (i.e., its odometer reading). Like its capabilities, the car’s attributes are represented as part of its design in its engineering diagrams (which, for example, include an

12

Chapter 1

Introduction to Computers and C++

odometer and a fuel gauge). As you drive an actual car, these attributes are carried along with the car. Every car maintains its own attributes. For example, each car knows how much gas is in its own gas tank, but not how much is in the tanks of other cars. An object, similarly, has attributes that it carries along as it’s used in a program. These attributes are specified as part of the object’s class. For example, a bank account object has a balance attribute that represents the amount of money in the account. Each bank account object knows the balance in the account it represents, but not the balances of the other accounts in the bank. Attributes are specified by the class’s data members.

Encapsulation Classes encapsulate (i.e., wrap) attributes and member functions into objects—an object’s attributes and member functions are intimately related. Objects may communicate with one another, but they’re normally not allowed to know how other objects are implemented—implementation details are hidden within the objects themselves. This information hiding, as we’ll see, is crucial to good software engineering. Inheritance A new class of objects can be created quickly and conveniently by inheritance—the new class absorbs the characteristics of an existing class, possibly customizing them and adding unique characteristics of its own. In our car analogy, an object of class “convertible” certainly is an object of the more general class “automobile,” but more specifically, the roof can be raised or lowered. Object-Oriented Analysis and Design (OOAD) Soon you’ll be writing programs in C++. How will you create the code (i.e., the program instructions) for your programs? Perhaps, like many programmers, you’ll simply turn on your computer and start typing. This approach may work for small programs (like the ones we present in the early chapters of the book), but what if you were asked to create a software system to control thousands of automated teller machines for a major bank? Or suppose you were asked to work on a team of 1,000 software developers building the next U.S. air traffic control system? For projects so large and complex, you should not simply sit down and start writing programs. To create the best solutions, you should follow a detailed analysis process for determining your project’s requirements (i.e., defining what the system is supposed to do) and developing a design that satisfies them (i.e., deciding how the system should do it). Ideally, you’d go through this process and carefully review the design (and have your design reviewed by other software professionals) before writing any code. If this process involves analyzing and designing your system from an object-oriented point of view, it’s called an object-oriented analysis and design (OOAD) process. Languages like C++ are object oriented. Programming in such a language, called object-oriented programming (OOP), allows you to implement an object-oriented design as a working system. The UML (Unified Modeling Language) Although many different OOAD processes exist, a single graphical language for communicating the results of any OOAD process has come into wide use. This language, known as the Unified Modeling Language (UML), is now the most widely used graphical scheme for modeling object-oriented systems. We present our first UML diagrams in Chapters 3 and 4, then use them in our deeper treatment of object-oriented programming through

1.7 Operating Systems

13

Chapter 13. In our optional ATM Software Engineering Case Study in Chapters 25–26 we present a simple subset of the UML’s features as we guide you through an object-oriented design experience.

1.7 Operating Systems Operating systems are software systems that make using computers more convenient for users, application developers and system administrators. Operating systems provide services that allow each application to execute safely, efficiently and concurrently (i.e., in parallel) with other applications. The software that contains the core components of the operating system is called the kernel. Popular desktop operating systems include Linux, Windows 7 and Mac OS X. Popular mobile operating systems used in smartphones and tablets include Google’s Android, BlackBerry OS and Apple’s iOS (for its iPhone, iPad and iPod Touch devices).

Windows—A Proprietary Operating System In the mid-1980s, Microsoft developed the Windows operating system, consisting of a graphical user interface built on top of DOS—an enormously popular personal-computer operating system of the time that users interacted with by typing commands. Windows borrowed from many concepts (such as icons, menus and windows) popularized by early Apple Macintosh operating systems and originally developed by Xerox PARC. Windows 7 is Microsoft’s latest operating system—its features include enhancements to the user interface, faster startup times, further refinement of security features, touch-screen and multi-touch support, and more. Windows is a proprietary operating system—it’s controlled by one company exclusively. Windows is by far the world’s most widely used operating system. Linux—An Open-Source Operating System The Linux operating system is perhaps the greatest success of the open-source movement. Open-source software is a software development style that departs from the proprietary development that dominated software’s early years. With open-source development, individuals and companies contribute their efforts in developing, maintaining and evolving software in exchange for the right to use that software for their own purposes, typically at no charge. Open-source code is often scrutinized by a much larger audience than proprietary software, so errors often get removed faster. Open source also encourages more innovation. Some organizations in the open-source community are the Eclipse Foundation (the Eclipse Integrated Development Environment helps C++ programmers conveniently develop software), the Mozilla Foundation (creators of the Firefox web browser), the Apache Software Foundation (creators of the Apache web server used to develop webbased applications) and SourceForge (which provides the tools for managing open source projects—it has over 260,000 of them under development). Rapid improvements to computing and communications, decreasing costs and open-source software have made it much easier and more economical to create a software-based business now than just a few decades ago. A great example is Facebook, which was launched from a college dorm room and built with open-source software.7 The Linux kernel is the core of the most popular open-source, freely distributed, fullfeatured operating system. It’s developed by a loosely organized team of volunteers, and is 7.

developers.facebook.com/opensource/.

14

Chapter 1

Introduction to Computers and C++

popular in servers, personal computers and embedded systems. Unlike that of proprietary operating systems like Microsoft’s Windows and Apple’s Mac OS X, Linux source code (the program code) is available to the public for examination and modification and is free to download and install. As a result, users of the operating system benefit from a community of developers actively debugging and improving the kernel, an absence of licensing fees and restrictions, and the ability to completely customize the operating system to meet specific needs. In 1991, Linus Torvalds, a 21-year-old student at the University of Helsinki, Finland, began developing the Linux kernel as a hobby. (The name Linux is derived from “Linus” and “UNIX”—an operating system developed by Bell Labs in 1969.) Torvalds wished to improve upon the design of Minix, an educational operating system created by Professor Andrew Tanenbaum of the Vrije Universiteit in Amsterdam. The Minix source code was publicly available to allow professors to demonstrate basic operating-system implementation concepts to their students. Torvalds released the first version of Linux in 1991. The favorable response led to the creation of a community that has continued to develop and support Linux. Developers downloaded, tested, and modified the Linux code, submitting bug fixes and feedback to Torvalds, who reviewed them and applied the improvements to the code. The 1994 release of Linux included many features commonly found in a mature operating system, making Linux a viable alternative to UNIX. Enterprise systems companies such as IBM and Oracle became increasingly interested in Linux as it continued to stabilize and spread to new platforms. A variety of issues—such as Microsoft’s market power, the small number of userfriendly Linux applications and the diversity of Linux distributions, such as Red Hat Linux, Ubuntu Linux and many others—have prevented widespread Linux use on desktop computers. But Linux has become extremely popular on servers and in embedded systems, such as Google’s Android-based smartphones.

Android Android—the fastest growing mobile and smartphone operating system—is based on the Linux kernel and Java. One benefit of developing Android apps is the openness of the platform. The operating system is open source and free. The Android operating system was developed by Android, Inc., which was acquired by Google in 2005. In 2007, the Open Handset Alliance™—a consortium of 34 companies initially and 79 by 2010—was formed to continue developing Android. As of December 2010, more than 300,000 Android smartphones were being activated each day!8 Android smartphones are now outselling iPhones.9 The Android operating system is used in numerous smartphones (such as the Motorola Droid, HTC EVO™ 4G, Samsung Vibrant™ and many more), e-reader devices (such as the Barnes and Noble Nook™), tablet computers (such as the Dell Streak, the Samsung Galaxy Tab and more), in-store touch-screen kiosks, cars, robots and multimedia players. Android smartphones include the functionality of a mobile phone, Internet client (for web browsing and Internet communication), MP3 player, gaming console, digital camera 8. 9.

www.pcmag.com/article2/0,2817,2374076,00.asp. mashable.com/2010/08/02/android-outselling-iphone-2/.

1.8 Programming Languages

15

and more, wrapped into handheld devices with full-color multitouch screens—these allow you to control the device with gestures involving one touch or multiple simultaneous touches. You can download apps directly onto your Android device through Android Market and other app marketplaces. As of December 2010, there were over 200,000 apps in Google’s Android Market.

1.8 Programming Languages In this section, we provide brief comments on several popular programming languages (Fig. 1.5). In the next section we introduce C++. Programming language Fortran

COBOL

Pascal

Ada

Basic

Description Fortran (FORmula TRANslator) was developed by IBM Corporation in the mid-1950s to be used for scientific and engineering applications that require complex mathematical computations. It’s still widely used and its latest versions support object-oriented programming. COBOL (COmmon Business Oriented Language) was developed in the late 1950s by computer manufacturers, the U.S. government and industrial computer users based on a language developed by Grace Hopper, a career U.S. Navy officer and computer scientist. COBOL is still widely used for commercial applications that require precise and efficient manipulation of large amounts of data. Its latest version supports object-oriented programming. Research in the 1960s resulted in structured programming—a disciplined approach to writing programs that are clearer, easier to test and debug and easier to modify than large programs produced with previous techniques. One of the more tangible results of this research was the development of Pascal by Professor Niklaus Wirth in 1971. It was designed for teaching structured programming and was popular in college courses for several decades. Ada, based on Pascal, was developed under the sponsorship of the U.S. Department of Defense (DOD) during the 1970s and early 1980s. The DOD wanted a single language that would fill most of its needs. The Pascal-based language was named after Lady Ada Lovelace, daughter of the poet Lord Byron. She’s credited with writing the world’s first computer program in the early 1800s (for the Analytical Engine mechanical computing device designed by Charles Babbage). Its latest version supports object-oriented programming. Basic was developed in the 1960s at Dartmouth College to familiarize novices with programming techniques. Many of its latest versions are object oriented.

Fig. 1.5 | Other programming languages. (Part 1 of 3.)

16

Chapter 1

Programming language C

Objective-C

Java

Visual Basic Visual C#

PHP

Perl

Python

Introduction to Computers and C++

Description C was implemented in 1972 by Dennis Ritchie at Bell Laboratories. It initially became widely known as the UNIX operating system’s development language. Today, most of the code for generalpurpose operating systems is written in C or C++. Objective-C is an object-oriented language based on C. It was developed in the early 1980s and later acquired by Next, which in turn was acquired by Apple. It has become the key programming language for the Mac OS X operating system and all iOS-powered devices (such as iPods, iPhones and iPads). Sun Microsystems in 1991 funded an internal corporate research project led by James Gosling, which resulted in the C++-based object-oriented programming language called Java. A key goal of Java is to be able to write programs that will run on a great variety of computer systems and computer-control devices. This is sometimes called “write once, run anywhere.” Java is used to develop large-scale enterprise applications, to enhance the functionality of web servers (the computers that provide the content we see in our web browsers), to provide applications for consumer devices (e.g., smartphones, television set-top boxes and more) and for many other purposes. Microsoft’s Visual Basic language was introduced in the early 1990s to simplify the development of Microsoft Windows applications. Its latest versions support object-oriented programming. Microsoft’s three object-oriented primary programming languages are Visual Basic (based on the original Basic), Visual C++ (based on C++) and C# (based on C++ and Java, and developed for integrating the Internet and the web into computer applications). PHP is an object-oriented, “open-source” (see Section 1.7) “scripting” language supported by a community of users and developers and is used by numerous websites including Wikipedia and Facebook. PHP is platform independent—implementations exist for all major UNIX, Linux, Mac and Windows operating systems. PHP also supports many databases, including MySQL. Perl (Practical Extraction and Report Language), one of the most widely used object-oriented scripting languages for web programming, was developed in 1987 by Larry Wall. It features rich textprocessing capabilities and flexibility. Python, another object-oriented scripting language, was released publicly in 1991. Developed by Guido van Rossum of the National Research Institute for Mathematics and Computer Science in Amsterdam (CWI), Python draws heavily from Modula3—a systems programming language. Python is “extensible”—it can be extended through classes and programming interfaces.

Fig. 1.5 | Other programming languages. (Part 2 of 3.)

1.9 C++ and a Typical C++ Development Environment

Programming language JavaScript

Ruby on Rails

Scala

17

Description JavaScript is the most widely used scripting language. It’s primarily used to add programmability to web pages—for example, animations and interactivity with the user. It’s provided with all major web browsers. Ruby—created in the mid-1990s by Yukihiro Matsumoto—is an open-source, object-oriented programming language with a simple syntax that’s similar to Perl and Python. Ruby on Rails combines the scripting language Ruby with the Rails web application framework developed by 37Signals. Their book, Getting Real (gettingreal.37signals.com/toc.php), is a must read for web developers. Many Ruby on Rails developers have reported productivity gains over other languages when developing database-intensive web applications. Ruby on Rails was used to build Twitter’s user interface. Scala (www.scala-lang.org/node/273)—short for “scalable language”—was designed by Martin Odersky, a professor at École Polytechnique Fédérale de Lausanne (EPFL) in Switzerland. Released in 2003, Scala uses both the object-oriented programming and functional programming paradigms and is designed to integrate with Java. Programming in Scala can reduce the amount of code in your applications significantly. Twitter and Foursquare use Scala.

Fig. 1.5 | Other programming languages. (Part 3 of 3.)

1.9 C++ and a Typical C++ Development Environment C++ evolved from C, which was developed by Dennis Ritchie at Bell Laboratories. C is available for most computers and is hardware independent. With careful design, it’s possible to write C programs that are portable to most computers. The widespread use of C with various kinds of computers (sometimes called hardware platforms) unfortunately led to many variations. A standard version of C was needed. The American National Standards Institute (ANSI) cooperated with the International Organization for Standardization (ISO) to standardize C worldwide; the joint standard document was published in 1990 and is referred to as ANSI/ISO 9899: 1990. C99 is the latest ANSI standard for the C programming language. It was developed to evolve the C language to keep pace with increasingly powerful hardware and ever more demanding user requirements. C99 also makes C more consistent with C++. For more information on C and C99, see our book C How to Program, 6/e and our C Resource Center (located at www.deitel.com/C). C++, an extension of C, was developed by Bjarne Stroustrup in the early 1980s at Bell Laboratories. C++ provides a number of features that “spruce up” the C language, but more importantly, it provides capabilities for object-oriented programming.

18

Chapter 1

Introduction to Computers and C++

You’ll begin developing customized, reusable classes and objects in Chapter 3, Introduction to Classes, Objects and Strings. The book is object oriented, where appropriate, from the start and throughout the text. We also provide an optional automated teller machine (ATM) case study in Chapters 25–26, which contains a complete C++ implementation. The case study presents a carefully paced introduction to object-oriented design using the UML—an industry standard graphical modeling language for developing object-oriented systems. We guide you through a friendly design experience intended for the novice.

C++ Standard Library C++ programs consist of pieces called classes and functions. You can program each piece yourself, but most C++ programmers take advantage of the rich collections of classes and functions in the C++ Standard Library. Thus, there are really two parts to learning the C++ “world.” The first is learning the C++ language itself; the second is learning how to use the classes and functions in the C++ Standard Library. We discuss many of these classes and functions. P. J. Plauger’s book, The Standard C Library (Upper Saddle River, NJ: Prentice Hall PTR, 1992), is a must read for programmers who need a deep understanding of the ANSI C library functions included in C++. Many special-purpose class libraries are supplied by independent software vendors.

Software Engineering Observation 1.2

Use a “building-block” approach to create programs. Avoid reinventing the wheel. Use existing pieces wherever possible. Called software reuse, this practice is central to objectoriented programming.

Software Engineering Observation 1.3

When programming in C++, you typically will use the following building blocks: classes and functions from the C++ Standard Library, classes and functions you and your colleagues create and classes and functions from various popular third-party libraries.

The advantage of creating your own functions and classes is that you’ll know exactly how they work. You’ll be able to examine the C++ code. The disadvantage is the time-consuming and complex effort that goes into designing, developing and maintaining new functions and classes that are correct and that operate efficiently.

Performance Tip 1.1

Using C++ Standard Library functions and classes instead of writing your own versions can improve program performance, because they’re written carefully to perform efficiently. This technique also shortens program development time.

Portability Tip 1.1

Using C++ Standard Library functions and classes instead of writing your own improves program portability, because they’re included in every C++ implementation.

We now explain the commonly used steps in creating and executing a C++ application using a C++ development environment (illustrated in Figs. 1.6–1.11). C++ systems generally consist of three parts: a program development environment, the language and the C++ Standard Library. C++ programs typically go through six phases: edit, preprocess,

1.9 C++ and a Typical C++ Development Environment

19

compile, link, load and execute. The following discussion explains a typical C++ program development environment.

Phase 1: Creating a Program Phase 1 consists of editing a file with an editor program, normally known simply as an editor (Fig. 1.6). You type a C++ program (typically referred to as source code) using the editor, make any necessary corrections and save the program on a secondary storage device, such as your hard drive. C++ source code filenames often end with the .cpp, .cxx, .cc or .C extensions (note that C is in uppercase) which indicate that a file contains C++ source code. See the documentation for your C++ compiler for more information on file-name extensions.

Editor

Disk

Phase 1: Programmer creates program in the editor and stores it on disk

Fig. 1.6 | Typical C++ development environment—editing phase. Two editors widely used on Linux systems are vi and emacs. C++ software packages for Microsoft Windows such as Microsoft Visual C++ (microsoft.com/express) have editors integrated into the programming environment. You can also use a simple text editor, such as Notepad in Windows, to write your C++ code. For organizations that develop substantial information systems, integrated development environments (IDEs) are available from many major software suppliers. IDEs provide tools that support the software-development process, including editors for writing and editing programs and debuggers for locating logic errors—errors that cause programs to execute incorrectly. Popular IDEs include Microsoft® Visual Studio 2010 Express Edition, Dev C++, NetBeans, Eclipse and CodeLite.

Phase 2: Preprocessing a C++ Program In Phase 2, you give the command to compile the program (Fig. 1.7). In a C++ system, a preprocessor program executes automatically before the compiler’s translation phase begins (so we call preprocessing Phase 2 and compiling Phase 3). The C++ preprocessor obeys commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. These manipulations usually include other text files to be compiled, and perform various text replacements. The most common preprocessor directives are discussed in the early chapters; a detailed discussion of preprocessor features appears in Appendix E, Preprocessor.

Preprocessor

Disk

Phase 2: Preprocessor program processes the code

Fig. 1.7 | Typical C++ development environment—preprocessor phase.

20

Chapter 1

Introduction to Computers and C++

Phase 3: Compiling a C++ Program In Phase 3, the compiler translates the C++ program into machine-language code—also referred to as object code (Fig. 1.8).

Compiler

Disk

Phase 3: Compiler creates object code and stores it on disk

Fig. 1.8 | Typical C++ development environment—compilation phase. Phase 4: Linking Phase 4 is called linking. C++ programs typically contain references to functions and data defined elsewhere, such as in the standard libraries or in the private libraries of groups of programmers working on a particular project (Fig. 1.9). The object code produced by the C++ compiler typically contains “holes” due to these missing parts. A linker links the object code with the code for the missing functions to produce an executable program (with no missing pieces). If the program compiles and links correctly, an executable image is produced.

Linker

Disk

Phase 4: Linker links the object code with the libraries, creates an executable file and stores it on disk

Fig. 1.9 | Typical C++ development environment—linking phase. Phase 5: Loading Phase 5 is called loading. Before a program can be executed, it must first be placed in memory (Fig. 1.10). This is done by the loader, which takes the executable image from disk and transfers it to memory. Additional components from shared libraries that support the program are also loaded. Primary Memory Loader Phase 5: Loader puts program in memory ...

Disk

Fig. 1.10 | Typical C++ development environment—loading phase.

1.10 Test-Driving a C++ Application

21

Phase 6: Execution Finally, the computer, under the control of its CPU, executes the program one instruction at a time (Fig. 1.11). Some modern computer architectures can execute several instructions in parallel. Primary Memory CPU

...

Phase 6: CPU takes each instruction and executes it, possibly storing new data values as the program executes

Fig. 1.11 | Typical C++ development environment—execution phase. Problems That May Occur at Execution Time Programs might not work on the first try. Each of the preceding phases can fail because of various errors that we’ll discuss throughout this book. For example, an executing program might try to divide by zero (an illegal operation for whole-number arithmetic in C++). This would cause the C++ program to display an error message. If this occurred, you’d have to return to the edit phase, make the necessary corrections and proceed through the remaining phases again to determine that the corrections fixed the problem(s). [Note: Most programs in C++ input or output data. Certain C++ functions take their input from cin (the standard input stream; pronounced “see-in”), which is normally the keyboard, but cin can be redirected to another device. Data is often output to cout (the standard output stream; pronounced “see-out”), which is normally the computer screen, but cout can be redirected to another device. When we say that a program prints a result, we normally mean that the result is displayed on a screen. Data may be output to other devices, such as disks and hardcopy printers. There is also a standard error stream referred to as cerr. The cerr stream (normally connected to the screen) is used for displaying error messages.

Common Programming Error 1.1

Errors such as division by zero occur as a program runs, so they’re called runtime errors or execution-time errors. Fatal runtime errors cause programs to terminate immediately without having successfully performed their jobs. Nonfatal runtime errors allow programs to run to completion, often producing incorrect results.

1.10 Test-Driving a C++ Application In this section, you’ll run and interact with your first C++ application. You’ll begin by running an entertaining guess-the-number game, which picks a number from 1 to 1000 and

22

Chapter 1

Introduction to Computers and C++

prompts you to guess it. If your guess is correct, the game ends. If your guess is not correct, the application indicates whether your guess is higher or lower than the correct number. There is no limit on the number of guesses you can make. [Note: For this test drive only, we’ve modified this application from the exercise you’ll be asked to create in Chapter 6, Functions and an Introduction to Recursion. Normally this application randomly selects the correct answer as you execute the program. The modified application uses the same correct answer every time the program executes (though this may vary by compiler), so you can use the same guesses we use in this section and see the same results as we walk you through interacting with your first C++ application.] We’ll demonstrate running a C++ application using the Windows Command Prompt and a shell on Linux. The application runs similarly on both platforms. Many development environments are available in which you can compile, build and run C++ applications, such as GNU C++, Dev C++, Microsoft Visual C++, CodeLite, NetBeans, Eclipse etc. Consult your instructor for information on your specific development environment. In the following steps, you’ll run the application and enter various numbers to guess the correct number. The elements and functionality that you see in this application are typical of those you’ll learn to program in this book. We use fonts to distinguish between features you see on the screen (e.g., the Command Prompt) and elements that are not directly related to the screen. We emphasize screen features like titles and menus (e.g., the File menu) in a semibold sans-serif Helvetica font and to emphasize filenames, text displayed by an application and values you should enter into an application (e.g., GuessNumber or 500) in a sans-serif Lucida font. As you’ve noticed, the defining occurrence of each term is set in blue, bold type. For the figures in this section, we point out significant parts of the application. To make these features more visible, we’ve modified the background color of the Command Prompt window (for the Windows test drive only). To modify the Command Prompt colors on your system, open a Command Prompt by selecting Start > All Programs > Accessories > Command Prompt, then right click the title bar and select Properties. In the "Command Prompt" Properties dialog box that appears, click the Colors tab, and select your preferred text and background colors.

Running a C++ Application from the Windows Command Prompt 1. Checking your setup. It’s important to read the Before You Begin section at www.deitel.com/books/cpphtp8/ to make sure that you’ve copied the book’s examples to your hard drive correctly. 2. Locating the completed application. Open a Command Prompt window. To change to the directory for the completed GuessNumber application, type cd C:\examples\ch01\GuessNumber\Windows, then press Enter (Fig. 1.12). The command cd is used to change directories.

Fig. 1.12 | Opening a Command Prompt window and changing the directory.

1.10 Test-Driving a C++ Application

23

3. Running the GuessNumber application. Now that you are in the directory that contains the GuessNumber application, type the command GuessNumber (Fig. 1.13) and press Enter. [Note: GuessNumber.exe is the actual name of the application; however, Windows assumes the .exe extension by default.]

Fig. 1.13 | Running the GuessNumber application. 4. Entering your first guess. The application displays "Please type your first guess.", then displays a question mark (?) as a prompt on the next line (Fig. 1.13). At the prompt, enter 500 (Fig. 1.14).

Fig. 1.14 | Entering your first guess. 5. Entering another guess. The application displays "Too high. Try again.", meaning that the value you entered is greater than the number the application chose as the correct guess. So, you should enter a lower number for your next guess. At the prompt, enter 250 (Fig. 1.15). The application again displays "Too high. Try again.", because the value you entered is still greater than the number that the application chose as the correct guess.

Fig. 1.15 | Entering a second guess and receiving feedback. 6. Entering additional guesses. Continue to play the game by entering values until you guess the correct number. The application will display "Excellent! You guessed the number!" (Fig. 1.16).

24

Chapter 1

Introduction to Computers and C++

Fig. 1.16 | Entering additional guesses and guessing the correct number. 7. Playing the game again or exiting the application. After you guess correctly, the application asks if you’d like to play another game (Fig. 1.16). At the "Would you like to play again (y or n)?" prompt, entering the one character y causes the application to choose a new number and displays the message “Please type your first guess.” followed by a question mark prompt (Fig. 1.17) so you can make your first guess in the new game. Entering the character n ends the application and returns you to the application’s directory at the Command Prompt (Fig. 1.18). Each time you execute this application from the beginning (i.e., Step 3), it will choose the same numbers for you to guess. 8. Close the Command Prompt window.

Fig. 1.17 | Playing the game again.

Fig. 1.18 | Exiting the game.

1.10 Test-Driving a C++ Application

25

Running a C++ Application Using GNU C++ with Linux For this test drive, we assume that you know how to copy the examples into your home directory. Please see your instructor if you have any questions regarding copying the files to your Linux system. Also, for the figures in this section, we use a bold highlight to point out the user input required by each step. The prompt in the shell on our system uses the tilde (~) character to represent the home directory, and each prompt ends with the dollar sign ($) character. The prompt will vary among Linux systems. 1. Locating the completed application. From a Linux shell, change to the completed GuessNumber application directory (Fig. 1.19) by typing cd Examples/ch01/GuessNumber/GNU_Linux

then pressing Enter. The command cd is used to change directories. ~$ cd examples/ch01/GuessNumber/GNU_Linux ~/examples/ch01/GuessNumber/GNU_Linux$

Fig. 1.19 | Changing to the GuessNumber application’s directory. 2. Compiling the GuessNumber application. To run an application on the GNU C++ compiler, you must first compile it by typing g++ GuessNumber.cpp -o GuessNumber

as in Fig. 1.20. This command compiles the application and produces an executable file called GuessNumber. ~/examples/ch01/GuessNumber/GNU_Linux$ g++ GuessNumber.cpp -o GuessNumber ~/examples/ch01/GuessNumber/GNU_Linux$

Fig. 1.20 | Compiling the GuessNumber application using the g++ command. 3. Running the GuessNumber application. To run the executable file GuessNumber, type ./GuessNumber at the next prompt, then press Enter (Fig. 1.21). ~/examples/ch01/GuessNumber/GNU_Linux$ ./GuessNumber I have a number between 1 and 1000. Can you guess my number? Please type your first guess. ?

Fig. 1.21 | Running the GuessNumber application. 4. Entering your first guess. The application displays "Please type your first guess.", then displays a question mark (?) as a prompt on the next line (Fig. 1.21). At the prompt, enter 500 (Fig. 1.22). [Note: This is the same application that we modified and test-drove for Windows, but the outputs could vary based on the compiler being used.]

26

Chapter 1

Introduction to Computers and C++

5. Entering another guess. The application displays "Too high. Try again.", meaning that the value you entered is greater than the number the application chose as the correct guess (Fig. 1.22). At the next prompt, enter 250 (Fig. 1.23). This time the application displays "Too low. Try again.", because the value you entered is less than the correct guess. 6. Entering additional guesses. Continue to play the game (Fig. 1.24) by entering values until you guess the correct number. When you guess correctly, the application displays "Excellent! You guessed the number." ~/examples/ch01/GuessNumber/GNU_Linux$ ./GuessNumber I have a number between 1 and 1000. Can you guess my number? Please type your first guess. ? 500 Too high. Try again. ?

Fig. 1.22 | Entering an initial guess. ~/examples/ch01/GuessNumber/GNU_Linux$ ./GuessNumber I have a number between 1 and 1000. Can you guess my number? Please type your first guess. ? 500 Too high. Try again. ? 250 Too low. Try again. ?

Fig. 1.23 | Entering a second guess and receiving feedback. Too low. Try again. ? 375 Too low. Try again. ? 437 Too high. Try again. ? 406 Too high. Try again. ? 391 Too high. Try again. ? 383 Too low. Try again. ? 387 Too high. Try again. ? 385 Too high. Try again. ? 384 Excellent! You guessed the number. Would you like to play again (y or n)?

Fig. 1.24 | Entering additional guesses and guessing the correct number.

1.11 Web 2.0: Going Social

27

7. Playing the game again or exiting the application. After you guess the correct number, the application asks if you’d like to play another game. At the "Would you like to play again (y or n)?" prompt, entering the one character y causes the application to choose a new number and displays the message "Please type your first guess." followed by a question mark prompt (Fig. 1.25) so you can make your first guess in the new game. Entering the character n ends the application and returns you to the application’s directory in the shell (Fig. 1.26). Each time you execute this application from the beginning (i.e., Step 3), it will choose the same numbers for you to guess. Excellent! You guessed the number. Would you like to play again (y or n)? y I have a number between 1 and 1000. Can you guess my number? Please type your first guess. ?

Fig. 1.25 | Playing the game again. Excellent! You guessed the number. Would you like to play again (y or n)? n ~/examples/ch01/GuessNumber/GNU_Linux$

Fig. 1.26 | Exiting the game.

1.11 Web 2.0: Going Social The web literally exploded in the mid-to-late 1990s, but the “dot com” economic bust brought hard times in the early 2000s. The resurgence that began in 2004 or so has been named Web 2.0. Google is widely regarded as the signature company of Web 2.0. Some other companies with “Web 2.0 characteristics” are YouTube (video sharing), FaceBook (social networking), Twitter (microblogging), Groupon (social commerce), Foursquare (mobile check-in), Salesforce (business software offered as online services), Craigslist (free classified listings), Flickr (photo sharing), Second Life (a virtual world), Skype (Internet telephony) and Wikipedia (a free online encyclopedia).

Google In 1996, Stanford computer science Ph.D. candidates Larry Page and Sergey Brin began collaborating on a new search engine. In 1997, they changed the name to Google—a play on the mathematical term googol, a quantity represented by the number “one” followed by 100 “zeros” (or 10100)—a staggeringly large number. Google’s ability to return extremely accurate search results quickly helped it become the most widely used search engine and one of the most popular websites in the world. Google continues to be an innovator in search technologies. For example, Google Goggles is a fascinating mobile app (available on Android and iPhone) that allows you to

28

Chapter 1

Introduction to Computers and C++

perform a Google search using a photo rather than entering text. You simply take pictures of a landmarks, books (covers or barcodes), logos, art or wine bottle labels, and Google Goggles scans the photo and returns search results. You can also take a picture of text (for example, a restaurant menu or a sign) and Google Goggles will translate it for you.

Ajax Ajax is one of the premier Web 2.0 software technologies. Ajax helps Internet-based applications perform like desktop applications—a difficult task, given that such applications suffer transmission delays as data is shuttled back and forth between your computer and server computers on the Internet. Using Ajax, applications like Google Maps have achieved excellent performance and approach the look-and-feel of desktop applications. Social Applications Over the last several years, there’s been a tremendous increase in the number of social applications on the web. Even though the computer industry is mature, these sites were still able to become phenomenally successful in a relatively short period of time. Figure 1.27 discusses a few of the social applications that are making an impact. Company

Description

Facebook

Facebook was launched from a Harvard dorm room in 2004 by classmates Mark Zuckerberg, Chris Hughes, Dustin Moskovitz and Eduardo Saverin and is already worth an estimated $70 billion. By January 2011, Facebook was the most active site on the Internet with more than 600 million users—nearly 9% of the Earth’s population—who spend 700 billion minutes on Facebook per month (www.time.com/time/specials/packages/article/ 0,28804,2036683_2037183,00.html). At its current rate of growth (about 5% per month), Facebook will reach one billion users in 2012, out of the two billion people on the Internet! The activity on the site makes it extremely attractive for application developers. Each day, over 20 million applications are installed by Facebook users (www.facebook.com/press/info.php?statistics). Twitter was founded in 2006 by Jack Dorsey, Evan Williams and Isaac “Biz” Stone—all from the podcast company, Odeo. Twitter has revolutionized microblogging. Users post tweets—messages of up to 140 characters in length. Approximately 95 million tweets are posted per day (twitter.com/about). You can follow the tweets of friends, celebrities, businesses, government representatives (including the U.S. President, who has 6.3 million followers), etc., or you can follow tweets by subject to track news, trends and more. At the time of this writing, Lady Gaga had the most followers (over 7.7 million). Twitter has become the point of origin for many breaking news stories worldwide. Groupon, a social commerce site, was launched by Andrew Mason in 2008. By January 2011, the company was valued around $15 billion, making it the fastest growing company ever! It’s now available in hundreds of markets worldwide. Groupon offers one daily deal in each market for restaurants, retailers, services, attractions and more. Deals are activated only after a minimum number of people sign up to buy the product or service. If you sign up for a deal

Twitter

Groupon

Fig. 1.27 | Social applications. (Part 1 of 2.)

1.12 Software Technologies

29

Company

Description

Groupon (cont.)

and it has yet to meet the minimum, you might be inclined to tell others about the deal by email, Facebook, Twitter, etc. If the deal does not meet the minimum sales, it’s cancelled. One of the most successful national Groupon deals to date was a certificate for $50 worth of merchandise from a major apparel company for $25. Over 440,000 vouchers were sold in one day. Foursquare—launched in 2009 by Dennis Crowley and Naveen Selvadurai—is a mobile check-in application that allows you to notify your friends of your whereabouts. You can download the app to your smartphone and link it to your Facebook and Twitter accounts so your friends can follow you from multiple platforms. If you do not have a smartphone, you can check in by text message. Foursquare uses GPS to determine your exact location. Businesses use Foursquare to send offers to users in the area. Launched in March 2009, Foursquare already has over 5 million users worldwide. Skype is a software product that allows you to make mostly free voice and video calls over the Internet using a technology called VoIP (Voice over IP; IP stands for “Internet Protocol”). Skype was founded in 2003 by Niklas Zennström and Dane Janus Friis. Just two years later, the company was sold to eBay for $2.6 billion. YouTube is a video-sharing site that was founded in 2005. Within one year, the company was purchased by Google for $1.65 billion. YouTube now accounts for 10% of all Internet traffic (www.webpronews.com/topnews/2010/04/16/ facebook-and-youtube-get-the-most-business-internet-traffic). Within one week of the release of Apple’s iPhone 3GS—the first iPhone model to offer video—mobile uploads to YouTube grew 400% (www.hypebot.com/hypebot/ 2009/06/youtube-reports-1700-jump-in-mobile-video.html).

Foursquare

Skype

YouTube

Fig. 1.27 | Social applications. (Part 2 of 2.)

1.12 Software Technologies Figure 1.28 lists a number of buzzwords that you’ll hear in the software development community. We’ve created Resource Centers on most of these topics, with more on the way. Technology

Description

Agile software development

Agile software development is a set of methodologies that try to get software implemented faster and using fewer resources than previous methodologies. Check out the Agile Alliance (www.agilealliance.org) and the Agile Manifesto (www.agilemanifesto.org). Refactoring involves reworking programs to make them clearer and easier to maintain while preserving their correctness and functionality. It’s widely employed with agile development methodologies. Many IDEs include refactoring tools to do major portions of the reworking automatically.

Refactoring

Fig. 1.28 | Software technologies. (Part 1 of 2.)

30

Chapter 1

Introduction to Computers and C++

Technology

Description

Design patterns

Design patterns are proven architectures for constructing flexible and maintainable object-oriented software. The field of design patterns tries to enumerate those recurring patterns, encouraging software designers to reuse them to develop better-quality software using less time, money and effort.

LAMP

MySQL is an open-source database management system. PHP is the most popular open-source server-side Internet “scripting” language for developing Internet-based applications. LAMP is an acronym for the set of opensource technologies that many developers use to build web applications— it stands for Linux, Apache, MySQL and PHP (or Perl or Python—two other languages used for similar purposes).

Software as a Service (SaaS)

Software has generally been viewed as a product; most software still is offered this way. If you want to run an application, you buy a software package from a software vendor—often a CD, DVD or web download. You then install that software on your computer and run it as needed. As new versions of the software appear, you upgrade your software, often requiring significant time and at considerable expense. This process can become cumbersome for organizations with tens of thousands of systems that must be maintained on a diverse array of computer equipment. With Software as a Service (SaaS), the software runs on servers elsewhere on the Internet. When that server is updated, all clients worldwide see the new capabilities—no local installation is needed. You access the service through a browser. Browsers are quite portable, so you can run the same applications on a wide variety of computers from anywhere in the world. Salesforce.com, Google, and Microsoft’s Office Live and Windows Live all offer SaaS.

Platform as a Service (PaaS)

Platform as a Service (PaaS) provides a computing platform for developing and running applications as a service over the web, rather than installing the tools on your computer. PaaS providers include Google App Engine, Amazon EC2, Bungee Labs and more.

Cloud computing

SaaS and PaaS are examples of cloud computing in which software, platforms and infrastructure (e.g., processing power and storage) are hosted on demand over the Internet. This provides users with flexibility, scalability and cost savings. For example, consider a company’s data storage needs which can fluctuate significantly over the course of a year. Rather than investing in large-scale storage hardware—which can be costly to purchase, maintain and secure, and would most likely not be used to capacity at all times—the company could purchase cloud-based services (such as Amazon S3, Google Storage, Microsoft Windows Azure™, Nirvanix™ and others) dynamically as needed.

Software Development Kit (SDK)

Software Development Kits (SDKs) include the tools and documentation developers use to program applications.

Fig. 1.28 | Software technologies. (Part 2 of 2.)

1.13 TR1, the New C++ Standard and the Open Source Boost Libraries

31

Figure 1.29 describes software product release categories. Version

Description

Alpha

An alpha version of software is the earliest release of a software product that’s still under active development. Alpha versions are often buggy, incomplete and unstable and are released to a relatively small number of developers for testing new features, getting early feedback, etc.

Beta

Beta versions are released to a larger number of developers later in the development process after most major bugs have been fixed and new features are nearly complete. Beta software is more stable, but still subject to change.

Release candidates

Release candidates are generally feature complete and (supposedly) bug free and ready for use by the community, which provides a diverse testing environment—the software is used on different systems, with varying constraints and for a variety of purposes. Any bugs that appear are corrected and eventually the final product is released to the general public. Software companies often distribute incremental updates over the Internet.

Continuous beta

Software that’s developed using this approach generally does not have version numbers (for example, Google search or Gmail). The software, which is hosted in the cloud (not installed on your computer), is constantly evolving so that users always have the latest version.

Fig. 1.29 | Software product release terminology.

1.13 Future of C++: TR1, the New C++ Standard and the Open Source Boost Libraries Bjarne Stroustrup, the creator of C++, has expressed his vision for the future of C++. The main goals for the new standard are to make C++ easier to learn, improve library building capabilities, and increase compatibility with the C programming language. Throughout the book, we discuss in optional sections various key features of the new C++ standard. In addition, Chapter 23 introduces the Boost C++ Libraries, Technical Report 1 (TR1) and more new C++ features. Technical Report 1 describes the proposed changes to the C++ Standard Library. These libraries add useful functionality to C++. The C++ Standards Committee is currently finishing the revision of the C++ Standard. The last standard was published in 1998. Work on the new standard began in 2003. At that time, it was referred to as C++0x because the standard was scheduled to be released before the end of the decade. The new standard includes most of the libraries in TR1 and changes to the core language. The Boost C++ Libraries are free, open-source libraries created by members of the C++ community. Boost has grown to over 100 libraries, with more being added regularly. Today there are thousands of programmers in the Boost open source community. Boost provides C++ programmers with useful libraries that work well with the existing C++ Standard Library. The Boost libraries can be used by C++ programmers working on a wide variety of platforms with many different compilers. Several of the Boost libraries are included in TR1 and will be part of the new standard. We overview the libraries included

32

Chapter 1

Introduction to Computers and C++

in TR1 and provide code examples for the “regular expression” and “smart pointer” libraries. Regular expressions are used to match specific character patterns in text. They can be used to validate data to ensure that it’s in a particular format, to replace parts of one string with another, or to split a string. Many common bugs in C and C++ code are related to pointers, a powerful programming capability C++ absorbed from C. Smart pointers help you avoid errors by providing additional functionality, typically strengthens the process of memory allocation and deallocation.

1.14 Keeping Up-to-Date with Information Technologies Figure 1.30 lists key technical and business publications that will help you stay up-to-date with the latest news and trends and technology. You can also find a growing list of Internet- and web-related Resource Centers at www.deitel.com/resourcecenters.html. Publication

URL

Bloomberg BusinessWeek CNET Computer World Engadget eWeek Fast Company Fortune InfoWorld Mashable PCWorld SD Times Slashdot Smarter Technology Technology Review Techcrunch Wired

www.businessweek.com news.cnet.com www.computerworld.com www.engadget.com www.eweek.com www.fastcompany.com/ money.cnn.com/magazines/fortune/ www.infoworld.com mashable.com www.pcworld.com www.sdtimes.com slashdot.org/ www.smartertechnology.com technologyreview.com techcrunch.com www.wired.com

Fig. 1.30 | Technical and business publications (many are free).

1.15 Wrap-Up In this chapter we discussed computer hardware, software, programming languages and operating systems. We introduced the basics of object technology. You learned about some of the exciting recent developments in the computer field. We overviewed a typical C++ program development environment and you test-drove a C++ application. We also discussed some key software development terminology.

Self-Review Exercises

33

In Chapter 2, you’ll create your first C++ applications. You’ll see how programs display messages on the screen and obtain information from the user at the keyboard for processing. You’ll see several examples that demonstrate how programs display messages on the screen and obtain information from the user at the keyboard for processing.

Self-Review Exercises 1.1

Fill in the blanks in each of the following statements: a) The company that popularized personal computing was . b) The computer that made personal computing legitimate in business and industry was . the c) Computers process data under the control of sets of instructions called . , , , , d) The key logical units of the computer are the and . e) The three types of languages discussed in the chapter are , and . f) The programs that translate high-level language programs into machine language are called . is a smartphone operating system based on the Linux kernel and Java. g) h) software is generally feature complete and (supposedly) bug free and ready for use by the community. which allows the dei) The Wii Remote, as well as many smartphones, uses a(n) vice to respond to motion.

1.2

Fill in the blanks in each of the following sentences about the C++ environment. a) C++ programs are normally typed into a computer using a(n) program. program executes before the compiler’s translation b) In a C++ system, a(n) phase begins. c) The program combines the output of the compiler with various library functions to produce an executable program. program transfers the executable program from disk to memory. d) The

1.3

Fill in the blanks in each of the following statements (based on Section 1.6): a) Objects have the property of —although objects may know how to communicate with one another across well-defined interfaces, they normally are not allowed to know how other objects are implemented. b) C++ programmers concentrate on creating , which contain data members and the member functions that manipulate those data members and provide services to clients. c) The process of analyzing and designing a system from an object-oriented point of view is called . d) With , new classes of objects are derived by absorbing characteristics of existing classes, then adding unique characteristics of their own. e) is a graphical language that allows people who design software systems to use an industry-standard notation to represent them. f) The size, shape, color and weight of an object are considered of the object’s class.

Answers to Self-Review Exercises 1.1 a) Apple. b) IBM Personal Computer. c) programs. d) input unit, output unit, memory unit, central processing unit, arithmetic and logic unit, secondary storage unit. e) machine lan-

34

Chapter 1

Introduction to Computers and C++

guages, assembly languages, high-level languages. f) compilers. g) Android. h) Release candidate. i) accelerometer. 1.2

a) editor. b) preprocessor. c) linker. d) loader.

1.3 a) information hiding. b) classes. c) object-oriented analysis and design (OOAD). d) inheritance. e) The Unified Modeling Language (UML). f) attributes.

Exercises 1.4

Fill in the blanks in each of the following statements: a) The logical unit of the computer that receives information from outside the computer . for use by the computer is the b) The process of instructing the computer to solve a problem is called . c) is a type of computer language that uses English-like abbreviations for machine-language instructions. is a logical unit of the computer that sends information which has already d) been processed by the computer to various devices so that it may be used outside the computer. and are logical units of the computer that retain information. e) f) is a logical unit of the computer that performs calculations. is a logical unit of the computer that makes logical decisions. g) h) languages are most convenient to the programmer for writing programs quickly and easily. . i) The only language a computer can directly understand is that computer’s j) is a logical unit of the computer that coordinates the activities of all the other logical units.

1.5

Fill in the blanks in each of the following statements: is used to develop large-scale enterprise applications, to enhance the functiona) ality of web servers, to provide applications for consumer devices and for many other purposes. initially became widely known as the development language of the Unix opb) erating system. c) The Web 2.0 company is the fastest growing company ever. d) The programming language was developed by Bjarne Stroustrup in the early 1980s at Bell Laboratories.

1.6

Fill in the blanks in each of the following statements: a) C++ programs normally go through six phases— , , , , and . b) A(n) provides many tools that support the software development process, such as editors for writing and editing programs, debuggers for locating logic errors in programs, and many other features. c) The command java invokes the , which executes Java programs. d) A(n) is a software application that simulates a computer, but hides the underlying operating system and hardware from the programs that interact with it. e) The takes the .class files containing the program’s bytecodes and transfers them to primary memory. f) The examines bytecodes to ensure that they’re valid.

1.7 You’re probably wearing on your wrist one of the world’s most common types of objects— a watch. Discuss how each of the following terms and concepts applies to the notion of a watch:

Making a Difference

35

object, attributes, behaviors, class, inheritance (consider, for example, an alarm clock), abstraction, modeling, messages, encapsulation, interface and information hiding.

Making a Difference Throughout the book we’ve included Making a Difference exercises in which you’ll be asked to work on problems that really matter to individuals, communities, countries and the world. For more information about worldwide organizations working to make a difference, and for related programming project ideas, visit our Making a Difference Resource Center at www.deitel.com/ makingadifference. 1.8 (Test Drive: Carbon Footprint Calculator) Some scientists believe that carbon emissions, especially from the burning of fossil fuels, contribute significantly to global warming and that this can be combatted if individuals take steps to limit their use of carbon-based fuels. Organizations and individuals are increasingly concerned about their “carbon footprints.” Websites such as TerraPass www.terrapass.com/carbon-footprint-calculator/

and Carbon Footprint www.carbonfootprint.com/calculator.aspx

provide carbon footprint calculators. Test drive these calculators to determine your carbon footprint. Exercises in later chapters will ask you to program your own carbon footprint calculator. To prepare for this, research the formulas for calculating carbon footprints. 1.9 (Test Drive: Body Mass Index Calculator) By recent estimates, two-thirds of the people in the United States are overweight and about half of those are obese. This causes significant increases in illnesses such as diabetes and heart disease. To determine whether a person is overweight or obese, you can use a measure called the body mass index (BMI). The United States Department of Health and Human Services provides a BMI calculator at www.nhlbisupport.com/bmi/. Use it to calculate your own BMI. An exercise in Chapter 2 will ask you to program your own BMI calculator. To prepare for this, research the formulas for calculating BMI. 1.10 (Attributes of Hybrid Vehicles) In this chapter you learned the basics of classes. Now you’ll begin “fleshing out” aspects of a class called “Hybrid Vehicle.” Hybrid vehicles are becoming increasingly popular, because they often get much better mileage than purely gasoline-powered vehicles. Browse the web and study the features of four or five of today’s popular hybrid cars, then list as many of their hybrid-related attributes as you can. For example, common attributes include city-miles-pergallon and highway-miles-per-gallon. Also list the attributes of the batteries (type, weight, etc.). 1.11 (Gender Neutrality) Many people want to eliminate sexism in all forms of communication. You’ve been asked to create a program that can process a paragraph of text and replace gender-specific words with gender-neutral ones. Assuming that you’ve been given a list of gender-specific words and their gender-neutral replacements (e.g., replace “wife” by “spouse,” “man” by “person,” “daughter” by “child” and so on), explain the procedure you’d use to read through a paragraph of text and manually perform these replacements. How might your procedure generate a strange term like “woperchild,” which is actually listed in the Urban Dictionary (www.urbandictionary.com)? In Chapter 4, you’ll learn that a more formal term for “procedure” is “algorithm,” and that an algorithm specifies the steps to be performed and the order in which to perform them. 1.12 (Privacy) Some online email services save all email correspondence for some period of time. Suppose a disgruntled employee of one of these online email services were to post all of the email correspondences for millions of people, including yours, on the Internet. Discuss the issues. 1.13 (Programmer Responsibility and Liability) As a programmer in industry, you may develop software that could affect people’s health or even their lives. Suppose a software bug in one of your

36

Chapter 1

Introduction to Computers and C++

programs were to cause a cancer patient to receive an excessive dose during radiation therapy and that the person is either severely injured or dies. Discuss the issues. 1.14 (2010 “Flash Crash”) An example of the consequences of our excessive dependency on computers was the so-called “flash crash” which occurred on May 6, 2010, when the U.S. stock market fell precipitously in a matter of minutes, wiping out trillions of dollars of investments, and then recovered within minutes. Use the Internet to investigate the causes of this crash and discuss the issues it raises.

Making a Difference Resources The Microsoft Image Cup is a global competition in which students use technology to try to solve some of the world’s most difficult problems, such as environmental sustainability, ending hunger, emergency response, literacy, combating HIV/AIDS and more. For more information about the competition and to learn about the projects developed by previous winners, visit www.imaginecup.com/ about. You can also find several project ideas submitted by worldwide charitable organizations at www.imaginecup.com/students/imagine-cup-solve-this. For additional ideas for programming projects that can make a difference, search the web for “making a difference” and visit the following websites: www.un.org/millenniumgoals

The United Nations Millennium Project seeks solutions to major worldwide issues such as environmental sustainability, gender equality, child and maternal health, universal education and more. www.ibm.com/smarterplanet/

The IBM® Smarter Planet website discusses how IBM is using technology to solve issues related to business, cloud computing, education, sustainability and more. www.gatesfoundation.org/Pages/home.aspx

The Bill and Melinda Gates Foundation provides grants to organizations that work to alleviate hunger, poverty and disease in developing countries. In the U.S., the foundation focusses on improving public education, particularly for people with few resources. www.nethope.org/

NetHope is a collaboration of humanitarian organizations worldwide working to solve technology problems such as connectivity, emergency response and more. www.rainforestfoundation.org/home

The Rainforest Foundation works to preserve rainforests and to protect the rights of the indigenous people who call the rainforests home. The site includes a list of things you can do to help. www.undp.org/

The United Nations Development Programme (UNDP) seeks solutions to global challenges such as crisis prevention and recovery, energy and the environment, democratic governance and more. www.unido.org

The United Nations Industrial Development Organization (UNIDO) seeks to reduce poverty, give developing countries the opportunity to participate in global trade, and promote energy efficiency and sustainability. www.usaid.gov/

USAID promotes global democracy, health, economic growth, conflict prevention, humanitarian aid and more. www.toyota.com/ideas-for-good/

Toyota’s Ideas for Good website describes several Toyota technologies that are making a difference— including their Advanced Parking Guidance System, Hybrid Synergy Drive®, Solar Powered Ventilation System, T.H.U.M.S. (Total Human Model for Safety) and Touch Tracer Display. You can participate in the Ideas for Good challenge by submitting a short essay or video describing how these technologies can be used for other good purposes.

2

Introduction to C++ Programming

What’s in a name? that which we call a rose By any other name would smell as sweet. —William Shakespeare

When faced with a decision, I always ask, “What would be the most fun?”

—Peggy Walker

High thoughts must have high language.

—Aristophanes

One person can make a difference and every person should try. —John F. Kennedy

Objectives

In this chapter you’ll learn: ■



■ ■

■ ■



To write simple computer programs in C++. To write simple input and output statements. To use fundamental types. Basic computer memory concepts. To use arithmetic operators. The precedence of arithmetic operators. To write simple decisionmaking statements.

38

Chapter 2

Introduction to C++ Programming

2.1 Introduction 2.2 First Program in C++: Printing a Line of Text 2.3 Modifying Our First C++ Program 2.4 Another C++ Program: Adding Integers

2.5 Memory Concepts 2.6 Arithmetic 2.7 Decision Making: Equality and Relational Operators 2.8 Wrap-Up

Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference

2.1 Introduction We now introduce C++ programming, which facilitates a disciplined approach to program design. Most of the C++ programs you’ll study in this book process information and display results. In this chapter, we present five examples that demonstrate how your programs can display messages and obtain information from the user for processing. The first three examples simply display messages on the screen. The next obtains two numbers from a user, calculates their sum and displays the result. The accompanying discussion shows you how to perform arithmetic calculations and save their results for later use. The fifth example demonstrates decision-making by showing you how to compare two numbers, then display messages based on the comparison results. We analyze each program one line at a time to help you ease your way into C++ programming.

2.2 First Program in C++: Printing a Line of Text C++ uses notations that may appear strange to nonprogrammers. We now consider a simple program that prints a line of text (Fig. 2.1). This program illustrates several important features of the C++ language. 1 2 3 4 5 6 7 8 9 10 11

// Fig. 2.1: fig02_01.cpp // Text-printing program. #include // allows program to output data to the screen // function main begins program execution int main() { std::cout << "Welcome to C++!\n"; // display message return 0; // indicate that program ended successfully } // end function main

Welcome to C++!

Fig. 2.1 | Text-printing program. Comments Lines 1 and 2 // Fig. 2.1: fig02_01.cpp // Text-printing program.

2.2 First Program in C++: Printing a Line of Text

39

each begin with //, indicating that the remainder of each line is a comment. You insert comments to document your programs and to help other people read and understand them. Comments do not cause the computer to perform any action when the program is run—they’re ignored by the C++ compiler and do not cause any machine-language object code to be generated. The comment Text-printing program describes the purpose of the program. A comment beginning with // is called a single-line comment because it terminates at the end of the current line. [Note: You also may use C’s style in which a comment—possibly containing many lines—begins with /* and ends with */.]

Good Programming Practice 2.1

Every program should begin with a comment that describes the purpose of the program. #include

Line 3

Preprocessor Directive

#include // allows program to output data to the screen

is a preprocessor directive, which is a message to the C++ preprocessor (introduced in Section 1.9). Lines that begin with # are processed by the preprocessor before the program is compiled. This line notifies the preprocessor to include in the program the contents of the input/output stream header . This header must be included for any program that outputs data to the screen or inputs data from the keyboard using C++’s stream input/output. The program in Fig. 2.1 outputs data to the screen, as we’ll soon see. We discuss headers in more detail in Chapter 6 and explain the contents of in Chapter 15.

Common Programming Error 2.1

Forgetting to include the header in a program that inputs data from the keyboard or outputs data to the screen causes the compiler to issue an error message.

Blank Lines and White Space Line 4 is simply a blank line. You use blank lines, space characters and tab characters (i.e., “tabs”) to make programs easier to read. Together, these characters are known as white space. White-space characters are normally ignored by the compiler. The main Function Line 5 // function main begins program execution

is another single-line comment indicating that program execution begins at the next line. Line 6 int main()

is a part of every C++ program. The parentheses after main indicate that main is a program building block called a function. C++ programs typically consist of one or more functions and classes (as you’ll learn in Chapter 3). Exactly one function in every program must be named main. Figure 2.1 contains only one function. C++ programs begin executing at function main, even if main is not the first function in the program. The keyword int to

40

Chapter 2

Introduction to C++ Programming

the left of main indicates that main “returns” an integer (whole number) value. A keyword is a word in code that is reserved by C++ for a specific use. The complete list of C++ keywords can be found in Fig. 4.3. We’ll explain what it means for a function to “return a value” when we demonstrate how to create your own functions in Section 3.3. For now, simply include the keyword int to the left of main in each of your programs. The left brace, {, (line 7) must begin the body of every function. A corresponding right brace, }, (line 11) must end each function’s body.

An Output Statement Line 8 std::cout << "Welcome to C++!\n"; // display message

instructs the computer to perform an action—namely, to print the string of characters contained between the double quotation marks. A string is sometimes called a character string or a string literal. We refer to characters between double quotation marks simply as strings. White-space characters in strings are not ignored by the compiler. The entire line 8, including std::cout, the << operator, the string "Welcome to C++!\n" and the semicolon (;), is called a statement. Every C++ statement must end with a semicolon (also known as the statement terminator). Preprocessor directives (like #include) do not end with a semicolon. Output and input in C++ are accomplished with streams of characters. Thus, when the preceding statement is executed, it sends the stream of characters Welcome to C++!\n to the standard output stream object—std::cout— which is normally “connected” to the screen.

Common Programming Error 2.2

Omitting the semicolon at the end of a C++ statement is a syntax error. The syntax of a programming language specifies the rules for creating proper programs in that language. A syntax error occurs when the compiler encounters code that violates C++’s language rules (i.e., its syntax). The compiler normally issues an error message to help you locate and fix the incorrect code. Syntax errors are also called compiler errors, compile-time errors or compilation errors, because the compiler detects them during the compilation phase. You cannot execute your program until you correct all the syntax errors in it. As you’ll see, some compilation errors are not syntax errors.

Good Programming Practice 2.2

Indent the body of each function one level within the braces that delimit the function’s body. This makes a program’s functional structure stand out and makes the program easier to read.

Good Programming Practice 2.3

Set a convention for the size of indent you prefer, then apply it uniformly. The tab key may be used to create indents, but tab stops may vary. We prefer three spaces per level of indent.

The std Namespace The std:: before cout is required when we use names that we’ve brought into the program by the preprocessor directive #include . The notation std::cout spec-

2.2 First Program in C++: Printing a Line of Text

41

ifies that we are using a name, in this case cout, that belongs to “namespace” std. The names cin (the standard input stream) and cerr (the standard error stream)—introduced in Chapter 1—also belong to namespace std. Namespaces are an advanced C++ feature that we discuss in depth in Chapter 24, Other Topics. For now, you should simply remember to include std:: before each mention of cout, cin and cerr in a program. This can be cumbersome—in Fig. 2.13, we introduce the using directive, which will enable you to omit std:: before each use of a name in the std namespace.

The Stream Insertion Operator and Es’cape Sequences The << operator is referred to as the stream insertion operator. When this program executes, the value to the operator’s right, the right operand, is inserted in the output stream. Notice that the operator points in the direction of where the data goes. The right operand’s characters normally print exactly as they appear between the double quotes. However, the characters \n are not printed on the screen (Fig. 2.1). The backslash (\) is called an escape character. It indicates that a “special” character is to be output. When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence. The escape sequence \n means newline. It causes the cursor (i.e., the current screen-position indicator) to move to the beginning of the next line on the screen. Some common escape sequences are listed in Fig. 2.2. Escape sequence \n \t \r \a \\ \' \"

Description Newline. Position the screen cursor to the beginning of the next line. Horizontal tab. Move the screen cursor to the next tab stop. Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. Alert. Sound the system bell. Backslash. Used to print a backslash character. Single quote. Use to print a single quote character. Double quote. Used to print a double quote character.

Fig. 2.2 | Escape sequences. The return Statement Line 10 return 0; // indicate that program ended successfully

is one of several means we’ll use to exit a function. When the return statement is used at the end of main, as shown here, the value 0 indicates that the program has terminated successfully. The right brace, }, (line 11) indicates the end of function main. According to the C++ standard, if program execution reaches the end of main without encountering a return statement, it’s assumed that the program terminated successfully—exactly as when the last statement in main is a return statement with the value 0. For that reason, we omit the return statement at the end of main in subsequent programs.

42

Chapter 2

Introduction to C++ Programming

2.3 Modifying Our First C++ Program We now present two examples that modify the program of Fig. 2.1 to print text on one line by using multiple statements and to print text on several lines by using a single statement.

Printing a Single Line of Text with Multiple Statements Welcome to C++! can be printed several ways. For example, Fig. 2.3 performs stream insertion in multiple statements (lines 8–9), yet produces the same output as the program of Fig. 2.1. [Note: From this point forward, we use a yellow background to highlight the key features each program introduces.] Each stream insertion resumes printing where the previous one stopped. The first stream insertion (line 8) prints Welcome followed by a space, and because this string did not end with \n, the second stream insertion (line 9) begins printing on the same line immediately following the space. 1 2 3 4 5 6 7 8 9 10

// Fig. 2.3: fig02_03.cpp // Printing a line of text with multiple statements. #include // allows program to output data to the screen // function main begins program execution int main() { std::cout << "Welcome "; std::cout << "to C++!\n"; } // end function main

Welcome to C++!

Fig. 2.3 | Printing a line of text with multiple statements. Printing Multiple Lines of Text with a Single Statement A single statement can print multiple lines by using newline characters, as in line 8 of Fig. 2.4. Each time the \n (newline) escape sequence is encountered in the output stream, the screen cursor is positioned to the beginning of the next line. To get a blank line in your output, place two newline characters back to back, as in line 8. 1 2 3 4 5 6 7 8 9

// Fig. 2.4: fig02_04.cpp // Printing multiple lines of text with a single statement. #include // allows program to output data to the screen // function main begins program execution int main() { std::cout << "Welcome\nto\n\nC++!\n"; } // end function main

Welcome to C++!

Fig. 2.4 | Printing multiple lines of text with a single statement.

2.4 Another C++ Program: Adding Integers

43

2.4 Another C++ Program: Adding Integers Our next program uses the input stream object std::cin and the stream extraction operator, >>, to obtain two integers typed by a user at the keyboard, computes the sum of these values and outputs the result using std::cout. Figure 2.5 shows the program and sample inputs and outputs. In the sample execution, we highlight the user’s input in bold. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

// Fig. 2.5: fig02_05.cpp // Addition program that displays the sum of two integers. #include // allows program to perform input and output // function main begins program execution int main() { // variable declarations int number1; // first integer to add int number2; // second integer to add int sum; // sum of number1 and number2 std::cout << "Enter first integer: "; // prompt user for data std::cin >> number1; // read first integer from user into number1 std::cout << "Enter second integer: "; // prompt user for data std::cin >> number2; // read second integer from user into number2 sum = number1 + number2; // add the numbers; store result in sum std::cout << "Sum is " << sum << std::endl; // display sum; end line } // end function main

Enter first integer: 45 Enter second integer: 72 Sum is 117

Fig. 2.5 | Addition program that displays the sum of two integers entered at the keyboard. The comments in lines 1 and 2 state the name of the file and the purpose of the program. The C++ preprocessor directive in line 3 includes the contents of the header. The program begins execution with function main (line 6). The left brace (line 7) begins main’s body and the corresponding right brace (line 22) ends it.

Variable Declarations Lines 9–11 int number1; // first integer to add int number2; // second integer to add int sum; // sum of number1 and number2

are declarations. The identifiers number1, number2 and sum are the names of variables. A variable is a location in the computer’s memory where a value can be stored for use by a program. These declarations specify that the variables number1, number2 and sum are data of type int, meaning that these variables will hold integer values, i.e., whole numbers such

44

Chapter 2

Introduction to C++ Programming

as 7, –11, 0 and 31914. All variables must be declared with a name and a data type before they can be used in a program. Several variables of the same type may be declared in one declaration or in multiple declarations. We could have declared all three variables in one declaration by using a comma-separated list as follows: int number1, number2, sum;

This makes the program less readable and prevents us from providing comments that describe each variable’s purpose.

Good Programming Practice 2.4

Place a space after each comma (,) to make programs more readable.

Fundamental Types We’ll soon discuss the type double for specifying real numbers, and the type char for specifying character data. Real numbers are numbers with decimal points, such as 3.4, 0.0 and –11.19. A char variable may hold only a single lowercase letter, a single uppercase letter, a single digit or a single special character (e.g., $ or *). Types such as int, double and char are called fundamental types. Fundamental-type names are keywords and therefore must appear in all lowercase letters. Appendix C contains the complete list of fundamental types. Identifiers A variable name (such as number1) is any valid identifier that is not a keyword. An identifier is a series of characters consisting of letters, digits and underscores ( _ ) that does not begin with a digit. C++ is case sensitive—uppercase and lowercase letters are different, so a1 and A1 are different identifiers.

Portability Tip 2.1

C++ allows identifiers of any length, but your C++ implementation may restrict identifier lengths. Use identifiers of 31 characters or fewer to ensure portability.

Good Programming Practice 2.5

Choosing meaningful identifiers makes a program self-documenting—a person can understand the program simply by reading it rather than having to refer to manuals or comments.

Good Programming Practice 2.6

Avoid using abbreviations in identifiers. This improves program readability.

Good Programming Practice 2.7

Do not use identifiers that begin with underscores and double underscores, because C++ compilers may use names like that for their own purposes internally. This will prevent the names you choose from being confused with names the compilers choose.

Placement of Variable Declarations Declarations of variables can be placed almost anywhere in a program, but they must appear before their corresponding variables are used in the program. For example, in the program of Fig. 2.5, the declaration in line 9

2.4 Another C++ Program: Adding Integers

45

int number1; // first integer to add

could have been placed immediately before line 14 std::cin >> number1; // read first integer from user into number1

the declaration in line 10 int number2; // second integer to add

could have been placed immediately before line 17 std::cin >> number2; // read second integer from user into number2

and the declaration in line 11 int sum; // sum of number1 and number2

could have been placed immediately before line 19 sum = number1 + number2; // add the numbers; store result in sum

Good Programming Practice 2.8

Always place a blank line between a declaration and adjacent executable statements. This makes the declarations stand out and contributes to program clarity.

Obtaining the First Value from the User Line 13 std::cout << "Enter first integer: "; // prompt user for data

displays Enter first integer: followed by a space. This message is called a prompt because it directs the user to take a specific action. We like to pronounce the preceding statement as “std::cout gets the character string "Enter first integer: ".” Line 14 std::cin >> number1; // read first integer from user into number1

uses the standard input stream object cin (of namespace std) and the stream extraction operator, >>, to obtain a value from the keyboard. Using the stream extraction operator with std::cin takes character input from the standard input stream, which is usually the keyboard. We like to pronounce the preceding statement as, “std::cin gives a value to number1” or simply “std::cin gives number1.” When the computer executes the preceding statement, it waits for the user to enter a value for variable number1. The user responds by typing an integer (as characters), then pressing the Enter key (sometimes called the Return key) to send the characters to the computer. The computer converts the character representation of the number to an integer and assigns (i.e., copies) this number (or value) to the variable number1. Any subsequent references to number1 in this program will use this same value. The std::cout and std::cin stream objects facilitate interaction between the user and the computer. Because this interaction resembles a dialog, it’s often called interactive computing.

Obtaining the Second Value from the User Line 16 std::cout << "Enter second integer: "; // prompt user for data

46

Chapter 2

Introduction to C++ Programming

prints Enter second integer: on the screen, prompting the user to take action. Line 17 std::cin >> number2; // read second integer from user into number2

obtains a value for variable number2 from the user.

Calculating the Sum of the Values Input by the User The assignment statement in line 19 sum = number1 + number2; // add the numbers; store result in sum

adds the values of variables number1 and number2 and assigns the result to variable sum using the assignment operator =. The statement is read as, “sum gets the value of number1 + number2.” Most calculations are performed in assignment statements. The = operator and the + operator are called binary operators because each has two operands. In the case of the + operator, the two operands are number1 and number2. In the case of the preceding = operator, the two operands are sum and the value of the expression number1 + number2.

Good Programming Practice 2.9

Place spaces on either side of a binary operator. This makes the operator stand out and makes the program more readable.

Displaying the Result Line 21 std::cout << "Sum is " << sum << std::endl; // display sum; end line

displays the character string Sum is followed by the numerical value of variable sum followed by std::endl—a so-called stream manipulator. The name endl is an abbreviation for “end line” and belongs to namespace std. The std::endl stream manipulator outputs a newline, then “flushes the output buffer.” This simply means that, on some systems where outputs accumulate in the machine until there are enough to “make it worthwhile” to display them on the screen, std::endl forces any accumulated outputs to be displayed at that moment. This can be important when the outputs are prompting the user for an action, such as entering data. The preceding statement outputs multiple values of different types. The stream insertion operator “knows” how to output each type of data. Using multiple stream insertion operators (<<) in a single statement is referred to as concatenating, chaining or cascading stream insertion operations. It’s unnecessary to have multiple statements to output multiple pieces of data. Calculations can also be performed in output statements. We could have combined the statements in lines 19 and 21 into the statement std::cout << "Sum is " << number1 + number2 << std::endl;

thus eliminating the need for the variable sum. A powerful feature of C++ is that you can create your own data types called classes (we introduce this capability in Chapter 3 and explore it in depth in Chapters 9 and 10). You can then “teach” C++ how to input and output values of these new data types using the >> and << operators (this is called operator overloading—a topic we explore in Chapter 11).

2.5 Memory Concepts

47

2.5 Memory Concepts Variable names such as number1, number2 and sum actually correspond to locations in the computer’s memory. Every variable has a name, a type, a size and a value. In the addition program of Fig. 2.5, when the statement in line 14 std::cin >> number1; // read first integer from user into number1

is executed, the integer typed by the user is placed into a memory location to which the name number1 has been assigned by the compiler. Suppose the user enters 45 as the value for number1. The computer will place 45 into the location number1, as shown in Fig. 2.6. When a value is placed in a memory location, the value overwrites the previous value in that location; thus, placing a new value into a memory location is said to be destructive. Returning to our addition program, suppose the user enters 72 when the statement std::cin >> number2; // read second integer from user into number2

is executed. This value is placed into the location number2, and memory appears as in Fig. 2.7. The variables’ locations are not necessarily adjacent in memory. Once the program has obtained values for number1 and number2, it adds these values and places the total into the variable sum. The statement sum = number1 + number2; // add the numbers; store result in sum

replaces whatever value was stored in sum. The calculated sum of number1 and number2 is placed into variable sum without regard to what value may already be in sum—that value is lost). After sum is calculated, memory appears as in Fig. 2.8. The values of number1 and number2 appear exactly as they did before the calculation. These values were used, but not destroyed, as the computer performed the calculation. Thus, when a value is read out of a memory location, the process is nondestructive. number1

45

Fig. 2.6 | Memory location showing the name and value of variable number1. number1

45

number2

72

Fig. 2.7 | Memory locations after storing values the variables for number1 and number2. number1

45

number2

72

sum

117

Fig. 2.8 | Memory locations after calculating and storing the sum of number1 and number2.

48

Chapter 2

Introduction to C++ Programming

2.6 Arithmetic Most programs perform arithmetic calculations. Figure 2.9 summarizes the C++ arithmetic operators. Note the use of various special symbols not used in algebra. The asterisk (*) indicates multiplication and the percent sign (%) is the modulus operator that will be discussed shortly. The arithmetic operators in Fig. 2.9 are all binary operators, i.e., operators that take two operands. For example, the expression number1 + number2 contains the binary operator + and the two operands number1 and number2. Integer division (i.e., where both the numerator and the denominator are integers) yields an integer quotient; for example, the expression 7 / 4 evaluates to 1 and the expression 17 / 5 evaluates to 3. Any fractional part in integer division is discarded (i.e., truncated)—no rounding occurs.

C++ operation Addition Subtraction Multiplication Division Modulus

C++ arithmetic operator

Algebraic expression

C++ expression

+

f+7 p–c bm or b ⋅ m x / y or -yx- or x ÷ y r mod s

f + 7

* / %

p - c b * m x / y r % s

Fig. 2.9 | Arithmetic operators. C++ provides the modulus operator, %, that yields the remainder after integer division. The modulus operator can be used only with integer operands. The expression x % y yields the remainder after x is divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2. In later chapters, we discuss many interesting applications of the modulus operator, such as determining whether one number is a multiple of another (a special case of this is determining whether a number is odd or even).

Arithmetic Expressions in Straight-Line Form Arithmetic expressions in C++ must be entered into the computer in straight-line form. Thus, expressions such as “a divided by b” must be written as a / b, so that all constants, variables and operators appear in a straight line. The algebraic notation a -b

is generally not acceptable to compilers, although some special-purpose software packages do support more natural notation for complex mathematical expressions.

Parentheses for Grouping Subexpressions Parentheses are used in C++ expressions in the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c we write a * ( b + c ). Rules of Operator Precedence C++ applies the operators in arithmetic expressions in a precise order determined by these rules of operator precedence, which are generally the same as those in algebra:

2.6 Arithmetic

49

1. Operators in expressions contained within pairs of parentheses are evaluated first. Parentheses are said to be at the “highest level of precedence.” In cases of nested, or embedded, parentheses, such as ( a * ( b + c ) )

the operators in the innermost pair of parentheses are applied first. 2. Multiplication, division and modulus operations are applied next. If an expression contains several multiplication, division and modulus operations, operators are applied from left to right. Multiplication, division and modulus are said to be on the same level of precedence. 3. Addition and subtraction operations are applied last. If an expression contains several addition and subtraction operations, operators are applied from left to right. Addition and subtraction also have the same level of precedence. The set of rules of operator precedence defines the order in which C++ applies operators. When we say that certain operators are applied from left to right, we are referring to the associativity of the operators. For example, the addition operators (+) in the expression a + b + c

associate from left to right, so a + b is calculated first, then c is added to that sum to determine the whole expression’s value. We’ll see that some operators associate from right to left. Figure 2.10 summarizes these rules of operator precedence. We expand this table as we introduce additional C++ operators. A complete precedence chart is included in Appendix A. Operator(s)

Operation(s)

Order of evaluation (precedence)

( )

Parentheses

*, /, %

Multiplication, Division, Modulus Addition Subtraction

Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. [Caution: If you have an expression such as (a + b) * (c - d) in which two sets of parentheses are not nested, but appear “on the same level,” the C++ Standard does not specify the order in which these parenthesized subexpressions will be evaluated.] Evaluated second. If there are several, they’re evaluated left to right.

+ -

Evaluated last. If there are several, they’re evaluated left to right.

Fig. 2.10 | Precedence of arithmetic operators. Sample Algebraic and C++ Expressions Now consider several expressions in light of the rules of operator precedence. Each example lists an algebraic expression and its C++ equivalent. The following is an example of an arithmetic mean (average) of five terms: Algebra: C++:

a+b+c+d+e m = ------------------------------------5

m = ( a + b + c + d + e ) / 5;

50

Chapter 2

Introduction to C++ Programming

The parentheses are required because division has higher precedence than addition. The entire quantity ( a + b + c + d + e ) is to be divided by 5. If the parentheses are erroneously omitted, we obtain a + b + c + d + e / 5, which evaluates incorrectly as e a + b + c + d + --5

The following is an example of the equation of a straight line: y = mx + b

Algebra: C++:

y = m * x + b;

No parentheses are required. The multiplication is applied first because multiplication has a higher precedence than addition. The following example contains modulus (%), multiplication, division, addition, subtraction and assignment operations: Algebra:

z = pr%q + w/x – y

C++:

z

=

p

6

*

r

1

%

q

2

+

w

4

/ 3

x

- y; 5

The circled numbers under the statement indicate the order in which C++ applies the operators. The multiplication, modulus and division are evaluated first in left-to-right order (i.e., they associate from left to right) because they have higher precedence than addition and subtraction. The addition and subtraction are applied next. These are also applied left to right. The assignment operator is applied last because its precedence is lower than that of any of the arithmetic operators.

Evaluation of a Second-Degree Polynomial To develop a better understanding of the rules of operator precedence, consider the evaluation of a second-degree polynomial y = ax 2 + bx + c: y

= 6

a

* 1

x

* 2

x

+ 4

b

* 3

x

+ c; 5

The circled numbers under the statement indicate the order in which C++ applies the operators. There is no arithmetic operator for exponentiation in C++, so we’ve represented x 2 as x * x. We’ll soon discuss the standard library function pow (“power”) that performs exponentiation. Because of some subtle issues related to the data types required by pow, we defer a detailed explanation of pow until Chapter 6. Suppose variables a, b, c and x in the preceding second-degree polynomial are initialized as follows: a = 2, b = 3, c = 7 and x = 5. Figure 2.11 illustrates the order in which the operators are applied and the final value of the expression. As in algebra, it’s acceptable to place unnecessary parentheses in an expression to make the expression clearer. These are called redundant parentheses. For example, the preceding assignment statement could be parenthesized as follows: y = ( a * x * x ) + ( b * x ) + c;

2.7 Decision Making: Equality and Relational Operators

Step 1.

y = 2 * 5 * 5 + 3 * 5 + 7;

51

(Leftmost multiplication)

2 * 5 is 10

Step 2.

y = 10 * 5 + 3 * 5 + 7;

(Leftmost multiplication)

10 * 5 is 50 Step 3.

y = 50 + 3 * 5 + 7;

(Multiplication before addition)

3 * 5 is 15 Step 4.

y = 50 + 15 + 7;

(Leftmost addition)

50 + 15 is 65 Step 5.

y = 65 + 7;

(Last addition)

65 + 7 is 72 Step 6.

y = 72

(Last operation—place 72 in y)

Fig. 2.11 | Order in which a second-degree polynomial is evaluated.

2.7 Decision Making: Equality and Relational Operators We now introduce a simple version of C++’s if statement that allows a program to take alternative action based on whether a condition is true or false. If the condition is true, the statement in the body of the if statement is executed. If the condition is false, the body statement is not executed. We’ll see an example shortly. Conditions in if statements can be formed by using the equality operators and relational operators summarized in Fig. 2.12. The relational operators all have the same level of precedence and associate left to right. The equality operators both have the same level of precedence, which is lower than that of the relational operators, and associate left to right.

Common Programming Error 2.3

Reversing the order of the pair of symbols in the operators !=, >= and <= (by writing them as =!, => and =<, respectively) is normally a syntax error. In some cases, writing != as =! will not be a syntax error, but almost certainly will be a logic error that has an effect at execution time. You’ll understand why when you learn about logical operators in Chapter 5. A fatal logic error causes a program to fail and terminate prematurely. A nonfatal logic error allows a program to continue executing, but usually produces incorrect results.

Common Programming Error 2.4

Confusing the equality operator == with the assignment operator = results in logic errors. Read the equality operator should be read “is equal to” or “double equals,” and the assignment operator should be read “gets” or “gets the value of” or “is assigned the value of.” As we discuss in Section 5.9, confusing these operators may not necessarily cause an easy-torecognize syntax error, but may cause extremely subtle logic errors.

52

Chapter 2

Introduction to C++ Programming

Standard algebraic equality or relational operator

C++ equality or relational operator

Sample C++ condition

Meaning of C++ condition

>

x > y

x is greater than y

<

x < y

x is less than y

>=

x >= y

x is greater than or equal to y

<=

x <= y

x is less than or equal to y

==

x == y

x is equal to y

!=

x != y

x is not equal to y

Relational operators

> < ≥ ≤ Equality operators = ≠

Fig. 2.12 | Equality and relational operators. Using the if Statement The following example (Fig. 2.13) uses six if statements to compare two numbers input by the user. If the condition in any of these if statements is satisfied, the output statement associated with that if statement is executed. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

// Fig. 2.13: fig02_13.cpp // Comparing integers using if statements, relational operators // and equality operators. #include // allows program to perform input and output using std::cout; // program uses cout using std::cin; // program uses cin using std::endl; // program uses endl // function main begins program execution int main() { int number1; // first integer to compare int number2; // second integer to compare cout << "Enter two integers to compare: "; // prompt user for data cin >> number1 >> number2; // read two integers from user if ( number1 == number2 ) cout << number1 << " == " << number2 << endl; if ( number1 != number2 ) cout << number1 << " != " << number2 << endl; if ( number1 < number2 ) cout << number1 << " < " << number2 << endl;

Fig. 2.13 | Comparing integers using if statements, relational operators and equality operators. (Part 1 of 2.)

2.7 Decision Making: Equality and Relational Operators 27 28 29 30 31 32 33 34 35 36

53

if ( number1 > number2 ) cout << number1 << " > " << number2 << endl; if ( number1 <= number2 ) cout << number1 << " <= " << number2 << endl; if ( number1 >= number2 ) cout << number1 << " >= " << number2 << endl; } // end function main

Enter two integers to compare: 3 7 3 != 7 3 < 7 3 <= 7 Enter two integers to compare: 22 12 22 != 12 22 > 12 22 >= 12 Enter two integers to compare: 7 7 7 == 7 7 <= 7 7 >= 7

Fig. 2.13 | Comparing integers using if statements, relational operators and equality operators. (Part 2 of 2.)

Directives Lines 6–8 using

using std::cout; // program uses cout using std::cin; // program uses cin using std::endl; // program uses endl

are using directives that eliminate the need to repeat the std:: prefix as we did in earlier programs. We can now write cout instead of std::cout, cin instead of std::cin and endl instead of std::endl, respectively, in the remainder of the program. In place of lines 6–8, many programmers prefer to use the directive using namespace std;

which enables a program to use all the names in any standard C++ header (such as ) that a program might include. From this point forward in the book, we’ll use the preceding directive in our programs.

Variable Declarations and Reading the Inputs from the User Lines 13–14 int number1; // first integer to compare int number2; // second integer to compare

declare the variables used in the program.

54

Chapter 2

Introduction to C++ Programming

The program uses cascaded stream extraction operations (line 17) to input two integers. Remember that we’re allowed to write cin (instead of std::cin) because of line 7. First a value is read into variable number1, then a value is read into variable number2.

Comparing Numbers The if statement in lines 19–20 if ( number1 == number2 ) cout << number1 << " == " << number2 << endl;

compares the values of variables number1 and number2 to test for equality. If the values are equal, the statement in line 20 displays a line of text indicating that the numbers are equal. If the conditions are true in one or more of the if statements starting in lines 22, 25, 28, 31 and 34, the corresponding body statement displays an appropriate line of text. Each if statement in Fig. 2.13 has a single statement in its body and each body statement is indented. In Chapter 4 we show how to specify if statements with multiple-statement bodies (by enclosing the body statements in a pair of braces, { }, creating what’s called a compound statement or a block).

Good Programming Practice 2.10

Indent the statement(s) in the body of an if statement to enhance readability.

Common Programming Error 2.5

Placing a semicolon immediately after the right parenthesis after the condition in an if statement is often a logic error (although not a syntax error). The semicolon causes the body of the if statement to be empty, so the if statement performs no action, regardless of whether or not its condition is true. Worse yet, the original body statement of the if statement now becomes a statement in sequence with the if statement and always executes, often causing the program to produce incorrect results.

White Space Note the use of white space in Fig. 2.13. Recall that white-space characters, such as tabs, newlines and spaces, are normally ignored by the compiler. So, statements may be split over several lines and may be spaced according to your preferences. It’s a syntax error to split identifiers, strings (such as "hello") and constants (such as the number 1000) over several lines.

Good Programming Practice 2.11

A lengthy statement may be spread over several lines. If a single statement must be split across lines, choose meaningful breaking points, such as after a comma in a comma-separated list, or after an operator in a lengthy expression. If a statement is split across two or more lines, indent all subsequent lines and left-align the group of indented lines.

Operator Precedence Figure 2.14 shows the precedence and associativity of the operators introduced in this chapter. The operators are shown top to bottom in decreasing order of precedence. All these operators, with the exception of the assignment operator =, associate from left to right. Addition is left-associative, so an expression like x + y + z is evaluated as if it had been written (x + y) + z. The assignment operator = associates from right to left, so an ex-

2.8 Wrap-Up

55

pression such as x = y = 0 is evaluated as if it had been written x = (y = 0), which, as we’ll soon see, first assigns 0 to y, then assigns the result of that assignment—0—to x. Operators () *

/

+

-

<<

>>

<

<=

==

!=

%

>

=

>=

Associativity

Type

[See caution in Fig. 2.10] left to right left to right left to right left to right left to right right to left

grouping parentheses multiplicative additive stream insertion/extraction relational equality assignment

Fig. 2.14 | Precedence and associativity of the operators discussed so far.

Good Programming Practice 2.12

Refer to the operator precedence and associativity chart (Appendix A) when writing expressions containing many operators. Confirm that the operators in the expression are performed in the order you expect. If you’re uncertain about the order of evaluation in a complex expression, break the expression into smaller statements or use parentheses to force the order of evaluation, exactly as you’d do in an algebraic expression. Be sure to observe that some operators such as assignment (=) associate right to left rather than left to right.

2.8 Wrap-Up You learned many important basic features of C++ in this chapter, including displaying data on the screen, inputting data from the keyboard and declaring variables of fundamental types. In particular, you learned to use the output stream object cout and the input stream object cin to build simple interactive programs. We explained how variables are stored in and retrieved from memory. You also learned how to use arithmetic operators to perform calculations. We discussed the order in which C++ applies operators (i.e., the rules of operator precedence), as well as the associativity of the operators. You also learned how C++’s if statement allows a program to make decisions. Finally, we introduced the equality and relational operators, which you use to form conditions in if statements. The non-object-oriented applications presented here introduced you to basic programming concepts. As you’ll see in Chapter 3, C++ applications typically contain just a few lines of code in function main—these statements normally create the objects that perform the work of the application, then the objects “take over from there.” In Chapter 3, you’ll learn how to implement your own classes and use objects of those classes in applications.

Summary Section 2.2 First Program in C++: Printing a Line of Text

• Single-line comments (p. 39) begin with //. You insert comments to document your programs and improve their readability.

56

Chapter 2

Introduction to C++ Programming

• Comments do not cause the computer to perform any action (p. 40) when the program is run— they’re ignored by the compiler and do not cause any machine-language object code to be generated. • A preprocessor directive (p. 39) begins with # and is a message to the C++ preprocessor. Preprocessor directives are processed before the program is compiled and don’t end with a semicolon. • The line #include (p. 39) tells the C++ preprocessor to include the contents of the input/output stream header, which contains information necessary to compile programs that use std::cin (p. 43) and std::cout (p. 40) and the stream insertion (<<, p. 40) and stream extraction (>>, p. 43) operators. • White space (i.e., blank lines, space characters and tab characters, p. 39) makes programs easier to read. White-space characters outside of literals are ignored by the compiler. • C++ programs begin executing at main (p. 39), even if main does not appear first in the program. • The keyword int to the left of main indicates that main “returns” an integer value. • The body (p. 40) of every function must be contained in braces ({ and }). • A string (p. 40) in double quotes is sometimes referred to as a character string, message or string literal. White-space characters in strings are not ignored by the compiler. • Every statement (p. 40) must end with a semicolon (also known as the statement terminator). • Output and input in C++ are accomplished with streams (p. 40) of characters. • The output stream object std::cout—normally connected to the screen—is used to output data. Multiple data items can be output by concatenating stream insertion (<<) operators. • The input stream object std::cin—normally connected to the keyboard—is used to input data. Multiple data items can be input by concatenating stream extraction (>>) operators. • The notation std::cout specifies that we are using cout from “namespace” std. • When a backslash (i.e., an escape character) is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence (p. 41). • The newline escape sequence \n (p. 41) moves the cursor to the beginning of the next line on the screen. • A message that directs the user to take a specific action is known as a prompt (p. 45). • C++ keyword return (p. 41) is one of several means to exit a function.

Section 2.4 Another C++ Program: Adding Integers

• All variables (p. 43) in a C++ program must be declared before they can be used. • A variable name is any valid identifier (p. 44) that is not a keyword. An identifier is a series of characters consisting of letters, digits and underscores ( _ ). Identifiers cannot start with a digit. Identifiers can be any length, but some systems or C++ implementations may impose length restrictions. • C++ is case sensitive (p. 44). • Most calculations are performed in assignment statements (p. 46). • A variable is a location in memory (p. 47) where a value can be stored for use by a program. • Variables of type int (p. 44) hold integer values, i.e., whole numbers such as 7, –11, 0, 31914.

Section 2.5 Memory Concepts

• Every variable stored in the computer’s memory has a name, a value, a type and a size. • Whenever a new value is placed in a memory location, the process is destructive (p. 47); i.e., the new value replaces the previous value in that location. The previous value is lost.

Self-Review Exercises

57

• When a value is read from memory, the process is nondestructive (p. 47); i.e., a copy of the value is read, leaving the original value undisturbed in the memory location. • The std::endl stream manipulator (p. 46) outputs a newline, then “flushes the output buffer.”

Section 2.6 Arithmetic

• C++ evaluates arithmetic expressions (p. 48) in a precise sequence determined by the rules of operator precedence (p. 48) and associativity (p. 49). • Parentheses may be used to group expressions. • Integer division (p. 48) yields an integer quotient. Any fractional part in integer division is truncated. • The modulus operator, % (p. 48), yields the remainder after integer division.

Section 2.7 Decision Making: Equality and Relational Operators

• The if statement (p. 51) allows a program to take alternative action based on whether a condition is met. The format for an if statement is if ( condition ) statement;

If the condition is true, the statement in the body of the if is executed. If the condition is not met, i.e., the condition is false, the body statement is skipped. • Conditions in if statements are commonly formed by using equality and relational operators (p. 51). The result of using these operators is always the value true or false. • The using directive (p. 53) using std::cout;

informs the compiler where to find cout (namespace std) and eliminates the need to repeat the std:: prefix. The directive using namespace std;

enables the program to use all the names in any included C++ standard library header.

Self-Review Exercises 2.1

Fill in the blanks in each of the following. a) Every C++ program begins execution at the function . b) A(n) begins the body of every function and a(n) ends the body. c) Every C++ statement ends with a(n) . d) The escape sequence \n represents the character, which causes the cursor to position to the beginning of the next line on the screen. e) The statement is used to make decisions.

2.2 State whether each of the following is true or false. If false, explain why. Assume the statement using std::cout; is used. a) Comments cause the computer to print the text after the // on the screen when the program is executed. b) The escape sequence \n, when output with cout and the stream insertion operator, causes the cursor to position to the beginning of the next line on the screen. c) All variables must be declared before they’re used. d) All variables must be given a type when they’re declared. e) C++ considers the variables number and NuMbEr to be identical. f) Declarations can appear almost anywhere in the body of a C++ function. g) The modulus operator (%) can be used only with integer operands.

58

Chapter 2

Introduction to C++ Programming

h) The arithmetic operators *, /, %, + and – all have the same level of precedence. i) A C++ program that prints three lines of output must contain three statements using cout and the stream insertion operator. 2.3 Write a single C++ statement to accomplish each of the following (assume that using directives have not been used): a) Declare the variables c, thisIsAVariable, q76354 and number to be of type int. b) Prompt the user to enter an integer. End your prompting message with a colon (:) followed by a space and leave the cursor positioned after the space. c) Read an integer from the user at the keyboard and store it in integer variable age. d) If the variable number is not equal to 7, print "The variable number is not equal to 7". e) Print the message "This is a C++ program" on one line. f) Print the message "This is a C++ program" on two lines. End the first line with C++. g) Print the message "This is a C++ program" with each word on a separate line. h) Print the message "This is a C++ program". Separate each word from the next by a tab. 2.4 Write a statement (or comment) to accomplish each of the following (assume that using directives have been used for cin, cout and endl): a) State that a program calculates the product of three integers. b) Declare the variables x, y, z and result to be of type int (in separate statements). c) Prompt the user to enter three integers. d) Read three integers from the keyboard and store them in the variables x, y and z. e) Compute the product of the three integers contained in variables x, y and z, and assign the result to the variable result. f) Print "The product is " followed by the value of the variable result. g) Return a value from main indicating that the program terminated successfully. 2.5 Using the statements you wrote in Exercise 2.4, write a complete program that calculates and displays the product of three integers. Add comments to the code where appropriate. [Note: You’ll need to write the necessary using directives.] 2.6 Identify and correct the errors in each of the following statements (assume that the statement using std::cout; is used): a) if ( c < 7 ); b)

cout << "c is less than 7\n"; if ( c => 7 ) cout << "c is equal to or greater than 7\n";

Answers to Self-Review Exercises 2.1

a)

main.

b) left brace ({), right brace (}).

c) semicolon. d) newline. e) if.

2.2

a) False. Comments do not cause any action to be performed when the program is executed. They’re used to document programs and improve their readability. b) True. c) True. d) True. e) False. C++ is case sensitive, so these variables are unique. f) True. g) True. h) False. The operators *, / and % have the same precedence, and the operators + and - have a lower precedence. i) False. One statement with cout and multiple \n escape sequences can print several lines.

2.3

a)

int c, thisIsAVariable, q76354, number;

Exercises b) c) d) e) f) g) h) 2.4

a) b)

59

std::cout << "Enter an integer: "; std::cin >> age; if ( number != 7 ) std::cout << "The variable number is not equal to 7\n"; std::cout << "This is a C++ program\n"; std::cout << "This is a C++\nprogram\n"; std::cout << "This\nis\na\nC++\nprogram\n"; std::cout << "This\tis\ta\tC++\tprogram\n"; // Calculate the product of three integers int x; int y; int z;

c) d) e) f) g) 2.5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

2.6

int result; cout << "Enter three integers: "; cin >> x >> y >> z; result = x * y * z; cout << "The product is " << result << endl; return 0;

(See program below.) // Calculate the product of three integers #include // allows program to perform input and output using namespace std; // program uses names from the std namespace // function main begins program execution int main() { int x; // first integer to multiply int y; // second integer to multiply int z; // third integer to multiply int result; // the product of the three integers cout << "Enter three integers: "; // prompt user for data cin >> x >> y >> z; // read three integers from user result = x * y * z; // multiply the three integers; store result cout << "The product is " << result << endl; // print result; end line } // end function main

a) Error: Semicolon after the right parenthesis of the condition in the if statement. Correction: Remove the semicolon after the right parenthesis. [Note: The result of this error is that the output statement executes whether or not the condition in the if statement is true.] The semicolon after the right parenthesis is a null (or empty) statement that does nothing. We’ll learn more about the null statement in Chapter 4. b) Error: The relational operator =>. Correction: Change => to >=, and you may want to change “equal to or greater than” to “greater than or equal to” as well.

Exercises 2.7

Discuss the meaning of each of the following objects: a) std::cin b) std::cout

60

Chapter 2

Introduction to C++ Programming

2.8

Fill in the blanks in each of the following: a) are used to document a program and improve its readability. . b) The object used to print information on the screen is c) A C++ statement that makes a decision is . d) Most calculations are normally performed by statements. object inputs values from the keyboard. e) The

2.9

Write a single C++ statement or line that accomplishes each of the following: a) Print the message "Enter two numbers". b) Assign the product of variables b and c to variable a. c) State that a program performs a payroll calculation (i.e., use text that helps to document a program). d) Input three integer values from the keyboard into integer variables a, b and c.

2.10

State which of the following are true and which are false. If false, explain your answers. a) C++ operators are evaluated from left to right. b) The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales, his_account_total, a, b, c, z, z2. c) The statement cout << "a = 5;"; is a typical example of an assignment statement. d) A valid C++ arithmetic expression with no parentheses is evaluated from left to right. e) The following are all invalid variable names: 3g, 87, 67h2, h22, 2h.

2.11

Fill in the blanks in each of the following: a) What arithmetic operations are on the same level of precedence as multiplication? . b) When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression? . c) A location in the computer’s memory that may contain different values at various times . throughout the execution of a program is called a(n)

2.12 What, if anything, prints when each of the following C++ statements is performed? If nothing prints, then answer “nothing.” Assume x = 2 and y = 3. a) cout << x; b) cout << x + x; c) cout << "x="; d) cout << "x = " << x; e) cout << x + y << " = " << y + x; f) z = x + y; g) cin >> x >> y; h) // cout << "x + y = " << x + y; i) cout << "\n"; 2.13

Which of the following C++ statements contain variables whose values are replaced? a) cin >> b >> c >> d >> e >> f; b) p = i + j + k + 7; c) cout << "variables whose values are replaced"; d) cout << "a = 5";

2.14 Given the algebraic equation y = ax 3 + 7, which of the following, if any, are correct C++ statements for this equation? a) y = a * x * x * x + 7; b) y = a * x * x * ( x + 7 ); c) y = ( a * x ) * x * ( x + 7 ); d) y = (a * x) * x * x + 7;

Exercises e) f)

61

y = a * ( x * x * x ) + 7; y = a * x * ( x * x + 7 );

2.15 (Order of Evalution) State the order of evaluation of the operators in each of the following C++ statements and show the value of x after each statement is performed. a) x = 7 + 3 * 6 / 2 - 1; b) x = 2 % 2 + 2 * 2 - 2 / 2; c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) ); 2.16 (Arithmetic) Write a program that asks the user to enter two numbers, obtains the two numbers from the user and prints the sum, product, difference, and quotient of the two numbers. 2.17 (Printing) Write a program that prints the numbers 1 to 4 on the same line with each pair of adjacent numbers separated by one space. Do this several ways: a) Using one statement with one stream insertion operator. b) Using one statement with four stream insertion operators. c) Using four statements. 2.18 (Comparing Integers) Write a program that asks the user to enter two integers, obtains the numbers from the user, then prints the larger number followed by the words "is larger." If the numbers are equal, print the message "These numbers are equal." 2.19 (Arithmetic, Smallest and Largest) Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers. The screen dialog should appear as follows: Input three different integers: 13 27 14 Sum is 54 Average is 18 Product is 4914 Smallest is 13 Largest is 27

2.20 (Diameter, Circumference and Area of a Circle) Write a program that reads in the radius of a circle as an integer and prints the circle’s diameter, circumference and area. Use the constant value 3.14159 for π. Do all calculations in output statements. [Note: In this chapter, we’ve discussed only integer constants and variables. In Chapter 4 we discuss floating-point numbers, i.e., values that can have decimal points.] 2.21 (Displaying Shapes with Asterisks) Write a program that prints a box, an oval, an arrow and a diamond as follows: ********* * * * * * * * * * * * * * * *********

2.22

* * * * *

*

*

***

***

*

*

* * * * *

* *** ***** * * * * * *

*

* *

*

*

* * *

* * *

*

*

* *

*

What does the following code print? cout << "*\n**\n***\n****\n*****" << endl;

2.23 (Largest and Smallest Integers) Write a program that reads in five integers and determines and prints the largest and the smallest integers in the group. Use only the programming techniques you learned in this chapter.

62

Chapter 2

Introduction to C++ Programming

2.24 (Odd or Even) Write a program that reads an integer and determines and prints whether it’s odd or even. [Hint: Use the modulus operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided by 2.] 2.25 (Multiples) Write a program that reads in two integers and determines and prints if the first is a multiple of the second. [Hint: Use the modulus operator.] 2.26 (Checkerboard Pattern) Display the following checkerboard pattern with eight output statements, then display the same pattern using as few statements as possible. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

2.27 (Integer Equivalent of a Character) Here is a peek ahead. In this chapter you learned about integers and the type int. C++ can also represent uppercase letters, lowercase letters and a considerable variety of special symbols. C++ uses small integers internally to represent each different character. The set of characters a computer uses and the corresponding integer representations for those characters are called that computer’s character set. You can print a character by enclosing that character in single quotes, as with cout << 'A'; // print an uppercase A

You can print the integer equivalent of a character using static_cast as follows: cout << static_cast< int >( 'A' ); // print 'A' as an integer

This is called a cast operation (we formally introduce casts in Chapter 4). When the preceding statement executes, it prints the value 65 (on systems that use the ASCII character set). Write a program that prints the integer equivalent of a character typed at the keyboard. Store the input in a variable of type char. Test your program several times using uppercase letters, lowercase letters, digits and special characters (like $). 2.28 (Digits of an Integer) Write a program that inputs a five-digit integer, separates the integer into its digits and prints them separated by three spaces each. [Hint: Use the integer division and modulus operators.] For example, if the user types in 42339, the program should print: 4

2

3

3

9

2.29 (Table) Using the techniques of this chapter, write a program that calculates the squares and cubes of the integers from 0 to 10. Use tabs to print the following neatly formatted table of values: integer 0 1 2 3 4 5 6 7 8 9 10

square 0 1 4 9 16 25 36 49 64 81 100

cube 0 1 8 27 64 125 216 343 512 729 1000

Making a Difference

63

Making a Difference 2.30 (Body Mass Index Calculator) We introduced the body mass index (BMI) calculator in Exercise 1.9. The formulas for calculating BMI are weightInPounds × 703 BMI = -----------------------------------------------------------------------------------heightInInches × heightInInches or weightInKi log rams BMI = --------------------------------------------------------------------------------------heightInMeters × heightInMeters Create a BMI calculator application that reads the user’s weight in pounds and height in inches (or, if you prefer, the user’s weight in kilograms and height in meters), then calculates and displays the user’s body mass index. Also, the application should display the following information from the Department of Health and Human Services/National Institutes of Health so the user can evaluate his/her BMI: BMI VALUES Underweight: Normal: Overweight: Obese:

less than 18.5 between 18.5 and 24.9 between 25 and 29.9 30 or greater

[Note: In this chapter, you learned to use the int type to represent whole numbers. The BMI calculations when done with int values will both produce whole-number results. In Chapter 4 you’ll learn to use the double type to represent numbers with decimal points. When the BMI calculations are performed with doubles, they’ll both produce numbers with decimal points—these are called “floating-point” numbers.] 2.31 (Car-Pool Savings Calculator) Research several car-pooling websites. Create an application that calculates your daily driving cost, so that you can estimate how much money could be saved by car pooling, which also has other advantages such as reducing carbon emissions and reducing traffic congestion. The application should input the following information and display the user’s cost per day of driving to work: a) Total miles driven per day. b) Cost per gallon of gasoline. c) Average miles per gallon. d) Parking fees per day. e) Tolls per day.

3 Nothing can have value without being an object of utility. —Karl Marx

Your public servants serve you right. —Adlai E. Stevenson

Knowing how to answer one who speaks, To reply to one who sends a message. —Amenemopel

Objectives

In this chapter you’ll learn: ■















How to define a class and use it to create an object. How to implement a class’s behaviors as member functions. How to implement a class’s attributes as data members. How to call a member function of an object to perform a task. The differences between data members of a class and local variables of a function. How to use a constructor to initialize an object’s data when the object is created. How to engineer a class to separate its interface from its implementation and encourage reuse. How to use objects of class string.

Introduction to Classes, Objects and Strings

3.1 Introduction 3.1 Introduction 3.2 Defining a Class with a Member Function 3.3 Defining a Member Function with a Parameter 3.4 Data Members, set Functions and get Functions 3.5 Initializing Objects with Constructors

65

3.6 Placing a Class in a Separate File for Reusability 3.7 Separating Interface from Implementation 3.8 Validating Data with set Functions 3.9 Wrap-Up

Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference

3.1 Introduction In Chapter 2, you created simple programs that displayed messages to the user, obtained information from the user, performed calculations and made decisions. In this chapter, you’ll begin writing programs that employ the basic concepts of object-oriented programming that we introduced in Section 1.6. One common feature of every program in Chapter 2 was that all the statements that performed tasks were located in function main. Typically, the programs you develop in this book will consist of function main and one or more classes, each containing data members and member functions. If you become part of a development team in industry, you might work on software systems that contain hundreds, or even thousands, of classes. In this chapter, we develop a simple, well-engineered framework for organizing object-oriented programs in C++. We present a carefully paced sequence of complete working programs to demonstrate creating and using your own classes. These examples begin our integrated case study on developing a grade-book class that instructors can use to maintain student test scores. We also introduce the C++ standard library class string.

3.2 Defining a Class with a Member Function We begin with an example (Fig. 3.1) that consists of class GradeBook (lines 8–16)—which, when it’s fully developed in Chapter 7, will represent a grade book that an instructor can use to maintain student test scores—and a main function (lines 19–23) that creates a GradeBook object. Function main uses this object and its member function to display a message on the screen welcoming the instructor to the grade-book program. 1 2 3 4 5

// Fig. 3.1: fig03_01.cpp // Define class GradeBook with a member function displayMessage, // create a GradeBook object, and call its displayMessage function. #include using namespace std;

Fig. 3.1 | Define class GradeBook with a member function displayMessage, create a GradeBook

object and call its displayMessage function. (Part 1 of 2.)

66 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Chapter 3

Introduction to Classes, Objects and Strings

// GradeBook class definition class GradeBook { public: // function that displays a welcome message to the GradeBook user void displayMessage() { cout << "Welcome to the Grade Book!" << endl; } // end function displayMessage }; // end class GradeBook // function main begins program execution int main() { GradeBook myGradeBook; // create a GradeBook object named myGradeBook myGradeBook.displayMessage(); // call object's displayMessage function } // end main

Welcome to the Grade Book!

Fig. 3.1 | Define class GradeBook with a member function displayMessage, create a GradeBook

object and call its displayMessage function. (Part 2 of 2.)

Class GradeBook Before function main (lines 19–23) can create a GradeBook object, we must tell the compiler what member functions and data members belong to the class. The GradeBook class definition (lines 8–16) contains a member function called displayMessage (lines 12–15) that displays a message on the screen (line 14). We need to make an object of class GradeBook (line 21) and call its displayMessage member function (line 22) to get line 14 to execute and display the welcome message. We’ll soon explain lines 21–22 in detail. The class definition begins in line 8 with the keyword class followed by the class name GradeBook. By convention, the name of a user-defined class begins with a capital letter, and for readability, each subsequent word in the class name begins with a capital letter. This capitalization style is often referred to as Pascal case, because the pattern of uppercase and lowercase letters resembles the silhouette of a camel. Every class’s body is enclosed in a pair of left and right braces ({ and }), as in lines 9 and 16. The class definition terminates with a semicolon (line 16).

Common Programming Error 3.1

Forgetting the semicolon at the end of a class definition is a syntax error.

Recall that the function main is always called automatically when you execute a program. Most functions do not get called automatically. As you’ll soon see, you must call member function displayMessage explicitly to tell it to perform its task. Line 10 contains the keyword public, which is an access specifier. Lines 12–15 define member function displayMessage. This member function appears after access specifier public: to indicate that the function is “available to the public”—that is, it can be called by other functions in the program (such as main), and by member functions of other

3.2 Defining a Class with a Member Function

67

classes (if there are any). Access specifiers are always followed by a colon (:). For the remainder of the text, when we refer to the access specifier public, we’ll omit the colon as we did in this sentence. Section 3.4 introduces the access specifier, private. Later in the book we’ll study the access specifier protected. Each function in a program performs a task and may return a value when it completes its task—for example, a function might perform a calculation, then return the result of that calculation. When you define a function, you must specify a return type to indicate the type of the value returned by the function when it completes its task. In line 12, keyword void to the left of the function name displayMessage is the function’s return type. Return type void indicates that displayMessage will not return any data to its calling function (in this example, line 22 of main, as we’ll see in a moment) when it completes its task. In Fig. 3.5, you’ll see an example of a function that does return a value. The name of the member function, displayMessage, follows the return type (line 12). By convention, function names begin with a lowercase first letter and all subsequent words in the name begin with a capital letter. This capitalization style is often refered to as camel case and is also used for variable names. The parentheses after the member function name indicate that this is a function. An empty set of parentheses, as shown in line 12, indicates that this member function does not require additional data to perform its task. You’ll see an example of a member function that does require additional data in Section 3.3. Line 12 is commonly referred to as a function header. Every function’s body is delimited by left and right braces ({ and }), as in lines 13 and 15. The body of a function contains statements that perform the function’s task. In this case, member function displayMessage contains one statement (line 14) that displays the message "Welcome to the Grade Book!". After this statement executes, the function has completed its task.

Testing Class GradeBook Next, we’d like to use class GradeBook in a program. As you saw in Chapter 2, the function main (lines 19–23) begins the execution of every program. In this program, we’d like to call class GradeBook’s displayMessage member function to display the welcome message. Typically, you cannot call a member function of a class until you create an object of that class. (As you’ll learn in Section 10.6, static member functions are an exception.) Line 21 creates an object of class GradeBook called myGradeBook. The variable’s type is GradeBook—the class we defined in lines 8–16. When we declare variables of type int, as we did in Chapter 2, the compiler knows what int is—it’s a fundamental type that’s “built into” C++. In line 21, however, the compiler does not automatically know what type GradeBook is—it’s a user-defined type. We tell the compiler what GradeBook is by including the class definition (lines 8–16). If we omitted these lines, the compiler would issue an error message. Each class you create becomes a new type that can be used to create objects. You can define new class types as needed; this is one reason why C++ is known as an extensible language. Line 22 calls the member function displayMessage using variable myGradeBook followed by the dot operator (.), the function name displayMessage and an empty set of parentheses. This call causes the displayMessage function to perform its task. At the beginning of line 22, “myGradeBook.” indicates that main should use the GradeBook object that was created in line 21. The empty parentheses in line 12 indicate that member func-

68

Chapter 3

Introduction to Classes, Objects and Strings

tion displayMessage does not require additional data to perform its task, which is why we called this function with empty parentheses in line 22. (In Section 3.3, you’ll see how to pass data to a function.) When displayMessage completes its task, the program reaches the end of main (line 23) and terminates.

UML Class Diagram for Class GradeBook Recall from Section 1.6 that the UML is a standardized graphical language used by software developers to represent their object-oriented systems. In the UML, each class is modeled in a UML class diagram as a rectangle with three compartments. Figure 3.2 presents a class diagram for class GradeBook (Fig. 3.1). The top compartment contains the class’s name centered horizontally and in boldface type. The middle compartment contains the class’s attributes, which correspond to data members in C++. This compartment is currently empty, because class GradeBook does not have any attributes. (Section 3.4 presents a version of class GradeBook with an attribute.) The bottom compartment contains the class’s operations, which correspond to member functions in C++. The UML models operations by listing the operation name followed by a set of parentheses. Class GradeBook has only one member function, displayMessage, so the bottom compartment of Fig. 3.2 lists one operation with this name. Member function displayMessage does not require additional information to perform its tasks, so the parentheses following displayMessage in the class diagram are empty, just as they are in the member function’s header in line 12 of Fig. 3.1. The plus sign (+) in front of the operation name indicates that displayMessage is a public operation in the UML (i.e., a public member function in C++). GradeBook + displayMessage( )

Fig. 3.2 | UML class diagram indicating that class GradeBook has a public displayMessage operation.

3.3 Defining a Member Function with a Parameter In our car analogy from Section 1.6, we mentioned that pressing a car’s gas pedal sends a message to the car to perform a task—make the car go faster. But how fast should the car accelerate? As you know, the farther down you press the pedal, the faster the car accelerates. So the message to the car includes both the task to perform and additional information that helps the car perform the task. This additional information is known as a parameter— the value of the parameter helps the car determine how fast to accelerate. Similarly, a member function can require one or more parameters that represent additional data it needs to perform its task. A function call supplies values—called arguments—for each of the function’s parameters. For example, to make a deposit into a bank account, suppose a deposit member function of an Account class specifies a parameter that represents the deposit amount. When the deposit member function is called, an argument value representing the deposit amount is copied to the member function’s parameter. The member function then adds that amount to the account balance.

3.3 Defining a Member Function with a Parameter

69

Defining and Testing Class GradeBook Our next example (Fig. 3.3) redefines class GradeBook (lines 9–18) with a displayMessage member function (lines 13–17) that displays the course name as part of the welcome message. The new version of displayMessage requires a parameter (courseName in line 13) that represents the course name to output. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

// Fig. 3.3: fig03_03.cpp // Define class GradeBook with a member function that takes a parameter, // create a GradeBook object and call its displayMessage function. #include #include // program uses C++ standard string class using namespace std; // GradeBook class definition class GradeBook { public: // function that displays a welcome message to the GradeBook user void displayMessage( string courseName ) { cout << "Welcome to the grade book for\n" << courseName << "!" << endl; } // end function displayMessage }; // end class GradeBook // function main begins program execution int main() { string nameOfCourse; // string of characters to store the course name GradeBook myGradeBook; // create a GradeBook object named myGradeBook // prompt for and input course name cout << "Please enter the course name:" << endl; getline( cin, nameOfCourse ); // read a course name with blanks cout << endl; // output a blank line // call myGradeBook's displayMessage function // and pass nameOfCourse as an argument myGradeBook.displayMessage( nameOfCourse ); } // end main

Please enter the course name: CS101 Introduction to C++ Programming Welcome to the grade book for CS101 Introduction to C++ Programming!

Fig. 3.3 | Define class GradeBook with a member function that takes a parameter, create a GradeBook

object and call its displayMessage function.

Before discussing the new features of class GradeBook, let’s see how the new class is used in main (lines 21–34). Line 23 creates a variable of type string called nameOfCourse

70

Chapter 3

Introduction to Classes, Objects and Strings

that will be used to store the course name entered by the user. A variable of type string represents a string of characters such as “CS101 Introduction to C++ Programming". A string is actually an object of the C++ Standard Library class string. This class is defined in header , and the name string, like cout, belongs to namespace std. To enable lines 13 and 23 to compile, line 5 includes the header. The using directive in line 6 allows us to simply write string in line 23 rather than std::string. For now, you can think of string variables like variables of other types such as int. You’ll learn additional string capabilities in Section 3.8 and in Chapter 18. Line 24 creates an object of class GradeBook named myGradeBook. Line 27 prompts the user to enter a course name. Line 28 reads the name from the user and assigns it to the nameOfCourse variable, using the library function getline to perform the input. Before we explain this line of code, let’s explain why we cannot simply write cin >> nameOfCourse;

to obtain the course name. In our sample program execution, we use the course name “CS101 Introduction to C++ Programming,” which contains multiple words separated by blanks. (Recall that we highlight user-supplied input in bold.) When cin is used with the stream extraction operator, it reads characters until the first white-space character is reached. Thus, only “CS101” would be read by the preceding statement. The rest of the course name would have to be read by subsequent input operations. In this example, we’d like the user to type the complete course name and press Enter to submit it to the program, and we’d like to store the entire course name in the string variable nameOfCourse. The function call getline( cin, nameOfCourse ) in line 28 reads characters (including the space characters that separate the words in the input) from the standard input stream object cin (i.e., the keyboard) until the newline character is encountered, places the characters in the string variable nameOfCourse and discards the newline character. When you press Enter while typing program input, a newline is inserted in the input stream. The header must be included in the program to use function getline, which belongs to namespace std. Line 33 calls myGradeBook’s displayMessage member function. The nameOfCourse variable in parentheses is the argument that’s passed to member function displayMessage so that it can perform its task. The value of variable nameOfCourse in main is copied to member function displayMessage’s parameter courseName in line 13. When you execute this program, member function displayMessage outputs as part of the welcome message the course name you type (in our sample execution, CS101 Introduction to C++ Programming).

More on Arguments and Parameters To specify in a function definition that the function requires data to perform its task, you place additional information in the function’s parameter list, which is located in the parentheses following the function name. The parameter list may contain any number of parameters, including none at all (represented by empty parentheses as in Fig. 3.1, line 12) to indicate that a function does not require any parameters. Member function displayMessage’s parameter list (Fig. 3.3, line 13) declares that the function requires one parameter. Each parameter specifies a type and an identifier. The type string and the identifier courseName indicate that member function displayMessage requires a string to perform its task. The member function body uses the parameter courseName to access the value that’s passed to the function in the function call (line 33 in main). Lines 15–16 display

3.4 Data Members, set Functions and get Functions

71

parameter courseName’s value as part of the welcome message. The parameter variable’s name (courseName in line 13) can be the same as or different from the argument variable’s name (nameOfCourse in line 33)—you’ll learn why in Chapter 6. A function can specify multiple parameters by separating each from the next with a comma. The number and order of arguments in a function call must match the number and order of parameters in the parameter list of the called member function’s header. Also, the argument types in the function call must be consistent with the types of the corresponding parameters in the function header. (As you’ll learn in subsequent chapters, an argument’s type and its corresponding parameter’s type need not always be identical, but they must be “consistent.”) In our example, the one string argument in the function call (i.e., nameOfCourse) exactly matches the one string parameter in the member-function definition (i.e., courseName).

Updated UML Class Diagram for Class GradeBook The UML class diagram of Fig. 3.4 models class GradeBook of Fig. 3.3. Like the class GradeBook defined in Fig. 3.1, this GradeBook class contains public member function displayMessage. However, this version of displayMessage has a parameter. The UML models a parameter by listing the parameter name, followed by a colon and the parameter type in the parentheses following the operation name. The UML has its own data types similar to those of C++. The UML is language independent—it’s used with many different programming languages—so its terminology does not exactly match that of C++. For example, the UML type String corresponds to the C++ type string. Member function displayMessage of class GradeBook (Fig. 3.3, lines 13–17) has a string parameter named courseName, so Fig. 3.4 lists courseName : String between the parentheses following the operation name displayMessage. This version of the GradeBook class still does not have any data members. GradeBook + displayMessage( courseName : String )

Fig. 3.4 | UML class diagram indicating that class GradeBook has a public displayMessage operation with a courseName parameter of UML type String.

3.4 Data Members, set Functions and get Functions In Chapter 2, we declared all of a program’s variables in its main function. Variables declared in a function definition’s body are known as local variables and can be used only from the line of their declaration in the function to closing right brace (}) of the block in which they’re declared. A local variable must be declared before it can be used in a function. A local variable cannot be accessed outside the function in which it’s declared. When a function terminates, the values of its local variables are lost. (You’ll see an exception to this in Chapter 6 when we discuss static local variables.) A class normally consists of one or more member functions that manipulate the attributes that belong to a particular object of the class. Attributes are represented as variables in a class definition. Such variables are called data members and are declared inside a class

72

Chapter 3

Introduction to Classes, Objects and Strings

definition but outside the bodies of the class’s member-function definitions. Each object of a class maintains its own copy of its attributes in memory. These attributes exist throughout the life of the object. The example in this section demonstrates a GradeBook class that contains a courseName data member to represent a particular GradeBook object’s course name.

Class with a Data Member, a set Function and a get Function In our next example, class GradeBook (Fig. 3.5) maintains the course name as a data member so that it can be used or modified at any time during a program’s execution. The class contains member functions setCourseName, getCourseName and displayMessage. Member function setCourseName stores a course name in a GradeBook data member. Member function getCourseName obtains the course name from that data member. Member function displayMessage—which now specifies no parameters—still displays a welcome message that includes the course name. However, as you’ll see, the function now obtains the course name by calling another function in the same class—getCourseName. GradeBook

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

// Fig. 3.5: fig03_05.cpp // Define class GradeBook that contains a courseName data member // and member functions to set and get its value; // Create and manipulate a GradeBook object with these functions. #include #include // program uses C++ standard string class using namespace std; // GradeBook class definition class GradeBook { public: // function that sets the course name void setCourseName( string name ) { courseName = name; // store the course name in the object } // end function setCourseName // function that gets the course name string getCourseName() { return courseName; // return the object's courseName } // end function getCourseName // function that displays a welcome message void displayMessage() { // this statement calls getCourseName to get the // name of the course this GradeBook represents cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } // end function displayMessage

Fig. 3.5 | Defining and testing class GradeBook with a data member and set and get functions. (Part 1 of 2.)

3.4 Data Members, set Functions and get Functions 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

73

private: string courseName; // course name for this GradeBook }; // end class GradeBook // function main begins program execution int main() { string nameOfCourse; // string of characters to store the course name GradeBook myGradeBook; // create a GradeBook object named myGradeBook // display initial value of courseName cout << "Initial course name is: " << myGradeBook.getCourseName() << endl; // prompt for, input and set course name cout << "\nPlease enter the course name:" << endl; getline( cin, nameOfCourse ); // read a course name with blanks myGradeBook.setCourseName( nameOfCourse ); // set the course name cout << endl; // outputs a blank line myGradeBook.displayMessage(); // display message with new course name } // end main

Initial course name is: Please enter the course name: CS101 Introduction to C++ Programming Welcome to the grade book for CS101 Introduction to C++ Programming!

Fig. 3.5 | Defining and testing class GradeBook with a data member and set and get functions. (Part 2 of 2.)

A typical instructor teaches multiple courses, each with its own course name. Line 34 declares that courseName is a variable of type string. Because the variable is declared in the class definition (lines 10–35) but outside the bodies of the class’s member-function definitions (lines 14–17, 20–23 and 26–32), the variable is a data member. Every instance (i.e., object) of class GradeBook contains one copy of each of the class’s data members—if there are two GradeBook objects, each has its own copy of courseName (one per object), as you’ll see in the example of Fig. 3.7. A benefit of making courseName a data member is that all the member functions of the class can manipulate any data members that appear in the class definition (in this case, courseName).

Access Specifiers public and private Most data-member declarations appear after the private access specifier. Variables or functions declared after access specifier private (and before the next access specifier if there is one) are accessible only to member functions of the class for which they’re declared (or to “friends” of the class, as you’ll see in Chapter 10, Classes: A Deeper Look, Part 2). Thus, data member courseName can be used only in member functions setCourseName, getCourseName and displayMessage of class GradeBook (or to “friends” of the class, if there were any).

74

Chapter 3

Introduction to Classes, Objects and Strings

Software Engineering Observation 3.1

Generally, data members should be declared declared public.

private

and member functions should be

Error-Prevention Tip 3.1

Make the data members of a class private and the member functions of the class public. This facilitates debugging because problems with data manipulations are localized to either the class’s member functions or the friends of the class.

Common Programming Error 3.2

An attempt by a function, which is not a member of a particular class (or a friend of that class) to access a private member of that class is a compilation error.

The default access for class members is private so all members after the class header and before the first access specifier (if there are any) are private. The access specifiers public and private may be repeated, but this is unnecessary and can be confusing. Declaring data members with access specifier private is known as data hiding. When a program creates a GradeBook object, data member courseName is encapsulated (hidden) in the object and can be accessed only by member functions of the object’s class. In class GradeBook, member functions setCourseName and getCourseName manipulate the data member courseName directly.

Member Functions setCourseName and getCourseName Member function setCourseName (lines 14–17) does not return any data when it completes its task, so its return type is void. The member function receives one parameter— name—which represents the course name that will be passed to it as an argument (as we’ll see in line 50 of main). Line 16 assigns name to data member courseName. In this example, setCourseName does not validate the course name—i.e., the function does not check that the course name adheres to any particular format or follows any other rules regarding what a “valid” course name looks like. Suppose, for instance, that a university can print student transcripts containing course names of only 25 characters or fewer. In this case, we might want class GradeBook to ensure that its data member courseName never contains more than 25 characters. We discuss validation in Section 3.8. Member function getCourseName (lines 20–23) returns a particular GradeBook object’s courseName. The member function has an empty parameter list, so it does not require additional data to perform its task. The function specifies that it returns a string. When a function that specifies a return type other than void is called and completes its task, the function uses a return statement (as in line 22) to return a result to its calling function. For example, when you go to an automated teller machine (ATM) and request your account balance, you expect the ATM to give you back a value that represents your balance. Similarly, when a statement calls member function getCourseName on a GradeBook object, the statement expects to receive the GradeBook’s course name (in this case, a string, as specified by the function’s return type). If you have a function square that returns the square of its argument, the statement result = square( 2 );

3.4 Data Members, set Functions and get Functions

75

returns 4 from function square and assigns to variable result the value 4. If you have a function maximum that returns the largest of three integer arguments, the statement biggest = maximum( 27, 114, 51 );

returns 114 from function maximum and assigns to variable biggest the value 114. The statements in lines 16 and 22 each use variable courseName (line 34) even though it was not declared in any of the member functions. We can do this because courseName is a data member of the class.

Member Function displayMessage Member function displayMessage (lines 26–32) does not return any data when it completes its task, so its return type is void. The function does not receive parameters, so its parameter list is empty. Lines 30–31 output a welcome message that includes the value of data member courseName. Line 30 calls member function getCourseName to obtain the value of courseName. Member function displayMessage could also access data member courseName directly, just as member functions setCourseName and getCourseName do. We explain shortly why it’s preferable to call member function getCourseName to obtain the value of courseName. Testing Class GradeBook The main function (lines 38–54) creates one object of class GradeBook and uses each of its member functions. Line 41 creates a GradeBook object named myGradeBook. Lines 44–45 display the initial course name by calling the object’s getCourseName member function. The first line of the output does not show a course name, because the object’s courseName data member (i.e., a string) is initially empty—by default, the initial value of a string is the so-called empty string, i.e., a string that does not contain any characters. Nothing appears on the screen when an empty string is displayed. Line 48 prompts the user to enter a course name. Local string variable nameOfCourse (declared in line 40) is set to the course name entered by the user, which is obtained by the call to the getline function (line 49). Line 50 calls object myGradeBook’s setCourseName member function and supplies nameOfCourse as the function’s argument. When the function is called, the argument’s value is copied to parameter name (line 14) of member function setCourseName. Then the parameter’s value is assigned to data member courseName (line 16). Line 52 skips a line; then line 53 calls object myGradeBook’s displayMessage member function to display the welcome message containing the course name. Software Engineering with Set and Get Functions A class’s private data members can be manipulated only by member functions of that class (and by “friends” of the class). So a client of an object—that is, any statement that calls the object’s member functions from outside the object—calls the class’s public member functions to request the class’s services for particular objects of the class. This is why the statements in function main call member functions setCourseName, getCourseName and displayMessage on a GradeBook object. Classes often provide public member functions to allow clients of the class to set (i.e., assign values to) or get (i.e., obtain the values of) private data members. These member function names need not begin with set or get, but this naming convention is common. In this example, the member function that sets the courseName data member is called setCourseName, and the member function that gets the value of the courseName data member is called getCourseName. Set functions are some-

76

Chapter 3

Introduction to Classes, Objects and Strings

times called mutators (because they mutate, or change, values), and get functions are also called accessors (because they access values). Recall that declaring data members with access specifier private enforces data hiding. Providing public set and get functions allows clients of a class to access the hidden data, but only indirectly. The client knows that it’s attempting to modify or obtain an object’s data, but the client does not know how the object performs these operations. In some cases, a class may internally represent a piece of data one way, but expose that data to clients in a different way. For example, suppose a Clock class represents the time of day as a private int data member time that stores the number of seconds since midnight. However, when a client calls a Clock object’s getTime member function, the object could return the time with hours, minutes and seconds in a string in the format "HH:MM:SS". Similarly, suppose the Clock class provides a set function named setTime that takes a string parameter in the "HH:MM:SS" format. Using string capabilities presented in Chapter 18, the setTime function could convert this string to a number of seconds, which the function stores in its private data member. The set function could also check that the value it receives represents a valid time (e.g., "12:30:45" is valid but "42:85:70" is not). The set and get functions allow a client to interact with an object, but the object’s private data remains safely encapsulated (i.e., hidden) in the object itself. The set and get functions of a class also should be used by other member functions within the class to manipulate the class’s private data, although these member functions can access the private data directly. In Fig. 3.5, member functions setCourseName and getCourseName are public member functions, so they’re accessible to clients of the class, as well as to the class itself. Member function displayMessage calls member function getCourseName to obtain the value of data member courseName for display purposes, even though displayMessage can access courseName directly—accessing a data member via its get function creates a better, more robust class (i.e., a class that’s easier to maintain and less likely to malfunction). If we decide to change the data member courseName in some way, the displayMessage definition will not require modification—only the bodies of the get and set functions that directly manipulate the data member will need to change. For example, suppose we want to represent the course name as two separate data members— courseNumber (e.g., "CS101") and courseTitle (e.g., "Introduction to C++ Programming"). Member function displayMessage can still issue a single call to member function getCourseName to obtain the full course name to display as part of the welcome message. In this case, getCourseName would need to build and return a string containing the courseNumber followed by the courseTitle. Member function displayMessage could continue to display the complete course title “CS101 Introduction to C++ Programming.” The benefits of calling a set function from another member function of the same class will become clear when we discuss validation in Section 3.8.

Good Programming Practice 3.1

Always try to localize the effects of changes to a class’s data members by accessing and manipulating the data members through their get and set functions.

Software Engineering Observation 3.2

Write programs that are clear and easy to maintain. Change is the rule rather than the exception. You should anticipate that your code will be modified.

3.5 Initializing Objects with Constructors

77

GradeBook’s

UML Class Diagram with a Data Member and set and get Functions Figure 3.6 contains an updated UML class diagram for the version of class GradeBook in Fig. 3.5. This diagram models GradeBook’s data member courseName as an attribute in the middle compartment. The UML represents data members as attributes by listing the attribute name, followed by a colon and the attribute type. The UML type of attribute courseName is String, which corresponds to string in C++. Data member courseName is private in C++, so the class diagram lists a minus sign (–) in front of the corresponding attribute’s name. The minus sign in the UML is equivalent to the private access specifier in C++. Class GradeBook contains three public member functions, so the class diagram lists three operations in the third compartment. Operation setCourseName has a String parameter called name. The UML indicates the return type of an operation by placing a colon and the return type after the parentheses following the operation name. Member function getCourseName of class GradeBook has a string return type in C++, so the class diagram shows a String return type in the UML. Operations setCourseName and displayMessage do not return values (i.e., they return void in C++), so the UML class diagram does not specify a return type after the parentheses of these operations. GradeBook – courseName : String + setCourseName( name : String ) + getCourseName( ) : String + displayMessage( )

Fig. 3.6 | UML class diagram for class GradeBook with a private courseName attribute and public operations setCourseName, getCourseName and displayMessage.

3.5 Initializing Objects with Constructors As mentioned in Section 3.4, when an object of class GradeBook (Fig. 3.5) is created, its data member courseName is initialized to the empty string by default. What if you want to provide a course name when you create a GradeBook object? Each class you declare can provide a constructor that can be used to initialize an object of the class when the object is created. A constructor is a special member function that must be defined with the same name as the class, so that the compiler can distinguish it from the class’s other member functions. An important difference between constructors and other functions is that constructors cannot return values, so they cannot specify a return type (not even void). Normally, constructors are declared public. C++ requires a constructor call for each object that’s created, which helps ensure that each object is initialized properly before it’s used in a program. The constructor call occurs implicitly when the object is created. If a class does not explicitly include a constructor, the compiler provides a default constructor—that is, a constructor with no parameters. For example, when line 41 of Fig. 3.5 creates a GradeBook object, the default constructor is called. The default constructor provided by the compiler creates a GradeBook object without giving any initial values to the object’s fundamental type data members. [Note: For data members that are objects of other classes, the default constructor implicitly calls each data member’s default constructor to ensure that the data member is initialized prop-

78

Chapter 3

Introduction to Classes, Objects and Strings

erly. This is why the string data member courseName (in Fig. 3.5) was initialized to the empty string—the default constructor for class string sets the string’s value to the empty string. You’ll learn more about initializing data members that are objects of other classes in Section 10.3.] In the example of Fig. 3.7, we specify a course name for a GradeBook object when the object is created (e.g., line 46). In this case, the argument "CS101 Introduction to C++ Programming" is passed to the GradeBook object’s constructor (lines 14–17) and used to initialize the courseName. Figure 3.7 defines a modified GradeBook class containing a constructor with a string parameter that receives the initial course name. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

// Fig. 3.7: fig03_07.cpp // Instantiating multiple objects of the GradeBook class and using // the GradeBook constructor to specify the course name // when each GradeBook object is created. #include #include // program uses C++ standard string class using namespace std; // GradeBook class definition class GradeBook { public: // constructor initializes courseName with string supplied as argument GradeBook( string name ) { setCourseName( name ); // call set function to initialize courseName } // end GradeBook constructor // function to set the course name void setCourseName( string name ) { courseName = name; // store the course name in the object } // end function setCourseName // function to get the course name string getCourseName() { return courseName; // return object's courseName } // end function getCourseName // display a welcome message to the GradeBook user void displayMessage() { // call getCourseName to get the courseName cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } // end function displayMessage private: string courseName; // course name for this GradeBook }; // end class GradeBook

Fig. 3.7 | Instantiating multiple objects of the GradeBook class and using the GradeBook constructor to specify the course name when each GradeBook object is created. (Part 1 of 2.)

3.5 Initializing Objects with Constructors 41 42 43 44 45 46 47 48 49 50 51 52 53

79

// function main begins program execution int main() { // create two GradeBook objects GradeBook gradeBook1( "CS101 Introduction to C++ Programming" ); GradeBook gradeBook2( "CS102 Data Structures in C++" ); // display initial value of courseName for each GradeBook cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; } // end main

gradeBook1 created for course: CS101 Introduction to C++ Programming gradeBook2 created for course: CS102 Data Structures in C++

Fig. 3.7 | Instantiating multiple objects of the GradeBook class and using the GradeBook constructor to specify the course name when each GradeBook object is created. (Part 2 of 2.) Defining a Constructor Lines 14–17 of Fig. 3.7 define a constructor for class GradeBook. Notice that the constructor has the same name as its class, GradeBook. A constructor specifies in its parameter list the data it requires to perform its task. When you create a new object, you place this data in the parentheses that follow the object name (as we did in lines 46–47). Line 14 indicates that class GradeBook’s constructor has a string parameter called name. Line 14 does not specify a return type, because constructors cannot return values (or even void). Line 16 in the constructor’s body passes the constructor’s parameter name to member function setCourseName (lines 20–23), which simply assigns the value of its parameter to data member courseName. You might be wondering why we make the call to setCourseName in line 16—the constructor certainly could perform the assignment courseName = name. In Section 3.8, we modify setCourseName to perform validation (ensuring that, in this case, the courseName is 25 or fewer characters in length). At that point the benefits of calling setCourseName from the constructor will become clear. Both the constructor (line 14) and the setCourseName function (line 20) use a parameter called name. You can use the same parameter names in different functions because the parameters are local to each function; they do not interfere with one another. Testing Class GradeBook Lines 43–53 of Fig. 3.7 define the main function that tests class GradeBook and demonstrates initializing GradeBook objects using a constructor. Line 46 creates and initializes GradeBook object gradeBook1. When this line executes, the GradeBook constructor (lines 14–17) is called (implicitly by C++) with the argument "CS101 Introduction to C++ Programming" to initialize gradeBook1’s course name. Line 47 repeats this process for GradeBook object gradeBook2, this time passing the argument "CS102 Data Structures in C++" to initialize gradeBook2’s course name. Lines 50–51 use each object’s getCourseName member function to obtain the course names and show that they were indeed initialized when the objects were created. The output confirms that each GradeBook object maintains its own copy of data member courseName.

80

Chapter 3

Introduction to Classes, Objects and Strings

Two Ways to Provide a Default Constructor for a Class Any constructor that takes no arguments is called a default constructor. A class can get a default constructor in one of two ways: 1. The compiler implicitly creates a default constructor in a class that does not define a constructor. Such a constructor does not initialize the class’s data members, but does call the default constructor for each data member that’s an object of another class. An uninitialized variable typically contains a “garbage” value. 2. You explicitly define a constructor that takes no arguments. Such a default constructor will call the default constructor for each data member that’s an object of another class and will perform additional initialization specified by you. If you define a constructor with arguments, C++ will not implicitly create a default constructor for that class. For each version of class GradeBook in Fig. 3.1, Fig. 3.3 and Fig. 3.5 the compiler implicitly defined a default constructor.

Error-Prevention Tip 3.2

Unless no initialization of your class’s data members is necessary (almost never), provide a constructor to ensure that your class’s data members are initialized with meaningful values when each new object of your class is created.

Software Engineering Observation 3.3

Data members can be initialized in a constructor, or their values may be set later after the object is created. However, it’s a good software engineering practice to ensure that an object is fully initialized before the client code invokes the object’s member functions. You should not rely on the client code to ensure that an object gets initialized properly.

Adding the Constructor to Class GradeBook’s UML Class Diagram The UML class diagram of Fig. 3.8 models the GradeBook class of Fig. 3.7, which has a constructor with a name parameter of type string (represented by type String in the UML). Like operations, the UML models constructors in the third compartment of a class in a class diagram. To distinguish a constructor from a class’s operations, the UML places the word “constructor” between guillemets (« and ») before the constructor’s name. By convention, you list the class’s constructor before other operations in the third compartment.

GradeBook – courseName : String «constructor» + GradeBook( name : String ) + setCourseName( name : String ) + getCourseName( ) : String + displayMessage( )

Fig. 3.8 | UML class diagram indicating that class GradeBook has a constructor with a name parameter of UML type String.

3.6 Placing a Class in a Separate File for Reusability

81

3.6 Placing a Class in a Separate File for Reusability One of the benefits of creating class definitions is that, when packaged properly, your classes can be reused by other programmers. For example, you can reuse C++ Standard Library type string in any C++ program by including the header (and, as you’ll see, by being able to link to the library’s object code). Programmers who wish to use our GradeBook class cannot simply include the file from Fig. 3.7 in another program. As you learned in Chapter 2, function main begins the execution of every program, and every program must have exactly one main function. If other programmers include the code from Fig. 3.7, they get extra “baggage”—our main function— and their programs will then have two main functions. Attempting to compile a program with two main functions produces an error when the compiler tries to compile the second main function it encounters. So, placing main in the same file with a class definition prevents that class from being reused by other programs. In this section, we demonstrate how to make class GradeBook reusable by separating it into another file from the main function.

Headers Each of the previous examples in the chapter consists of a single .cpp file, also known as a source-code file, that contains a GradeBook class definition and a main function. When building an object-oriented C++ program, it’s customary to define reusable source code (such as a class) in a file that by convention has a .h filename extension—known as a header. Programs use #include preprocessor directives to include headers and take advantage of reusable software components, such as type string provided in the C++ Standard Library and user-defined types like class GradeBook. Our next example separates the code from Fig. 3.7 into two files—GradeBook.h (Fig. 3.9) and fig03_10.cpp (Fig. 3.10). As you look at the header in Fig. 3.9, notice that it contains only the GradeBook class definition (lines 8–38), the appropriate headers and a using directive. The main function that uses class GradeBook is defined in the source-code file fig03_10.cpp (Fig. 3.10) in lines 8–18. To help you prepare for the larger programs you’ll encounter later in this book and in industry, we often use a separate source-code file containing function main to test our classes (this is called a driver program). You’ll soon learn how a source-code file with main can use the class definition found in a header to create objects of a class. 1 2 3 4 5 6 7 8 9 10 11 12 13

// Fig. 3.9: GradeBook.h // GradeBook class definition in a separate file from main. #include #include // class GradeBook uses C++ standard string class using namespace std; // GradeBook class definition class GradeBook { public: // constructor initializes courseName with string supplied as argument GradeBook( string name ) {

Fig. 3.9 |

GradeBook

class definition in a separate file from main. (Part 1 of 2.)

82 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

Chapter 3

setCourseName( name ); // call set function to initialize courseName } // end GradeBook constructor // function to set the course name void setCourseName( string name ) { courseName = name; // store the course name in the object } // end function setCourseName // function to get the course name string getCourseName() { return courseName; // return object's courseName } // end function getCourseName // display a welcome message to the GradeBook user void displayMessage() { // call getCourseName to get the courseName cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } // end function displayMessage private: string courseName; // course name for this GradeBook }; // end class GradeBook

Fig. 3.9 |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Introduction to Classes, Objects and Strings

GradeBook

class definition in a separate file from main. (Part 2 of 2.)

// Fig. 3.10: fig03_10.cpp // Including class GradeBook from file GradeBook.h for use in main. #include #include "GradeBook.h" // include definition of class GradeBook using namespace std; // function main begins program execution int main() { // create two GradeBook objects GradeBook gradeBook1( "CS101 Introduction to C++ Programming" ); GradeBook gradeBook2( "CS102 Data Structures in C++" ); // display initial value of courseName for each GradeBook cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; } // end main

gradeBook1 created for course: CS101 Introduction to C++ Programming gradeBook2 created for course: CS102 Data Structures in C++

Fig. 3.10 | Including class GradeBook from file GradeBook.h for use in main.

3.6 Placing a Class in a Separate File for Reusability

83

Including a Header That Contains a User-Defined Class A header such as GradeBook.h (Fig. 3.9) cannot be used as a complete program, because it does not contain a main function. If you try to compile and link GradeBook.h by itself to create an executable application, Microsoft Visual C++ 2010 produces the linker error message: error LNK2001: unresolved external symbol _mainCRTStartup

To compile and link with GNU C++ on Linux, you must first include the header in a .cpp source-code file, then GNU C++ produces a linker error message containing: undefined reference to 'main'

This error indicates that the linker could not locate the program’s main function. To test class GradeBook (defined in Fig. 3.9), you must write a separate source-code file containing a main function (such as Fig. 3.10) that instantiates and uses objects of the class. The compiler doesn’t know what a GradeBook is because it’s a user-defined type. In fact, the compiler doesn’t even know the classes in the C++ Standard Library. To help it understand how to use a class, we must explicitly provide the compiler with the class’s definition—that’s why, for example, to use type string, a program must include the header. This enables the compiler to determine the amount of memory that it must reserve for each string object and ensure that a program calls a string’s member functions correctly. To create GradeBook objects gradeBook1 and gradeBook2 in lines 11–12 of Fig. 3.10, the compiler must know the size of a GradeBook object. While objects conceptually contain data members and member functions, C++ objects actually contain only data. The compiler creates only one copy of the class’s member functions and shares that copy among all the class’s objects. Each object, of course, needs its own copy of the class’s data members, because their contents can vary among objects (such as two different BankAccount objects having two different balances). The member-function code, however, is not modifiable, so it can be shared among all objects of the class. Therefore, the size of an object depends on the amount of memory required to store the class’s data members. By including GradeBook.h in line 4, we give the compiler access to the information it needs (Fig. 3.9, line 37) to determine the size of a GradeBook object and to determine whether objects of the class are used correctly (in lines 11–12 and 15–16 of Fig. 3.10). Line 4 instructs the C++ preprocessor to replace the directive with a copy of the contents of GradeBook.h (i.e., the GradeBook class definition) before the program is compiled. When the source-code file fig03_10.cpp is compiled, it now contains the GradeBook class definition (because of the #include), and the compiler is able to determine how to create GradeBook objects and see that their member functions are called correctly. Now that the class definition is in a header (without a main function), we can include that header in any program that needs to reuse our GradeBook class.

How Headers Are Located Notice that the name of the GradeBook.h header in line 4 of Fig. 3.10 is enclosed in quotes (" ") rather than angle brackets (< >). Normally, a program’s source-code files and userdefined headers are placed in the same directory. When the preprocessor encounters a header name in quotes, it attempts to locate the header in the same directory as the file in which the #include directive appears. If the preprocessor cannot find the header in that

84

Chapter 3

Introduction to Classes, Objects and Strings

directory, it searches for it in the same location(s) as the C++ Standard Library headers. When the preprocessor encounters a header name in angle brackets (e.g., ), it assumes that the header is part of the C++ Standard Library and does not look in the directory of the program that’s being preprocessed.

Error-Prevention Tip 3.3

To ensure that the preprocessor can locate headers correctly, #include preprocessor directives should place user-defined headers names in quotes (e.g., "GradeBook.h") and place C++ Standard Library headers names in angle brackets (e.g., ).

Additional Software Engineering Issues Now that class GradeBook is defined in a header, the class is reusable. Unfortunately, placing a class definition in a header as in Fig. 3.9 still reveals the entire implementation of the class to the class’s clients—GradeBook.h is simply a text file that anyone can open and read. Conventional software engineering wisdom says that to use an object of a class, the client code needs to know only what member functions to call, what arguments to provide to each member function and what return type to expect from each member function. The client code does not need to know how those functions are implemented. If client code does know how a class is implemented, the programmer might write client code based on the class’s implementation details. Ideally, if that implementation changes, the class’s clients should not have to change. Hiding the class’s implementation details makes it easier to change the class’s implementation while minimizing, and hopefully eliminating, changes to client code. In Section 3.7, we show how to break up the GradeBook class into two files so that 1. the class is reusable, 2. the clients of the class know what member functions the class provides, how to call them and what return types to expect, and 3. the clients do not know how the class’s member functions are implemented.

3.7 Separating Interface from Implementation In the preceding section, we showed how to promote software reusability by separating a class definition from the client code (e.g., function main) that uses the class. We now introduce another fundamental principle of good software engineering—separating interface from implementation.

Interface of a Class Interfaces define and standardize the ways in which things such as people and systems interact with one another. For example, a radio’s controls serve as an interface between the radio’s users and its internal components. The controls allow users to perform a limited set of operations (such as changing the station, adjusting the volume, and choosing between AM and FM stations). Various radios may implement these operations differently—some provide push buttons, some provide dials and some support voice commands. The interface specifies what operations a radio permits users to perform but does not specify how the operations are implemented inside the radio.

3.7 Separating Interface from Implementation

85

Similarly, the interface of a class describes what services a class’s clients can use and how to request those services, but not how the class carries out the services. A class’s public interface consists of the class’s public member functions (also known as the class’s public services). For example, class GradeBook’s interface (Fig. 3.9) contains a constructor and member functions setCourseName, getCourseName and displayMessage. GradeBook’s clients (e.g., main in Fig. 3.10) use these functions to request the class’s services. As you’ll soon see, you can specify a class’s interface by writing a class definition that lists only the member-function names, return types and parameter types.

Separating the Interface from the Implementation In our prior examples, each class definition contained the complete definitions of the class’s public member functions and the declarations of its private data members. However, it’s better software engineering to define member functions outside the class definition, so that their implementation details can be hidden from the client code. This practice ensures that you do not write client code that depends on the class’s implementation details. If you were to do so, the client code would be more likely to “break” if the class’s implementation changed. Given that one class could have many clients, such a change could cause wide-ranging problems in a software system. The program of Figs. 3.11–3.13 separates class GradeBook’s interface from its implementation by splitting the class definition of Fig. 3.9 into two files—the header GradeBook.h (Fig. 3.11) in which class GradeBook is defined, and the source-code file GradeBook.cpp (Fig. 3.12) in which GradeBook’s member functions are defined. By convention, member-function definitions are placed in a source-code file of the same base name (e.g., GradeBook) as the class’s header but with a .cpp filename extension. The source-code file fig03_13.cpp (Fig. 3.13) defines function main (the client code). The code and output of Fig. 3.13 are identical to that of Fig. 3.10. Figure 3.14 shows how this three-file program is compiled from the perspectives of the GradeBook class programmer and the client-code programmer—we’ll explain this figure in detail. GradeBook.h: Defining a Class’s Interface with Function Prototypes Header GradeBook.h (Fig. 3.11) contains another version of GradeBook’s class definition (lines 9–18). This version is similar to the one in Fig. 3.9, but the function definitions in Fig. 3.9 are replaced here with function prototypes (lines 12–15) that describe the class’s public interface without revealing the class’s member-function implementations. A function prototype is a declaration of a function that tells the compiler the function’s name, its return type and the types of its parameters. Also, the header still specifies the class’s private data member (line 17) as well. Again, the compiler must know the data members of the class to determine how much memory to reserve for each object of the class. Including the header GradeBook.h in the client code (line 5 of Fig. 3.13) provides the compiler with the information it needs to ensure that the client code calls the member functions of class GradeBook correctly. The function prototype in line 12 (Fig. 3.11) indicates that the constructor requires one string parameter. Recall that constructors don’t have return types, so no return type appears in the function prototype. Member function setCourseName’s function prototype indicates that setCourseName requires a string parameter and does not return a value (i.e., its return type is void). Member function getCourseName’s function prototype indicates that the function does not require parameters and returns a string. Finally, member

86 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Chapter 3

Introduction to Classes, Objects and Strings

// Fig. 3.11: GradeBook.h // GradeBook class definition. This file presents GradeBook's public // interface without revealing the implementations of GradeBook's member // functions, which are defined in GradeBook.cpp. #include // class GradeBook uses C++ standard string class using namespace std; // GradeBook class definition class GradeBook { public: GradeBook( string ); // constructor that initializes courseName void setCourseName( string ); // function that sets the course name string getCourseName(); // function that gets the course name void displayMessage(); // function that displays a welcome message private: string courseName; // course name for this GradeBook }; // end class GradeBook

Fig. 3.11 |

GradeBook

class definition containing function prototypes that specify the interface

of the class.

function displayMessage’s function prototype (line 15) specifies that displayMessage does not require parameters and does not return a value. These function prototypes are the same as the corresponding function headers in Fig. 3.9, except that the parameter names (which are optional in prototypes) are not included and each function prototype must end with a semicolon.

Good Programming Practice 3.2

Although parameter names in function prototypes are optional (they’re ignored by the compiler), many programmers use these names for documentation purposes.

Error-Prevention Tip 3.4

Parameter names in a function prototype (which, again, are ignored by the compiler) can be misleading if the names used do not match those used in the function definition. For this reason, many programmers create function prototypes by copying the first line of the corresponding function definitions (when the source code for the functions is available), then appending a semicolon to the end of each prototype. GradeBook.cpp: Defining Member Functions in a Separate Source-Code File Source-code file GradeBook.cpp (Fig. 3.12) defines class GradeBook’s member functions, which were declared in lines 12–15 of Fig. 3.11. The definitions appear in lines 9–32 and are nearly identical to the member-function definitions in lines 12–35 of Fig. 3.9. Each member-function name in the function headers (lines 9, 15, 21 and 27) is preceded by the class name and ::, which is known as the binary scope resolution operator. This “ties” each member function to the (now separate) GradeBook class definition (Fig. 3.11), which declares the class’s member functions and data members. Without “GradeBook::” preceding each function name, these functions would not be recognized by the compiler as member functions of class GradeBook—the compiler would consider them

3.7 Separating Interface from Implementation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

87

// Fig. 3.12: GradeBook.cpp // GradeBook member-function definitions. This file contains // implementations of the member functions prototyped in GradeBook.h. #include #include "GradeBook.h" // include definition of class GradeBook using namespace std; // constructor initializes courseName with string supplied as argument GradeBook::GradeBook( string name ) { setCourseName( name ); // call set function to initialize courseName } // end GradeBook constructor // function to set the course name void GradeBook::setCourseName( string name ) { courseName = name; // store the course name in the object } // end function setCourseName // function to get the course name string GradeBook::getCourseName() { return courseName; // return object's courseName } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { // call getCourseName to get the courseName cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } // end function displayMessage

Fig. 3.12 |

GradeBook

member-function definitions represent the implementation of class

GradeBook.

“free” or “loose” functions, like main. These are also called global functions. Such functions cannot access GradeBook’s private data or call the class’s member functions, without specifying an object. So, the compiler would not be able to compile these functions. For example, lines 17 and 23 that access variable courseName would cause compilation errors because courseName is not declared as a local variable in each function—the compiler would not know that courseName is already declared as a data member of class GradeBook.

Common Programming Error 3.3

When defining a class’s member functions outside that class, omitting the class name and binary scope resolution operator (::) preceding the function names causes errors.

To indicate that the member functions in GradeBook.cpp are part of class GradeBook, we must first include the GradeBook.h header (line 5 of Fig. 3.12). This allows us to access the class name GradeBook in the GradeBook.cpp file. When compiling GradeBook.cpp, the compiler uses the information in GradeBook.h to ensure that

88

Chapter 3

Introduction to Classes, Objects and Strings

1. the first line of each member function (lines 9, 15, 21 and 27) matches its prototype in the GradeBook.h file—for example, the compiler ensures that getCourseName accepts no parameters and returns a string, and that 2. each member function knows about the class’s data members and other member functions—for example, lines 17 and 23 can access variable courseName because it’s declared in GradeBook.h as a data member of class GradeBook, and lines 11 and 30 can call functions setCourseName and getCourseName, respectively, because each is declared as a member function of the class in GradeBook.h (and because these calls conform with the corresponding prototypes).

Testing Class GradeBook Figure 3.13 performs the same GradeBook object manipulations as Fig. 3.10. Separating GradeBook’s interface from the implementation of its member functions does not affect the way that this client code uses the class. It affects only how the program is compiled and linked, which we discuss in detail shortly. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

// Fig. 3.13: fig03_13.cpp // GradeBook class demonstration after separating // its interface from its implementation. #include #include "GradeBook.h" // include definition of class GradeBook using namespace std; // function main begins program execution int main() { // create two GradeBook objects GradeBook gradeBook1( "CS101 Introduction to C++ Programming" ); GradeBook gradeBook2( "CS102 Data Structures in C++" ); // display initial value of courseName for each GradeBook cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; } // end main

gradeBook1 created for course: CS101 Introduction to C++ Programming gradeBook2 created for course: CS102 Data Structures in C++

Fig. 3.13 |

GradeBook

class demonstration after separating its interface from its

implementation.

As in Fig. 3.10, line 5 of Fig. 3.13 includes the GradeBook.h header so that the compiler can ensure that GradeBook objects are created and manipulated correctly in the client code. Before executing this program, the source-code files in Fig. 3.12 and Fig. 3.13 must both be compiled, then linked together—that is, the member-function calls in the client code need to be tied to the implementations of the class’s member functions—a job performed by the linker.

3.7 Separating Interface from Implementation

89

The Compilation and Linking Process The diagram in Fig. 3.14 shows the compilation and linking process that results in an executable GradeBook application that can be used by instructors. Often a class’s interface and implementation will be created and compiled by one programmer and used by a separate programmer who implements the client code that uses the class. So, the diagram shows what’s required by both the class-implementation programmer and the client-code programmer. The dashed lines in the diagram show the pieces required by the class-implementation programmer, the client-code programmer and the GradeBook application user, respectively. [Note: Figure 3.14 is not a UML diagram.] A class-implementation programmer responsible for creating a reusable GradeBook class creates the header GradeBook.h and the source-code file GradeBook.cpp that

Client Code Programmer

Class Implementation Programmer GradeBook.cpp

implementation file

GradeBook.h

class definition/interface

compiler

GradeBook class's

object code

main function (client source code)

compiler

C++ Standard Library object code

main function's

object code

linker

GradeBook

executable application GradeBook

Application User

Fig. 3.14 | Compilation and linking process that produces an executable application.

90

Chapter 3

Introduction to Classes, Objects and Strings

#includes

the header, then compiles the source-code file to create GradeBook’s object code. To hide the class’s member-function implementation details, the class-implementation programmer would provide the client-code programmer with the header GradeBook.h (which specifies the class’s interface and data members) and the GradeBook object code (i.e., the machine-language instructions that represent GradeBook’s member functions). The client-code programmer is not given GradeBook.cpp, so the client remains unaware of how GradeBook’s member functions are implemented. The client code needs to know only GradeBook’s interface to use the class and must be able to link its object code. Since the interface of the class is part of the class definition in the GradeBook.h header, the client-code programmer must have access to this file and must #include it in the client’s source-code file. When the client code is compiled, the compiler uses the class definition in GradeBook.h to ensure that the main function creates and manipulates objects of class GradeBook correctly. To create the executable GradeBook application, the last step is to link 1. the object code for the main function (i.e., the client code), 2. the object code for class GradeBook’s member-function implementations and 3. the C++ Standard Library object code for the C++ classes (e.g., string) used by the class-implementation programmer and the client-code programmer. The linker’s output is the executable GradeBook application that instructors can use to manage their students’ grades. Compilers and IDEs typically invoke the linker for you after compiling your code. For further information on compiling multiple-source-file programs, see your compiler’s documentation. We provide links to various C++ compilers in our C++ Resource Center at www.deitel.com/cplusplus/.

3.8 Validating Data with set Functions In Section 3.4, we introduced set functions for allowing clients of a class to modify the value of a private data member. In Fig. 3.5, class GradeBook defines member function setCourseName to simply assign a value received in its parameter name to data member courseName. This member function does not ensure that the course name adheres to any particular format or follows any other rules regarding what a “valid” course name looks like. As we stated earlier, suppose that a university can print student transcripts containing course names of only 25 characters or less. If the university uses a system containing GradeBook objects to generate the transcripts, we might want class GradeBook to ensure that its data member courseName never contains more than 25 characters. The program of Figs. 3.15–3.17 enhances class GradeBook’s member function setCourseName to perform this validation (also known as validity checking).

Class Definition Notice that GradeBook’s class definition (Fig. 3.15)—and hence, its interface—is identical to that of Fig. 3.11. Since the interface remains unchanged, clients of this class need not be changed when the definition of member function setCourseName is modified. This enables clients to take advantage of the improved GradeBook class simply by linking the client code to the updated GradeBook’s object code. GradeBook

3.8 Validating Data with set Functions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

91

// Fig. 3.15: GradeBook.h // GradeBook class definition presents the public interface of // the class. Member-function definitions appear in GradeBook.cpp. #include // program uses C++ standard string class using namespace std; // GradeBook class definition class GradeBook { public: GradeBook( string ); // constructor that initializes a GradeBook object void setCourseName( string ); // function that sets the course name string getCourseName(); // function that gets the course name void displayMessage(); // function that displays a welcome message private: string courseName; // course name for this GradeBook }; // end class GradeBook

Fig. 3.15 |

GradeBook

class definition.

Validating the Course Name with GradeBook Member Function setCourseName The enhancement to class GradeBook is in the definition of setCourseName (Fig. 3.16, lines 16–29). The if statement in lines 18–19 determines whether parameter name contains a valid course name (i.e., a string of 25 or fewer characters). If the course name is valid, line 19 stores it in data member courseName. Note the expression name.length() in line 18. This is a member-function call just like myGradeBook.displayMessage(). The C++ Standard Library’s string class defines a member function length that returns the number of characters in a string object. Parameter name is a string object, so the call name.length() returns the number of characters in name. If this value is less than or equal to 25, name is valid and line 19 executes. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

// Fig. 3.16: GradeBook.cpp // Implementations of the GradeBook member-function definitions. // The setCourseName function performs validation. #include #include "GradeBook.h" // include definition of class GradeBook using namespace std; // constructor initializes courseName with string supplied as argument GradeBook::GradeBook( string name ) { setCourseName( name ); // validate and store courseName } // end GradeBook constructor // function that sets the course name; // ensures that the course name has at most 25 characters void GradeBook::setCourseName( string name ) {

Fig. 3.16 | Member-function definitions for class GradeBook with a set function that validates the length of data member courseName. (Part 1 of 2.)

92 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

Chapter 3

Introduction to Classes, Objects and Strings

if ( name.length() <= 25 ) // if name has 25 or fewer characters courseName = name; // store the course name in the object if ( name.length() > 25 ) // if name has more than 25 characters { // set courseName to first 25 characters of parameter name courseName = name.substr( 0, 25 ); // start at 0, length of 25 cout << "Name \"" << name << "\" exceeds maximum length (25).\n" << "Limiting courseName to first 25 characters.\n" << endl; } // end if } // end function setCourseName // function to get the course name string GradeBook::getCourseName() { return courseName; // return object's courseName } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { // call getCourseName to get the courseName cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } // end function displayMessage

Fig. 3.16 | Member-function definitions for class GradeBook with a set function that validates the length of data member courseName. (Part 2 of 2.)

The if statement in lines 21–28 handles the case in which setCourseName receives an invalid course name (i.e., a name that is more than 25 characters long). Even if parameter name is too long, we still want to leave the GradeBook object in a consistent state—that is, a state in which the object’s data member courseName contains a valid value (i.e., a string of 25 characters or less). Thus, we truncate the specified course name and assign the first 25 characters of name to the courseName data member (unfortunately, this could truncate the course name awkwardly). Standard class string provides member function substr (short for “substring”) that returns a new string object created by copying part of an existing string object. The call in line 24 (i.e., name.substr( 0, 25 )) passes two integers (0 and 25) to name’s member function substr. These arguments indicate the portion of the string name that substr should return. The first argument specifies the starting position in the original string from which characters are copied—the first character in every string is considered to be at position 0. The second argument specifies the number of characters to copy. Therefore, the call in line 24 returns a 25-character substring of name starting at position 0 (i.e., the first 25 characters in name). For example, if name holds the value "CS101 Introduction to Programming in C++", substr returns "CS101 Introduction to Pro". After the call to substr, line 24 assigns the substring returned by substr to data member courseName. In this way, setCourseName ensures that courseName is always assigned a string containing 25 or fewer characters. If the member function has to truncate the course name to make it valid, lines 26–27 display a warning message.

3.8 Validating Data with set Functions The

if

93

statement in lines 21–28 contains two body statements—one to set the

courseName to the first 25 characters of parameter name and one to print an accompanying

message to the user. Both statements should execute when name is too long, so we place them in a pair of braces, { }. Recall from Chapter 2 that this creates a block. You’ll learn more about placing multiple statements in a control statement’s body in Chapter 4. The statement in lines 26–27 could also appear without a stream insertion operator at the start of the second line of the statement, as in: cout << "Name \"" << name << "\" exceeds maximum length (25).\n" "Limiting courseName to first 25 characters.\n" << endl;

The C++ compiler combines adjacent string literals, even if they appear on separate lines of a program. Thus, in the statement above, the C++ compiler would combine the string literals "\" exceeds maximum length (25).\n" and "Limiting courseName to first 25 characters.\n" into a single string literal that produces output identical to that of lines 26–27 in Fig. 3.16. This behavior allows you to print lengthy strings by breaking them across lines in your program without including additional stream insertion operations.

Testing Class GradeBook Figure 3.17 demonstrates the modified version of class GradeBook (Figs. 3.15–3.16) featuring validation. Line 12 creates a GradeBook object named gradeBook1. Recall that the GradeBook constructor calls setCourseName to initialize data member courseName. In previous versions of the class, the benefit of calling setCourseName in the constructor was not evident. Now, however, the constructor takes advantage of the validation provided by setCourseName. The constructor simply calls setCourseName, rather than duplicating its validation code. When line 12 of Fig. 3.17 passes an initial course name of "CS101 Introduction to Programming in C++" to the GradeBook constructor, the constructor passes this value to setCourseName, where the actual initialization occurs. Because this course name contains more than 25 characters, the body of the second if statement executes, causing courseName to be initialized to the truncated 25-character course name "CS101 Introduction to Pro" (the truncated part is highlighted in red in line 12). The output in Fig. 3.17 contains the warning message output by lines 26–27 of Fig. 3.16 in member function setCourseName. Line 13 creates another GradeBook object called gradeBook2—the valid course name passed to the constructor is exactly 25 characters. Lines 16–19 of Fig. 3.17 display the truncated course name for gradeBook1 (we highlight this in blue in the program output) and the course name for gradeBook2. Line 22 calls gradeBook1’s setCourseName member function directly, to change the course name in the GradeBook object to a shorter name that does not need to be truncated. Then, lines 25–28 output the course names for the GradeBook objects again. 1 2 3 4 5

// Fig. 3.17: fig03_17.cpp // Create and manipulate a GradeBook object; illustrate validation. #include #include "GradeBook.h" // include definition of class GradeBook using namespace std;

Fig. 3.17 | Creating and manipulating a GradeBook object in which the course name is limited to 25 characters in length. (Part 1 of 2.)

94 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

Chapter 3

Introduction to Classes, Objects and Strings

// function main begins program execution int main() { // create two GradeBook objects; // initial course name of gradeBook1 is too long GradeBook gradeBook1( "CS101 Introduction to Programming in C++" ); GradeBook gradeBook2( "CS102 C++ Data Structures" ); // display each GradeBook's courseName cout << "gradeBook1's initial course name is: " << gradeBook1.getCourseName() << "\ngradeBook2's initial course name is: " << gradeBook2.getCourseName() << endl; // modify myGradeBook's courseName (with a valid-length string) gradeBook1.setCourseName( "CS101 C++ Programming" ); // display each GradeBook's courseName cout << "\ngradeBook1's course name is: " << gradeBook1.getCourseName() << "\ngradeBook2's course name is: " << gradeBook2.getCourseName() << endl; } // end main

Name "CS101 Introduction to Programming in C++" exceeds maximum length (25). Limiting courseName to first 25 characters. gradeBook1's initial course name is: CS101 Introduction to Pro gradeBook2's initial course name is: CS102 C++ Data Structures gradeBook1's course name is: CS101 C++ Programming gradeBook2's course name is: CS102 C++ Data Structures

Fig. 3.17 | Creating and manipulating a GradeBook object in which the course name is limited to 25 characters in length. (Part 2 of 2.)

Additional Notes on Set Functions A public set function such as setCourseName should carefully scrutinize any attempt to modify the value of a data member (e.g., courseName) to ensure that the new value is appropriate for that data item. For example, an attempt to set the day of the month to 37 should be rejected, an attempt to set a person’s weight to zero or a negative value should be rejected, an attempt to set a grade on an exam to 185 (when the proper range is zero to 100) should be rejected, and so on

Software Engineering Observation 3.4

Making data members private and controlling access, especially write access, to those data members through public member functions helps ensure data integrity.

Error-Prevention Tip 3.5

The benefits of data integrity are not automatic simply because data members are made private—you must provide appropriate validity checking and report the errors.

3.9 Wrap-Up

95

A class’s set functions can return values to the class’s clients indicating that attempts were made to assign invalid data to objects of the class. A client can test the return value of a set function to determine whether the attempt to modify the object was successful and to take appropriate action. In C++, clients of objects can be notified of invalid values via the exception-handling mechanism, which we begin discussing in Chapter 7 and present indepth in Chapter 16. To keep the program of Figs. 3.15–3.17 simple at this early point in the book, setCourseName in Fig. 3.16 just prints an appropriate message.

3.9 Wrap-Up In this chapter, you created user-defined classes, and created and used objects of those classes. We declared data members of a class to maintain data for each object of the class. We also defined member functions that operate on that data. You learned how to call an object’s member functions to request the services the object provides and how to pass data to those member functions as arguments. We discussed the difference between a local variable of a member function and a data member of a class. We also showed how to use a constructor to specify initial values for an object’s data members. You learned how to separate the interface of a class from its implementation to promote good software engineering. We presented a diagram that shows the files that class-implementation programmers and client-code programmers need to compile the code they write. We demonstrated how set functions can be used to validate an object’s data and ensure that objects are maintained in a consistent state. UML class diagrams were used to model classes and their constructors, member functions and data members. In the next chapter, we begin our introduction to control statements, which specify the order in which a function’s actions are performed.

Summary Section 3.2 Defining a Class with a Member Function

• A class definition (p. 66) contains the data members and member functions that define the class’s attributes and behaviors, respectively. • A class definition begins with the keyword class followed immediately by the class name. • By convention, the name of a user-defined class (p. 67) begins with a capital letter and, for readability, each subsequent word in the class name begins with a capital letter. • Every class’s body (p. 66) is enclosed in a pair of braces ({ and }) and ends with a semicolon. • Member functions that appear after access specifier public (p. 66) can be called by other functions in a program and by member functions of other classes. • Access specifiers are always followed by a colon (:). • Keyword void (p. 67) is a special return type which indicates that a function will perform a task but will not return any data to its calling function when it completes its task. • By convention, function names (p. 67) begin with a lowercase first letter and all subsequent words in the name begin with a capital letter. • An empty set of parentheses after a function name indicates that the function does not require additional data to perform its task. • Every function’s body is delimited by left and right braces ({ and }). • Typically, you cannot call a member function until you create an object of its class. • Each new class you create becomes a new type in C++.

96

Chapter 3

Introduction to Classes, Objects and Strings

• In the UML, each class is modeled in a class diagram (p. 68) as a rectangle with three compartments, which (top to bottom) contain the class’s name, attributes and operations, respectively. • The UML models operations as the operation name followed by parentheses. A plus sign (+) preceding the name indicates a public operation (i.e., a public member function in C++).

Section 3.3 Defining a Member Function with a Parameter

• A member function can require one or more parameters (p. 68) that represent additional data it needs to perform its task. A function call supplies an argument (p. 68) for each function parameter. • A member function is called by following the object name with a dot (.) operator (p. 67), the function name and a set of parentheses containing the function’s arguments. • A variable of C++ Standard Library class string (p. 69) represents a string of characters. This class is defined in header , and the name string belongs to namespace std. • Function getline (from header , p. 70) reads characters from its first argument until a newline character is encountered, then places the characters (not including the newline) in the string variable specified as its second argument. The newline character is discarded. • A parameter list (p. 70) may contain any number of parameters, including none at all (represented by empty parentheses) to indicate that a function does not require any parameters. • The number of arguments in a function call must match the number of parameters in the parameter list of the called member function’s header. Also, the argument types in the function call must be consistent with the types of the corresponding parameters in the function header. • The UML models a parameter of an operation by listing the parameter name, followed by a colon and the parameter type between the parentheses following the operation name. • The UML has its own data types. Not all the UML data types have the same names as the corresponding C++ types. The UML type String corresponds to the C++ type string.

Section 3.4 Data Members, set Functions and get Functions

• Variables declared in a function’s body are local variables (p. 71) and can be used only from the point of their declaration in the function to the immediately following closing right brace (}). • A local variable must be declared before it can be used in a function. A local variable cannot be accessed outside the function in which it’s declared. • Data members (p. 71) normally are private (p. 73). Variables or functions declared private are accessible only to member functions of the class in which they’re declared, or to friends of the class. • When a program creates (instantiates) an object, its private data members are encapsulated (hidden, p. 74) in the object and can be accessed only by member functions of the object’s class. • When a function that specifies a return type other than void is called and completes its task, the function returns a result to its calling function. • By default, the initial value of a string is the empty string (p. 75)—i.e., a string that does not contain any characters. Nothing appears on the screen when an empty string is displayed. • A class often provides public member functions to allow the class’s clients to set or get (p. 75) private data members. The names of these member functions normally begin with set or get. • Set and get functions allow clients of a class to indirectly access the hidden data. The client does not know how the object performs these operations. • A class’s set and get functions should be used by other member functions of the class to manipulate the class’s private data. If the class’s data representation is changed, member functions that access the data only via the set and get functions will not require modification. • A public set function should carefully scrutinize any attempt to modify the value of a data member to ensure that the new value is appropriate for that data item.

Self-Review Exercises

97

• The UML represents data members as attributes by listing the attribute name, followed by a colon and the attribute type. Private attributes are preceded by a minus sign (–) in the UML. • The UML indicates the return type of an operation by placing a colon and the return type after the parentheses following the operation name. • UML class diagrams do not specify return types for operations that do not return values.

Section 3.5 Initializing Objects with Constructors

• Each class should provide a constructor (p. 77) to initialize an object of the class when the object is created. A constructor must be defined with the same name as the class. • A difference between constructors and functions is that constructors cannot return values, so they cannot specify a return type (not even void). Normally, constructors are declared public. • C++ requires a constructor call at the time each object is created, which helps ensure that every object is initialized before it’s used in a program. • A constructor with no parameters is a default constructor (p. 77). If you do not provide a constructor, the compiler provides a default constructor. You can also define a default constructor explicitly. If you define a constructor for a class, C++ will not create a default constructor. • The UML models constructors as operations in a class diagram’s third compartment with the word “constructor” between guillemets (« and ») before the constructor’s name.

Section 3.6 Placing a Class in a Separate File for Reusability

• Class definitions, when packaged properly, can be reused by programmers worldwide. • It’s customary to define a class in a header (p. 81) that has a .h filename extension.

Section 3.7 Separating Interface from Implementation

• If the class’s implementation changes, the class’s clients should not be required to change. • Interfaces define and standardize the ways in which things such as people and systems interact. • A class’s public interface (p. 85) describes the public member functions that are made available to the class’s clients. The interface describes what services (p. 85) clients can use and how to request those services, but does not specify how the class carries out the services. • Separating interface from implementation (p. 84) makes programs easier to modify. Changes in the class’s implementation do not affect the client as long as the class’s interface remains unchanged. • A function prototype (p. 85) contains a function’s name, its return type and the number, types and order of the parameters the function expects to receive. • Once a class is defined and its member functions are declared (via function prototypes), the member functions should be defined in a separate source-code file. • For each member function defined outside of its corresponding class definition, the function name must be preceded by the class name and the binary scope resolution operator (::, p. 86).

Section 3.8 Validating Data with set Functions

• Class string’s length member function (p. 91) returns the number of characters in a string. • Class string’s member function substr (p. 92) returns a new string containing a copy of part of an existing string. The first argument specifies the starting position in the original string. The second specifies the number of characters to copy.

Self-Review Exercises 3.1

Fill in the blanks in each of the following: a) Every class definition contains the keyword class’s name. b) A class definition is typically stored in a file with the

followed immediately by the filename extension.

98

Chapter 3

Introduction to Classes, Objects and Strings

c) Each parameter in a function header specifies both a(n) and a(n) . d) When each object of a class maintains its own copy of an attribute, the variable that represents the attribute is also known as a(n) . e) Keyword public is a(n) . f) Return type indicates that a function will perform a task but will not return any information when it completes its task. from the library reads characters until a newline character g) Function is encountered, then copies those characters into the specified string. h) When a member function is defined outside the class definition, the function header must include the class name and the , followed by the function name to “tie” the member function to the class definition. i) The source-code file and any other files that use a class can include the class’s header via preprocessor directive. a(n) 3.2

State whether each of the following is true or false. If false, explain why. a) By convention, function names begin with a capital letter and all subsequent words in the name begin with a capital letter. b) Empty parentheses following a function name in a function prototype indicate that the function does not require any parameters to perform its task. c) Data members or member functions declared with access specifier private are accessible to member functions of the class in which they’re declared. d) Variables declared in the body of a particular member function are known as data members and can be used in all member functions of the class. e) Every function’s body is delimited by left and right braces ({ and }). f) Any source-code file that contains int main() can be used to execute a program. g) The types of arguments in a function call must be consistent with the types of the corresponding parameters in the function prototype’s parameter list.

3.3

What is the difference between a local variable and a data member?

3.4 Explain the purpose of a function parameter. What’s the difference between a parameter and an argument?

Answers to Self-Review Exercises 3.1 a) class. b) .h. c) type, name. d) data member. e) access specifier. f) h) binary scope resolution operator (::). i) #include.

void.

g) getline.

3.2 a) False. Function names begin with a lowercase letter and all subsequent words in the name begin with a capital letter. b) True. c) True. d) False. Such variables are local variables and can be used only in the member function in which they’re declared. e) True. f) True. g) True. 3.3 A local variable is declared in the body of a function and can be used only from its declaration to the closing brace of the block in which it’s declared. A data member is declared in a class, but not in the body of any of the class’s member functions. Every object of a class has a separate copy of the class’s data members. Data members are accessible to all member functions of the class. 3.4 A parameter represents additional information that a function requires to perform its task. Each parameter required by a function is specified in the function header. An argument is the value supplied in the function call. When the function is called, the argument value is passed into the function parameter so that the function can perform its task.

Exercises 3.5 (Function Prototypes and Definitions) Explain the difference between a function prototype and a function definition.

Exercises

99

3.6 (Default Constructor) What’s a default constructor? How are an object’s data members initialized if a class has only an implicitly defined default constructor? 3.7

(Data Members) Explain the purpose of a data member.

3.8 (Header and Source-Code Files) What’s a header? What’s a source-code file? Discuss the purpose of each. 3.9 (Using a Class Without a using Directive) Explain how a program could use class string without inserting a using directive. 3.10 (Set and Get Functions) Explain why a class might provide a set function and a get function for a data member. 3.11

(Modifying Class GradeBook) Modify class GradeBook (Figs. 3.11–3.12) as follows: a) Include a second string data member that represents the course instructor’s name. b) Provide a set function to change the instructor’s name and a get function to retrieve it. c) Modify the constructor to specify course name and instructor name parameters. d) Modify function displayMessage to output the welcome message and course name, then the string "This course is presented by: " followed by the instructor’s name. Use your modified class in a test program that demonstrates the class’s new capabilities.

3.12 (Account Class) Create an Account class that a bank might use to represent customers’ bank accounts. Include a data member of type int to represent the account balance. [Note: In subsequent chapters, we’ll use numbers that contain decimal points (e.g., 2.75)—called floating-point values— to represent dollar amounts.] Provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it’s greater than or equal to 0. If not, set the balance to 0 and display an error message indicating that the initial balance was invalid. Provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account’s balance. If it does, the balance should be left unchanged and the function should print a message indicating "Debit amount exceeded account balance." Member function getBalance should return the current balance. Create a program that creates two Account objects and tests the member functions of class Account. 3.13 (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four data members—a part number (type string), a part description (type string), a quantity of the item being purchased (type int) and a price per item (type int). [Note: In subsequent chapters, we’ll use numbers that contain decimal points (e.g., 2.75)—called floating-point values—to represent dollar amounts.] Your class should have a constructor that initializes the four data members. Provide a set and a get function for each data member. In addition, provide a member function named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as an int value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a test program that demonstrates class Invoice’s capabilities. 3.14 (Employee Class) Create a class called Employee that includes three pieces of information as data members—a first name (type string), a last name (type string) and a monthly salary (type int). [Note: In subsequent chapters, we’ll use numbers that contain decimal points (e.g., 2.75)— called floating-point values—to represent dollar amounts.] Your class should have a constructor that initializes the three data members. Provide a set and a get function for each data member. If the monthly salary is not positive, set it to 0. Write a test program that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10 percent raise and display each Employee’s yearly salary again.

100

Chapter 3 Introduction to Classes, Objects and Strings

3.15 (Date Class) Create a class called Date that includes three pieces of information as data members—a month (type int), a day (type int) and a year (type int). Your class should have a constructor with three parameters that uses the parameters to initialize the three data members. For the purpose of this exercise, assume that the values provided for the year and day are correct, but ensure that the month value is in the range 1–12; if it isn’t, set the month to 1. Provide a set and a get function for each data member. Provide a member function displayDate that displays the month, day and year separated by forward slashes (/). Write a test program that demonstrates class Date’s capabilities.

Making a Difference 3.16 (Target-Heart-Rate Calculator) While exercising, you can use a heart-rate monitor to see that your heart rate stays within a safe range suggested by your trainers and doctors. According to the American Heart Association (AHA) (www.americanheart.org/presenter.jhtml?identifier=4736), the formula for calculating your maximum heart rate in beats per minute is 220 minus your age in years. Your target heart rate is a range that is 50–85% of your maximum heart rate. [Note: These formulas are estimates provided by the AHA. Maximum and target heart rates may vary based on the health, fitness and gender of the individual. Always consult a physician or qualified health care professional before beginning or modifying an exercise program.] Create a class called HeartRates. The class attributes should include the person’s first name, last name and date of birth (consisting of separate attributes for the month, day and year of birth). Your class should have a constructor that receives this data as parameters. For each attribute provide set and get functions. The class also should include a function getAge that calculates and returns the person’s age (in years), a function getMaxiumumHeartRate that calculates and returns the person’s maximum heart rate and a function getTargetHeartRate that calculates and returns the person’s target heart rate. Since you do not yet know how to obtain the current date from the computer, function getAge should prompt the user to enter the current month, day and year before calculating the person’s age. Write an application that prompts for the person’s information, instantiates an object of class HeartRates and prints the information from that object— including the person’s first name, last name and date of birth—then calculates and prints the person’s age in (years), maximum heart rate and target-heart-rate range. 3.17 (Computerization of Health Records) A health care issue that has been in the news lately is the computerization of health records. This possibility is being approached cautiously because of sensitive privacy and security concerns, among others. [We address such concerns in later exercises.] Computerizing health records could make it easier for patients to share their health profiles and histories among their various health care professionals. This could improve the quality of health care, help avoid drug conflicts and erroneous drug prescriptions, reduce costs and in emergencies, could save lives. In this exercise, you’ll design a “starter” HealthProfile class for a person. The class attributes should include the person’s first name, last name, gender, date of birth (consisting of separate attributes for the month, day and year of birth), height (in inches) and weight (in pounds). Your class should have a constructor that receives this data. For each attribute, provide set and get functions. The class also should include functions that calculate and return the user’s age in years, maximum heart rate and target-heart-rate range (see Exercise 3.16), and body mass index (BMI; see Exercise 2.30). Write an application that prompts for the person’s information, instantiates an object of class HealthProfile for that person and prints the information from that object—including the person’s first name, last name, gender, date of birth, height and weight—then calculates and prints the person’s age in years, BMI, maximum heart rate and target-heart-rate range. It should also display the “BMI values” chart from Exercise 2.30. Use the same technique as Exercise 3.16 to calculate the person’s age.

4

Control Statements: Part 1

Let’s all move one place on. —Lewis Carroll

The wheel is come full circle. —William Shakespeare

How many apples fell on Newton’s head before he took the hint! —Robert Frost

All the evolution we know of proceeds from the vague to the definite. —Charles Sanders Peirce

Objectives

In this chapter you’ll learn: ■











Basic problem-solving techniques. To develop algorithms through the process of topdown, stepwise refinement. To use the if and if…else selection statements to choose among alternative actions. To use the while repetition statement to execute statements in a program repeatedly. Counter-controlled repetition and sentinel-controlled repetition. To use the increment, decrement and assignment operators.

102

Chapter 4 Control Statements: Part 1

4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Selection Statement 4.6 if…else Double-Selection Statement 4.7 while Repetition Statement

4.8 Formulating Algorithms: CounterControlled Repetition 4.9 Formulating Algorithms: SentinelControlled Repetition 4.10 Formulating Algorithms: Nested Control Statements 4.11 Assignment Operators 4.12 Increment and Decrement Operators 4.13 Wrap-Up

Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference

4.1 Introduction Before writing a program to solve a problem, we must have a thorough understanding of the problem and a carefully planned approach to solving it. When writing a program, we must also understand the available building blocks and employ proven program construction techniques. In this chapter and in Chapter 5, Control Statements: Part 2, we discuss these issues as we present the theory and principles of structured programming. The concepts presented here are crucial to building effective classes and manipulating objects. In this chapter, we introduce C++’s if, if…else and while statements, three of the building blocks that allow you to specify the logic required for member functions to perform their tasks. We devote a portion of this chapter (and Chapters 5 and 7) to further developing the GradeBook class. In particular, we add a member function to the GradeBook class that uses control statements to calculate the average of a set of student grades. Another example demonstrates additional ways to combine control statements. We introduce C++’s assignment, increment and decrement operators. These additional operators abbreviate and simplify many program statements.

4.2 Algorithms Any solvable computing problem can be solved by executing of a series of actions in a specific order. A procedure for solving a problem in terms of 1. the actions to execute and 2. the order in which the actions execute is called an algorithm. The following example demonstrates that correctly specifying the order in which the actions execute is important. Consider the “rise-and-shine algorithm” followed by one junior executive for getting out of bed and going to work: (1) Get out of bed, (2) take off pajamas, (3) take a shower, (4) get dressed, (5) eat breakfast, (6) carpool to work. This routine gets the executive to work prepared to make critical decisions. Suppose the same steps are performed in a different order: (1) Get out of bed, (2) take off pajamas, (3) get dressed, (4) take a shower, (5) eat breakfast, (6) carpool to work. In this case, our junior executive shows up for work

4.3 Pseudocode

103

soaking wet. Specifying the order in which statements (actions) execute is called program control. This chapter investigates program control using C++’s control statements.

4.3 Pseudocode Pseudocode (or “fake” code) is an artificial and informal language that helps you develop algorithms without having to worry about the details of C++ language syntax. The pseudocode we present is helpful for developing algorithms that will be converted to structured C++ programs. Pseudocode is similar to everyday English; it’s convenient and user friendly, although it isn’t an actual computer programming language. Pseudocode does not execute on computers. Rather, it helps you “think out” a program before attempting to write it in a programming language, such as C++. The style of pseudocode we present consists purely of characters, so you can type pseudocode conveniently, using any editor program. A carefully prepared pseudocode program can easily be converted to a corresponding C++ program. In many cases, this simply requires replacing pseudocode statements with C++ equivalents. Pseudocode normally describes only executable statements, which cause specific actions to occur after you convert a program from pseudocode to C++ and the program is compiled and run on a computer. Declarations (that do not have initializers or do not involve constructor calls) are not executable statements. For example, the declaration int counter;

tells the compiler the type of variable counter and instructs the compiler to reserve space in memory for the variable. This declaration does not cause any action—such as input, output or a calculation—to occur when the program executes. We typically do not include variable declarations in our pseudocode. Some programmers choose to list variables and mention their purposes at the beginning of pseudocode programs. Let’s look at an example of pseudocode that may be written to help a programmer create the addition program of Fig. 2.5. This pseudocode (Fig. 4.1) corresponds to the algorithm that inputs two integers from the user, adds these integers and displays their sum. We show the complete pseudocode listing here—we’ll show how to create pseudocode from a problem statement later in the chapter. Lines 1–2 correspond to the statements in lines 13–14 of Fig. 2.5. Notice that the pseudocode statements are simply English statements that convey what task is to be performed in C++. Likewise, lines 4–5 correspond to the statements in lines 16–17 of Fig. 2.5 and lines 7–8 correspond to the statements in lines 19 and 21 of Fig. 2.5. 1 2 3 4 5 6 7 8

Prompt the user to enter the first integer Input the first integer Prompt the user to enter the second integer Input the second integer Add first integer and second integer, store result Display result

Fig. 4.1 | Pseudocode for the addition program of Fig. 2.5.

104

Chapter 4 Control Statements: Part 1

4.4 Control Structures Normally, statements in a program execute one after the other in the order in which they’re written. This is called sequential execution. Various C++ statements we’ll soon discuss enable you to specify that the next statement to execute may be other than the next one in sequence. This is called transfer of control. During the 1960s, it became clear that the indiscriminate use of transfers of control was the root of much difficulty experienced by software development groups. The finger of blame was pointed at the goto statement, which allows you to specify a transfer of control to one of a wide range of possible destinations in a program (creating what’s often called “spaghetti code”). The notion of so-called structured programming became almost synonymous with “goto elimination.” The research of Böhm and Jacopini1 demonstrated that programs could be written without any goto statements. It became the challenge of the era for programmers to shift their styles to “goto-less programming.” It was not until the 1970s that programmers started taking structured programming seriously. The results have been impressive, as software development groups have reported reduced development times, more frequent ontime delivery of systems and more frequent within-budget completion of software projects. The key to these successes is that structured programs are clearer, are easier to debug, test and modify and are more likely to be bug-free in the first place. Böhm and Jacopini’s work demonstrated that all programs could be written in terms of only three control structures, namely, the sequence structure, the selection structure and the repetition structure. The term “control structures” comes from the field of computer science. When we introduce C++’s implementations of control structures, we’ll refer to them in the terminology of the C++ standard document as “control statements.”

Sequence Structure in C++ The sequence structure is built into C++. Unless directed otherwise, the computer executes C++ statements one after the other in the order in which they’re written—that is, in sequence. The UML activity diagram of Fig. 4.2 illustrates a typical sequence structure in which two calculations are performed in order. C++ allows you to have as many actions as

add grade to total

add 1 to counter

Corresponding C++ statement: total = total + grade;

Corresponding C++ statement: counter = counter + 1;

Fig. 4.2 | Sequence-structure activity diagram. 1.

Böhm, C., and G. Jacopini, “Flow Diagrams, Turing Machines, and Languages with Only Two Formation Rules,” Communications of the ACM, Vol. 9, No. 5, May 1966, pp. 366–371.

4.4 Control Structures

105

you want in a sequence structure. As you’ll soon see, anywhere a single action may be placed, you may place several actions in sequence. In this figure, the two statements add a grade to a total variable and add the value 1 to a counter variable. Such statements might appear in a program that averages several student grades. To calculate an average, the total of the grades being averaged is divided by the number of grades. A counter variable would be used to keep track of the number of values being averaged. You’ll see similar statements in the program of Section 4.8. An activity diagram models the workflow (also called the activity) of a portion of a software system. Such workflows may include a portion of an algorithm, such as the sequence structure in Fig. 4.2. Activity diagrams are composed of special-purpose symbols, such as action state symbols (a rectangle with its left and right sides replaced with arcs curving outward), diamonds and small circles; these symbols are connected by transition arrows, which represent the flow of the activity. Activity diagrams clearly show how control structures operate. Consider the sequence-structure activity diagram of Fig. 4.2. It contains two action states that represent actions to perform. Each action state contains an action expression—e.g., “add grade to total” or “add 1 to counter”—that specifies a particular action to perform. Other actions might include calculations or input/output operations. The arrows in the activity diagram are called transition arrows. These arrows represent transitions, which indicate the order in which the actions represented by the action states occur—the program that implements the activities illustrated by the activity diagram in Fig. 4.2 first adds grade to total, then adds 1 to counter. The solid circle at the top of the diagram represents the activity’s initial state—the beginning of the workflow before the program performs the modeled activities. The solid circle surrounded by a hollow circle that appears at the bottom of the activity diagram represents the final state—the end of the workflow after the program performs its activities. Figure 4.2 also includes rectangles with the upper-right corners folded over. These are called notes in the UML—explanatory remarks that describe the purpose of symbols in the diagram. Figure 4.2 uses UML notes to show the C++ code associated with each action state in the activity diagram. A dotted line connects each note with the element that the note describes. Activity diagrams normally do not show the C++ code that implements the activity. We use notes for this purpose here to illustrate how the diagram relates to C++ code. For more information on the UML, see our optional case study, which appears in Chapters 25–26, and visit our UML Resource Center at www.deitel.com/UML/.

Selection Statements in C++ C++ provides three types of selection statements (discussed in this chapter and Chapter 5). The if selection statement either performs (selects) an action if a condition is true or skips the action if the condition is false. The if…else selection statement performs an action if a condition is true or performs a different action if the condition is false. The switch selection statement (Chapter 5) performs one of many different actions, depending on the value of an integer expression. The if selection statement is a single-selection statement because it selects or ignores a single action (or, as you’ll soon see, a single group of actions). The if…else statement is called a double-selection statement because it selects between two different actions (or groups of actions). The switch selection statement is called a multiple-selection statement because it selects among many different actions (or groups of actions).

106

Chapter 4 Control Statements: Part 1

Repetition Statements in C++ C++ provides three types of repetition statements (also called looping statements or loops) for performing statements repeatedly while a condition (called the loop-continuation condition) remains true. These are the while, do…while and for statements. (Chapter 5 presents the do…while and for statements.) The while and for statements perform the action (or group of actions) in their bodies zero or more times—if the loop-continuation condition is initially false, the action (or group of actions) will not execute. The do…while statement performs the action (or group of actions) in its body at least once. Each of the words if, else, switch, while, do and for is a C++ keyword. Keywords must not be used as identifiers, such as variable names, and must be spelled with only lowercase letters. Figure 4.3 provides a complete list of C++ keywords.

Common Programming Error 4.1

Using a keyword as an identifier is a syntax error.

C++ Keywords Keywords common to the C and C++ programming languages auto

break

case

char

const

continue

default

do

double

else

enum

extern

float

for

goto

if

int

long

register

return

short

signed

sizeof

static

struct

switch

typedef

union

unsigned

void

volatile

while

C++-only keywords and

and_eq

asm

bitand

bitor

bool

catch

class

compl

const_cast

delete

dynamic_cast

explicit

export

false

friend

inline

mutable

namespace

new

not

not_eq

operator

or

or_eq

private

protected

public

reinterpret_cast

static_cast

template

this

throw

true

try

typeid

typename

using

virtual

wchar_t

xor

xor_eq

C++0x keywords alignof

axiom

char16_t

char32_t

concept

concept_map

constexpr

decltype

late_check

nullptr

requires

static_assert

thread_local

Fig. 4.3 | C++ keywords. Summary of Control Statements in C++ C++ has only three kinds of control structures, which from this point forward we refer to as control statements: the sequence statement, selection statements (three types—if,

4.5 if Selection Statement if…else

and

switch)

and repetition statements (three types—while,

107 for

and

do…while). Each program combines these control statements as appropriate for the algo-

rithm the program implements. We can model each control statement as an activity diagram with initial and final states that represent a control statement’s entry point and exit point, respectively. These single-entry/single-exit control statements make it easy to build programs—control statements are attached to one another by connecting the exit point of one to the entry point of the next. This is similar to the way a child stacks building blocks, so we call this control-statement stacking. You’ll see that there’s only one other way to connect control statements—called control-statement nesting, in which one control statement is contained inside another.

Software Engineering Observation 4.1

Any C++ program can be constructed from only seven different types of control statements (sequence, if, if…else, switch, while, do…while and for) combined in only two ways (control-statement stacking and control-statement nesting).

4.5 if Selection Statement Programs use selection statements to choose among alternative courses of action. For example, suppose the passing grade on an exam is 60. The pseudocode statement If student’s grade is greater than or equal to 60 Print “Passed” determines whether the condition “student’s grade is greater than or equal to 60” is true or false. If the condition is true, “Passed” is printed and the next pseudocode statement in order is “performed” (remember that pseudocode is not a real programming language). If the condition is false, the print statement is ignored and the next pseudocode statement in order is performed. The indentation of the second line is optional, but it’s recommended because it emphasizes the inherent structure of structured programs. The preceding pseudocode If statement can be written in C++ as if ( grade >= 60 ) cout << "Passed";

The C++ code corresponds closely to the pseudocode. This is one of the properties of pseudocode that make it such a useful program development tool. Figure 4.4 illustrates the single-selection if statement. It contains what is perhaps the most important symbol in an activity diagram—the diamond or decision symbol, which indicates that a decision is to be made. A decision symbol indicates that the workflow will continue along a path determined by the symbol’s associated guard conditions, which can be true or false. Each transition arrow emerging from a decision symbol has a guard condition (specified in square brackets above or next to the transition arrow). If a particular guard condition is true, the workflow enters the action state to which that transition arrow points. In Fig. 4.4, if the grade is greater than or equal to 60, the program prints “Passed” to the screen, then transitions to the final state of this activity. If the grade is less than 60, the program immediately transitions to the final state without displaying a message.

108

Chapter 4 Control Statements: Part 1

[grade >= 60]

print “Passed”

[grade < 60]

Fig. 4.4 |

if

single-selection statement activity diagram.

You saw in Chapter 2 that decisions can be based on conditions containing relational or equality operators. Actually, in C++, a decision can be based on any expression—if the expression evaluates to zero, it’s treated as false; if the expression evaluates to nonzero, it’s treated as true. C++ provides the data type bool for variables that can hold only the values true and false—each of these is a C++ keyword.

Portability Tip 4.1

For compatibility with earlier versions of C, which used integers for Boolean values, the bool value true also can be represented by any nonzero value (compilers typically use 1) and the bool value false also can be represented as the value zero.

The if statement is a single-entry/single-exit statement. We’ll see that the activity diagrams for the remaining control statements also contain initial states, transition arrows, action states that indicate actions to perform, decision symbols (with associated guard conditions) that indicate decisions to be made and final states. Envision seven bins, each containing only empty UML activity diagrams of one of the seven types of control statements. Your task, then, is assembling a program from the activity diagrams of as many of each type of control statement as the algorithm demands, combining the activity diagrams in only two possible ways (stacking or nesting), then filling in the action states and decisions with action expressions and guard conditions in a manner appropriate to form a structured implementation for the algorithm. We’ll continue discussing the variety of ways in which actions and decisions may be written.

4.6 if…else Double-Selection Statement The if single-selection statement performs an indicated action only when the condition is true; otherwise the action is skipped. The if…else double-selection statement allows you to specify an action to perform when the condition is true and a different action to perform when the condition is false. For example, the pseudocode statement If student’s grade is greater than or equal to 60 Print “Passed” Else Print “Failed”

4.6 if…else Double-Selection Statement

109

prints “Passed” if the student’s grade is greater than or equal to 60, but prints “Failed” if the student’s grade is less than 60. In either case, after printing occurs, the next pseudocode statement in sequence is “performed.” The preceding pseudocode If…Else statement can be written in C++ as if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

The body of the else is also indented.

Good Programming Practice 4.1

Whatever indentation convention you choose should be applied consistently. It’s difficult to read programs that do not obey uniform spacing conventions.

Good Programming Practice 4.2

If there are several levels of indentation, each level should be indented the same additional amount of space to promote readability and maintainability.

Figure 4.5 illustrates the the if…else statement’s flow of control.

print “Failed”

Fig. 4.5 |

if…else

[grade < 60]

[grade >= 60]

print “Passed”

double-selection statement activity diagram.

Conditional Operator (?:) C++ provides the conditional operator (?:), which is closely related to the if…else statement. The conditional operator is C++’s only ternary operator—it takes three operands. The operands, together with the conditional operator, form a conditional expression. The first operand is a condition, the second operand is the value for the entire conditional expression if the condition is true and the third operand is the value for the entire conditional expression if the condition is false. For example, the output statement cout << ( grade >= 60 ? "Passed" : "Failed" );

contains a conditional expression, grade >= 60 ? "Passed" : "Failed", that evaluates to the string "Passed" if the condition grade >= 60 is true, but evaluates to "Failed" if the condition is false. Thus, the statement with the conditional operator performs essentially the same as the preceding if…else statement. As we’ll see, the precedence of the conditional operator is low, so the parentheses in the preceding expression are required.

110

Chapter 4 Control Statements: Part 1

Error-Prevention Tip 4.1

To avoid precedence problems (and for clarity), place conditional expressions (that appear in larger expressions) in parentheses.

The values in a conditional expression also can be actions to execute. For example, the following conditional expression also prints "Passed" or "Failed": grade >= 60 ? cout << "Passed" : cout << "Failed";

The preceding conditional expression is read, “If grade is greater than or equal to 60, then cout << "Passed"; otherwise, cout << "Failed".” This, too, is comparable to the preceding if…else statement. Conditional expressions can appear in some program locations where if…else statements cannot.

Nested if…else Statements Nested if…else statements test for multiple cases by placing if…else selection statements inside other if…else selection statements. For example, the following pseudocode if…else statement prints A for exam grades greater than or equal to 90, B for grades in the range 80 to 89, C for grades in the range 70 to 79, D for grades in the range 60 to 69 and F for all other grades: If student’s grade is greater than or equal to 90 Print “A” Else If student’s grade is greater than or equal to 80 Print “B” Else If student’s grade is greater than or equal to 70 Print “C” Else If student’s grade is greater than or equal to 60 Print “D” Else Print “F” This pseudocode can be written in C++ as if ( studentGrade >= 90 ) // 90 and above gets "A" cout << "A"; else if ( studentGrade >= 80 ) // 80-89 gets "B" cout << "B"; else if ( studentGrade >= 70 ) // 70-79 gets "C" cout << "C"; else if ( studentGrade >= 60 ) // 60-69 gets "D" cout << "D"; else // less than 60 gets "F" cout << "F";

4.6 if…else Double-Selection Statement

111

If studentGrade is greater than or equal to 90, the first four conditions are true, but only the output statement after the first test executes. Then, the program skips the else-part of the “outermost” if…else statement. Most write the preceding if…else statement as if ( studentGrade >= 90 ) cout << "A"; else if ( studentGrade >= cout << "B"; else if ( studentGrade >= cout << "C"; else if ( studentGrade >= cout << "D"; else // less than 60 gets cout << "F";

// 90 and above gets "A" 80 ) // 80-89 gets "B" 70 ) // 70-79 gets "C" 60 ) // 60-69 gets "D" "F"

The two forms are identical except for the spacing and indentation, which the compiler ignores. The latter form is popular because it avoids deep indentation of the code to the right, which can force lines to wrap.

Performance Tip 4.1

A nested if…else statement can perform much faster than a series of single-selection if statements because of the possibility of early exit after one of the conditions is satisfied.

Performance Tip 4.2

In a nested if…else statement, test the conditions that are more likely to be true at the beginning of the nested statement. This will enable the nested if…else statement to run faster by exiting earlier than if infrequently occurring cases were tested first.

Dangling-else Problem The C++ compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({ and }). This behavior can lead to what’s referred to as the dangling-else problem. For example, if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5"; else cout << "x is <= 5";

appears to indicate that if x is greater than 5, the nested if statement determines whether is also greater than 5. If so, "x and y are > 5" is output. Otherwise, it appears that if x is not greater than 5, the else part of the if…else outputs "x is <= 5". Beware! This nested if…else statement does not execute as it appears. The compiler actually interprets the statement as y

if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5"; else cout << "x is <= 5";

in which the body of the first if is a nested if…else. The outer if statement tests whether x is greater than 5. If so, execution continues by testing whether y is also greater than 5.

112

Chapter 4 Control Statements: Part 1

If the second condition is true, the proper string—"x and y are > 5"—is displayed.7However, if the second condition is false, the string "x is <= 5" is displayed, even though we know that x is greater than 5. To force the nested if…else statement to execute as originally intended, we can write it as follows: if ( x > 5 ) { if ( y > 5 ) cout << "x and y are > 5"; } else cout << "x is <= 5";

The braces ({}) indicate to the compiler that the second if statement is in the body of the first if and that the else is associated with the first if. Exercises 4.23–4.24 further investigate the dangling-else problem.

Blocks The if selection statement expects only one statement in its body. Similarly, the if and else parts of an if…else statement each expect only one body statement. To include several statements in the body of an if or in either part of an if…else, enclose the statements in braces ({ and }). A set of statements contained within a pair of braces is called a compound statement or a block. We use the term “block” from this point forward.

Software Engineering Observation 4.2

A block can be placed anywhere in a program that a single statement can be placed.

The following example includes a block in the else part of an if…else statement. if ( studentGrade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; }

In this case, if studentGrade is less than 60, the program executes both statements in the body of the else and prints Failed. You must take this course again.

Notice the braces surrounding the two statements in the else clause. These braces are important. Without the braces, the statement cout << "You must take this course again.\n";

would be outside the body of the else part of the if and would execute regardless of whether the grade was less than 60. This is a logic error.

4.7 while Repetition Statement

113

Just as a block can be placed anywhere a single statement can be placed, it’s also possible to have no statement at all, which is called a null statement or an empty statement. The null statement is represented by placing a semicolon (;) where a statement would normally be.

Common Programming Error 4.2

Placing a semicolon after the condition in an if statement leads to a logic error in singleselection if statements and a syntax error in double-selection if…else statements (when the if part contains an actual body statement).

4.7 while Repetition Statement A repetition statement specifies that a program should repeat an action while some condition remains true. The pseudocode statement While there are more items on my shopping list Purchase next item and cross it off my list describes the repetition that occurs during a shopping trip. The condition, “there are more items on my shopping list” is either true or false. If it’s true, then the action, “Purchase next item and cross it off my list” is performed. This action will be performed repeatedly while the condition remains true. The statement contained in the While repetition statement constitutes the body of the While, which can be a single statement or a block. Eventually, the condition will become false (when the last item on the shopping list has been purchased and crossed off the list). At this point, the repetition terminates, and the first pseudocode statement after the repetition statement executes. As an example of C++’s while repetition statement, consider a program segment designed to find the first power of 3 larger than 100. Suppose the integer variable product has been initialized to 3. When the following while repetition statement finishes executing, product contains the result: int product = 3; while ( product <= 100 ) product = 3 * product;

When the while statement begins execution, product’s value is 3. Each repetition multiplies product by 3, so product takes on the values 9, 27, 81 and 243 successively. When product becomes 243, the while statement condition—product <= 100—becomes false. This terminates the repetition, so the final value of product is 243. At this point, program execution continues with the next statement after the while statement.

Common Programming Error 4.3

Not providing, in the body of a while statement, an action that eventually causes the condition in the while to become false normally results in a logic error called an infinite loop, in which the repetition statement never terminates. This can make a program appear to “hang” or “freeze” if the loop body does not contain statements that interact with the user.

The UML activity diagram of Fig. 4.6 illustrates the flow of control that corresponds to the preceding while statement. Once again, the symbols in the diagram (besides the initial state, transition arrows, a final state and three notes) represent an action state and a

114

Chapter 4 Control Statements: Part 1

decision. This diagram also introduces the UML’s merge symbol, which joins two flows of activity into one flow of activity. The UML represents both the merge symbol and the decision symbol as diamonds. In this diagram, the merge symbol joins the transitions from the initial state and from the action state, so they both flow into the decision that determines whether the loop should begin (or continue) executing. The decision and merge symbols can be distinguished by the number of “incoming” and “outgoing” transition arrows. A decision symbol has one transition arrow pointing to the diamond and two or more transition arrows pointing out from the diamond to indicate possible transitions from that point. In addition, each transition arrow pointing out of a decision symbol has a guard condition next to it. A merge symbol has two or more transition arrows pointing to the diamond and only one transition arrow pointing from the diamond, to indicate multiple activity flows merging to continue the activity. Unlike the decision symbol, the merge symbol does not have a counterpart in C++ code.

merge

decision

[product <= 100]

triple product value

[product > 100] Corresponding C++ statement: product = 3 * product;

Fig. 4.6 |

while

repetition statement UML activity diagram.

The diagram of Fig. 4.6 clearly shows the repetition of the while statement discussed earlier in this section. The transition arrow emerging from the action state points to the merge, which transitions back to the decision that’s tested each time through the loop until the guard condition product > 100 becomes true. Then the while statement exits (reaches its final state) and control passes to the next statement in sequence in the program.

Performance Tip 4.3

A small performance improvement for code that executes many times in a loop can result in substantial overall performance improvement.

4.8 Formulating Algorithms: Counter-Controlled Repetition To illustrate how programmers develop algorithms, this section and Section 4.9 solve two variations of a class average problem. Consider the following problem statement: A class of ten students took a quiz. The grades (0 to 100) for this quiz are available to you. Calculate and display the total of the grades and the class average.

4.8 Formulating Algorithms: Counter-Controlled Repetition

115

The class average is equal to the sum of the grades divided by the number of students. The algorithm for solving this problem on a computer must input each of the grades, calculate the average and print the result.

Pseudocode Algorithm with Counter-Controlled Repetition Let’s use pseudocode to list the actions to execute and specify the order in which these actions should occur. We use counter-controlled repetition to input the grades one at a time. This technique uses a variable called a counter to control the number of times a group of statements will execute (also known as the number of iterations of the loop). Counter-controlled repetition is often called definite repetition because the number of repetitions is known before the loop begins executing. In this example, repetition terminates when the counter exceeds 10. This section presents a fully developed pseudocode algorithm (Fig. 4.7) and a version of class GradeBook (Fig. 4.8–Fig. 4.9) that implements the algorithm in a C++ member function. The section then presents an application (Fig. 4.10) that demonstrates the algorithm in action. In Section 4.9 we demonstrate how to use pseudocode to develop such an algorithm from scratch.

Software Engineering Observation 4.3

Experience has shown that the most difficult part of solving a problem on a computer is developing the algorithm for the solution. The process of producing a working C++ program from the algorithm is typically straightforward. 1 2 3 4 5 6 7 8 9 10 11 12

Set total to zero Set grade counter to one While grade counter is less than or equal to ten Prompt the user to enter the next grade Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the total of the grades for all students in the class Print the class average

Fig. 4.7 | Pseudocode for solving the class average problem with counter-controlled repetition. Note the references in the pseudocode algorithm of Fig. 4.7 to a total and a counter. A total is a variable used to accumulate the sum of several values. A counter is a variable used to count—in this case, the grade counter indicates which of the 10 grades is about to be entered by the user. Variables used to store totals are normally initialized to zero before being used in a program; otherwise, the sum would include the previous value stored in the total’s memory location.

Enhancing GradeBook Validation Let’s consider an enhancement we made to class GradeBook. In Fig. 3.16, our setCourseName member function validated the course name by testing whether the course name’s

116

Chapter 4 Control Statements: Part 1

length was less than or equal to 25 characters, using an if statement. If this was true, the course name would be set. This code was followed by an if statement that tested whether the course name’s length was larger than 25 characters (in which case the course name would be shortened). The second if statement’s condition is the exact opposite of the first if statement’s condition. If one condition evaluates to true, the other must evaluate to false. Such a situation is ideal for an if…else statement, so we’ve modified our code, replacing the two if statements with one if…else statement (lines 18–25 of Fig. 4.9).

Implementing Counter-Controlled Repetition in Class GradeBook Class GradeBook (Fig. 4.8–Fig. 4.9) contains a constructor (declared in line 11 of Fig. 4.8 and defined in lines 9–12 of Fig. 4.9) that assigns a value to the class’s data member courseName (declared in line 17 of Fig. 4.8). Lines 16–26, 29–32 and 35–29 of Fig. 4.9 define member functions setCourseName, getCourseName and displayMessage, respectively. Lines 42–68 define member function determineClassAverage, which implements the class average algorithm described by the pseudocode in Fig. 4.7. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

// Fig. 4.8: GradeBook.h // Definition of class GradeBook that determines a class average. // Member functions are defined in GradeBook.cpp #include // program uses C++ standard string class using namespace std; // GradeBook class definition class GradeBook { public: GradeBook( string ); // constructor initializes course name void setCourseName( string ); // function to set the course name string getCourseName(); // function to retrieve the course name void displayMessage(); // display a welcome message void determineClassAverage(); // averages grades entered by the user private: string courseName; // course name for this GradeBook }; // end class GradeBook

Fig. 4.8 | Class average problem using counter-controlled repetition: GradeBook header. 1 2 3 4 5 6 7 8 9 10

// Fig. 4.9: GradeBook.cpp // Member-function definitions for class GradeBook that solves the // class average program with counter-controlled repetition. #include #include "GradeBook.h" // include definition of class GradeBook using namespace std; // constructor initializes courseName with string supplied as argument GradeBook::GradeBook( string name ) {

Fig. 4.9 | Class average problem using counter-controlled repetition: GradeBook source code file. (Part 1 of 3.)

4.8 Formulating Algorithms: Counter-Controlled Repetition 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

117

setCourseName( name ); // validate and store courseName } // end GradeBook constructor // function to set the course name; // ensures that the course name has at most 25 characters void GradeBook::setCourseName( string name ) { if ( name.length() <= 25 ) // if name has 25 or fewer characters courseName = name; // store the course name in the object else // if name is longer than 25 characters { // set courseName to first 25 characters of parameter name courseName = name.substr( 0, 25 ); // select first 25 characters cout << "Name \"" << name << "\" exceeds maximum length (25).\n" << "Limiting courseName to first 25 characters.\n" << endl; } // end if...else } // end function setCourseName // function to retrieve the course name string GradeBook::getCourseName() { return courseName; } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" << endl; } // end function displayMessage // determine class average based on 10 grades entered by user void GradeBook::determineClassAverage() { int total; // sum of grades entered by user int gradeCounter; // number of the grade to be entered next int grade; // grade value entered by user int average; // average of grades // initialization phase total = 0; // initialize total gradeCounter = 1; // initialize loop counter // processing phase while ( gradeCounter <= 10 ) // loop 10 times { cout << "Enter grade: "; // prompt for input cin >> grade; // input next grade total = total + grade; // add grade to total gradeCounter = gradeCounter + 1; // increment counter by 1 } // end while

Fig. 4.9 | Class average problem using counter-controlled repetition: GradeBook source code file. (Part 2 of 3.)

118 62 63 64 65 66 67 68

Chapter 4 Control Statements: Part 1

// termination phase average = total / 10; // integer division yields integer result // display total and average of grades cout << "\nTotal of all 10 grades is " << total << endl; cout << "Class average is " << average << endl; } // end function determineClassAverage

Fig. 4.9 | Class average problem using counter-controlled repetition: GradeBook source code file. (Part 3 of 3.) Lines 44–47 (Fig. 4.9) declare local variables total, gradeCounter, grade and to be of type int. Variable grade stores the user input. Notice that the preceding declarations appear in the body of member function determineClassAverage. In this chapter’s versions of class GradeBook, we simply read and process a set of grades. The averaging calculation is performed in member function determineClassAverage using local variables—we do not preserve any information about student grades in the class’s data members. In Chapter 7, Arrays and Vectors, we modify class GradeBook to maintain the grades in memory using a data member that refers to a data structure known as an array. This allows a GradeBook object to perform various calculations on a set of grades without requiring the user to enter the grades multiple times. Lines 50–51 initialize total to 0 and gradeCounter to 1 before they’re used in calculations. Counter variables are normally initialized to zero or one, depending on their use. An uninitialized variable contains a “garbage” value (also called an undefined value)—the value last stored in the memory location reserved for that variable. The variables grade and average (for the user input and calculated average, respectively) need not be initialized— their values will be assigned as they’re input or calculated later in the function. average

Common Programming Error 4.4

Not initializing counters and totals can lead to logic errors.

Error-Prevention Tip 4.2

Initialize each counter and total, either in its declaration or in an assignment statement. Totals are normally initialized to 0. Counters are normally initialized to 0 or 1, depending on how they’re used.

Good Programming Practice 4.3

Declare each variable on a separate line with its own comment for readability.

Line 54 indicates that the while statement should continue looping (also called iterating) as long as gradeCounter’s value is less than or equal to 10. While this condition remains true, the while statement repeatedly executes the statements between the braces that delimit its body (lines 55–60). Line 56 displays the prompt "Enter grade: ". This line corresponds to the pseudocode statement “Prompt the user to enter the next grade.” Line 57 reads the grade entered by the user and assigns it to variable grade. This line corresponds to the pseudocode statement “Input the next grade.” Recall that variable grade was not initialized earlier in the pro-

4.8 Formulating Algorithms: Counter-Controlled Repetition

119

gram, because the program obtains the value for grade from the user during each iteration of the loop. Line 58 adds the new grade entered by the user to the total and assigns the result to total, which replaces its previous value. Line 59 adds 1 to gradeCounter to indicate that the program has processed a grade and is ready to input the next grade from the user. Incrementing gradeCounter eventually causes gradeCounter to exceed 10. At that point the while loop terminates because its condition (line 54) becomes false. When the loop terminates, line 63 performs the averaging calculation and assigns its result to the variable average. Line 66 displays the text "Total of all 10 grades is " followed by variable total’s value. Line 67 then displays the text "Class average is " followed by variable average’s value. Member function determineClassAverage then returns control to the calling function (i.e., main in Fig. 4.10).

Demonstrating Class GradeBook Figure 4.10 contains this application’s main function, which creates an object of class GradeBook and demonstrates its capabilities. Line 9 of Fig. 4.10 creates a new GradeBook object called myGradeBook. The string in line 9 is passed to the GradeBook constructor (lines 9–12 of Fig. 4.9). Line 11 of Fig. 4.10 calls myGradeBook’s displayMessage member function to display a welcome message to the user. Line 12 then calls myGradeBook’s determineClassAverage member function to allow the user to enter 10 grades, for which the member function then calculates and prints the average—the member function performs the algorithm shown in the pseudocode of Fig. 4.7. 1 2 3 4 5 6 7 8 9 10 11 12 13

// Fig. 4.10: fig04_10.cpp // Create GradeBook object and invoke its determineClassAverage function. #include "GradeBook.h" // include definition of class GradeBook int main() { // create GradeBook object myGradeBook and // pass course name to constructor GradeBook myGradeBook( "CS101 C++ Programming" ); myGradeBook.displayMessage(); // display welcome message myGradeBook.determineClassAverage(); // find average of 10 grades } // end main

Welcome to the grade book for CS101 C++ Programming Enter Enter Enter Enter Enter

grade: grade: grade: grade: grade:

67 78 89 67 87

Fig. 4.10 | Class average problem using counter-controlled repetition: Creating an object of class GradeBook (Fig. 4.8–Fig. 4.9) and invoking its determineClassAverage member function. (Part 1 of 2.)

120

Enter Enter Enter Enter Enter

Chapter 4 Control Statements: Part 1

grade: grade: grade: grade: grade:

98 93 85 82 100

Total of all 10 grades is 846 Class average is 84

Fig. 4.10 | Class average problem using counter-controlled repetition: Creating an object of class GradeBook (Fig. 4.8–Fig. 4.9) and invoking its determineClassAverage member function. (Part 2 of 2.)

Notes on Integer Division and Truncation The averaging calculation performed in response to the function call in line 12 of Fig. 4.10 produces an integer result. The sample execution indicates that the sum of the grade values is 846, which, when divided by 10, should yield 84.6—a number with a decimal point. However, the result of the calculation total / 10 (line 63 of Fig. 4.9) is the integer 84, because total and 10 are both integers. Dividing two integers results in integer division— any fractional part of the calculation is lost (i.e., truncated). We’ll see how to obtain a result that includes a decimal point from the averaging calculation in the next section.

Common Programming Error 4.5

Assuming that integer division rounds (rather than truncates) can lead to incorrect results. For example, 7 ÷ 4, which yields 1.75 in conventional arithmetic, truncates to 1 in integer arithmetic, rather than rounding to 2.

In Fig. 4.9, if line 63 used gradeCounter rather than 10, the output for this program would display an incorrect value, 76. This would occur because in the final iteration of the while statement, gradeCounter was incremented to the value 11 in line 59.

Common Programming Error 4.6

Using a loop’s counter-control variable in a calculation after the loop often causes a common logic error called an off-by-one error. In a counter-controlled loop that counts up by one each time through the loop, the loop terminates when the counter’s value is one higher than its last legitimate value (i.e., 11 in the case of counting from 1 to 10).

4.9 Formulating Algorithms: Sentinel-Controlled Repetition Let’s generalize the class average problem. Consider the following problem: Develop a class average program that processes grades for an arbitrary number of students each time it’s run.

In the previous example, the problem statement specified the number of students, so the number of grades (10) was known in advance. In this example, no indication is given of how many grades the user will enter during the program’s execution. The program must process an arbitrary number of grades. How can the program determine when to stop the input of grades? How will it know when to calculate and print the class average?

4.9 Formulating Algorithms: Sentinel-Controlled Repetition

121

To solve this problem, we can use a special value called a sentinel value (also called a signal value, a dummy value or a flag value) to indicate “end of data entry.” After typing the legitimate grades, the user types the sentinel value to indicate that the last grade has been entered. Sentinel-controlled repetition is often called indefinite repetition because the number of repetitions is not known before the loop begins executing. The sentinel value must be chosen so that it’s not confused with an acceptable input value. Grades are normally nonnegative integers, so –1 is an acceptable sentinel value. Thus, a run of the program might process inputs such as 95, 96, 75, 74, 89 and –1. The program would then compute and print the class average for the grades 95, 96, 75, 74 and 89. Since –1 is the sentinel value, it should not enter into the averaging calculation.

Developing the Pseudocode Algorithm with Top-Down, Stepwise Refinement: The Top and First Refinement We approach the class average program with a technique called top-down, stepwise refinement, a technique that’s essential to the development of well-structured programs. We begin with a pseudocode representation of the top—a single statement that conveys the overall function of the program: Determine the class average for the quiz for an arbitrary number of students The top is, in effect, a complete representation of a program. Unfortunately, the top (as in this case) rarely conveys sufficient detail from which to write a program. So we now begin the refinement process. We divide the top into a series of smaller tasks and list these in the order in which they need to be performed. This results in the following first refinement. Initialize variables Input, sum and count the quiz grades Calculate and print the total of all student grades and the class average This refinement uses only the sequence structure—these steps execute in order.

Software Engineering Observation 4.4

Each refinement, as well as the top itself, is a complete specification of the algorithm; only the level of detail varies.

Software Engineering Observation 4.5

Many programs can be divided logically into three phases: an initialization phase that initializes the program variables; a processing phase that inputs data values and adjusts program variables (such as counters and totals) accordingly; and a termination phase that calculates and outputs the final results.

Proceeding to the Second Refinement The preceding Software Engineering Observation is often all you need for the first refinement in the top-down process. In the second refinement, we commit to specific variables. In this example, we need a running total of the numbers, a count of how many numbers have been processed, a variable to receive the value of each grade as it’s input by the user and a variable to hold the calculated average. The pseudocode statement Initialize variables

122

Chapter 4 Control Statements: Part 1

can be refined as follows: Initialize total to zero Initialize counter to zero Only the variables total and counter need to be initialized before they’re used. The variables average and grade (for the calculated average and the user input, respectively) need not be initialized, because their values will be replaced as they’re calculated or input. The pseudocode statement Input, sum and count the quiz grades requires a repetition statement (i.e., a loop) that successively inputs each grade. We don’t know in advance how many grades are to be processed, so we’ll use sentinel-controlled repetition. The user enters legitimate grades one at a time. After entering the last legitimate grade, the user enters the sentinel value. The program tests for the sentinel value after each grade is input and terminates the loop when the user enters the sentinel value. The second refinement of the preceding pseudocode statement is then Prompt the user to enter the first grade Input the first grade (possibly the sentinel) While the user has not yet entered the sentinel Add this grade into the running total Add one to the grade counter Prompt the user to enter the next grade Input the next grade (possibly the sentinel) In pseudocode, we do not use braces around the statements that form the body of the While structure. We simply indent the statements under the While to show that they belong to the While. Again, pseudocode is only an informal program development aid. The pseudocode statement Calculate and print the total of all student grades and the class average can be refined as follows: If the counter is not equal to zero Set the average to the total divided by the counter Print the total of the grades for all students in the class Print the class average else Print “No grades were entered” We test for the possibility of division by zero—normally a fatal logic error that, if undetected, would cause the program to fail (often called “crashing”). The complete second refinement of the pseudocode for the class average problem is shown in Fig. 4.11.

Common Programming Error 4.7

An attempt to divide by zero normally causes a fatal runtime error.

4.9 Formulating Algorithms: Sentinel-Controlled Repetition

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

123

Initialize total to zero Initialize counter to zero Prompt the user to enter the first grade Input the first grade (possibly the sentinel) While the user has not yet entered the sentinel Add this grade into the running total Add one to the grade counter Prompt the user to enter the next grade Input the next grade (possibly the sentinel) If the counter is not equal to zero Set the average to the total divided by the counter Print the total of the grades for all students in the class Print the class average else Print “No grades were entered”

Fig. 4.11 | Class average problem pseudocode algorithm with sentinel-controlled repetition.

Error-Prevention Tip 4.3

When performing division by an expression whose value could be zero, explicitly test for this possibility and handle it appropriately in your program (such as by printing an error message) rather than allowing the fatal error to occur. We’ll say more about dealing with these kinds of errors when we discuss exception handling.

The pseudocode in Fig. 4.11 solves the more general class average problem. This algorithm required only two levels of refinement. Sometimes more levels are necessary.

Software Engineering Observation 4.6

Terminate the top-down, stepwise refinement process when the pseudocode algorithm is specified in sufficient detail for you to be able to convert the pseudocode to C++. Typically, implementing the C++ program is then straightforward.

Software Engineering Observation 4.7

Many experienced programmers write programs without ever using program development tools like pseudocode. These programmers feel that their ultimate goal is to solve the problem on a computer and that writing pseudocode merely delays the production of final outputs. Although this method might work for simple and familiar problems, it can lead to serious difficulties in large, complex projects.

Implementing Sentinel-Controlled Repetition in Class GradeBook Figures 4.12–4.13 show class GradeBook containing member function determineClassAverage that implements the pseudocode algorithm of Fig. 4.11 (this class is demonstrated in Fig. 4.14). Although each grade entered is an integer, the averaging calculation is likely to produce a number with a decimal point—in other words, a real number or float-

124

Chapter 4 Control Statements: Part 1

ing-point number (e.g., 7.33, 0.0975 or 1000.12345). The type int cannot represent such a number, so this class must use another type to do so. C++ provides several data types for storing floating-point numbers in memory, including float and double. The primary difference between these types is that, compared to float variables, double variables can typically store numbers with larger magnitude and finer detail (i.e., more digits to the right of the decimal point—also known as the number’s precision). This program introduces a special operator called a cast operator to force the averaging calculation to produce a floating-point numeric result. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

// Fig. 4.12: GradeBook.h // Definition of class GradeBook that determines a class average. // Member functions are defined in GradeBook.cpp #include // program uses C++ standard string class using namespace std; // GradeBook class definition class GradeBook { public: GradeBook( string ); // constructor initializes course name void setCourseName( string ); // function to set the course name string getCourseName(); // function to retrieve the course name void displayMessage(); // display a welcome message void determineClassAverage(); // averages grades entered by the user private: string courseName; // course name for this GradeBook }; // end class GradeBook

Fig. 4.12 | Class average problem using sentinel-controlled repetition: GradeBook header. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

// Fig. 4.13: GradeBook.cpp // Member-function definitions for class GradeBook that solves the // class average program with sentinel-controlled repetition. #include #include // parameterized stream manipulators #include "GradeBook.h" // include definition of class GradeBook using namespace std; // constructor initializes courseName with string supplied as argument GradeBook::GradeBook( string name ) { setCourseName( name ); // validate and store courseName } // end GradeBook constructor // function to set the course name; // ensures that the course name has at most 25 characters void GradeBook::setCourseName( string name ) {

Fig. 4.13 | Class average problem using sentinel-controlled repetition: GradeBook source code file. (Part 1 of 3.)

4.9 Formulating Algorithms: Sentinel-Controlled Repetition 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

125

if ( name.length() <= 25 ) // if name has 25 or fewer characters courseName = name; // store the course name in the object else // if name is longer than 25 characters { // set courseName to first 25 characters of parameter name courseName = name.substr( 0, 25 ); // select first 25 characters cout << "Name \"" << name << "\" exceeds maximum length (25).\n" << "Limiting courseName to first 25 characters.\n" << endl; } // end if...else } // end function setCourseName // function to retrieve the course name string GradeBook::getCourseName() { return courseName; } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" << endl; } // end function displayMessage // determine class average based on 10 grades entered by user void GradeBook::determineClassAverage() { int total; // sum of grades entered by user int gradeCounter; // number of grades entered int grade; // grade value double average; // number with decimal point for average // initialization phase total = 0; // initialize total gradeCounter = 0; // initialize loop counter // processing phase // prompt for input and read grade from user cout << "Enter grade or -1 to quit: "; cin >> grade; // input grade or sentinel value // loop until sentinel value read from user while ( grade != -1 ) // while grade is not -1 { total = total + grade; // add grade to total gradeCounter = gradeCounter + 1; // increment counter // prompt for input and read next grade from user cout << "Enter grade or -1 to quit: "; cin >> grade; // input grade or sentinel value } // end while

Fig. 4.13 | Class average problem using sentinel-controlled repetition: GradeBook source code file. (Part 2 of 3.)

126 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

Chapter 4 Control Statements: Part 1

// termination phase if ( gradeCounter != 0 ) // if user entered at least one grade... { // calculate average of all grades entered average = static_cast< double >( total ) / gradeCounter; // display total and average (with two digits of precision) cout << "\nTotal of all " << gradeCounter << " grades entered is " << total << endl; cout << "Class average is " << setprecision( 2 ) << fixed << average << endl; } // end if else // no grades were entered, so output appropriate message cout << "No grades were entered" << endl; } // end function determineClassAverage

Fig. 4.13 | Class average problem using sentinel-controlled repetition: GradeBook source code file. (Part 3 of 3.) 1 2 3 4 5 6 7 8 9 10 11 12 13

// Fig. 4.14: fig04_14.cpp // Create GradeBook object and invoke its determineClassAverage function. #include "GradeBook.h" // include definition of class GradeBook int main() { // create GradeBook object myGradeBook and // pass course name to constructor GradeBook myGradeBook( "CS101 C++ Programming" ); myGradeBook.displayMessage(); // display welcome message myGradeBook.determineClassAverage(); // find average of 10 grades } // end main

Welcome to the grade book for CS101 C++ Programming Enter Enter Enter Enter

grade grade grade grade

or or or or

-1 -1 -1 -1

to to to to

quit: quit: quit: quit:

97 88 72 -1

Total of all 3 grades entered is 257 Class average is 85.67

Fig. 4.14 | Class average problem using sentinel-controlled repetition: Creating a GradeBook object and invoking its determineClassAverage member function. This example stacks control statements on top of one another—the while statement (lines 60–68 of Fig. 4.13) is immediately followed by an if…else statement (lines 71– 83) in sequence. Much of the code in this program is identical to the code in Fig. 4.9, so we concentrate on the new features and issues. Line 48 declares the double variable average. Recall that we used an int variable in the preceding example to store the class average. Using type double in the current example

4.9 Formulating Algorithms: Sentinel-Controlled Repetition

127

allows us to store the class average calculation’s result as a floating-point number. Line 52 initializes the variable gradeCounter to 0, because no grades have been entered yet. Remember that this program uses sentinel-controlled repetition. To keep an accurate record of the number of grades entered, the program increments variable gradeCounter only when the user enters a valid grade value and the program completes the processing of the grade. Finally, notice that both input statements (lines 57 and 67) are preceded by an output statement that prompts the user for input.

Good Programming Practice 4.4

Prompt the user for each keyboard input. The prompt should indicate the form of the input and any special input values. In a sentinel-controlled loop, the prompts requesting data entry should explicitly remind the user what the sentinel value is.

Program Logic for Sentinel-Controlled Repetition vs. Counter-Controlled Repetition Compare the program logic for sentinel-controlled repetition in this application with that for counter-controlled repetition in Fig. 4.9. In counter-controlled repetition, each iteration of the while statement (lines 54–60 of Fig. 4.9) reads a value from the user, for the specified number of iterations. In sentinel-controlled repetition, the program reads the first value (lines 56–57 of Fig. 4.13) before reaching the while. This value determines whether the program’s flow of control should enter the body of the while. If the condition of the while is false, the user entered the sentinel value, so the body of the while does not execute (i.e., no grades were entered). If, on the other hand, the condition is true, the body begins execution, and the loop adds the grade value to the total (line 62) and increments gradeCounter (line 63). Then lines 66–67 in the loop’s body prompt for and input the next value from the user. Next, program control reaches the closing right brace (}) of the body in line 68, so execution continues with the test of the while’s condition (line 60). The condition uses the most recent grade input by the user to determine whether the loop’s body should execute again. The value of variable grade is always input from the user immediately before the program tests the while condition. This allows the program to determine whether the value just input is the sentinel value before the program processes that value (i.e., adds it to the total and increments gradeCounter). If the sentinel value is input, the loop terminates, and the program does not add the value –1 to the total. After the loop terminates, the if…else statement in lines 71–83 executes. The condition in line 71 determines whether any grades were entered. If none were, the else part (lines 82–83) of the if…else statement executes and displays the message "No grades were entered" and the member function returns control to the calling function. Notice the block in the while loop in Fig. 4.13. Without the braces, the last three statements in the body of the loop would fall outside the loop, causing the computer to interpret this code incorrectly, as follows: // loop until sentinel value read from user while ( grade != -1 ) total = total + grade; // add grade to total gradeCounter = gradeCounter + 1; // increment counter // prompt for input and read next grade from user cout << "Enter grade or -1 to quit: "; cin >> grade;

128

Chapter 4 Control Statements: Part 1

This would cause an infinite loop in the program if the user did not input –1 for the first grade (in line 57).

Common Programming Error 4.8

Omitting the braces that delimit a block can lead to logic errors, such as infinite loops. To prevent this problem, some programmers enclose the body of every control statement in braces, even if the body contains only a single statement.

Floating-Point Number Precision and Memory Requirements Variables of type float represent single-precision floating-point numbers and have approximately seven significant digits on most 32-bit systems. Variables of type double represent double-precision floating-point numbers. These require twice as much memory as float variables and provide approximately 15 significant digits on most 32-bit systems— approximately double the precision of float variables. Most programmers represent floating-point numbers with type double. In fact, C++ treats all floating-point numbers you type in a program’s source code (such as 7.33 and 0.0975) as double values by default. Such values in the source code are known as floating-point constants. See Appendix C, Fundamental Types, for the ranges of values for floats and doubles. In conventional arithmetic, floating-point numbers often arise as a result of division—when we divide 10 by 3, the result is 3.3333333…, with the sequence of 3s repeating infinitely. The computer allocates only a fixed amount of space to hold such a value, so clearly the stored floating-point value can be only an approximation.

Common Programming Error 4.9

Using floating-point numbers in a manner that assumes they’re represented exactly (e.g., using them in comparisons for equality) can lead to incorrect results. Floating-point numbers are represented only approximately.

Although floating-point numbers are not always 100 percent precise, they have numerous applications. For example, when we speak of a “normal” body temperature of 98.6, we do not need to be precise to a large number of digits. When we read the temperature on a thermometer as 98.6, it may actually be 98.5999473210643. Calling this number simply 98.6 is fine for most applications involving body temperatures. Due to the imprecise nature of floating-point numbers, type double is preferred over type float, because double variables can represent floating-point numbers more accurately. For this reason, we use type double throughout the book.

Converting Between Fundamental Types Explicitly and Implicitly The variable average is declared to be of type double (line 48 of Fig. 4.13) to capture the fractional result of our calculation. However, total and gradeCounter are both integer variables. Recall that dividing two integers results in integer division, in which any fractional part of the calculation is lost truncated). In the following statement: average = total / gradeCounter;

the division occurs first—the result’s fractional part is lost before it’s assigned to average. To perform a floating-point calculation with integers, we must create temporary floatingpoint values. C++ provides the unary cast operator to accomplish this task. Line 74 uses the cast operator static_cast(total) to create a temporary floating-point copy

4.9 Formulating Algorithms: Sentinel-Controlled Repetition

129

of its operand in parentheses—total. Using a cast operator in this manner is called explicit conversion. The value stored in total is still an integer. The calculation now consists of a floating-point value (the temporary double version of total) divided by the integer gradeCounter. The compiler knows how to evaluate only expressions in which the operand types are identical. To ensure that the operands are of the same type, the compiler performs an operation called promotion (also called implicit conversion) on selected operands. For example, in an expression containing values of data types int and double, C++ promotes int operands to double values. In our example, we are treating total as a double (by using the unary cast operator), so the compiler promotes gradeCounter to double, allowing the calculation to be performed—the result of the floating-point division is assigned to average. In Chapter 6, Functions and an Introduction to Recursion, we discuss all the fundamental data types and their order of promotion. Cast operators are available for use with every data type and with class types as well. The static_cast operator is formed by following keyword static_cast with angle brackets (< and >) around a data-type name. The cast operator is a unary operator—an operator that takes only one operand. In Chapter 2, we studied the binary arithmetic operators. C++ also supports unary versions of the plus (+) and minus (-) operators, so that you can write such expressions as -7 or +5. Cast operators have higher precedence than other unary operators, such as unary + and unary -. This precedence is higher than that of the multiplicative operators *, / and %, and lower than that of parentheses. We indicate the cast operator with the notation static_cast() in our precedence charts.

Formatting for Floating-Point Numbers The formatting capabilities in Fig. 4.13 are discussed here briefly and explained in depth in Chapter 15, Stream Input/Output. The call to setprecision in line 79 (with an argument of 2) indicates that double variable average should be printed with two digits of precision to the right of the decimal point (e.g., 92.37). This call is referred to as a parameterized stream manipulator (because of the 2 in parentheses). Programs that use these calls must contain the preprocessor directive (line 5) #include

The manipulator endl is a nonparameterized stream manipulator (because it isn’t followed by a value or expression in parentheses) and does not require the header. If the precision is not specified, floating-point values are normally output with six digits of precision (i.e., the default precision on most 32-bit systems today), although we’ll see an exception to this in a moment. The stream manipulator fixed (line 79) indicates that floating-point values should be output in so-called fixed-point format, as opposed to scientific notation. Scientific notation is a way of displaying a number as a floating-point number between the values of 1.0 and 10.0, multiplied by a power of 10. For instance, the value 3,100.0 would be displayed in scientific notation as 3.1 × 103. Scientific notation is useful when displaying values that are very large or very small. Formatting using scientific notation is discussed further in Chapter 15. Fixed-point formatting, on the other hand, is used to force a floating-point number to display a specific number of digits. Specifying fixed-point formatting also forces the decimal point and trailing zeros to print, even if the value is a whole number amount, such as 88.00. Without the fixed-point formatting option, such a value prints in C++ as 88 without the trailing zeros and without the decimal point. When the stream

130

Chapter 4 Control Statements: Part 1

manipulators fixed and setprecision are used in a program, the printed value is rounded to the number of decimal positions indicated by the value passed to setprecision (e.g., the value 2 in line 79), although the value in memory remains unaltered. For example, the values 87.946 and 67.543 are output as 87.95 and 67.54, respectively. It’s also possible to force a decimal point to appear by using stream manipulator showpoint. If showpoint is specified without fixed, then trailing zeros will not print. Like endl, stream manipulators fixed and showpoint do not use parameters, nor do they require the header. Both can be found in header . Lines 79 and 80 of Fig. 4.13 output the class average rounded to the nearest hundredth and with exactly two digits to the right of the decimal point. The parameterized stream manipulator (line 79) indicates that variable average’s value should be displayed with two digits of precision to the right of the decimal point—indicated by setprecision( 2 ). The three grades entered during the sample execution of the program in Fig. 4.14 total 257, which yields the average 85.666666….

4.10 Formulating Algorithms: Nested Control Statements For the next example, we once again formulate an algorithm by using pseudocode and topdown, stepwise refinement, and write a corresponding C++ program. We’ve seen that control statements can be stacked on top of one another (in sequence). Here, we examine the only other structured way control statements can be connected, namely, by nesting one control statement within another. Consider the following problem statement: A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, ten of the students who completed this course took the exam. The college wants to know how well its students did on the exam. You’ve been asked to write a program to summarize the results. You’ve been given a list of these 10 students. Next to each name is written a 1 if the student passed the exam or a 2 if the student failed. Your program should analyze the results of the exam as follows: 1. Input each test result (i.e., a 1 or a 2). Display the prompting message “Enter result” each time the program requests another test result. 2. Count the number of test results of each type. 3. Display a summary of the test results indicating the number of students who passed and the number who failed. 4. If more than eight students passed the exam, print the message “Bonus to instructor!”

After reading the problem statement carefully, we make the following observations: 1. The program must process test results for 10 students. A counter-controlled loop can be used because the number of test results is known in advance. 2. Each test result is a number—either a 1 or a 2. Each time the program reads a test result, the program must determine whether the number is a 1 or a 2. We test for a 1 in our algorithm. If the number is not a 1, we assume that it’s a 2. (Exercise 4.20 considers the consequences of this assumption.) 3. Two counters are used to keep track of the exam results—one to count the number of students who passed the exam and one to count the number of students who failed the exam.

4.10 Formulating Algorithms: Nested Control Statements

131

4. After the program has processed all the results, it must decide whether more than eight students passed the exam. Let’s proceed with top-down, stepwise refinement. We begin with a pseudocode representation of the top: Analyze exam results and decide whether a bonus should be paid Once again, it’s important to emphasize that the top is a complete representation of the program, but several refinements are likely to be needed before the pseudocode evolves naturally into a C++ program. Our first refinement is Initialize variables Input the 10 exam results, and count passes and failures Display a summary of the exam results and decide whether a bonus should be paid Here, too, even though we have a complete representation of the entire program, further refinement is necessary. We now commit to specific variables. Counters are needed to record the passes and failures, a counter will be used to control the looping process and a variable is needed to store the user input. The last variable is not initialized, because its value is read from the user during each iteration of the loop. The pseudocode statement Initialize variables can be refined as follows: Initialize passes to zero Initialize failures to zero Initialize student counter to one Notice that only the counters are initialized at the start of the algorithm. The pseudocode statement Input the 10 exam results, and count passes and failures requires a loop that successively inputs the result of each exam. Here it’s known in advance that there are precisely 10 exam results, so counter-controlled looping is appropriate. Inside the loop (i.e., nested within the loop), an if…else statement will determine whether each exam result is a pass or a failure and will increment the appropriate counter. The refinement of the preceding pseudocode statement is then While student counter is less than or equal to 10 Prompt the user to enter the next exam result Input the next exam result If the student passed Add one to passes Else Add one to failures Add one to student counter We use blank lines to isolate the If…Else control structure, which improves readability.

132

Chapter 4 Control Statements: Part 1 The pseudocode statement Display a summary of the exam results and decide whether a bonus should be paid

can be refined as follows: Display the number of passes Display the number of failures If more than eight students passed Display “Bonus to instructor!” The complete second refinement appears in Fig. 4.15. Blank lines set off the While structure for readability. This pseudocode is now sufficiently refined for conversion to C++. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Initialize passes to zero Initialize failures to zero Initialize student counter to one While student counter is less than or equal to 10 Prompt the user to enter the next exam result Input the next exam result If the student passed Add one to passes Else Add one to failures Add one to student counter Display the number of passes Display the number of failures If more than eight students passed Display “Bonus to instructor!”

Fig. 4.15 | Pseudocode for examination-results problem. Conversion to Class Analysis The program that implements the pseudocode algorithm is shown in Fig. 4.16. This example does not contain a class—it contains just a source code file with function main performing all the application’s work. In this chapter and in Chapter 3, you’ve seen examples consisting of one class (including the header and source code files for this class), as well as another source code file testing the class. This source code file contained function main, which created an object of the class and called its member functions. Occasionally, when it does not make sense to try to create a reusable class to demonstrate a concept, we’ll use an example contained entirely within the main function of a single source code file. Lines 9–12 declare the variables used to process the examination results. We’ve taken advantage of a feature of C++ that allows variable initialization to be incorporated into

4.10 Formulating Algorithms: Nested Control Statements 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

// Fig. 4.16: fig04_16.cpp // Examination-results problem: Nested control statements. #include using namespace std; int main() { // initializing variables in declarations int passes = 0; // number of passes int failures = 0; // number of failures int studentCounter = 1; // student counter int result; // one exam result (1 = pass, 2 = fail) // process 10 students using counter-controlled loop while ( studentCounter <= 10 ) { // prompt user for input and obtain value from user cout << "Enter result (1 = pass, 2 = fail): "; cin >> result; // input result // if...else nested in while if ( result == 1 ) // passes = passes + 1; // else // failures = failures + 1; //

if result is 1, increment passes; else result is not 1, so increment failures

// increment studentCounter so loop eventually terminates studentCounter = studentCounter + 1; } // end while // termination phase; display number of passes and failures cout << "Passed " << passes << "\nFailed " << failures << endl; // determine whether more than eight students passed if ( passes > 8 ) cout << "Bonus to instructor!" << endl; } // end main

Enter result Enter result Enter result Enter result Enter result Enter result Enter result Enter result Enter result Enter result Passed 6 Failed 4

(1 (1 (1 (1 (1 (1 (1 (1 (1 (1

= = = = = = = = = =

pass, pass, pass, pass, pass, pass, pass, pass, pass, pass,

2 2 2 2 2 2 2 2 2 2

= = = = = = = = = =

fail): fail): fail): fail): fail): fail): fail): fail): fail): fail):

1 2 2 1 1 1 2 1 1 2

Fig. 4.16 | Examination-results problem: Nested control statements. (Part 1 of 2.)

133

134

Chapter 4 Control Statements: Part 1

Enter result (1 = pass, Enter result (1 = pass, Enter result (1 = pass, Enter result (1 = pass, Enter result (1 = pass, Enter result (1 = pass, Enter result (1 = pass, Enter result (1 = pass, Enter result (1 = pass, Enter result (1 = pass, Passed 9 Failed 1 Bonus to instructor!

2 2 2 2 2 2 2 2 2 2

= = = = = = = = = =

fail): fail): fail): fail): fail): fail): fail): fail): fail): fail):

1 1 1 1 2 1 1 1 1 1

Fig. 4.16 | Examination-results problem: Nested control statements. (Part 2 of 2.) declarations (passes is initialized to 0, failures is initialized to 0 and studentCounter is initialized to 1). Looping programs may require initialization at the beginning of each repetition; such reinitialization normally would be performed by assignment statements rather than in declarations or by moving the declarations inside the loop bodies. The while statement (lines 15–29) loops 10 times. Each iteration inputs and processes one exam result. The if…else statement (lines 22–25) for processing each result is nested in the while statement. If the result is 1, the if…else statement increments passes; otherwise, it assumes the result is 2 and increments failures. Line 28 increments studentCounter before the loop condition is tested again in line 15. After 10 values have been input, the loop terminates and line 32 displays the number of passes and the number of failures. The if statement in lines 35–36 determines whether more than eight students passed the exam and, if so, outputs the message "Bonus to instructor!". Figure 4.16 shows the input and output from two sample executions of the program. At the end of the second sample execution, the condition in line 35 is true—more than eight students passed the exam, so the program outputs a message indicating that the instructor should receive a bonus.

4.11 Assignment Operators C++ provides several assignment operators for abbreviating assignment expressions. For example, the statement c = c + 3;

can be abbreviated with the addition assignment operator += as c += 3;

which adds the value of the expression on the operator’s right to the value of the variable on the operator’s left and stores the result in the left-side variable. Any statement of the form variable = variable operator expression;

in which the same variable appears on both sides of the assignment operator and operator is one of the binary operators +, -, *, /, or % (or a few others we’ll discuss later in the text), can be written in the form variable operator= expression;

4.12 Increment and Decrement Operators

135

Thus the assignment c += 3 adds 3 to c. Figure 4.17 shows the arithmetic assignment operators, sample expressions using these operators and explanations. Assignment operator Assume: int

Sample expression

Explanation

Assigns

c = 3, d = 5, e = 4, f = 6, g = 12;

to c to d 20 to e 2 to f 3 to g

+=

c += 7

c = c + 7

10

-=

d -= 4

d = d - 4

1

*=

e *= 5

e = e * 5

/=

f /= 3

f = f / 3

%=

g %= 9

g = g % 9

Fig. 4.17 | Arithmetic assignment operators.

4.12 Increment and Decrement Operators In addition to the arithmetic assignment operators, C++ also provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable. These are the unary increment operator, ++, and the unary decrement operator, --, which are summarized in Fig. 4.18. A program can increment by 1 the value of a variable called c using the increment operator, ++, rather than the expression c = c + 1 or c += 1. An increment or decrement operator that’s prefixed to (placed before) a variable is referred to as the prefix increment or prefix decrement operator, respectively. An increment or decrement operator that’s postfixed to (placed after) a variable is referred to as the postfix increment or postfix decrement operator, respectively.

Operator

Called

Sample expression

++

preincrement

++a

++

postincrement

a++

--

predecrement

--b

--

postdecrement

b--

Explanation Increment a by 1, then use the new value of a in the expression in which a resides. Use the current value of a in the expression in which a resides, then increment a by 1. Decrement b by 1, then use the new value of b in the expression in which b resides. Use the current value of b in the expression in which b resides, then decrement b by 1.

Fig. 4.18 | Increment and decrement operators. Using the prefix increment (or decrement) operator to add (or subtract) 1 from a variable is known as preincrementing (or predecrementing) the variable. Preincrementing (or predecrementing) causes the variable to be incremented (decremented) by 1, then the new value of the variable is used in the expression in which it appears. Using the postfix incre-

136

Chapter 4 Control Statements: Part 1

ment (or decrement) operator to add (or subtract) 1 from a variable is known as postincrementing (or postdecrementing) the variable. Postincrementing (or postdecrementing) causes the current value of the variable to be used in the expression in which it appears, then the variable’s value is incremented (decremented) by 1.

Good Programming Practice 4.5

Unlike binary operators, the unary increment and decrement operators should be placed next to their operands, with no intervening spaces.

Figure 4.19 demonstrates the difference between the prefix increment and postfix increment versions of the ++ increment operator. The decrement operator (--) works similarly. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 4.19: fig04_19.cpp // Preincrementing and postincrementing. #include using namespace std; int main() { int c; // demonstrate postincrement c = 5; // assign 5 to c cout << c << endl; // print 5 cout << c++ << endl; // print 5 then postincrement cout << c << endl; // print 6 cout << endl; // skip a line // demonstrate preincrement c = 5; // assign 5 to c cout << c << endl; // print 5 cout << ++c << endl; // preincrement then print 6 cout << c << endl; // print 6 } // end main

5 5 6 5 6 6

Fig. 4.19 | Preincrementing and postincrementing. Line 11 initializes c to 5, and line 12 outputs c’s initial value. Line 13 outputs the value of the expression c++. This postincrements the variable c, so c’s original value (5) is output, then c’s value is incremented. Thus, line 13 outputs c’s initial value (5) again. Line 14 outputs c’s new value (6) to prove that the variable’s value was incremented in line 13. Line 19 resets c’s value to 5, and line 20 outputs that value. Line 21 outputs the value of the expression ++c. This expression preincrements c, so its value is incremented, then

4.12 Increment and Decrement Operators

137

the new value (6) is output. Line 22 outputs c’s value again to show that the value of c is still 6 after line 21 executes. The arithmetic assignment operators and the increment and decrement operators can be used to simplify program statements. The three assignment statements in Fig. 4.16: passes = passes + 1; failures = failures + 1; studentCounter = studentCounter + 1;

can be written more concisely with assignment operators as passes += 1; failures += 1; studentCounter += 1;

with prefix increment operators as ++passes; ++failures; ++studentCounter;

or with postfix increment operators as passes++; failures++; studentCounter++;

When you increment (++) or decrement (--) an integer variable in a statement by itself, the preincrement and postincrement forms have the same logical effect, and the predecrement and postdecrement forms have the same logical effect. It’s only when a variable appears in the context of a larger expression that preincrementing the variable and postincrementing the variable have different effects (and similarly for predecrementing and postdecrementing).

Common Programming Error 4.10

Attempting to use the increment or decrement operator on an expression other than a modifiable variable name or reference, e.g., writing ++(x + 1), is a syntax error.

Figure 4.20 shows the precedence and associativity of the operators introduced to this point. The operators are shown top-to-bottom in decreasing order of precedence. The second column indicates the associativity of the operators at each level of precedence. Notice that the conditional operator (?:), the unary operators preincrement (++), predecrement (--), plus (+) and minus (-), and the assignment operators =, +=, -=, *=, /= and %= associate from right to left. All other operators in Fig. 4.20 associate from left to right. The third column names the various groups of operators. Operators

Associativity

Type

::

left to right [See caution in Fig. 2.10]

scope resolution grouping parentheses

()

Fig. 4.20 | Operator precedence for the operators encountered so far in the text. (Part 1 of 2.)

138

Chapter 4 Control Statements: Part 1

Operators ++

--

static_cast()

++

--

+

*

/

%

+

-

<<

>>

<

<=

==

!=

-

>

>=

-=

*=

?: =

+=

/=

%=

Associativity

Type

left to right right to left left to right left to right left to right left to right left to right right to left right to left

unary (postfix) unary (prefix) multiplicative additive insertion/extraction relational equality conditional assignment

Fig. 4.20 | Operator precedence for the operators encountered so far in the text. (Part 2 of 2.)

4.13 Wrap-Up This chapter presented basic problem-solving techniques that you use in building classes and developing member functions for these classes. We demonstrated how to construct an algorithm (i.e., an approach to solving a problem) in pseudocode, then how to refine the algorithm through pseudocode development, resulting in C++ code that can be executed as part of a function. You learned how to use top-down, stepwise refinement to plan out the actions that a function must perform and the order in which it must perform them. You learned that only three types of control structures—sequence, selection and repetition—are needed to develop any algorithm. We demonstrated two of C++’s selection statements—the if single-selection statement and the if…else double-selection statement. The if statement is used to execute a set of statements based on a condition—if the condition is true, the statements execute; if it isn’t, the statements are skipped. The if…else double-selection statement is used to execute one set of statements if a condition is true, and another set of statements if the condition is false. We then discussed the while repetition statement, where a set of statements are executed repeatedly as long as a condition is true. We used control-statement stacking to total and compute the average of a set of student grades with counter- and sentinel-controlled repetition, and we used controlstatement nesting to analyze and make decisions based on a set of exam results. We introduced assignment operators, which can be used for abbreviating statements. We presented the increment and decrement operators, which can be used to add or subtract the value 1 from a variable. In the next chapter, we continue our discussion of control statements, introducing the for, do…while and switch statements.

Summary Section 4.2 Algorithms

• An algorithm (p. 102) is a procedure for solving a problem in terms of the actions to execute and the order in which to execute them. • Specifying the order in which statements execute in a program is called program control (p. 103).

Summary

139

Section 4.3 Pseudocode

• Pseudocode (p. 103) helps you think out a program before writing it in a programming language.

Section 4.4 Control Structures

• An activity diagram models the workflow (also called the activity, p. 105) of a software system. • Activity diagrams (p. 105) are composed of symbols, such as action state symbols, diamonds and small circles, that are connected by transition arrows representing the flow of the activity. • Like pseudocode, activity diagrams help you develop and represent algorithms. • An action state is represented as a rectangle with its left and right sides replaced with arcs curving outward. The action expression (p. 105) appears inside the action state. • The arrows in an activity diagram represent transitions (p. 105), which indicate the order in which the actions represented by action states occur. • The solid circle in an activity diagram represents the initial state (p. 105)—the beginning of the workflow before the program performs the modeled actions. • The solid circle surrounded by a hollow circle that appears at the bottom of the activity diagram represents the final state (p. 105)—the end of the workflow after the program performs its actions. • Rectangles with the upper-right corners folded over are called notes (p. 105) in the UML. A dotted line (p. 105) connects each note with the element that the note describes. • A decision symbol (p. 107) in an activity diagram indicates that a decision is to be made. The workflow follows a path determined by the associated guard conditions. Each transition arrow emerging from a decision symbol has a guard condition. If a guard condition is true, the workflow enters the action state to which the transition arrow points. • There are three types of control structures (p. 104)—sequence, selection and repetition. • The sequence structure is built in—by default, statements execute in the order they appear. • A selection structure chooses among alternative courses of action.

Section 4.5 if Selection Statement

• The if single-selection statement (p. 107) either performs (selects) an action if a condition is true, or skips the action if the condition is false.

Section 4.6 if…else Double-Selection Statement

• The if…else double-selection statement (p. 108) performs (selects) an action if a condition is true and performs a different action if the condition is false. • To include several statements in an if’s body (or the body of an else for an if…else statement), enclose the statements in braces ({ and }). A set of statements contained in braces is called a block (p. 112). A block can be placed anywhere in a program that a single statement can be placed. • A null statement (p. 113), indicating that no action is to be taken, is indicated by a semicolon (;).

Section 4.7 while Repetition Statement

• A repetition statement (p. 113) repeats an action while some condition remains true. • A UML merge symbol (p. 114) has two or more transition arrows pointing to the diamond and only one pointing from it, to indicate multiple activity flows merging to continue the activity.

Section 4.8 Formulating Algorithms: Counter-Controlled Repetition

• Counter-controlled repetition (p. 115) is used when the number of repetitions is known before a loop begins executing, i.e., when there is definite repetition.

140

Chapter 4 Control Statements: Part 1

• The stream manipulator fixed (p. 129) indicates that floating-point values should be output in so-called fixed-point format, as opposed to scientific notation.

Section 4.9 Formulating Algorithms: Sentinel-Controlled Repetition

• Top-down, stepwise refinement (p. 121) is a process for refining pseudocode by maintaining a complete representation of the program during each refinement. • Sentinel-controlled repetition (p. 122) is used when the number of repetitions is not known before a loop begins executing, i.e., when there is indefinite repetition. • A value that contains a fractional part is referred to as a floating-point number and is represented approximately by data types such as float and double (p. 124). • The unary cast operator static_cast (p. 124) can be used to create a temporary floating-point copy of its operand. • Unary operators (p. 129) take only one operand; binary operators take two. • The parameterized stream manipulator setprecision (p. 129) indicates the number of digits of precision that should be displayed to the right of the decimal point.

Section 4.10 Formulating Algorithms: Nested Control Statements

• A nested control statement (p. 130) appears in the body of another control statement.

Section 4.11 Assignment Operators

• The arithmetic operators +=, -=, *=, /= and %= abbreviate assignment expressions (p. 134).

Section 4.12 Increment and Decrement Operators

• The increment (++) and decrement (--) operators (p. 135) increment or decrement a variable by 1, respectively. If the operator is prefixed to the variable, the variable is incremented or decremented by 1 first, then its new value is used in the expression in which it appears. If the operator is postfixed to the variable, the variable is first used in the expression in which it appears, then the variable’s value is incremented or decremented by 1.

Self-Review Exercises 4.1

Answer each of the following questions. a) All programs can be written in terms of three types of control structures: , and . b) The selection statement is used to execute one action when a condition is true or a different action when that condition is false. c) Repeating a set of instructions a specific number of times is called repetition. d) When it isn’t known in advance how many times a set of statements will be repeated, a(n) value can be used to terminate the repetition.

4.2

Write four different C++ statements that each add 1 to integer variable x.

4.3

Write C++ statements to accomplish each of the following: a) In one statement, assign the sum of the current value of x and y to z and postincrement the value of x. b) Determine whether the value of the variable count is greater than 10. If it is, print "Count is greater than 10."

c) Predecrement the variable x by 1, then subtract it from the variable total. d) Calculate the remainder after q is divided by divisor and assign the result to q. Write this statement two different ways.

Answers to Self-Review Exercises 4.4

141

Write C++ statements to accomplish each of the following tasks. a) Declare variables sum and x to be of type int. b) Set variable x to 1. c) Set variable sum to 0. d) Add variable x to variable sum and assign the result to variable sum. e) Print "The sum is: " followed by the value of variable sum.

4.5 Combine the statements that you wrote in Exercise 4.4 into a program that calculates and prints the sum of the integers from 1 to 10. Use the while statement to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11. 4.6 State the values of each of these int variables after the calculation is performed. Assume that, when each statement begins executing, all variables have the integer value 5. a) product *= x++; b) quotient /= ++x; 4.7

Write single C++ statements or portions of statements that do the following: a) Input integer variable x with cin and >>. b) Input integer variable y with cin and >>. c) Set integer variable i to 1. d) Set integer variable power to 1. e) Multiply variable power by x and assign the result to power. f) Preincrement variable i by 1. g) Determine whether i is less than or equal to y. h) Output integer variable power with cout and <<.

4.8 Write a C++ program that uses the statements in Exercise 4.7 to calculate x raised to the y power. The program should have a while repetition statement. 4.9

Identify and correct the errors in each of the following: a) while ( c <= 5 ) { product *= c;

b) c)

++c; cin << value; if ( gender == 1 ) cout << "Woman" << endl; else; cout << "Man" << endl;

4.10

What’s wrong with the following while repetition statement? while ( z >= 0 ) sum += z;

Answers to Self-Review Exercises 4.1 a) Sequence, selection and repetition. b) if…else. c) Counter-controlled or definite. d) Sentinel, signal, flag or dummy. 4.2

x = x + 1; x += 1; ++x; x++;

4.3

a)

z = x++ + y;

142

Chapter 4 Control Statements: Part 1 b)

if ( count > 10 )

c) d)

total -= --x;

cout << "Count is greater than 10" << endl; q %= divisor; q = q % divisor;

4.4

a)

int sum;

b) c) d)

x = 1;

e) 4.5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

int x; sum = 0; sum += x;

or

sum = sum + x; cout << "The sum is: " << sum << endl;

See the following code: // Exercise 4.5 Solution: ex04_05.cpp // Calculate the sum of the integers from 1 to 10. #include using namespace std; int main() { int sum; // stores sum of integers 1 to 10 int x; // counter x = 1; // count from 1 sum = 0; // initialize sum while ( x <= 10 ) // loop 10 times { sum += x; // add x to sum ++x; // increment x } // end while cout << "The sum is: " << sum << endl; } // end main

The sum is: 55

4.6

1 2 3 4 5 6 7 8 9 10 11 12 13

a) b) =

product = 25, x = 6; quotient = 0, x = 6;

// Exercise 4.6 Solution: ex04_06.cpp // Calculate the value of product and quotient. #include using namespace std; int main() { int x = 5; int product = 5; int quotient = 5; // part a product *= x++; // part a statement

Answers to Self-Review Exercises

14 15 16 17 18 19 20 21 22

cout << "Value of product after calculation: " << product << endl; cout << "Value of x after calculation: " << x << endl << endl; // part b x = 5; // reset value of x quotient /= ++x; // part b statement cout << "Value of quotient after calculation: " << quotient << endl; cout << "Value of x after calculation: " << x << endl << endl; } // end main

Value of product after calculation: 25 Value of x after calculation: 6 Value of quotient after calculation: 0 Value of x after calculation: 6

4.7

a) b) c) d) e) f) g) h)

4.8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

cin >> x; cin >> y; i = 1; power = 1; power *= x;

or

power = power * x; ++i; if ( i <= y ) cout << power << endl;

See the following code: // Exercise 4.8 Solution: ex04_08.cpp // Raise x to the y power. #include using namespace std; int main() { int x; // base int y; // exponent int i; // counts from 1 to y int power; // used to calculate x raised to power y i = 1; // initialize i to begin counting from 1 power = 1; // initialize power cout << "Enter base as an integer: "; cin >> x; // input base

// prompt for base

cout << "Enter exponent as an integer: "; // prompt for exponent cin >> y; // input exponent // count from 1 to y and multiply power by x each time while ( i <= y ) { power *= x; ++i; } // end while cout << power << endl; // display result } // end main

143

144

Chapter 4 Control Statements: Part 1

Enter base as an integer: 2 Enter exponent as an integer: 3 8

a) Error: Missing the closing right brace of the while body. Correction: Add closing right brace after the statement c++;. b) Error: Used stream insertion instead of stream extraction. Correction: Change << to >>. c) Error: Semicolon after else results in a logic error. The second output statement will always be executed. Correction: Remove the semicolon after else.

4.9

4.10 The value of the variable z is never changed in the while statement. Therefore, if the loopcontinuation condition (z >= 0) is initially true, an infinite loop is created. To prevent the infinite loop, z must be decremented so that it eventually becomes less than 0.

Exercises 4.11

(Correct the Code Errors) Identify and correct the error(s) in each of the following: a) if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else

b)

cout << "Age is less than 65 << endl"; if ( age >= 65 ) cout << "Age is greater than or equal to 65" << endl; else;

c)

cout << "Age is less than 65 << endl"; int x = 1, total; while ( x <= 10 ) { total += x; ++x;

d)

} While ( x <= 100 ) total += x;

e)

++x; while ( y > 0 ) { cout << y << endl; ++y; }

4.12 1 2 3 4 5 6 7 8

(What Does this Program Do?) What does the following program print?

// Exercise 4.12: ex04_12.cpp // What does this program print? #include using namespace std; int main() { int y; // declare y

Exercises

9 10 11 12 13 14 15 16 17 18 19 20 21

145

int x = 1; // initialize x int total = 0; // initialize total while ( x <= 10 ) // loop 10 times { y = x * x; // perform calculation cout << y << endl; // output result total += y; // add y to total x++; // increment counter x } // end while cout << "Total is " << total << endl; // display result } // end main

For Exercises 4.13–4.16, perform each of these steps: a) b) c) d)

Read the problem statement. Formulate the algorithm using pseudocode and top-down, stepwise refinement. Write a C++ program. Test, debug and execute the C++ program.

4.13 (Gas Mileage) Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several trips by recording miles driven and gallons used for each trip. Develop a C++ program that uses a while statement to input the miles driven and gallons used for each trip. The program should calculate and display the miles per gallon obtained for each trip and print the combined miles per gallon obtained for all tankfuls up to this point. Enter miles driven (-1 to quit): 287 Enter gallons used: 13 MPG this trip: 22.076923 Total MPG: 22.076923 Enter miles driven (-1 to quit): 200 Enter gallons used: 10 MPG this trip: 20.000000 Total MPG: 21.173913 Enter the miles driven (-1 to quit): 120 Enter gallons used: 5 MPG this trip: 24.000000 Total MPG: 21.678571 Enter the miles used (-1 to quit): -1

4.14 (Credit Limits) Develop a C++ program that will determine whether a department-store customer has exceeded the credit limit on a charge account. For each customer, the following facts are available: a) Account number (an integer) b) Balance at the beginning of the month c) Total of all items charged by this customer this month d) Total of all credits applied to this customer's account this month e) Allowed credit limit The program should use a while statement to input each of these facts, calculate the new balance (= beginning balance + charges – credits) and determine whether the new balance exceeds the customer’s credit limit. For those customers whose credit limit is exceeded, the program should display the customer’s account number, credit limit, new balance and the message “Credit Limit Exceeded.”

146

Chapter 4 Control Statements: Part 1

Enter account number (or -1 to quit): 100 Enter beginning balance: 5394.78 Enter total charges: 1000.00 Enter total credits: 500.00 Enter credit limit: 5500.00 New balance is 5894.78 Account: 100 Credit limit: 5500.00 Balance: 5894.78 Credit Limit Exceeded. Enter Account Number (or -1 to quit): 200 Enter beginning balance: 1000.00 Enter total charges: 123.45 Enter total credits: 321.00 Enter credit limit: 1500.00 New balance is 802.45 Enter Account Number (or -1 to quit): -1

4.15 (Sales Commission Calculator) A large company pays its salespeople on a commission basis. The salespeople each receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of chemicals in a week receives $200 plus 9% of $5000, or a total of $650. Develop a C++ program that uses a while statement to input each salesperson’s gross sales for last week and calculates and displays that salesperson’s earnings. Process one salesperson’s figures at a time. Enter sales in dollars (-1 to end): 5000.00 Salary is: $650.00 Enter sales in dollars (-1 to end): 6000.00 Salary is: $740.00 Enter sales in dollars (-1 to end): 7000.00 Salary is: $830.00 Enter sales in dollars (-1 to end): -1

4.16 (Salary Calculator) Develop a C++ program that uses a while statement to determine the gross pay for each of several employees. The company pays “straight time” for the first 40 hours worked by each employee and pays “time-and-a-half” for all hours worked in excess of 40 hours. You are given a list of the employees of the company, the number of hours each employee worked last week and the hourly rate of each employee. Your program should input this information for each employee and should determine and display the employee’s gross pay. Enter hours worked (-1 to end): 39 Enter hourly rate of the employee ($00.00): 10.00 Salary is $390.00 Enter hours worked (-1 to end): 40 Enter hourly rate of the employee ($00.00): 10.00 Salary is $400.00 Enter hours worked (-1 to end): 41 Enter hourly rate of the employee ($00.00): 10.00 Salary is $415.00 Enter hours worked (-1 to end): -1

Exercises

147

4.17 (Find the Largest) The process of finding the largest number (i.e., the maximum of a group of numbers) is used frequently in computer applications. For example, a program that determines the winner of a sales contest inputs the number of units sold by each salesperson. The salesperson who sells the most units wins the contest. Write a C++ program that uses a while statement to determine and print the largest number of 10 numbers input by the user. Your program should use three variables, as follows: counter: number: largest:

A counter to count to 10 (i.e., to keep track of how many numbers have been input and to determine when all 10 numbers have been processed). The current number input to the program. The largest number found so far.

4.18 (Tabular Output) Write a C++ program that uses a while statement and the tab escape sequence \t to print the following table of values: N

10*N

100*N

1000*N

1 2 3 4 5

10 20 30 40 50

100 200 300 400 500

1000 2000 3000 4000 5000

4.19 (Find the Two Largest Numbers) Using an approach similar to that in Exercise 4.17, find the two largest values among the 10 numbers. [Note: You must input each number only once.] 4.20 (Validating User Input) The examination-results program of Fig. 4.16 assumes that any value input by the user that’s not a 1 must be a 2. Modify the application to validate its inputs. On any input, if the value entered is other than 1 or 2, keep looping until the user enters a correct value. 4.21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

// Exercise 4.21: ex04_21.cpp // What does this program print? #include using namespace std; int main() { int count = 1; // initialize count while ( count <= 10 ) // loop 10 times { // output line of text cout << ( count % 2 ? "****" : "++++++++" ) << endl; ++count; // increment count } // end while } // end main

4.22 1 2 3 4 5 6 7

(What Does this Program Do?) What does the following program print?

(What Does this Program Do?) What does the following program print?

// Exercise 4.22: ex04_22.cpp // What does this program print? #include using namespace std; int main() {

148

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Chapter 4 Control Statements: Part 1

int row = 10; // initialize row int column; // declare column while ( row >= 1 ) // loop until row < 1 { column = 1; // set column to 1 as iteration begins while ( column <= 10 ) // loop 10 times { cout << ( row % 2 ? "<" : ">" ); // output ++column; // increment column } // end inner while --row; // decrement row cout << endl; // begin new output line } // end outer while } // end main

4.23 (Dangling-else Problem) State the output for each of the following when x is 9 and y is 11 and when x is 11 and y is 9. The compiler ignores the indentation in a C++ program. The C++ compiler always associates an else with the previous if unless told to do otherwise by the placement of braces {}. On first glance, you may not be sure which if and else match, so this is referred to as the “dangling-else” problem. We eliminated the indentation from the following code to make the problem more challenging. [Hint: Apply indentation conventions you’ve learned.] a) if ( x < 10 ) if ( y > 10 ) cout << "*****" << endl; else cout << "#####" << endl;

b)

cout << "$$$$$" << endl; if ( x < 10 ) { if ( y > 10 ) cout << "*****" << endl; } else { cout << "#####" << endl; cout << "$$$$$" << endl; }

4.24 (Another Dangling-else Problem) Modify the following code to produce the output shown. Use proper indentation techniques. You must not make any changes other than inserting braces. The compiler ignores indentation in a C++ program. We eliminated the indentation from the following code to make the problem more challenging. [Note: It’s possible that no modification is necessary.] if ( if ( cout else cout cout cout

y == 8 ) x == 5 ) << "@@@@@" << endl; << "#####" << endl; << "$$$$$" << endl; << "&&&&&" << endl;

Exercises a) Assuming x

= 5

and y

= 8,

the following output is produced.

b) Assuming x

= 5

and y

= 8,

the following output is produced.

c) Assuming x

= 5

and y

= 8,

the following output is produced.

149

@@@@@ $$$$$ &&&&&

@@@@@

@@@@@ &&&&&

d) Assuming x = 5 and y = 7, the following output is produced. [Note: The last three output statements after the else are all part of a block.] ##### $$$$$ &&&&&

4.25 (Square of Asterisks) Write a program that reads in the size of the side of a square then prints a hollow square of that size out of asterisks and blanks. Your program should work for squares of all side sizes between 1 and 20. For example, if your program reads a size of 5, it should print ***** * * * * * * *****

4.26 (Palindromes) A palindrome is a number or a text phrase that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a five-digit integer and determines whether it’s a palindrome. [Hint: Use the division and modulus operators to separate the number into its individual digits.] 4.27 (Printing the Decimal Equivalent of a Binary Number) Input an integer containing only 0s and 1s (i.e., a “binary” integer) and print its decimal equivalent. Use the modulus and division operators to pick off the “binary” number’s digits one at a time from right to left. Much as in the decimal number system, where the rightmost digit has a positional value of 1, the next digit left has a positional value of 10, then 100, then 1000, and so on, in the binary number system the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, and so on. Thus the decimal number 234 can be interpreted as 2 * 100 + 3 * 10 + 4 * 1. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8, or 13. [Note: To learn more about binary numbers, refer to Appendix D.] 4.28 (Checkerboard Pattern of Asterisks) Write a program that displays the following checkerboard pattern. Your program must use only three output statements, one of each of the following forms:

150

Chapter 4 Control Statements: Part 1 cout << "* "; cout << ' '; cout << endl;

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

4.29 (Multiples of 2 with an Infinite Loop) Write a program that prints the powers of the integer 2, namely 2, 4, 8, 16, 32, 64, etc. Your while loop should not terminate (i.e., you should create an infinite loop). To do this, simply use the keyword true as the expression for the while statement. What happens when you run this program? 4.30 (Calculating a Circle’s Diameter, Circumference and Area) Write a program that reads the radius of a circle (as a double value) and computes and prints the diameter, the circumference and the area. Use the value 3.14159 for π. 4.31 What’s wrong with the following statement? Provide the correct statement to accomplish what the programmer was probably trying to do. cout << ++( x + y );

4.32 (Sides of a Triangle) Write a program that reads three nonzero mines and prints whether they could represent the sides of a triangle.

double

values and deter-

4.33 (Sides of a Right Triangle) Write a program that reads three nonzero integers and determines and prints whether they’re the sides of a right triangle. 4.34 (Factorial) The factorial of a nonnegative integer n is written n! (pronounced “n factorial”) and is defined as follows: n! = n · (n – 1) · (n – 2) · … · 1 (for values of n greater than 1) and n! = 1 (for n = 0 or n = 1). For example, 5! = 5 · 4 · 3 · 2 · 1, which is 120. Use while statements in each of the following: a) Write a program that reads a nonnegative integer and computes and prints its factorial. b) Write a program that estimates the value of the mathematical constant e by using the formula: 1 1 1 e = 1 + ----- + ----- + ----- + … 1! 2! 3! Prompt the user for the desired accuracy of e (i.e., the number of terms in the summation). c) Write a program that computes the value of ex by using the formula 2

3

x x x x e = 1 + ----- + ----- + ----- + … 1! 2! 3!

Prompt the user for the desired accuracy of e (i.e., the number of terms in the summation).

Making a Difference 4.35 (Enforcing Privacy with Cryptography) The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field

Making a Difference

151

of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a simple scheme for encrypting and decrypting data. A company that wants to send data over the Internet has asked you to write a program that will encrypt it so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number. [Optional reading project: Research “public key cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may also want to investigate the RSA scheme, which is widely used in industrial-strength applications.] 4.36 (World Population Growth) World population has grown considerably over the centuries. Continued growth could eventually challenge the limits of breathable air, drinkable water, arable cropland and other limited resources. There is evidence that growth has been slowing in recent years and that world population could peak some time this century, then start to decline. For this exercise, research world population growth issues online. Be sure to investigate various viewpoints. Get estimates for the current world population and its growth rate (the percentage by which it is likely to increase this year). Write a program that calculates world population growth each year for the next 75 years, using the simplifying assumption that the current growth rate will stay constant. Print the results in a table. The first column should display the year from year 1 to year 75. The second column should display the anticipated world population at the end of that year. The third column should display the numerical increase in the world population that would occur that year. Using your results, determine the year in which the population would be double what it is today, if this year’s growth rate were to persist.

5 Not everything that can be counted counts, and not every thing that counts can be counted. —Albert Einstein

Who can control his fate? —William Shakespeare

The used key is always bright. —Benjamin Franklin

Intelligence … is the faculty of making artificial objects, especially tools to make tools.

—Henri Bergson

Objectives In this chapter you’ll learn: ■











The essentials of countercontrolled repetition. To use for and do…while to execute statements in a program repeatedly. To implement multiple selection using the switch selection statement. How break and continue alter the flow of control. To use the logical operators to form complex conditional expressions in control statements. To avoid the consequences of confusing the equality and assignment operators.

Control Statements: Part 2

5.1 Introduction 5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 for Repetition Statement 5.4 Examples Using the for Statement 5.5 do…while Repetition Statement 5.6 switch Multiple-Selection Statement

153

5.7 break and continue Statements 5.8 Logical Operators 5.9 Confusing the Equality (==) and Assignment (=) Operators 5.10 Structured Programming Summary 5.11 Wrap-Up

Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference

5.1 Introduction In this chapter, we continue our presentation of structured programming by introducing C++’s remaining control statements. The control statements we study here and those you learned in Chapter 4 will help you build and manipulate objects. We continue our early emphasis on object-oriented programming that began with a discussion of basic concepts in Chapter 1 and extensive object-oriented code examples and exercises in Chapters 3–4. In this chapter, we demonstrate the for, do…while and switch statements. Through short examples using while and for, we explore counter-controlled repetition. We expand the GradeBook class to use a switch statement to count the number of A, B, C, D and F grades in a set of letter grades entered by the user. We introduce the break and continue program control statements. We discuss the logical operators, which enable you to use more powerful conditional expressions. We also examine the common error of confusing the equality (==) and assignment (=) operators, and how to avoid it.

5.2 Essentials of Counter-Controlled Repetition This section uses the while repetition statement to formalize the elements required to perform counter-controlled repetition. Counter-controlled repetition requires 1. the name of a control variable (or loop counter) 2. the initial value of the control variable 3. the loop-continuation condition that tests for the final value of the control variable (i.e., whether looping should continue) 4. the increment (or decrement) by which the control variable is modified each time through the loop. The program in Fig. 5.1 prints the numbers from 1 to 10. The declaration in line 8 names the control variable (counter), declares it to be an integer, reserves space for it in memory and sets it to an initial value of 1. Declarations that require initialization are executable statements. In C++, it’s more precise to call a declaration that also reserves memory a definition. Because definitions are declarations, too, we’ll use the term “declaration” except when the distinction is important. Variable counter (line 8) also could have been declared and initializeed with int counter; // declare control variable counter = 1; // initialize control variable to 1

154 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

Chapter 5 Control Statements: Part 2

// Fig. 5.1: fig05_01.cpp // Counter-controlled repetition. #include using namespace std; int main() { int counter = 1; // declare and initialize control variable while ( counter <= 10 ) // loop-continuation condition { cout << counter << " "; ++counter; // increment control variable by 1 } // end while cout << endl; // output a newline } // end main

1 2 3 4 5 6 7 8 9 10

Fig. 5.1 | Counter-controlled repetition. Line 13 increments the loop counter by 1 each time the loop’s body is performed. The loop-continuation condition (line 10) in the while statement determines whether the value of the control variable is less than or equal to 10 (the final value for which the condition is true). The body of this while executes even when the control variable is 10. The loop terminates when the control variable is greater than 10 (i.e., when counter is 11). Figure 5.1 can be made more concise by initializing counter to 0 and by replacing the while statement with while ( ++counter <= 10 ) // loop-continuation condition cout << counter << " ";

This code saves a statement, because the incrementing is done in the while condition before the condition is tested. Also, the code eliminates the braces around the body of the while, because the while now contains only one statement. Coding in such a condensed fashion can lead to programs that are more difficult to read, debug, modify and maintain.

Common Programming Error 5.1

Floating-point values are approximate, so controlling counting loops with floating-point variables can result in imprecise counter values and inaccurate tests for termination.

Error-Prevention Tip 5.1

Control counting loops with integer values.

Good Programming Practice 5.1

Too many levels of nesting can make a program difficult to understand. As a rule, try to avoid using more than three levels of indentation.

5.3 for Repetition Statement

155

5.3 for Repetition Statement In addition to while, C++ provides the for repetition statement, which specifies the counter-controlled repetition details in a single line of code. To illustrate the power of for, let’s rewrite the program of Fig. 5.1. The result is shown in Fig. 5.2. 1 2 3 4 5 6 7 8 9 10 11 12 13 14

// Fig. 5.2: fig05_02.cpp // Counter-controlled repetition with the for statement. #include using namespace std; int main() { // for statement header includes initialization, // loop-continuation condition and increment. for ( int counter = 1; counter <= 10; ++counter ) cout << counter << " "; cout << endl; // output a newline } // end main

1 2 3 4 5 6 7 8 9 10

Fig. 5.2 | Counter-controlled repetition with the for statement. When the for statement (lines 10–11) begins executing, the control variable counter is declared and initialized to 1. Then, the loop-continuation condition (line 10 between the semicolons) counter <= 10 is checked. The initial value of counter is 1, so the condition is satisfied and the body statement (line 11) prints the value of counter, namely 1. Then, the expression ++counter increments control variable counter and the loop begins again with the loop-continuation test. The control variable is now equal to 2, so the final value is not exceeded and the program performs the body statement again. This process continues until the loop body has executed 10 times and the control variable counter is incremented to 11—this causes the loop-continuation test to fail and repetition to terminate. The program continues by performing the first statement after the for statement (in this case, the output statement in line 13).

Statement Header Components Figure 5.3 takes a closer look at the for statement header (line 10) of Fig. 5.2. Notice that the for statement header “does it all”—it specifies each of the items needed for countercontrolled repetition with a control variable. If there’s more than one statement in the body of the for, braces are required to enclose the body of the loop. If you incorrectly wrote counter < 10 as the loop-continuation condition in Fig. 5.2, then the loop would execute only 9 times. This is a common off-by-one error. for

Common Programming Error 5.2

Using an incorrect relational operator or using an incorrect final value of a loop counter in the condition of a while or for statement can cause off-by-one errors.

156

Chapter 5 Control Statements: Part 2

for

keyword

Control variable name

Required Final value of control Required semicolon semicolon variable for which separator the condition is true separator

for ( int counter = 1; counter <= 10; counter++ ) Initial value of control variable

Fig. 5.3 |

for

Loop-continuation condition

Increment of control variable

statement header components.

Good Programming Practice 5.2

Using the final value in the condition of a while or for statement and using the <= relational operator will help avoid off-by-one errors. For a loop used to print the values 1 to 10, for example, the loop-continuation condition should be counter <= 10 rather than counter < 10 (which is an off-by-one error) or counter < 11 (which is nevertheless correct). Many programmers prefer so-called zero-based counting, in which, to count 10 times through the loop, counter would be initialized to zero and the loop-continuation test would be counter < 10.

The general form of the for statement is for ( initialization; loopContinuationCondition; increment )

statement

where the initialization expression initializes the loop’s control variable, loopContinuationCondition determines whether the loop should continue executing and increment increments the control variable. In most cases, the for statement can be represented by an equivalent while statement, as follows: initialization; while ( loopContinuationCondition ) { }

statement increment;

There’s an exception to this rule, which we’ll discuss in Section 5.7. If the initialization expression declares the control variable (i.e., its type is specified before its name), the control variable can be used only in the body of the for statement— the control variable will be unknown outside the for statement. This restricted use of the control variable name is known as the variable’s scope. The scope of a variable specifies where it can be used in a program. Scope is discussed in detail in Chapter 6. As we’ll see, the initialization and increment expressions can be comma-separated lists of expressions. The commas, as used in these expressions, are comma operators, which guarantee that lists of expressions evaluate from left to right. The comma operator has the lowest precedence of all C++ operators. The value and type of a comma-separated list of expressions is the value and type of the rightmost expression. The comma operator is often used

5.3 for Repetition Statement

157

in for statements. Its primary application is to enable you to use multiple initialization expressions and/or multiple increment expressions. For example, there may be several control variables in a single for statement that must be initialized and incremented.

Good Programming Practice 5.3

Place only expressions involving the control variables in the initialization and increment sections of a for statement.

The three expressions in the for statement header are optional (but the two semicolon separators are required). If the loopContinuationCondition is omitted, C++ assumes that the condition is true, thus creating an infinite loop. One might omit the initialization expression if the control variable is initialized earlier in the program. One might omit the increment expression if the increment is calculated by statements in the body of the for or if no increment is needed. The increment expression in the for statement acts as a stand-alone statement at the end of the body of the for. Therefore, for integer counters, the expressions counter = counter + 1 counter += 1 ++counter counter++

are all equivalent in the increment expression (when no other code appears there). The integer variable being incremented here does not appear in a larger expression, so both preincrementing and postincrementing actually have the same effect.

Common Programming Error 5.3

Placing a semicolon immediately to the right of the right parenthesis of a for header makes the body of that for statement an empty statement. This is usually a logic error.

The initialization, loop-continuation condition and increment expressions of a for statement can contain arithmetic expressions. For example, if x = 2 and y = 10, and x and y are not modified in the loop body, the for header for ( int j = x; j <= 4 * x * y; j += y / x )

is equivalent to for ( int j = 2; j <= 80; j += 5 )

The “increment” of a for statement can be negative, in which case it’s really a decrement and the loop actually counts downward (as shown in Section 5.4). If the loop-continuation condition is initially false, the body of the for statement is not performed. Instead, execution proceeds with the statement following the for. Frequently, the control variable is printed or used in calculations in the body of a for statement, but this is not required. It’s common to use the control variable for controlling repetition while never mentioning it in the body of the for statement.

Error-Prevention Tip 5.2

Although the value of the control variable can be changed in the body of a for statement, avoid doing so, because this practice can lead to subtle logic errors.

158

Chapter 5 Control Statements: Part 2

Statement UML Activity Diagram The for repetition statement’s UML activity diagram is similar to that of the while statement (Fig. 4.6). Figure 5.4 shows the activity diagram of the for statement in Fig. 5.2. The diagram makes it clear that initialization occurs once before the loop-continuation test is evaluated the first time, and that incrementing occurs each time through the loop after the body statement executes. Note that (besides an initial state, transition arrows, a merge, a final state and several notes) the diagram contains only action states and a decision.

for

Initialize control variable

[counter <= 10]

int counter =

1

Display the counter value

Increment the control variable

cout cout << << counter counter << << "" "; ";

counter++

[counter > 10]

Determine whether looping should continue

Fig. 5.4 | UML activity diagram for the for statement in Fig. 5.2.

5.4 Examples Using the for Statement The following examples show methods of varying the control variable in a for statement. In each case, we write the appropriate for statement header. Note the change in the relational operator for loops that decrement the control variable. a) Vary the control variable from 1 to 100 in increments of 1. for ( int i = 1; i <= 100; ++i )

b) Vary the control variable from 100 down to 1 in decrements of 1. for ( int i = 100; i >= 1; --i )

c) Vary the control variable from 7 to 77 in steps of 7. for ( int i = 7; i <= 77; i += 7 )

d) Vary the control variable from 20 down to 2 in steps of -2. for ( int i = 20; i >= 2; i -= 2 )

5.4 Examples Using the for Statement e)

159

Vary the control variable over the following sequence of values: 2, 5, 8, 11, 14, 17. for ( int i = 2; i <= 17; i += 3 )

f)

Vary the control variable over the following sequence of values: 99, 88, 77, 66, 55. for ( int i = 99; i >= 55; i -= 11 )

Common Programming Error 5.4

Not using the proper relational operator in the loop-continuation condition of a loop that counts downward (such as incorrectly using i <= 1 instead of i >= 1 in a loop counting down to 1) is a logic error that yields incorrect results when the program runs.

Application: Summing the Even Integers from 2 to 20 The program of Fig. 5.5 uses a for statement to sum the even integers from 2 to 20. Each iteration of the loop (lines 11–12) adds control variable number’s value to variable total. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

// Fig. 5.5: fig05_05.cpp // Summing integers with the for statement. #include using namespace std; int main() { int total = 0; // initialize total // total even integers from 2 through 20 for ( int number = 2; number <= 20; number += 2 ) total += number; cout << "Sum is " << total << endl; // display results } // end main

Sum is 110

Fig. 5.5 | Summing integers with the for statement. The body of the for statement in Fig. 5.5 actually could be merged into the increment portion of the for header by using the comma operator as follows: for ( int number = 2; // initialization number <= 20; // loop continuation condition total += number, number += 2 ) // total and increment ; // empty body

Good Programming Practice 5.4

Although statements preceding a for and statements in the body of a for often can be merged into the for header, doing so can make the program more difficult to read, maintain, modify and debug.

160

Chapter 5 Control Statements: Part 2

Application: Compound Interest Calculations Consider the following problem statement: A person invests $1000.00 in a savings account yielding 5 percent interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts: a = p ( 1 + r )n where p is the original amount invested (i.e., the principal), r is the annual interest rate, n is the number of years and a is the amount on deposit at the end of the nth year.

The for statement (Fig. 5.6, lines 21–28) performs the indicated calculation for each of the 10 years the money remains on deposit, varying a control variable from 1 to 10 in increments of 1. C++ does not include an exponentiation operator, so we use the standard library function pow (line 24). The function pow(x, y) calculates the value of x raised to the yth power. In this example, the algebraic expression (1 + r)n is written as pow(1.0 + rate, year), where variable rate represents r and variable year represents n. Function pow takes two arguments of type double and returns a double value. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

// Fig. 5.6: fig05_06.cpp // Compound interest calculations with for. #include #include #include // standard C++ math library using namespace std; int main() { double amount; // amount on deposit at end of each year double principal = 1000.0; // initial amount before interest double rate = .05; // interest rate // display headers cout << "Year" << setw( 21 ) << "Amount on deposit" << endl; // set floating-point number format cout << fixed << setprecision( 2 ); // calculate amount on deposit for each of ten years for ( int year = 1; year <= 10; ++year ) { // calculate new amount for specified year amount = principal * pow( 1.0 + rate, year ); // display the year and the amount cout << setw( 4 ) << year << setw( 21 ) << amount << endl; } // end for } // end main

Fig. 5.6 | Compound interest calculations with for. (Part 1 of 2.)

5.4 Examples Using the for Statement

Year 1 2 3 4 5 6 7 8 9 10

161

Amount on deposit 1050.00 1102.50 1157.63 1215.51 1276.28 1340.10 1407.10 1477.46 1551.33 1628.89

Fig. 5.6 | Compound interest calculations with for. (Part 2 of 2.) This program will not compile without including header (line 5). Function requires two double arguments. Variable year is an integer. Header includes information that tells the compiler to convert the value of year to a temporary double representation before calling the function. This information is contained in pow’s function prototype. Chapter 6 summarizes other math library functions. pow

Common Programming Error 5.5

Forgetting to include the appropriate header when using standard library functions (e.g., in a program that uses math library functions) is a compilation error.



A Caution about Using Type float or double for Monetary Amounts Lines 10–12 declare the double variables amount, principal and rate. We did this for simplicity because we’re dealing with fractional parts of dollars, and we need a type that allows decimal points in its values. Unfortunately, this can cause trouble. Here’s a simple explanation of what can go wrong when using float or double to represent dollar amounts (assuming setprecision(2) is used to specify two digits of precision when printing): Two dollar amounts stored in the machine could be 14.234 (which prints as 14.23) and 18.673 (which prints as 18.67). When these amounts are added, they produce the internal sum 32.907, which prints as 32.91. Thus your printout could appear as 14.23 + 18.67 ------32.91

but a person adding the individual numbers as printed would expect the sum 32.90! You’ve been warned! In the exercises, we explore the use of integers to perform monetary calculations. [Note: Some third-party vendors sell C++ class libraries that perform precise monetary calculations.]

Good Programming Practice 5.5

Do not use variables of type float or double to perform monetary calculations. The imprecision of floating-point numbers can cause incorrect monetary values.

Using Stream Manipulators to Format Numeric Output The output statement in line 18 before the for loop and the output statement in line 27 in the for loop combine to print the values of the variables year and amount with the for-

162

Chapter 5 Control Statements: Part 2

matting specified by the parameterized stream manipulators setprecision and setw and the nonparameterized stream manipulator fixed. The stream manipulator setw(4) specifies that the next value output should appear in a field width of 4—i.e., cout prints the value with at least 4 character positions. If the value to be output is less than 4 character positions wide, the value is right justified in the field by default. If the value to be output is more than 4 character positions wide, the field width is extended to accommodate the entire value. To indicate that values should be output left justified, simply output nonparameterized stream manipulator left (found in header ). Right justification can be restored by outputting nonparameterized stream manipulator right. The other formatting in the output statements indicates that variable amount is printed as a fixed-point value with a decimal point (specified in line 18 with the stream manipulator fixed) right justified in a field of 21 character positions (specified in line 27 with setw(21)) and two digits of precision to the right of the decimal point (specified in line 18 with manipulator setprecision(2)). We applied the stream manipulators fixed and setprecision to the output stream (i.e., cout) before the for loop because these format settings remain in effect until they’re changed—such settings are called sticky settings and they do not need to be applied during each iteration of the loop. However, the field width specified with setw applies only to the next value output. We discuss C++’s powerful input/output formatting capabilities in Chapter 15, Stream Input/Output. The calculation 1.0 + rate, which appears as an argument to the pow function, is contained in the body of the for statement. In fact, this calculation produces the same result during each iteration of the loop, so repeating it is wasteful—it should be performed once before the loop.

Performance Tip 5.1

Avoid placing expressions whose values do not change inside loops—but, even if you do, many of today’s sophisticated optimizing compilers will automatically place such expressions outside the loops in the generated machine-language code.

Performance Tip 5.2

Many compilers contain optimization features that improve the performance of the code you write, but it’s still better to write good code from the start.

Be sure to try our Peter Minuit problem in Exercise 5.29. This problem demonstrates the wonders of compound interest.

5.5 do…while Repetition Statement The do…while repetition statement is similar to the while statement. In the while statement, the loop-continuation condition test occurs at the beginning of the loop before the body of the loop executes. The do…while statement tests the loop-continuation condition after the loop body executes; therefore, the loop body always executes at least once. When a do…while terminates, execution continues with the statement after the while clause. It’s not necessary to use braces in the do…while statement if there’s only one statement in the body; however, most programmers include the braces to avoid confusion between the while and do…while statements. For example, while ( condition )

5.5 do…while Repetition Statement normally is regarded as the header of a while statement. A around the single statement body appears as do

do…while

163

with no braces

statement

while ( condition );

which can be confusing. You might misinterpret the last line—while( condition );—as a statement containing as its body an empty statement. Thus, the do…while with one statement often is written as follows to avoid confusion: while

do {

statement

} while ( condition );

Figure 5.7 uses a do…while statement to print the numbers 1–10. Upon entering the statement, line 12 outputs counter’s value and line 13 increments counter. Then the program evaluates the loop-continuation test at the bottom of the loop (line 14). If the condition is true, the loop continues from the first body statement in the do…while (line 12). If the condition is false, the loop terminates and the program continues with the next statement after the loop (line 16). do…while

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

// Fig. 5.7: fig05_07.cpp // do...while repetition statement. #include using namespace std; int main() { int counter = 1; // initialize counter do {

cout << counter << " "; // display counter ++counter; // increment counter } while ( counter <= 10 ); // end do...while cout << endl; // output a newline } // end main

1 2 3 4 5 6 7 8 9 10

Fig. 5.7 |

do…while

repetition statement.

do…while Statement UML Activity Diagram Figure 5.8 contains the do…while statement’s UML activity diagram, which makes it clear that the loop-continuation condition is not evaluated until after the loop performs its body at least once. Compare this activity diagram with that of the while statement (Fig. 4.6).

164

Chapter 5 Control Statements: Part 2

cout << counter << " ";

counter++

Determine whether looping should continue

Display the counter value

Increment the control variable

[counter <= 10] [counter > 10]

Fig. 5.8 | UML activity diagram for the do…while repetition statement of Fig. 5.7.

5.6 switch Multiple-Selection Statement C++ provides the switch multiple-selection statement to perform many different actions based on the possible values of a variable or expression. Each action is associated with the value of a constant integral expression (i.e., any combination of character and integer constants that evaluates to a constant integer value).

Class with switch Statement to Count A, B, C, D and F Grades This next version of the GradeBook class asks the user to enter a set of letter grades, then displays a summary of the number of students who received each grade. The class uses a switch to determine whether each grade entered is an A, B, C, D or F and to increment the appropriate grade counter. Class GradeBook is defined in Fig. 5.9, and its memberfunction definitions appear in Fig. 5.10. Figure 5.11 shows sample inputs and outputs of the main program that uses class GradeBook to process a set of grades. Like earlier versions of the class definition, the GradeBook class definition (Fig. 5.9) contains function prototypes for member functions setCourseName (line 12), getCourseName (line 13) and displayMessage (line 14), as well as the class’s constructor (line 11). The class definition also declares private data member courseName (line 18). Class GradeBook (Fig. 5.9) now contains five additional private data members (lines 19–23)—counter variables for each grade category (i.e., A, B, C, D and F). The class also contains two additional public member functions—inputGrades and displayGradeReport. Member function inputGrades (declared in line 15) reads an arbitrary number of letter grades from the user using sentinel-controlled repetition and updates the appropriate GradeBook

5.6 switch Multiple-Selection Statement

165

grade counter for each grade entered. Member function displayGradeReport (declared in line 16) outputs a report containing the number of students who received each letter grade. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

// Fig. 5.9: GradeBook.h // Definition of class GradeBook that counts A, B, C, D and F grades. // Member functions are defined in GradeBook.cpp #include // program uses C++ standard string class using namespace std; // GradeBook class definition class GradeBook { public: GradeBook( string ); // constructor initializes course name void setCourseName( string ); // function to set the course name string getCourseName(); // function to retrieve the course name void displayMessage(); // display a welcome message void inputGrades(); // input arbitrary number of grades from user void displayGradeReport(); // display a report based on the grades private: string courseName; // course name for this GradeBook int aCount; // count of A grades int bCount; // count of B grades int cCount; // count of C grades int dCount; // count of D grades int fCount; // count of F grades }; // end class GradeBook

Fig. 5.9 |

GradeBook

class definition.

Source-code file GradeBook.cpp (Fig. 5.10) contains the member-function definitions for class GradeBook. Notice that lines 13–17 in the constructor initialize the five grade counters to 0—when a GradeBook object is first created, no grades have been entered yet. As you’ll soon see, these counters are incremented in member function inputGrades as the user enters grades. The definitions of member functions setCourseName, getCourseName and displayMessage are identical to those in the earlier versions of class GradeBook. 1 2 3 4 5 6 7 8 9 10 11 12

// Fig. 5.10: GradeBook.cpp // Member-function definitions for class GradeBook that // uses a switch statement to count A, B, C, D and F grades. #include #include "GradeBook.h" // include definition of class GradeBook using namespace std; // constructor initializes courseName with string supplied as argument; // initializes counter data members to 0 GradeBook::GradeBook( string name ) { setCourseName( name ); // validate and store courseName

Fig. 5.10 |

GradeBook

class uses switch statement to count letter grades. (Part 1 of 3.)

166 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

Chapter 5 Control Statements: Part 2

aCount = 0; // initialize count bCount = 0; // initialize count cCount = 0; // initialize count dCount = 0; // initialize count fCount = 0; // initialize count } // end GradeBook constructor

of of of of of

A B C D F

grades grades grades grades grades

to to to to to

0 0 0 0 0

// function to set the course name; limits name to 25 or fewer characters void GradeBook::setCourseName( string name ) { if ( name.length() <= 25 ) // if name has 25 or fewer characters courseName = name; // store the course name in the object else // if name is longer than 25 characters { // set courseName to first 25 characters of parameter name courseName = name.substr( 0, 25 ); // select first 25 characters cout << "Name \"" << name << "\" exceeds maximum length (25).\n" << "Limiting courseName to first 25 characters.\n" << endl; } // end if...else } // end function setCourseName // function to retrieve the course name string GradeBook::getCourseName() { return courseName; } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { // this statement calls getCourseName to get the // name of the course this GradeBook represents cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" << endl; } // end function displayMessage // input arbitrary number of grades from user; update grade counter void GradeBook::inputGrades() { int grade; // grade entered by user cout << "Enter the letter grades." << endl << "Enter the EOF character to end input." << endl; // loop until user types end-of-file key sequence while ( ( grade = cin.get() ) != EOF ) { // determine which grade was entered switch ( grade ) // switch statement nested in while { case 'A': // grade was uppercase A case 'a': // or lowercase a ++aCount; // increment aCount break; // necessary to exit switch

Fig. 5.10 |

GradeBook

class uses switch statement to count letter grades. (Part 2 of 3.)

5.6 switch Multiple-Selection Statement 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 101 102 103 104 105 106 107 108 109 110 111

case 'B': // case 'b': // ++bCount; break; //

grade was uppercase B or lowercase b // increment bCount exit switch

case 'C': // case 'c': // ++cCount; break; //

grade was uppercase C or lowercase c // increment cCount exit switch

case 'D': // case 'd': // ++dCount; break; //

grade was uppercase D or lowercase d // increment dCount exit switch

case 'F': // case 'f': // ++fCount; break; //

grade was uppercase F or lowercase f // increment fCount exit switch

167

case '\n': // ignore newlines, case '\t': // tabs, case ' ': // and spaces in input break; // exit switch default: // catch all other characters cout << "Incorrect letter grade entered." << " Enter a new grade." << endl; break; // optional; will exit switch anyway } // end switch } // end while } // end function inputGrades // display a report based on the grades entered void GradeBook::displayGradeReport() { // output summary of results cout << "\n\nNumber of students who received << "\nA: " << aCount // display number of << "\nB: " << bCount // display number of << "\nC: " << cCount // display number of << "\nD: " << dCount // display number of << "\nF: " << fCount // display number of << endl; } // end function displayGradeReport

Fig. 5.10 |

GradeBook

by user

each letter grade:" A grades B grades C grades D grades F grades

class uses switch statement to count letter grades. (Part 3 of 3.)

Reading Character Input The user enters letter grades for a course in member function inputGrades (lines 49–98). In the while header, in line 57, the parenthesized assignment (grade = cin.get()) executes first. The cin.get() function reads one character from the keyboard and stores that character in integer variable grade (declared in line 51). Normally, characters are stored in

168

Chapter 5 Control Statements: Part 2

variables of type char; however, characters can be stored in any integer data type, because types short, int and long are guaranteed to be at least as big as type char. Thus, we can treat a character either as an integer or as a character, depending on its use. For example, the statement cout << "The character (" << 'a' << ") has the value " << static_cast< int > ( 'a' ) << endl;

prints the character a and its integer value as follows: The character (a) has the value 97

The integer 97 is the character’s numerical representation in the computer. Most computers today use the Unicode character set in which 97 represents the lowercase letter 'a'. Appendix B shows the characters and decimal equivalents from the ASCII (American Standard Code for Information Interchange) character set, which is a subset of Unicode. Generally, assignment statements have the value that’s assigned to the variable on the left side of the =. Thus, the value of the assignment expression grade = cin.get() is the same as the value returned by cin.get() and assigned to the variable grade. The fact that assignment expressions have values can be useful for assigning the same value to several variables. For example, a = b = c = 0;

first evaluates c = 0 (because the = operator associates from right to left). The variable b is then assigned the value of c = 0 (which is 0). Then, a is assigned the value of b = (c = 0) (which is also 0). In the program, the value of grade = cin.get() is compared with the value of EOF (a symbol whose acronym stands for “end-of-file”). We use EOF (which normally has the value –1) as the sentinel value. However, you do not type the value –1, nor do you type the letters EOF as the sentinel value. Rather, you type a system-dependent keystroke combination that means “end-of-file” to indicate that you have no more data to enter. EOF is a symbolic integer constant that is included into the program via the header1. If the value assigned to grade is equal to EOF, the while loop (lines 57–97) terminates. We’ve chosen to represent the characters entered into this program as ints, because EOF has type int. On UNIX/Linux systems and many others, end-of-file is entered by typing d

on a line by itself. This notation means to press and hold down the Ctrl key, then press the d key. On other systems such as Microsoft Windows, end-of-file can be entered by typing z

[Note: In some cases, you must press Enter after the preceding key sequence. Also, the characters ^Z sometimes appear on the screen to represent end-of-file, as shown in Fig. 5.11.]

Portability Tip 5.1

The keystroke combinations for entering end-of-file are system dependent.

1.

To compile this program on Linux, you’ll also need to include the header which defines the EOF constant.

5.6 switch Multiple-Selection Statement

169

Portability Tip 5.2

Testing for the symbolic constant EOF rather than –1 makes programs more portable. The ANSI/ISO C standard, from which C++ adopts the definition of EOF, states that EOF is a negative integral value, so EOF could have different values on different systems.

In this program, the user enters grades at the keyboard. When the user presses the Enter (or the Return) key, the characters are read by the cin.get() function, one character at a time. If the character entered is not end-of-file, the flow of control enters the switch statement (lines 60–96), which increments the appropriate letter-grade counter.

Statement Details The switch statement consists of a series of case labels and an optional default case. These are used in this example to determine which counter to increment, based on a grade. When the flow of control reaches the switch, the program evaluates the expression in the parentheses (i.e., grade) following keyword switch (line 60). This is called the controlling expression. The switch statement compares the value of the controlling expression with each case label. Assume the user enters the letter C as a grade. The program compares C to each case in the switch. If a match occurs (case 'C': in line 72), the program executes the statements for that case. For the letter C, line 74 increments cCount by 1. The break statement (line 75) causes program control to proceed with the first statement after the switch—in this program, control transfers to line 97. This line marks the end of the body of the while loop that inputs grades (lines 57–97), so control flows to the while’s condition (line 57) to determine whether the loop should continue executing. The cases in our switch explicitly test for the lowercase and uppercase versions of the letters A, B, C, D and F. Note the cases in lines 62–63 that test for the values 'A' and 'a' (both of which represent the grade A). Listing cases consecutively with no statements between them enables the cases to perform the same set of statements—when the controlling expression evaluates to either 'A' or 'a', the statements in lines 64–65 will execute. Each case can have multiple statements. The switch selection statement does not require braces around multiple statements in each case. Without break statements, each time a match occurs in the switch, the statements for that case and subsequent cases execute until a break statement or the end of the switch is encountered. This feature is perfect for writing a concise program that displays the iterative song “The Twelve Days of Christmas” in Exercise 5.28. switch

Common Programming Error 5.6

Forgetting a break statement when one is needed in a switch statement is a logic error.

Common Programming Error 5.7

Omitting the space between the word case and the integral value tested in a switch statement—e.g., writing case3: instead of case 3:—is a logic error. The switch statement will not perform the appropriate actions when the controlling expression has a value of 3.

Providing a default Case If no match occurs between the controlling expression’s value and a case label, the default case (lines 92–95) executes. We use the default case in this example to process all controlling-expression values that are neither valid grades nor newline, tab or space char-

170

Chapter 5 Control Statements: Part 2

acters. If no match occurs, the default case executes, and lines 93–94 print an error message indicating that an incorrect letter grade was entered. If no match occurs in a switch statement that does not contain a default case, program control continues with the first statement after the switch.

Error-Prevention Tip 5.3

Provide a default case in switch statements. Cases not explicitly tested in a switch statement without a default case are ignored. Including a default case focuses you on the need to process exceptional conditions. There are situations in which no default processing is needed. Although the case clauses and the default case clause in a switch statement can occur in any order, it’s common practice to place the default clause last.

Good Programming Practice 5.6

The last case in a switch statement does not require a break statement. Some programmers include this break for clarity and for symmetry with other cases.

Ignoring Newline, Tab and Blank Characters in Input Lines 87–90 in the switch statement of Fig. 5.10 cause the program to skip newline, tab and blank characters. Reading characters one at a time can cause problems. To have the program read the characters, we must send them to the computer by pressing the Enter key. This places a newline character in the input after the character we wish to process. Often, this newline character must be specially processed. By including these cases in our switch statement, we prevent the error message in the default case from being printed each time a newline, tab or space is encountered in the input. Testing Class GradeBook Figure 5.11 creates a GradeBook object (line 8). Line 10 invokes the its displayMessage member function to output a welcome message to the user. Line 11 invokes member function object’s inputGrades to read a set of grades from the user and keep track of how many students received each grade. The output window in Fig. 5.11 shows an error message displayed in response to entering an invalid grade (i.e., E). Line 12 invokes GradeBook member function displayGradeReport (defined in lines 101–111 of Fig. 5.10), which outputs a report based on the grades entered (as in the output in Fig. 5.11). 1 2 3 4 5 6 7 8 9 10 11 12 13

// Fig. 5.11: fig05_11.cpp // Create GradeBook object, input grades and display grade report. #include "GradeBook.h" // include definition of class GradeBook int main() { // create GradeBook object GradeBook myGradeBook( "CS101 C++ Programming" ); myGradeBook.displayMessage(); // display welcome message myGradeBook.inputGrades(); // read grades from user myGradeBook.displayGradeReport(); // display report based on grades } // end main

Fig. 5.11 | Creating a GradeBook object and calling its member functions. (Part 1 of 2.)

5.6 switch Multiple-Selection Statement

171

Welcome to the grade book for CS101 C++ Programming! Enter the letter grades. Enter the EOF character to end input. a B c C A d f C E Incorrect letter grade entered. Enter a new grade. D A b ^Z Number of students who received each letter grade: A: 3 B: 2 C: 3 D: 2 F: 1

Fig. 5.11 | Creating a GradeBook object and calling its member functions. (Part 2 of 2.) Statement UML Activity Diagram Figure 5.12 shows the UML activity diagram for the general switch multiple-selection statement. Most switch statements use a break in each case to terminate the switch statement after processing the case. Figure 5.12 emphasizes this by including break statements in the activity diagram. Without the break statement, control would not transfer to the first statement after the switch statement after a case is processed. Instead, control would transfer to the next case’s actions. The diagram makes it clear that the break statement at the end of a case causes control to exit the switch statement immediately. Again, note that (besides an initial state, transition arrows, a final state and several notes) the diagram contains action states and decisions. Also, the diagram uses merge symbols to merge the transitions from the break statements to the final state. When using the switch statement, remember that each case can be used to test only a constant integral expression—any combination of character constants and integer constants that evaluates to a constant integer value. A character constant is represented as the specific character in single quotes, such as 'A'. An integer constant is simply an integer value. Also, each case label can specify only one constant integral expression. switch

Common Programming Error 5.8

Specifying a nonconstant integral expression in a switch’s case label is a syntax error.

172

Chapter 5 Control Statements: Part 2

[true]

case a

case a actions(s)

break

case b actions(s)

break

case z actions(s)

break

[false] [true]

case b

[false]

...

[true]

case z

[false] default actions(s)

Fig. 5.12 |

switch multiple-selection statement UML activity diagram with break statements.

Common Programming Error 5.9

Providing case labels with identical values in a switch statement is a compilation error.

In Chapter 13, we present a more elegant way to implement switch logic. We’ll use a technique called polymorphism to create programs that are often clearer, more concise, easier to maintain and easier to extend than programs that use switch logic.

Notes on Data Types C++ has flexible data type sizes (see Appendix C, Fundamental Types). Different applications, for example, might need integers of different sizes. C++ provides several integer types. The range of integer values for each type depends on the particular computer’s hardware. In addition to the types int and char, C++ provides the types short (an abbreviation of short int) and long (an abbreviation of long int). The minimum range of values for short integers is –32,768 to 32,767. For the vast majority of integer calculations, long integers are sufficient. The minimum range of values for long integers is –2,147,483,648 to 2,147,483,647. On most computers, ints are equivalent either to short or to long. The range of values for an int is at least the same as that for short integers and no larger than that for long integers. The data type char can be used to represent any of the characters in the computer’s character set. It also can be used to represent small integers.

5.7 break and continue Statements

173

5.7 break and continue Statements C++ also provides statements break and continue to alter the flow of control. The preceding section showed how break can be used to terminate a switch statement’s execution. This section discusses how to use break in a repetition statement.

Statement The break statement, when executed in a while, for, do…while or switch statement, causes immediate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch statement. Figure 5.13 demonstrates the break statement (line 13) exiting a for repetition statement. break

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

// Fig. 5.13: fig05_13.cpp // break statement exiting a for statement. #include using namespace std; int main() { int count; // control variable also used after loop terminates for ( count = 1; count <= 10; ++count ) // loop 10 times { if ( count == 5 ) break; // break loop only if count is 5 cout << count << " "; } // end for cout << "\nBroke out of loop at count = " << count << endl; } // end main

1 2 3 4 Broke out of loop at count = 5

Fig. 5.13 |

break

statement exiting a for statement.

When the if statement detects that count is 5, the break statement executes. This terminates the for statement, and the program proceeds to line 18 (immediately after the for statement), which displays a message indicating the control variable value that terminated the loop. The for statement fully executes its body only four times instead of 10. The control variable count is defined outside the for statement header, so that we can use the control variable both in the loop’s body and after the loop completes its execution. continue Statement The continue statement, when executed in a while, for or do…while statement, skips the remaining statements in the body of that statement and proceeds with the next iteration of the loop. In while and do…while statements, the loop-continuation test evaluates immediately after the continue statement executes. In the for statement, the increment expression executes, then the loop-continuation test evaluates.

174

Chapter 5 Control Statements: Part 2

Figure 5.14 uses the continue statement (line 11) in a for statement to skip the output statement (line 13) when the nested if (lines 10–11) determines that the value of count is 5. When the continue statement executes, program control continues with the increment of the control variable in the for header (line 8) and loops five more times. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

// Fig. 5.14: fig05_14.cpp // continue statement terminating an iteration of a for statement. #include using namespace std; int main() { for ( int count = 1; count <= 10; ++count ) // loop 10 times { if ( count == 5 ) // if count is 5, continue; // skip remaining code in loop cout << count << " "; } // end for cout << "\nUsed continue to skip printing 5" << endl; } // end main

1 2 3 4 6 7 8 9 10 Used continue to skip printing 5

Fig. 5.14 |

continue

statement terminating an iteration of a for statement.

In Section 5.3, we stated that the while statement could be used in most cases to represent the for statement. The one exception occurs when the increment expression in the while statement follows the continue statement. In this case, the increment does not execute before the program tests the loop-continuation condition, and the while does not execute in the same manner as the for.

Good Programming Practice 5.7

Some programmers feel that break and continue violate structured programming. The effects of these statements can be achieved by structured programming techniques we soon will learn, so these programmers do not use break and continue. Most programmers consider the use of break in switch statements acceptable.

Software Engineering Observation 5.1

There’s a tension between achieving quality software engineering and achieving the bestperforming software. Often, one of these goals is achieved at the expense of the other. For all but the most performance-intensive situations, apply the following guidelines: First, make your code simple and correct; then make it fast and small, but only if necessary.

5.8 Logical Operators So far we’ve studied only simple conditions, such as counter <= 10, total > 1000 and numthese conditions in terms of the relational operators

ber != sentinelValue. We expressed

5.8 Logical Operators

175

>, <, >=

and <=, and the equality operators == and !=. Each decision tested precisely one condition. To test multiple conditions while making a decision, we performed these tests in separate statements or in nested if or if…else statements. C++ provides logical operators that are used to form more complex conditions by combining simple conditions. The logical operators are && (logical AND), || (logical OR) and ! (logical NOT, also called logical negation).

Logical AND (&&) Operator Suppose that we wish to ensure that two conditions are both true before we choose a certain path of execution. In this case, we can use the && (logical AND) operator, as follows: if ( gender == 1 && age >= 65 ) ++seniorFemales;

This if statement contains two simple conditions. The condition gender == 1 is used here to determine whether a person is a female. The condition age >= 65 determines whether a person is a senior citizen. The simple condition to the left of the && operator evaluates first. If necessary, the simple condition to the right of the && operator evaluates next. As we’ll discuss shortly, the right side of a logical AND expression is evaluated only if the left side is true. The if statement then considers the combined condition gender == 1 && age >= 65

This condition is true if and only if both of the simple conditions are true. Finally, if this combined condition is indeed true, the statement in the if statement’s body increments the count of seniorFemales. If either (or both) of the simple conditions are false, then the program skips the incrementing and proceeds to the statement following the if. The preceding combined condition can be made more readable by adding redundant parentheses: ( gender == 1 ) && ( age >= 65 )

Common Programming Error 5.10

Although 3 < x < 7 is a mathematically correct condition, it does not evaluate as you might expect in C++. Use ( 3 < x && x < 7 ) to get the proper evaluation in C++.

Figure 5.15 summarizes the && operator. The table shows all four possible combinations of false and true values for expression1 and expression2. Such tables are often called truth tables. C++ evaluates to false or true all expressions that include relational operators, equality operators and/or logical operators. expression1

expression2

expression1 && expression2

false

false

false

false

true

false

true

false

false

true

true

true

Fig. 5.15 |

&&

(logical AND) operator truth table.

176

Chapter 5 Control Statements: Part 2

Logical OR (||) Operator Now let’s consider the || (logical OR) operator. Suppose we wish to ensure that either or both of two conditions are true before we choose a certain path of execution. In this case, we use the || operator, as in the following program segment: if ( ( semesterAverage >= 90 ) || ( finalExam >= 90 ) ) cout << "Student grade is A" << endl;

This preceding condition contains two simple conditions. The simple condition

semesterAverage >= 90 evaluates to determine whether the student deserves an “A” in the

course because of a solid performance throughout the semester. The simple condition finalExam >= 90 evaluates to determine whether the student deserves an “A” in the course because of an outstanding performance on the final exam. The if statement then considers the combined condition ( semesterAverage >= 90 ) || ( finalExam >= 90 )

and awards the student an “A” if either or both of the simple conditions are true. The message “Student grade is A” prints unless both of the simple conditions are false. Figure 5.16 is a truth table for the logical OR operator (||). expression1

expression2

expression1 || expression2

false

false

false

false

true

true

true

false

true

true

true

true

Fig. 5.16 |

||

(logical OR) operator truth table.

The && operator has a higher precedence than the || operator. Both operators associate from left to right. An expression containing && or || operators evaluates only until the truth or falsehood of the expression is known. Thus, evaluation of the expression ( gender == 1 ) && ( age >= 65 )

stops immediately if gender is not equal to 1 (i.e., the entire expression is false) and continues if gender is equal to 1 (i.e., the entire expression could still be true if the condition age >= 65 is true). This performance feature for the evaluation of logical AND and logical OR expressions is called short-circuit evaluation.

Performance Tip 5.3

In expressions using operator &&, if the separate conditions are independent of one another, make the condition most likely to be false the leftmost condition. In expressions using operator ||, make the condition most likely to be true the leftmost condition. This use of short-circuit evaluation can reduce a program’s execution time.

Logical Negation (!) Operator C++ provides the ! (logical NOT, also called logical negation) operator to “reverse” a condition’s meaning. The unary logical negation operator has only a single condition as an

5.8 Logical Operators

177

operand. The unary logical negation operator is placed before a condition when we are interested in choosing a path of execution if the original condition (without the logical negation operator) is false, such as in the following program segment: if ( !( grade == sentinelValue ) ) cout << "The next grade is " << grade << endl;

The parentheses around the condition grade == sentinelValue are needed because the logical negation operator has a higher precedence than the equality operator. You can often avoid the ! operator by using an appropriate relational or equality operator. For example, the preceding if statement also can be written as follows: if ( grade != sentinelValue ) cout << "The next grade is " << grade << endl;

This flexibility often can help you express a condition in a more “natural” or convenient manner. Figure 5.17 is a truth table for the logical negation operator (!). expression

!expression

false

true

true

false

Fig. 5.17 | ! (logical negation) operator truth table.

Logical Operators Example Figure 5.18 demonstrates the logical operators by producing their truth tables. The output shows each expression that’s evaluated and its bool result. By default, bool values true and false are displayed by cout and the stream insertion operator as 1 and 0, respectively. We use stream manipulator boolalpha (a sticky manipulator) in line 9 to specify that the value of each bool expression should be displayed as either the word “true” or the word “false.” For example, the result of the expression false && false in line 10 is false, so the second line of output includes the word “false.” Lines 9–13 produce the truth table for &&. Lines 16–20 produce the truth table for ||. Lines 23–25 produce the truth table for !. 1 2 3 4 5 6 7 8 9 10 11 12 13

// Fig. 5.18: fig05_18.cpp // Logical operators. #include using namespace std; int main() { // create truth table for && (logical AND) operator cout << boolalpha << "Logical AND (&&)" << "\nfalse && false: " << ( false && false ) << "\nfalse && true: " << ( false && true ) << "\ntrue && false: " << ( true && false ) << "\ntrue && true: " << ( true && true ) << "\n\n";

Fig. 5.18 | Logical operators. (Part 1 of 2.)

178 14 15 16 17 18 19 20 21 22 23 24 25 26

Chapter 5 Control Statements: Part 2

// create truth table for || (logical OR) operator cout << "Logical OR (||)" << "\nfalse || false: " << ( false || false ) << "\nfalse || true: " << ( false || true ) << "\ntrue || false: " << ( true || false ) << "\ntrue || true: " << ( true || true ) << "\n\n"; // create truth table for ! (logical negation) operator cout << "Logical NOT (!)" << "\n!false: " << ( !false ) << "\n!true: " << ( !true ) << endl; } // end main

Logical AND (&&) false && false: false false && true: false true && false: false true && true: true Logical OR (||) false || false: false false || true: true true || false: true true || true: true Logical NOT (!) !false: true !true: false

Fig. 5.18 | Logical operators. (Part 2 of 2.) Summary of Operator Precedence and Associativity Figure 5.19 adds the logical and comma operators to the operator precedence and associativity chart. The operators are shown from top to bottom, in decreasing order of precedence. Operators

Associativity

Type

::

left to right [See caution in Fig. 2.10] left to right right to left left to right left to right left to right left to right left to right

scope resolution grouping parentheses unary (postfix) unary (prefix) multiplicative additive insertion/extraction relational equality

() ++

--

static_cast<

++

--

+

*

/

%

+

-

<<

>>

<

<=

==

!=

>

-

>=

!

type

>()

Fig. 5.19 | Operator precedence and associativity. (Part 1 of 2.)

5.9 Confusing the Equality (==) and Assignment (=) Operators

Operators

Associativity

Type

&&

left to right left to right right to left right to left left to right

logical AND logical OR conditional assignment comma

|| ?: =

+=

-=

*=

,

/=

%=

179

Fig. 5.19 | Operator precedence and associativity. (Part 2 of 2.)

5.9 Confusing the Equality (==) and Assignment (=) Operators There’s one error that C++ programmers, no matter how experienced, tend to make so frequently that we feel it requires a separate section. That error is accidentally swapping the operators == (equality) and = (assignment). What makes this so damaging is that it ordinarily does not cause syntax errors—statements with these errors tend to compile correctly and the programs run to completion, often generating incorrect results through runtime logic errors. Some compilers issue a warning when = is used in a context where == is expected. Two aspects of C++ contribute to these problems. One is that any expression that produces a value can be used in the decision portion of any control statement. If the value of the expression is zero, it’s treated as false, and if the value is nonzero, it’s treated as true. The second is that assignments produce a value—namely, the value assigned to the variable on the left side of the assignment operator. For example, suppose we intend to write if ( payCode == 4 ) cout << "You get a bonus!" << endl;

but we accidentally write if ( payCode = 4 ) cout << "You get a bonus!" << endl;

The first if statement properly awards a bonus to the person whose payCode is equal to 4. The second one—with the error—evaluates the assignment expression in the if condition to the constant 4. Any nonzero value is interpreted as true, so this condition is always true and the person always receives a bonus regardless of what the actual paycode is! Even worse, the paycode has been modified when it was only supposed to be examined!

Common Programming Error 5.11

Using operator == for assignment and using operator = for equality are logic errors.

Error-Prevention Tip 5.4

Programmers normally write conditions such as x == 7 with the variable name on the left and the constant on the right. By placing the constant on the left, as in 7 == x, you’ll be protected by the compiler if you accidentally replace the == operator with = . The compiler treats this as a compilation error, because you can’t change the value of a constant. This will prevent the potential devastation of a runtime logic error.

180

Chapter 5 Control Statements: Part 2

Variable names are said to be lvalues (for “left values”) because they can be used on the left side of an assignment operator. Constants are said to be rvalues (for “right values”) because they can be used on only the right side of an assignment operator. Lvalues can also be used as rvalues, but not vice versa. There’s another equally unpleasant situation. Suppose you want to assign a value to a variable with a simple statement like x = 1;

but instead write x == 1;

Here, too, this is not a syntax error. Rather, the compiler simply evaluates the conditional expression. If x is equal to 1, the condition is true and the expression evaluates to the value true. If x is not equal to 1, the condition is false and the expression evaluates to the value false. Regardless of the expression’s value, there’s no assignment operator, so the value simply is lost. The value of x remains unaltered, probably causing an execution-time logic error. Unfortunately, we do not have a handy trick available to help you with this problem!

Error-Prevention Tip 5.5

Use your text editor to search for all occurrences of = in your program and check that you have the correct assignment operator or logical operator in each place.

5.10 Structured Programming Summary Just as architects design buildings by employing the collective wisdom of their profession, so should programmers design programs. Our field is younger than architecture is, and our collective wisdom is sparser. We’ve learned that structured programming produces programs that are easier than unstructured programs to understand, test, debug, modify, and even prove correct in a mathematical sense. Figure 5.20 uses activity diagrams to summarize C++’s control statements. The initial and final states indicate the single entry point and the single exit point of each control statement. Arbitrarily connecting individual symbols in an activity diagram can lead to unstructured programs. Therefore, the programming profession uses only a limited set of control statements that can be combined in only two simple ways to build structured programs. For simplicity, only single-entry/single-exit control statements are used—there’s only one way to enter and only one way to exit each control statement. Connecting control statements in sequence to form structured programs is simple—the final state of one control statement is connected to the initial state of the next—that is, they’re placed one after another in a program. We’ve called this control-statement stacking. The rules for forming structured programs also allow for control statements to be nested. Figure 5.21 shows the rules for forming structured programs. The rules assume that action states may be used to indicate any action. The rules also assume that we begin with the so-called simplest activity diagram (Fig. 5.22), consisting of only an initial state, an action state, a final state and transition arrows. Applying the rules of Fig. 5.21 always results in an activity diagram with a neat, building-block appearance. For example, repeatedly applying Rule 2 to the simplest

5.10 Structured Programming Summary

Sequence

181

Selection switch statement with breaks (multiple selection)

if statement

(single selection) [t]

[t]

[f]

break

[f]

...

[t]

break

[f]

if…else statement

(double selection)

...

[f]

[t] [t]

break

[f] default processing

Repetition while statement

do…while statement

for statement

initialization [t] [f]

[t] [f] [t]

body

increment

[f]

Fig. 5.20 | C++’s single-entry/single-exit sequence, selection and repetition statements.

182

Chapter 5 Control Statements: Part 2

Rules for forming structured programs 1) 2) 3) 4)

Begin with the “simplest activity diagram” (Fig. 5.22). Any action state can be replaced by two action states in sequence. Any action state can be replaced by any control statement (sequence, if, if…else, switch, while, do…while or for). Rules 2 and 3 can be applied as often as you like and in any order.

Fig. 5.21 | Rules for forming structured programs.

action state

Fig. 5.22 | Simplest activity diagram. activity diagram results in an activity diagram containing many action states in sequence (Fig. 5.23). Rule 2 generates a stack of control statements, so let’s call Rule 2 the stacking rule. The vertical dashed lines in Fig. 5.23 are not part of the UML. We use them to separate the four activity diagrams that demonstrate Rule 2 of Fig. 5.21 being applied. apply Rule 2

action state

apply Rule 2

apply Rule 2

action state

action state

action state

action state

action state

...

action state

action state

Fig. 5.23 | Repeatedly applying Rule 2 of Fig. 5.21 to the simplest activity diagram.

5.10 Structured Programming Summary

183

Rule 3 is the nesting rule. Repeatedly applying Rule 3 to the simplest activity diagram results in one with neatly nested control statements. For example, in Fig. 5.24, the action state in the simplest activity diagram is replaced with a double-selection (if…else) statement. Then Rule 3 is applied again to the action states in the double-selection statement, replacing each with a double-selection statement. The dashed action-state symbols around each of the double-selection statements represent an action state that was replaced in the preceding activity diagram. [Note: The dashed arrows and dashed action state symbols shown in Fig. 5.24 are not part of the UML. They’re used here as pedagogic devices to illustrate that any action state may be replaced with a control statement.]

apply Rule 3

action state apply Rule 3

action state

action state

action state

apply Rule 3

action state

action state

action state

Fig. 5.24 | Applying Rule 3 of Fig. 5.21 to the simplest activity diagram several times. Rule 4 generates larger, more involved and more deeply nested statements. The diagrams that emerge from applying the rules in Fig. 5.21 constitute the set of all possible

184

Chapter 5 Control Statements: Part 2

activity diagrams and hence the set of all possible structured programs. The beauty of the structured approach is that we use only seven simple single-entry/single-exit control statements and assemble them in only two simple ways. If the rules in Fig. 5.21 are followed, an activity diagram with illegal syntax (such as that in Fig. 5.25) cannot be created. If you’re uncertain about whether a particular diagram is legal, apply the rules of Fig. 5.21 in reverse to reduce the diagram to the simplest activity diagram. If it’s reducible to the simplest activity diagram, the original diagram is structured; otherwise, it isn’t. action state

action state

action state

action state

Fig. 5.25 | Activity diagram with illegal syntax. Structured programming promotes simplicity. Böhm and Jacopini have given us the result that only three forms of control are needed: • Sequence • Selection • Repetition The sequence structure is trivial. Simply list the statements to execute in the order in which they should execute. Selection is implemented in one of three ways: • if statement (single selection) • if…else statement (double selection) • switch statement (multiple selection) It’s straightforward to prove that the simple if statement is sufficient to provide any form of selection—everything that can be done with the if…else statement and the switch statement can be implemented (although perhaps not as clearly and efficiently) by combining if statements. Repetition is implemented in one of three ways: • while statement • do…while statement • for statement It’s straightforward to prove that the while statement is sufficient to provide any form of repetition. Everything that can be done with the do…while statement and the for statement can be done (although perhaps not as smoothly) with the while statement.

5.11 Wrap-Up

185

Combining these results illustrates that any form of control ever needed in a C++ program can be expressed in terms of the following: •

sequence



if



while

statement (selection) statement (repetition)

and that these control statements can be combined in only two ways—stacking and nesting. Indeed, structured programming promotes simplicity.

5.11 Wrap-Up We’ve now completed our introduction to control statements, which enable you to control the flow of execution in functions. Chapter 4 discussed the if, if…else and while statements. This chapter demonstrated the for, do…while and switch statements. We showed that any algorithm can be developed using combinations of the sequence structure, the three types of selection statements—if, if…else and switch—and the three types of repetition statements—while, do…while and for. We discussed how you can combine these building blocks to utilize proven program construction and problem-solving techniques. You used the break and continue statements to alter a repetition statement’s flow of control. This chapter also introduced logical operators, which enable you to use more complex conditional expressions in control statements. Finally, we examined the common errors of confusing the equality and assignment operators and provided suggestions for avoiding these errors. In Chapter 6, we examine functions in greater depth.

Summary Section 5.2 Essentials of Counter-Controlled Repetition

• In C++, it’s more precise to call a declaration that also reserves memory a definition (p. 153).

Section 5.3 for Repetition Statement

• The for repetition statement (p. 155) handles all the details of counter-controlled repetition. • The general format of the for statement is initialization; loopContinuationCondition; increment ) statement

for (

• • • • •

where initialization initializes the control variable, loopContinuationCondition determines whether the loop should continue executing and increment increments or decrements the control variable. Typically, for statements are used for counter-controlled repetition and while statements are used for sentinel-controlled repetition. The scope of a variable (p. 156) specifies where it can be used in a program. The comma operator (p. 156) has the lowest precedence of all C++ operators. The value and type of a comma-separated list of expressions is the value and type of the rightmost expression in the list. The initialization, loop-continuation condition and increment expressions of a for statement can contain arithmetic expressions. Also, the increment of a for statement can be negative. If the loop-continuation condition in a for header is initially false, the body of the for statement is not performed. Instead, execution proceeds with the statement following the for.

186

Chapter 5 Control Statements: Part 2

Section 5.4 Examples Using the for Statement

• Standard library function pow(x, y) (p. 160) calculates the value of x raised to the yth power. Function pow takes two arguments of type double and returns a double value. • Parameterized stream manipulator setw (p. 162) specifies the field width in which the next value output should appear, right justified by default. If the value is larger than the field width, the field width is extended to accommodate the entire value. Stream manipulator left (p. 162) causes a value to be left justified and right can be used to restore right justification. • Sticky settings are those output-formatting settings that remain in effect until they’re changed.

Section 5.5 do…while Repetition Statement

• The do…while repetition statement tests the loop-continuation condition at the end of the loop, so the body of the loop will be executed at least once. The format for the do…while statement is do {

statement condition

} while (

);

Section 5.6 switch Multiple-Selection Statement

• The switch multiple-selection statement (p. 164) performs different actions based on its controlling expression’s value. • A switch statement consists of a series of case labels (p. 169) and an optional default case (p. 169). • Function cin.get() reads one character from the keyboard. Characters normally are stored in variables of type char (p. 168). A character can be treated either as an integer or as a character. • The expression in the parentheses following switch is called the controlling expression (p. 169). The switch statement compares the value of the controlling expression with each case label. • Consecutive cases with no statements between the perform the same set of statements. • Each case label can specify only one constant integral expression. • Each case can have multiple statements. The switch selection statement differs from other control statements in that it does not require braces around multiple statements in each case. • C++ provides several data types to represent integers—int, char, short and long. The range of integer values for each type depends on the particular computer’s hardware.

Section 5.7 break and continue Statements

• The break statement (p. 173), when executed in one of the repetition statements (for, while and do…while), causes immediate exit from the statement. • The continue statement (p. 173), when executed in a repetition statement, skips any remaining statements in the loop’s body and proceeds with the next iteration of the loop. In a while or do…while statement, execution continues with the next evaluation of the condition. In a for statement, execution continues with the increment expression in the for statement header.

Section 5.8 Logical Operators

• Logical operators (p. 175) enable you to form complex conditions by combining simple conditions. The logical operators are && (logical AND), || (logical OR) and ! (logical negation). • The && (logical AND, p. 175) operator ensures that two conditions are both true. • The || (logical OR, p. 176) operator ensures that either or both of two conditions are true. • An expression containing && or || operators evaluates only until the truth or falsehood of the expression is known. This performance feature for the evaluation of logical AND and logical OR expressions is called short-circuit evaluation (p. 176).

Self-Review Exercises

187

• The ! (logical NOT, also called logical negation; p. 176) operator enables a programmer to “reverse” the meaning of a condition. The unary logical negation operator is placed before a condition to choose a path of execution if the original condition (without the logical negation operator) is false. In most cases, you can avoid using logical negation by expressing the condition with an appropriate relational or equality operator. • When used as a condition, any nonzero value implicitly converts to true; 0 (zero) implicitly converts to false. • By default, bool values true and false are displayed by cout as 1 and 0, respectively. Stream manipulator boolalpha (p. 177) specifies that the value of each bool expression should be displayed as either the word “true” or the word “false.”

Section 5.9 Confusing the Equality (==) and Assignment (=) Operators

• Any expression that produces a value can be used in the decision portion of any control statement. If the value of the expression is zero, it’s treated as false, and if the value is nonzero, it’s treated as true. • An assignment produces a value—namely, the value assigned to the variable on the left side of the assignment operator.

Section 5.10 Structured Programming Summary

• Any form of control can be expressed in terms of sequence, selection and repetition statements, and these can be combined in only two ways—stacking and nesting.

Self-Review Exercises 5.1

State whether the following are true or false. If the answer is false, explain why. a) The default case is required in the switch selection statement. b) The break statement is required in the default case of a switch selection statement to exit the switch properly. c) The expression ( x > y && a < b ) is true if either the expression x > y is true or the expression a < b is true. d) An expression containing the || operator is true if either or both of its operands are true.

5.2

Write a C++ statement or a set of C++ statements to accomplish each of the following: a) Sum the odd integers between 1 and 99 using a for statement. Assume the integer variables sum and count have been declared. b) Print the value 333.546372 in a 15-character field with precisions of 1, 2 and 3. Print each number on the same line. Left-justify each number in its field. What three values print? c) Calculate the value of 2.5 raised to the power 3 using function pow. Print the result with a precision of 2 in a field width of 10 positions. What prints? d) Print the integers from 1 to 20 using a while loop and the counter variable x. Assume that the variable x has been declared, but not initialized. Print only 5 integers per line. [Hint: When x % 5 is 0, print a newline character; otherwise, print a tab character.] e) Repeat Exercise 5.2(d) using a for statement.

5.3

Find the errors in each of the following code segments and explain how to correct them. a) x = 1; while ( x <= 10 ); ++x; }

188

Chapter 5 Control Statements: Part 2 b)

for ( y = .1; y != 1.0; y += .1 )

c)

switch ( n )

cout << y << endl; { case 1: cout << "The number is 1" << endl; case 2: cout << "The number is 2" << endl; break; default: cout << "The number is not 1 or 2" << endl; break; }

d) The following code should print the values 1 to 10. n = 1; while ( n < 10 ) cout << n++ << endl;

Answers to Self-Review Exercises 5.1

a) False. The default case is optional. Nevertheless, it’s considered good software engineering to always provide a default case. b) False. The break statement is used to exit the switch statement. The break statement is not required when the default case is the last case. Nor will the break statement be required if having control proceed with the next case makes sense. c) False. When using the && operator, both of the relational expressions must be true for the entire expression to be true. d) True.

5.2

a)

sum = 0; for ( count = 1; count <= 99; count += 2 )

b)

sum += count; cout << fixed << left << setprecision( 1 ) << setw( 15 ) << 333.546372 << setprecision( 2 ) << setw( 15 ) << 333.546372 << setprecision( 3 ) << setw( 15 ) << 333.546372 << endl;

Output is: c) d)

333.5

333.55

333.546

cout << fixed << setprecision( 2 ) << setw( 10 ) << pow( 2.5, 3 ) << endl;

Output is:

15.63 x = 1; while ( x <= 20 ) { if ( x % 5 == 0 ) cout << x << endl; else cout << x << '\t'; ++x; }

Exercises e)

5.3

189

for ( x = 1; x <= 20; ++x ) { if ( x % 5 == 0 ) cout << x << endl; else cout << x << '\t'; }

a) Error: The semicolon after the while header causes an infinite loop. Correction: Replace the semicolon by a {, or remove both the ; and the }. b) Error: Using a floating-point number to control a for repetition statement. Correction: Use an int and perform the proper calculation to get the values you desire. for ( y = 1; y != 10; ++y ) cout << ( static_cast< double >( y ) / 10 ) << endl;

c) Error: Missing break statement in the first case. Correction: Add a break statement at the end of the first case. This is not an error if you want the statement of case 2: to execute every time the case 1: statement executes. d) Error: Improper relational operator used in the loop-continuation condition. Correction: Use <= rather than <, or change 10 to 11.

Exercises 5.4

(Find the Code Errors) Find the error(s), if any, in each of the following: a) For ( x = 100, x >= 1, ++x ) cout << x << endl;

b) The following code should print whether integer value is odd or even: switch ( value % 2 ) { case 0: cout << "Even integer" << endl; case 1: cout << "Odd integer" << endl; }

c) The following code should output the odd integers from 19 to 1: for ( x = 19; x >= 1; x += 2 ) cout << x << endl;

d) The following code should output the even integers from 2 to 100: counter = 2; do {

cout << counter << endl; counter += 2; } While ( counter < 100 );

5.5 (Summing Integers) Write a program that uses a for statement to sum a sequence of integers. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value per input statement. A typical input sequence might be 5 100 200 300 400 500

where the 5 indicates that the subsequent 5 values are to be summed. 5.6 (Averaging Integers) Write a program that uses a for statement to calculate the average of several integers. Assume the last value read is the sentinel 9999. For example, the sequence 10 8 11 7 9 9999 indicates that the program should calculate the average of all the values preceding 9999.

190 5.7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

Chapter 5 Control Statements: Part 2 (What Does This Program Do?) What does the following program do?

// Exercise 5.7: ex05_07.cpp // What does this program print? #include using namespace std; int main() { int x; // declare x int y; // declare y // prompt user for input cout << "Enter two integers in the range 1-20: "; cin >> x >> y; // read values for x and y for ( int i = 1; i <= y; ++i ) // count from 1 to y { for ( int j = 1; j <= x; ++j ) // count from 1 to x cout << '@'; // output @ cout << endl; // begin new line } // end outer for } // end main

5.8 (Find the Smallest Integer) Write a program that uses a for statement to find the smallest of several integers. Assume that the first value read specifies the number of values remaining. 5.9 (Product of Odd Integers) Write a program that uses a for statement to calculate and print the product of the odd integers from 1 to 15. 5.10 (Factorials) The factorial function is used frequently in probability problems. Using the definition of factorial in Exercise 4.34, write a program that uses a for statement to evaluate the factorials of the integers from 1 to 5. Print the results in tabular format. What difficulty might prevent you from calculating the factorial of 20? 5.11 (Compound Interest) Modify the compound interest program of Section 5.4 to repeat its steps for the interest rates 5%, 6%, 7%, 8%, 9% and 10%. Use a for statement to vary the interest rate. 5.12 (Drawing Patterns with Nested for Loops) Write a program that uses for statements to print the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form cout << '*'; (this causes the asterisks to print side by side). [Hint: The last two patterns require that each line begin with an appropriate number of blanks. Extra credit: Combine your code from the four separate problems into a single program that prints all four patterns side by side by making clever use of nested for loops.] (a)

* ** *** **** ***** ****** ******* ******** ********* **********

(b)

********** ********* ******** ******* ****** ***** **** *** ** *

(c)

********** ********* ******** ******* ****** ***** **** *** ** *

(d)

* ** *** **** ***** ****** ******* ******** ********* **********

5.13 (Bar Chart) One interesting application of computers is drawing graphs and bar charts. Write a program that reads five numbers (each between 1 and 30). Assume that the user enters only

Exercises

191

valid values. For each number that is read, your program should print a line containing that number of adjacent asterisks. For example, if your program reads the number 7, it should print *******. 5.14 (Calculating Total Sales) A mail order house sells five different products whose retail prices are: product 1 — $2.98, product 2—$4.50, product 3—$9.98, product 4—$4.49 and product 5— $6.87. Write a program that reads a series of pairs of numbers as follows: a) product number b) quantity sold Your program should use a switch statement to determine the retail price for each product. Your program should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results. 5.15 (GradeBook Modification) Modify the GradeBook program of Fig. 5.9–Fig. 5.11 to calculate the grade-point average. A grade of A is worth 4 points, B is worth 3 points, and so on. 5.16 (Compound Interest Calculation) Modify Fig. 5.6 so it uses only integers to calculate the compound interest. [Hint: Treat all monetary amounts as numbers of pennies. Then “break” the result into its dollar and cents portions by using the division and modulus operations. Insert a period.] 5.17

(What Prints?) Assume i = 1, j = 2, k = 3 and m = 2. What does each statement print? a) cout << ( i == 1 ) << endl; b) cout << ( j == 3 ) << endl; c) cout << ( i >= 1 && j < 4 ) << endl; d) cout << ( m <= 99 && k < m ) << endl; e) cout << ( j >= i || k == m ) << endl; f) cout << ( k + m < j || 3 - j >= k ) << endl; g) cout << ( !m ) << endl; h) cout << ( !( j - m ) ) << endl; i) cout << ( !( k > m ) ) << endl;

5.18 (Number Systems Table) Write a program that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range 1–256. If you are not familiar with these number systems, read Appendix D, first. [Hint: You can use the stream manipulators dec, oct and hex to display integers in decimal, octal and hexadecimal formats, respectively.] (Calculating π) Calculate the value of π from the infinite series 4 4 4 4 4 π = 4 – --- + --- – --- + --- – ------ + … 3 5 7 9 11 Print a table that shows the approximate value of π after each of the first 1000 terms of this series.

5.19

5.20 (Pythagorean Triples) A right triangle can have sides that are all integers. A set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse. Find all Pythagorean triples for side1, side2 and hypotenuse all no larger than 500. Use a triple-nested for loop that tries all possibilities. This is an example of brute force computing. You’ll learn in more advanced computer science courses that there are many interesting problems for which there’s no known algorithmic approach other than sheer brute force. 5.21 (Calculating Salaries) A company pays its employees as managers (who receive a fixed weekly salary), hourly workers (who receive a fixed hourly wage for up to the first 40 hours they work and “time-and-a-half”—1.5 times their hourly wage—for overtime hours worked), commission workers (who receive $250 plus 5.7 percent of their gross weekly sales), or pieceworkers (who receive a fixed amount of money per item for each of the items they produce—each pieceworker in this company works on only one type of item). Write a program to compute the weekly pay for each employee. You do not know the number of employees in advance. Each type of employee has its own pay code: Man-

192

Chapter 5 Control Statements: Part 2

agers have code 1, hourly workers have code 2, commission workers have code 3 and pieceworkers have code 4. Use a switch to compute each employee’s pay according to that employee’s paycode. Within the switch, prompt the user (i.e., the payroll clerk) to enter the appropriate facts your program needs to calculate each employee’s pay according to that employee’s paycode. 5.22 (De Morgan’s Laws) In this chapter, we discussed the logical operators &&, || and !. De Morgan’s laws can sometimes make it more convenient for us to express a logical expression. These laws state that the expression !( condition1 && condition2 ) is logically equivalent to the expression ( !condition1 || !condition2 ). Also, the expression !( condition1 || condition2 ) is logically equivalent to the expression ( !condition1 && !condition2 ). Use De Morgan’s laws to write equivalent expressions for each of the following, then write a program to show that the original expression and the new expression in each case are equivalent: a) !( x < 5 ) && !( y >= 7 ) b) !( a == b ) || !( g != 5 ) c) !( ( x <= 8 ) && ( y > 4 ) ) d) !( ( i > 4 ) || ( j <= 6 ) ) 5.23 (Diamond of Asterisks) Write a program that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single blank or a single newline. Maximize your use of repetition (with nested for statements) and minimize the number of output statements. * *** ***** ******* ********* ******* ***** *** *

5.24 (Diamond of Asterisks Modification) Modify Exercise 5.23 to read an odd number in the range 1 to 19 to specify the number of rows in the diamond, then display a diamond of the appropriate size. 5.25 (Removing break and continue) A criticism of the break and continue statements is that each is unstructured. These statements can always be replaced by structured statements. Describe in general how you’d remove any break statement from a loop in a program and replace it with some structured equivalent. [Hint: The break statement leaves a loop from within the body of the loop. Another way to leave is by failing the loop-continuation test. Consider using in the loop-continuation test a second test that indicates “early exit because of a ‘break’ condition.”] Use the technique you developed here to remove the break statement from the program of Fig. 5.13. 5.26 1 2 3 4 5 6 7 8 9 10 11 12

What does the following program segment do?

for ( int i = 1; i <= 5; ++i ) { for ( int j = 1; j <= 3; ++j ) { for ( int k = 1; k <= 4; ++k ) cout << '*'; cout << endl; } // end inner for cout << endl; } // end outer for

Making a Difference

193

5.27 (Removing the continue Statement) Describe in general how you’d remove any continue statement from a loop in a program and replace it with some structured equivalent. Use the technique you developed here to remove the continue statement from the program of Fig. 5.14. 5.28 (“The Twelve Days of Christmas” Song) Write a program that uses repetition and switch statements to print the song “The Twelve Days of Christmas.” One switch statement should be used to print the day (i.e., “first,” “second,” etc.). A separate switch statement should be used to print the remainder of each verse. Visit the website www.12days.com/library/carols/ 12daysofxmas.htm for the complete lyrics to the song. 5.29 (Peter Minuit Problem) Legend has it that, in 1626, Peter Minuit purchased Manhattan Island for $24.00 in barter. Did he make a good investment? To answer this question, modify the compound interest program of Fig. 5.6 to begin with a principal of $24.00 and to calculate the amount of interest on deposit if that money had been kept on deposit until this year (e.g., 384 years through 2010). Place the for loop that performs the compound interest calculation in an outer for loop that varies the interest rate from 5% to 10% to observe the wonders of compound interest.

Making a Difference 5.30 (Global Warming Facts Quiz) The controversial issue of global warming has been widely publicized by the film An Inconvenient Truth, featuring former Vice President Al Gore. Mr. Gore and a U.N. network of scientists, the Intergovernmental Panel on Climate Change, shared the 2007 Nobel Peace Prize in recognition of “their efforts to build up and disseminate greater knowledge about man-made climate change.” Research both sides of the global warming issue online (you might want to search for phrases like “global warming skeptics”). Create a five-question multiplechoice quiz on global warming, each question having four possible answers (numbered 1–4). Be objective and try to fairly represent both sides of the issue. Next, write an application that administers the quiz, calculates the number of correct answers (zero through five) and returns a message to the user. If the user correctly answers five questions, print “Excellent”; if four, print “Very good”; if three or fewer, print “Time to brush up on your knowledge of global warming,” and include a list of the websites where you found your facts. 5.31 (Tax Plan Alternatives; The “FairTax”) There are many proposals to make taxation fairer. Check out the FairTax initiative in the United States at www.fairtax.org/site/PageServer?pagename=calculator

Research how the proposed FairTax works. One suggestion is to eliminate income taxes and most other taxes in favor of a 23% consumption tax on all products and services that you buy. Some FairTax opponents question the 23% figure and say that because of the way the tax is calculated, it would be more accurate to say the rate is 30%—check this carefully. Write a program that prompts the user to enter expenses in various expense categories they have (e.g., housing, food, clothing, transportation, education, health care, vacations), then prints the estimated FairTax that person would pay. 5.32 (Facebook User Base Growth) According to CNNMoney.com, Facebook hit 500 million users in July of 2010 and its user base has been growing at a rate of 5% per month. Using the compound-growth technique you learned in Fig. 5.6 and assuming this growth rate continues, how many months will it take for Facebook to grow its user base to one billion users? How many months will it take for Facebook to grow its user base to two billion users (which, at the time of this writing, was the total number of people on the Internet)?

6 Form ever follows function. —Louis Henri Sullivan

E pluribus unum. (One composed of many.)

—Virgil

O! call back yesterday, bid time return. —William Shakespeare

Answer me in one word.

—William Shakespeare

There is a point at which methods devour themselves. —Frantz Fanon

Objectives

In this chapter you’ll learn: ■













To construct programs modularly from functions. To use common math library functions. The mechanisms for passing data to functions and returning results. How the function call/return mechanism is supported by the function call stack and activation records. To use random number generation to implement game-playing applications. How the visibility of identifiers is limited to specific regions of programs. To write and use recursive functions.

Functions and an Introduction to Recursion

6.1 Introduction 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 6.10

Introduction Program Components in C++ Math Library Functions Function Definitions with Multiple Parameters Function Prototypes and Argument Coercion C++ Standard Library Headers Case Study: Random Number Generation Case Study: Game of Chance; Introducing enum Storage Classes Scope Rules

195

6.11 Function Call Stack and Activation Records 6.12 Functions with Empty Parameter Lists 6.13 Inline Functions 6.14 References and Reference Parameters 6.15 Default Arguments 6.16 Unary Scope Resolution Operator 6.17 Function Overloading 6.18 Function Templates 6.19 Recursion 6.20 Example Using Recursion: Fibonacci Series 6.21 Recursion vs. Iteration 6.22 Wrap-Up

Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference

6.1 Introduction Most computer programs that solve real-world problems are much larger than the programs presented in the first few chapters of this book. Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces, or components. This technique is called divide and conquer. We’ll overview a portion of the C++ Standard Library’s math functions. Next, you’ll learn how to declare a function with more than one parameter. We’ll also present additional information about function prototypes and how the compiler uses them to convert the type of an argument in a function call to the type specified in a function’s parameter list, if necessary. Next, we’ll take a brief diversion into simulation techniques with random number generation and develop a version of the casino dice game called craps that uses most of the programming techniques you’ve learned. We then present C++’s storage classes and scope rules. These determine the period during which an object exists in memory and where its identifier can be referenced in a program. You’ll learn how C++ keeps track of which function is currently executing, how parameters and other local variables of functions are maintained in memory and how a function knows where to return after it completes execution. We discuss topics that help improve program performance—inline functions that can eliminate the overhead of a function call and reference parameters that can be used to pass large data items to functions efficiently. Many of the applications you develop will have more than one function of the same name. This technique, called function overloading, is used to implement functions that perform similar tasks for arguments of different types or possibly for different numbers of arguments. We consider function templates—a mechanism for defining a family of overloaded functions. The chapter concludes with a discussion of functions that call themselves, either directly, or indirectly (through another function)—a topic called recursion.

196

Chapter 6 Functions and an Introduction to Recursion

6.2 Program Components in C++ As you’ve seen, C++ programs are typically written by combining new functions and classes you write with “prepackaged” functions and classes available in the C++ Standard Library. The C++ Standard Library provides a rich collection of functions for common mathematical calculations, string manipulations, character manipulations, input/output, error checking and many other useful operations. Functions allow you to modularize a program by separating its tasks into self-contained units. You’ve used a combination of library functions and your own functions in every program you’ve written. Functions you write are referred to as user-defined functions or programmer-defined functions. The statements in function bodies are written only once, are reused from perhaps several locations in a program and are hidden from other functions. There are several motivations for modularizing a program with functions. One is the divide-and-conquer approach. Another is software reuse. For example, in earlier programs, we did not have to define how to read a line of text from the keyboard—C++ provides this capability via the getline function of the header. A third motivation is to avoid repeating code. Also, dividing a program into meaningful functions makes the program easier to debug and maintain.

Software Engineering Observation 6.1

To promote software reusability, every function should be limited to performing a single, well-defined task, and the name of the function should express that task effectively.

As you know, a function is invoked by a function call, and when the called function completes its task, it either returns a result or simply returns control to the caller. An analogy to this program structure is the hierarchical form of management (Figure 6.1). A boss (similar to the calling function) asks a worker (similar to the called function) to perform a task and report back (i.e., return) the results after completing the task. The boss function does not know how the worker function performs its designated tasks. The worker may also call other worker functions, unbeknownst to the boss. This hiding of implementation details promotes good software engineering. Figure 6.1 shows the boss function communicating with several worker functions. The boss function divides the responsibilities among the worker functions, and worker1 acts as a “boss function” to worker4 and worker5.

boss

worker1

worker4

worker2

worker5

Fig. 6.1 | Hierarchical boss function/worker function relationship.

worker3

6.3 Math Library Functions

197

6.3 Math Library Functions Sometimes functions, such as main, are not members of a class. Such functions are called global functions. Like a class’s member functions, the function prototypes for global functions are placed in headers, so that the global functions can be reused in any program that includes the header and that can link to the function’s object code. For example, recall that we used function pow of the header to raise a value to a power in Figure 5.6. We introduce various functions from the header here to present the concept of global functions that do not belong to a particular class. The header provides a collection of functions that enable you to perform common mathematical calculations. For example, you can calculate the square root of 900.0 with the function call sqrt( 900.0 )

The preceding expression evaluates to 30.0. Function sqrt takes an argument of type double and returns a double result. There’s no need to create any objects before calling function sqrt. Also, all functions in the header are global functions—therefore, each is called simply by specifying the name of the function followed by parentheses containing the function’s arguments. Function arguments may be constants, variables or more complex expressions. If c = 13.0, d = 3.0 and f = 4.0, then the statement cout << sqrt( c + d * f ) << endl;

displays the square root of 13.0 + 3.0 * 4.0 = 25.0—namely, 5.0. Some math library functions are summarized in Fig. 6.2. In the figure, the variables x and y are of type double. Function

Description

Example

ceil( x )

rounds x to the smallest integer not less than x trigonometric cosine of x (x in radians) exponential function ex

ceil( 9.2 )

cos( x ) exp( x )

is 10.0 is -9.0 is 1.0

ceil( -9.8 ) cos( 0.0 )

is 2.718282 is 7.389056 fabs( 5.1 ) is 5.1 fabs( 0.0 ) is 0.0 fabs( -8.76 ) is 8.76 floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 fmod( 2.6, 1.2 ) is 0.2 exp( 1.0 ) exp( 2.0 )

fabs( x )

absolute value of x

floor( x )

rounds x to the largest integer not greater than x remainder of x/y as a floatingpoint number natural logarithm of x (base e)

fmod( x, y ) log( x )

log( 2.718282 ) log( 7.389056 )

log10( x )

logarithm of x (base 10)

log10( 10.0 ) log10( 100.0 )

Fig. 6.2 | Math library functions. (Part 1 of 2.)

is is

1.0 2.0

is 1.0 is 2.0

198

Chapter 6 Functions and an Introduction to Recursion

Function

Description

Example

pow( x, y )

x raised to power y (xy)

pow( 2, 7 )

is 128 is 3 is 0

pow( 9, .5 ) sin( x ) sqrt( x ) tan( x )

trigonometric sine of x (x in radians) square root of x (where x is a nonnegative value) trigonometric tangent of x (x in radians)

sin( 0.0 ) sqrt( 9.0 ) tan( 0.0 )

is is

3.0

0

Fig. 6.2 | Math library functions. (Part 2 of 2.)

6.4 Function Definitions with Multiple Parameters Let’s consider functions with multiple parameters. Figures 6.3–6.5 modify class GradeBook by including a user-defined function called maximum that determines and returns the largest of three int grades. When the application executes, the main function (lines 5–13 of Fig. 6.5) creates one GradeBook object (line 8) and calls its inputGrades member function (line 11) to read three integer grades from the user. In class GradeBook’s implementation file (Fig. 6.4), lines 52–53 of member function inputGrades prompt the user to enter three integer values and read them from the user. Line 56 calls member function maximum (defined in lines 60–73). Function maximum determines the largest value, then the return statement (line 72) returns that value to the point at which function inputGrades invoked maximum (line 56). Member function inputGrades then stores maximum’s return value in data member maximumGrade. This value is then output by calling function displayGradeReport (line 12 of Fig. 6.5). [Note: We named this function displayGradeReport because subsequent versions of class GradeBook will use this function to display a complete grade report, including the maximum and minimum grades.] In Chapter 7, Arrays and Vectors, we’ll enhance class GradeBook to process an arbitrary number of grades. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

// Fig. 6.3: GradeBook.h // Definition of class GradeBook that finds the maximum of three grades. // Member functions are defined in GradeBook.cpp #include // program uses C++ standard string class using namespace std; // GradeBook class definition class GradeBook { public: GradeBook( string ); // constructor initializes course name void setCourseName( string ); // function to set the course name string getCourseName(); // function to retrieve the course name void displayMessage(); // display a welcome message void inputGrades(); // input three grades from user

Fig. 6.3 |

GradeBook

header. (Part 1 of 2.)

6.4 Function Definitions with Multiple Parameters 16 17 18 19 20 21

void displayGradeReport(); // display a report based on the grades int maximum( int, int, int ); // determine max of 3 values private: string courseName; // course name for this GradeBook int maximumGrade; // maximum of three grades }; // end class GradeBook

Fig. 6.3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

199

GradeBook

header. (Part 2 of 2.)

// Fig. 6.4: GradeBook.cpp // Member-function definitions for class GradeBook that // determines the maximum of three grades. #include using namespace std; #include "GradeBook.h" // include definition of class GradeBook // constructor initializes courseName with string supplied as argument; // initializes maximumGrade to 0 GradeBook::GradeBook( string name ) { setCourseName( name ); // validate and store courseName maximumGrade = 0; // this value will be replaced by the maximum grade } // end GradeBook constructor // function to set the course name; limits name to 25 or fewer characters void GradeBook::setCourseName( string name ) { if ( name.length() <= 25 ) // if name has 25 or fewer characters courseName = name; // store the course name in the object else // if name is longer than 25 characters { // set courseName to first 25 characters of parameter name courseName = name.substr( 0, 25 ); // select first 25 characters cout << "Name \"" << name << "\" exceeds maximum length (25).\n" << "Limiting courseName to first 25 characters.\n" << endl; } // end if...else } // end function setCourseName // function to retrieve the course name string GradeBook::getCourseName() { return courseName; } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { // this statement calls getCourseName to get the // name of the course this GradeBook represents cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" << endl; } // end function displayMessage

Fig. 6.4 |

GradeBook

class defines function maximum. (Part 1 of 2.)

200 44 45 46 47 48 49 50 51 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

Chapter 6 Functions and an Introduction to Recursion

// input three grades from user; determine maximum void GradeBook::inputGrades() { int grade1; // first grade entered by user int grade2; // second grade entered by user int grade3; // third grade entered by user cout << "Enter three integer grades: "; cin >> grade1 >> grade2 >> grade3; // store maximum in member maximumGrade maximumGrade = maximum( grade1, grade2, grade3 ); } // end function inputGrades // returns the maximum of its three integer parameters int GradeBook::maximum( int x, int y, int z ) { int maximumValue = x; // assume x is the largest to start // determine whether y is greater than maximumValue if ( y > maximumValue ) maximumValue = y; // make y the new maximumValue // determine whether z is greater than maximumValue if ( z > maximumValue ) maximumValue = z; // make z the new maximumValue return maximumValue; } // end function maximum // display a report based on the grades entered by user void GradeBook::displayGradeReport() { // output maximum of grades entered cout << "Maximum of grades entered: " << maximumGrade << endl; } // end function displayGradeReport

Fig. 6.4 | 1 2 3 4 5 6 7 8 9 10 11

GradeBook

class defines function maximum. (Part 2 of 2.)

// Fig. 6.5: fig06_05.cpp // Create GradeBook object, input grades and display grade report. #include "GradeBook.h" // include definition of class GradeBook int main() { // create GradeBook object GradeBook myGradeBook( "CS101 C++ Programming" ); myGradeBook.displayMessage(); // display welcome message myGradeBook.inputGrades(); // read grades from user

Fig. 6.5 | Demonstrating function maximum. (Part 1 of 2.)

6.4 Function Definitions with Multiple Parameters 12 13

201

myGradeBook.displayGradeReport(); // display report based on grades } // end main

Welcome to the grade book for CS101 C++ Programming! Enter three integer grades: 86 67 75 Maximum of grades entered: 86

Welcome to the grade book for CS101 C++ Programming! Enter three integer grades: 67 86 75 Maximum of grades entered: 86

Welcome to the grade book for CS101 C++ Programming! Enter three integer grades: 67 75 86 Maximum of grades entered: 86

Fig. 6.5 | Demonstrating function maximum. (Part 2 of 2.)

Software Engineering Observation 6.2

The commas used in line 56 of Fig. 6.4 to separate the arguments to function maximum are not comma operators as discussed in Section 5.3. The comma operator guarantees that its operands are evaluated left to right. The order of evaluation of a function’s arguments, however, is not specified by the C++ standard. Thus, different compilers can evaluate function arguments in different orders. The C++ standard does guarantee that all arguments in a function call are evaluated before the called function executes.

Portability Tip 6.1

Sometimes when a function’s arguments are expressions, such as those with calls to other functions, the order in which the compiler evaluates the arguments could affect the values of one or more of the arguments. If the evaluation order changes between compilers, the argument values passed to the function could vary, causing subtle logic errors.

Error-Prevention Tip 6.1

If you have doubts about the order of evaluation of a function’s arguments and whether the order would affect the values passed to the function, evaluate the arguments in separate assignment statements before the function call, assign the result of each expression to a local variable, then pass those variables as arguments to the function.

Member function maximum’s prototype (Fig. 6.3, line 17) indicates that the function returns an integer value, has the name maximum and requires three integer parameters to perform its task. The function header (Fig. 6.4, line 60) matches the function prototype and indicates that the parameter names are x, y and z. When maximum is called (Fig. 6.4, line 56), the parameter x is initialized with the value of the argument grade1, the parameter y is initialized with the value of the argument grade2 and the parameter z is initialized with

202

Chapter 6 Functions and an Introduction to Recursion

the value of the argument grade3. There must be one argument in the function call for each parameter (also called a formal parameter) in the function definition. Notice that multiple parameters are specified in both the function prototype and the function header as a comma-separated list. The compiler refers to the function prototype to check that calls to maximum contain the correct number and types of arguments and that the types of the arguments are in the correct order. In addition, the compiler uses the prototype to ensure that the value returned by the function can be used correctly in the expression that called the function (e.g., a function call that returns void cannot be used as the right side of an assignment statement). Each argument must be consistent with the type of the corresponding parameter. For example, a parameter of type double can receive values like 7.35, 22 or –0.03456, but not a string like "hello". If the arguments passed to a function do not match the types specified in the function’s prototype, the compiler attempts to convert the arguments to those types. Section 6.5 discusses this conversion.

Common Programming Error 6.1

Declaring function parameters of the same type as double x, y instead of double x, double y is a syntax error—a type is required for each parameter in the parameter list.

Common Programming Error 6.2

Compilation errors occur if the function prototype, header and calls do not all agree in the number, type and order of arguments and parameters, and in the return type. Linker errors and other types of errors can occur as well as you’ll see later in the book.

Software Engineering Observation 6.3

A function that has many parameters may be performing too many tasks. Consider dividing the function into smaller functions that perform the separate tasks. Limit the function header to one line if possible.

To determine the maximum value (lines 60–73 of Fig. 6.4), we begin with the assumption that parameter x contains the largest value, so line 62 of function maximum declares local variable maximumValue and initializes it with the value of parameter x. Of course, it’s possible that parameter y or z contains the actual largest value, so we must compare each of these values with maximumValue. The if statement in lines 65–66 determines whether y is greater than maximumValue and, if so, assigns y to maximumValue. The if statement in lines 69–70 determines whether z is greater than maximumValue and, if so, assigns z to maximumValue. At this point the largest of the three values is in maximumValue, so line 72 returns that value to the call in line 56. When program control returns to the point in the program where maximum was called, maximum’s parameters x, y and z are no longer accessible to the program. There are three ways to return control to the point at which a function was invoked. If the function does not return a result (i.e., it has a void return type), control returns when the program reaches the function-ending right brace, or by execution of the statement return;

If the function does return a result, the statement return expression;

evaluates expression and returns the value of expression to the caller.

6.5 Function Prototypes and Argument Coercion

203

6.5 Function Prototypes and Argument Coercion A function prototype (also called a function declaration) tells the compiler the name of a function, the type of data it returns, the number of parameters it expects to receive, the types of those parameters and the order in which the parameters of those types are expected.

Software Engineering Observation 6.4

Function prototypes are required. Use #include preprocessor directives to obtain function prototypes for the C++ Standard Library functions from the headers of the appropriate libraries (e.g., the prototype for sqrt is in header ; a partial list of C++ Standard Library headers appears in Section 6.6). Also use #include to obtain headers containing function prototypes written by you or other programmers.

Common Programming Error 6.3

If a function is defined before it’s invoked, then its definition also serves as the function’s prototype, so a separate prototype is unnecessary. If a function is invoked before it’s defined, and that function does not have a function prototype, a compilation error occurs.

Software Engineering Observation 6.5

Always provide function prototypes, even though it’s possible to omit them when functions are defined before they’re used (in which case the function header acts as the function prototype as well). Providing the prototypes avoids tying the code to the order in which functions are defined (which can easily change as a program evolves).

Function Signatures The portion of a function prototype that includes the name of the function and the types of its arguments is called the function signature or simply the signature. The function signature does not specify the function’s return type. Functions in the same scope must have unique signatures. The scope of a function is the region of a program in which the function is known and accessible. We’ll say more about scope in Section 6.10.

Common Programming Error 6.4

It’s a compilation error if two functions in the same scope have the same signature but different return types.

In Fig. 6.3, if the function prototype in line 17 had been written void maximum( int, int, int );

the compiler would report an error, because the void return type in the function prototype would differ from the int return type in the function header. Similarly, such a prototype would cause the statement cout << maximum( 6, 7, 0 );

to generate a compilation error, because that statement depends on maximum to return a value to be displayed.

Argument Coercion An important feature of function prototypes is argument coercion—i.e., forcing arguments to the appropriate types specified by the parameter declarations. For example, a pro-

204

Chapter 6 Functions and an Introduction to Recursion

gram can call a function with an integer argument, even though the function prototype specifies a double argument—the function will still work correctly.

Argument Promotion Rules Sometimes, argument values that do not correspond precisely to the parameter types in the function prototype can be converted by the compiler to the proper type before the function is called. These conversions occur as specified by C++’s promotion rules. The promotion rules indicate the implicit conversions that the compiler can perform between fundamental types. An int can be converted to a double. However, a double converted to an int truncates the fractional part of the double value. Keep in mind that double variables can hold numbers of much greater magnitude than int variables, so the loss of data may be considerable. Values may also be modified when converting large integer types to small integer types (e.g., long to short), signed to unsigned or unsigned to signed. Unsigned integers range from 0 to approximately twice the positive range of the corresponding signed type. The promotion rules apply to expressions containing values of two or more data types; such expressions are also referred to as mixed-type expressions. The type of each value in a mixed-type expression is promoted to the “highest” type in the expression (actually a temporary version of each value is created and used for the expression—the original values remain unchanged). Promotion also occurs when the type of a function argument does not match the parameter type specified in the function definition or prototype. Figure 6.6 lists the fundamental data types in order from “highest type” to “lowest type.” Data types long double double float unsigned long long int long long int unsigned long int long int unsigned int

(synonymous with unsigned long long; in the new standard) (synonymous with long long; in the new standard) (synonymous with unsigned long) (synonymous with long) (synonymous with unsigned)

int unsigned short int short int

(synonymous with unsigned (synonymous with short)

short)

unsigned char char bool

Fig. 6.6 | Promotion hierarchy for fundamental data types. Converting values to lower fundamental types can result in incorrect values. Therefore, a value can be converted to a lower fundamental type only by explicitly assigning the value to a variable of lower type (some compilers will issue a warning in this case) or by using a cast operator (see Section 4.9). Function argument values are converted to the parameter types in a function prototype as if they were being assigned directly to variables

6.6 C++ Standard Library Headers

205

of those types. If a square function that uses an integer parameter is called with a floatingpoint argument, the argument is converted to int (a lower type), and square could return an incorrect value. For example, square(4.5) returns 16, not 20.25.

Common Programming Error 6.5

Converting from a higher data type in the promotion hierarchy to a lower type, or between signed and unsigned, can corrupt the data value, causing a loss of information.

Common Programming Error 6.6

It’s a compilation error if the arguments in a function call do not match the number and types of the parameters declared in the corresponding function prototype. It’s also an error if the number of arguments in the call matches, but the arguments cannot be implicitly converted to the expected types.

6.6 C++ Standard Library Headers The C++ Standard Library is divided into many portions, each with its own header. The headers contain the function prototypes for the related functions that form each portion of the library. The headers also contain definitions of various class types and functions, as well as constants needed by those functions. A header “instructs” the compiler on how to interface with library and user-written components. Figure 6.7 lists some common C++ Standard Library headers, most of which are discussed later in the book. The term “macro” that’s used several times in Fig. 6.7 is discussed in detail in Appendix E, Preprocessor. Header names ending in.h are “old-style” headers that have been superseded by the C++ Standard Library headers. We use only the C++ Standard Library versions of each header in this book to ensure that our examples will work on most standard C++ compilers. Standard Library header







Explanation Contains function prototypes for the C++ standard input and output functions, introduced in Chapter 2, and is covered in more detail in Chapter 15, Stream Input/Output. Contains function prototypes for stream manipulators that format streams of data. This header is first used in Section 4.9 and is discussed in more detail in Chapter 15, Stream Input/Output. Contains function prototypes for math library functions (Section 6.3). Contains function prototypes for conversions of numbers to text, text to numbers, memory allocation, random numbers and various other utility functions. Portions of the header are covered in Section 6.7; Chapter 11, Operator Overloading; Class string; Chapter 16, Exception Handling: A Deeper Look; Chapter 21, Bits, Characters, C Strings and structs; and Appendix F, C Legacy Code Topics. Contains function prototypes and types for manipulating the time and date. This header is used in Section 6.7.

Fig. 6.7 | C++ Standard Library headers. (Part 1 of 3.)

206

Chapter 6 Functions and an Introduction to Recursion

Standard Library header , , , , , ,

,

,





Explanation These headers contain classes that implement the C++ Standard Library containers. Containers store data during a program’s execution. The header is first introduced in Chapter 7, Arrays and Vectors. We discuss all these headers in Chapter 22, Standard Template Library (STL). Contains function prototypes for functions that test characters for certain properties (such as whether the character is a digit or a punctuation), and function prototypes for functions that can be used to convert lowercase letters to uppercase letters and vice versa. These topics are discussed in Chapter 21, Bits, Characters, C Strings and structs. Contains function prototypes for C-style string-processing functions. This header is used in Chapter 11, Operator Overloading; Class string. Contains classes for runtime type identification (determining data types at execution time). This header is discussed in Section 13.8. These headers contain classes that are used for exception handling (discussed in Chapter 16, Exception Handling: A Deeper Look). Contains classes and functions used by the C++ Standard Library to allocate memory to the C++ Standard Library containers. This header is used in Chapter 16, Exception Handling: A Deeper Look. Contains function prototypes for functions that perform input from and output to files on disk (discussed in Chapter 17, File Processing). Contains the definition of class string from the C++ Standard Library (discussed in Chapter 18, Class string and String Stream Processing). Contains function prototypes for functions that perform input from strings in memory and output to strings in memory (discussed in Chapter 18, Class string and String Stream Processing). Contains classes and functions used by C++ Standard Library algorithms. This header is used in Chapter 22. Contains classes for accessing data in the C++ Standard Library containers. This header is used in Chapter 22. Contains functions for manipulating data in C++ Standard Library containers. This header is used in Chapter 22. Contains macros for adding diagnostics that aid program debugging. This header is used in Appendix E, Preprocessor. Contains the floating-point size limits of the system. Contains the integral size limits of the system. Contains function prototypes for the C-style standard input/output library functions. Contains classes and functions normally used by stream processing to process data in the natural form for different languages (e.g., monetary formats, sorting strings, character presentation, etc.).

Fig. 6.7 | C++ Standard Library headers. (Part 2 of 3.)

6.7 Case Study: Random Number Generation

Standard Library header

207

Explanation Contains classes for defining the numerical data type limits on each computer platform. Contains classes and functions that are used by many C++ Standard Library headers.

Fig. 6.7 | C++ Standard Library headers. (Part 3 of 3.)

6.7 Case Study: Random Number Generation We now take a brief and hopefully entertaining diversion into a popular programming application, namely simulation and game playing. In this and the next section, we develop a game-playing program that includes multiple functions. The element of chance can be introduced into computer applications by using the C++ Standard Library function rand. Consider the following statement: i = rand();

The function rand generates an unsigned integer between 0 and RAND_MAX (a symbolic constant defined in the header). You can determine the value of RAND_MAX for your system simply by displaying the constant. If rand truly produces integers at random, every number between 0 and RAND_MAX has an equal chance (or probability) of being chosen each time rand is called. The range of values produced directly by the function rand often is different than what a specific application requires. For example, a program that simulates coin tossing might require only 0 for “heads” and 1 for “tails.” A program that simulates rolling a sixsided die would require random integers in the range 1 to 6. A program that randomly predicts the next type of spaceship (out of four possibilities) that will fly across the horizon in a video game might require random integers in the range 1 through 4.

Rolling a Six-Sided Die To demonstrate rand, Fig. 6.8 simulates 20 rolls of a six-sided die and displays the value of each roll. The function prototype for the rand function is in . To produce integers in the range 0 to 5, we use the modulus operator (%) with rand as follows: rand() % 6

This is called scaling. The number 6 is called the scaling factor. We then shift the range of numbers produced by adding 1 to our previous result. Figure 6.8 confirms that the results are in the range 1 to 6. 1 2 3 4

// Fig. 6.8: fig06_08.cpp // Shifted and scaled random integers. #include #include

Fig. 6.8 | Shifted, scaled integers produced by 1

+ rand() % 6.

(Part 1 of 2.)

208 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Chapter 6 Functions and an Introduction to Recursion

#include // contains function prototype for rand using namespace std; int main() { // loop 20 times for ( int counter = 1; counter <= 20; ++counter ) { // pick random number from 1 to 6 and output it cout << setw( 10 ) << ( 1 + rand() % 6 ); // if counter is divisible by 5, start a new line of output if ( counter % 5 == 0 ) cout << endl; } // end for } // end main 6 5 6 6

6 1 6 2

5 1 2 3

5 5 4 4

Fig. 6.8 | Shifted, scaled integers produced by 1

6 3 2 1

+ rand() % 6.

(Part 2 of 2.)

Rolling a Six-Sided Die 6,000,000 Times To show that the numbers produced by rand occur with approximately equal likelihood, Fig. 6.9 simulates 6,000,000 rolls of a die. Each integer in the range 1 to 6 should appear approximately 1,000,000 times. This is confirmed by the program’s output. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

// Fig. 6.9: fig06_09.cpp // Rolling a six-sided die 6,000,000 times. #include #include #include // contains function prototype for rand using namespace std; int main() { int frequency1 int frequency2 int frequency3 int frequency4 int frequency5 int frequency6

= = = = = =

0; 0; 0; 0; 0; 0;

// // // // // //

count count count count count count

of of of of of of

1s 2s 3s 4s 5s 6s

rolled rolled rolled rolled rolled rolled

int face; // stores most recently rolled value // summarize results of 6,000,000 rolls of a die for ( int roll = 1; roll <= 6000000; ++roll ) {

Fig. 6.9 | Rolling a six-sided die 6,000,000 times. (Part 1 of 2.)

6.7 Case Study: Random Number Generation 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

209

face = 1 + rand() % 6; // random number from 1 to 6 // determine roll value 1-6 and increment appropriate counter switch ( face ) { case 1: ++frequency1; // increment the 1s counter break; case 2: ++frequency2; // increment the 2s counter break; case 3: ++frequency3; // increment the 3s counter break; case 4: ++frequency4; // increment the 4s counter break; case 5: ++frequency5; // increment the 5s counter break; case 6: ++frequency6; // increment the 6s counter break; default: // invalid value cout << "Program should never get here!"; } // end switch } // end for cout << "Face" cout << " 1" << "\n 2" << "\n 3" << "\n 4" << "\n 5" << "\n 6" } // end main

Face 1 2 3 4 5 6

<< << << << << << <<

setw( setw( setw( setw( setw( setw( setw(

13 13 13 13 13 13 13

) ) ) ) ) ) )

<< << << << << << <<

"Frequency" << endl; // output headers frequency1 frequency2 frequency3 frequency4 frequency5 frequency6 << endl;

Frequency 999702 1000823 999378 998898 1000777 1000422

Fig. 6.9 | Rolling a six-sided die 6,000,000 times. (Part 2 of 2.) As the output shows, we can simulate the rolling of a six-sided die by scaling and shifting the values produced by rand. The program should never get to the default case (lines 45–46) in the switch structure, because the switch’s controlling expression (face) always has values in the range 1–6; however, we provide the default case as a matter of good practice. After we study arrays in Chapter 7, we show how to replace the entire switch structure in Fig. 6.9 elegantly with a single-line statement.

210

Chapter 6 Functions and an Introduction to Recursion

Error-Prevention Tip 6.2

Provide a default case in a switch to catch errors even if you are absolutely, positively certain that you have no bugs!

Randomizing the Random Number Generator Executing the program of Fig. 6.8 again produces 6 5 6 6

6 1 6 2

5 1 2 3

5 5 4 4

6 3 2 1

The program prints exactly the same sequence of values shown in Fig. 6.8. How can these be random numbers? When debugging a simulation program, this repeatability is essential for proving that corrections to the program work properly. Function rand actually generates pseudorandom numbers. Repeatedly calling rand produces a sequence of numbers that appears to be random. However, the sequence repeats itself each time the program executes. Once a program has been thoroughly debugged, it can be conditioned to produce a different sequence of random numbers for each execution. This is called randomizing and is accomplished with the C++ Standard Library function srand. Function srand takes an unsigned integer argument and seeds the rand function to produce a different sequence of random numbers for each execution. The new C++ standard provides additional random number capabilities that can produce nondeterministic random numbers—a set of random numbers that can’t be predicted. Such random number generators are used in simulations and security scenarios where predictability is undesirable.

Using Function srand Figure 6.10 demonstrates function srand. The program uses the data type unsigned, which is short for unsigned int. An int is stored in at least two bytes of memory (typically four bytes on 32-bit systems and as much as eight bytes on 64-bit systems) and can have positive and negative values. A variable of type unsigned int is also stored in at least two bytes of memory. A two-byte unsigned int can have only nonnegative values in the range 0–65535. A four-byte unsigned int can have only nonnegative values in the range 0– 4294967295. Function srand takes an unsigned int value as an argument. The function prototype for the srand function is in header . 1 2 3 4 5 6 7 8 9 10

// Fig. 6.10: fig06_10.cpp // Randomizing the die-rolling program. #include #include #include // contains prototypes for functions srand and rand using namespace std; int main() { unsigned seed; // stores the seed entered by the user

Fig. 6.10 | Randomizing the die-rolling program. (Part 1 of 2.)

6.7 Case Study: Random Number Generation 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

211

cout << "Enter seed: "; cin >> seed; srand( seed ); // seed random number generator // loop 10 times for ( int counter = 1; counter <= 10; ++counter ) { // pick random number from 1 to 6 and output it cout << setw( 10 ) << ( 1 + rand() % 6 ); // if counter is divisible by 5, start a new line of output if ( counter % 5 == 0 ) cout << endl; } // end for } // end main

Enter seed: 67 6 1

1 6

4 1

6 6

2 4

Enter seed: 432 4 3

6 1

3 5

1 4

6 2

Enter seed: 67 6 1

1 6

4 1

6 6

2 4

Fig. 6.10 | Randomizing the die-rolling program. (Part 2 of 2.) Let’s run the program several times and observe the results. Notice that the program produces a different sequence of random numbers each time it executes, provided that the user enters a different seed. We used the same seed in the first and third sample outputs, so the same series of 10 numbers is displayed in each of those outputs. To randomize without having to enter a seed each time, we may use a statement like srand( time( 0 ) );

This causes the computer to read its clock to obtain the value for the seed. Function time (with the argument 0 as written in the preceding statement) typically returns the current time as the number of seconds since January 1, 1970, at midnight Greenwich Mean Time (GMT). This value is converted to an unsigned integer and used as the seed to the random number generator. The function prototype for time is in .

Generalized Scaling and Shifting of Random Numbers Previously, we simulated the rolling of a six-sided die with the statement face = 1 + rand() % 6;

which always assigns an integer (at random) to variable face in the range 1 ≤ face ≤ 6. The width of this range (i.e., the number of consecutive integers in the range) is 6 and the

212

Chapter 6 Functions and an Introduction to Recursion

starting number in the range is 1. Referring to the preceding statement, we see that the width of the range is determined by the number used to scale rand with the modulus operator (i.e., 6), and the starting number of the range is equal to the number (i.e., 1) that is added to the expression rand % 6. We can generalize this result as number = shiftingValue + rand() % scalingFactor;

where shiftingValue is equal to the first number in the desired range of consecutive integers and scalingFactor is equal to the width of the desired range of consecutive integers.

6.8 Case Study: Game of Chance; Introducing enum One of the most popular games of chance is a dice game known as “craps,” which is played in casinos and back alleys worldwide. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5 and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first roll, the player wins. If the sum is 2, 3 or 12 on the first roll (called “craps”), the player loses (i.e., the “house” wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first roll, then that sum becomes the player’s “point.” To win, you must continue rolling the dice until you “make your point.” The player loses by rolling a 7 before making the point.

The program in Fig. 6.11 simulates the game. In the rules, notice that the player must roll two dice on the first roll and on all subsequent rolls. We define function rollDice (lines 63–75) to roll the dice and compute and print their sum. The function is defined once, but called from lines 21 and 45. The function takes no arguments and returns the sum of the two dice, so empty parentheses and the return type int are indicated in the function prototype (line 8) and function header (line 63). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

// Fig. 6.11: fig06_11.cpp // Craps simulation. #include #include // contains prototypes for functions srand and rand #include // contains prototype for function time using namespace std; int rollDice(); // rolls dice, calculates and displays sum int main() { // enumeration with constants that represent the game status enum Status { CONTINUE, WON, LOST }; // all caps in constants int myPoint; // point if no win or loss on first roll Status gameStatus; // can contain CONTINUE, WON or LOST // randomize random number generator using current time srand( time( 0 ) ); int sumOfDice = rollDice(); // first roll of the dice

Fig. 6.11 | Craps simulation. (Part 1 of 3.)

6.8 Case Study: Game of Chance; Introducing enum 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

213

// determine game status and point (if needed) based on first roll switch ( sumOfDice ) { case 7: // win with 7 on first roll case 11: // win with 11 on first roll gameStatus = WON; break; case 2: // lose with 2 on first roll case 3: // lose with 3 on first roll case 12: // lose with 12 on first roll gameStatus = LOST; break; default: // did not win or lose, so remember point gameStatus = CONTINUE; // game is not over myPoint = sumOfDice; // remember the point cout << "Point is " << myPoint << endl; break; // optional at end of switch } // end switch // while game is not complete while ( gameStatus == CONTINUE ) // not WON or LOST { sumOfDice = rollDice(); // roll dice again // determine game status if ( sumOfDice == myPoint ) // win by making point gameStatus = WON; else if ( sumOfDice == 7 ) // lose by rolling 7 before point gameStatus = LOST; } // end while // display won or lost message if ( gameStatus == WON ) cout << "Player wins" << endl; else cout << "Player loses" << endl; } // end main // roll dice, calculate sum and display results int rollDice() { // pick random die values int die1 = 1 + rand() % 6; // first die roll int die2 = 1 + rand() % 6; // second die roll int sum = die1 + die2; // compute sum of die values // display results of this roll cout << "Player rolled " << die1 << " + " << die2 << " = " << sum << endl; return sum; // end function rollDice } // end function rollDice

Fig. 6.11 | Craps simulation. (Part 2 of 3.)

214

Chapter 6 Functions and an Introduction to Recursion

Player rolled 2 + 5 = 7 Player wins

Player rolled 6 + 6 = 12 Player loses

Player rolled Point is 4 Player rolled Player rolled Player rolled Player rolled Player rolled Player rolled Player rolled Player rolled Player loses

Player rolled Point is 6 Player rolled Player rolled Player rolled Player rolled Player wins

1 + 3 = 4 4 2 6 2 2 1 4 4

+ + + + + + + +

6 4 4 3 4 1 4 3

= = = = = = = =

10 6 10 5 6 2 8 7

3 + 3 = 6 5 4 2 1

+ + + +

3 5 1 5

= = = =

8 9 3 6

Fig. 6.11 | Craps simulation. (Part 3 of 3.) The game is reasonably involved. The player may win or lose on the first roll or on any subsequent roll. The program uses variable gameStatus to keep track of this. Variable gameStatus is declared to be of new type Status. Line 13 declares a user-defined type called an enumeration. An enumeration, introduced by the keyword enum and followed by a type name (in this case, Status), is a set of integer constants represented by identifiers. The values of these enumeration constants start at 0, unless specified otherwise, and increment by 1. In the preceding enumeration, the constant CONTINUE has the value 0, WON has the value 1 and LOST has the value 2. The identifiers in an enum must be unique, but separate enumeration constants can have the same integer value.

Good Programming Practice 6.1

Capitalize the first letter of an identifier used as a user-defined type name.

Good Programming Practice 6.2

Use only uppercase letters in enumeration constant names. This makes these constants stand out in a program and reminds you that enumeration constants are not variables.

Variables of user-defined type Status can be assigned only one of the three values declared in the enumeration. When the game is won, the program sets variable gameStatus to WON (lines 28 and 49). When the game is lost, the program sets variable

6.9 Storage Classes

215

gameStatus to LOST (lines 33 and 52). Otherwise, the program sets variable gameStatus to CONTINUE (line 36) to indicate that the dice must be rolled again. Another popular enumeration is enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };

which creates user-defined type Months with enumeration constants representing the months of the year. The first value in the preceding enumeration is explicitly set to 1, so the remaining values increment from 1, resulting in the values 1 through 12. Any enumeration constant can be assigned an integer value in the enumeration definition, and subsequent enumeration constants each have a value 1 higher than the preceding constant in the list until the next explicit setting. After the first roll, if the game is won or lost, the program skips the body of the while statement (lines 43–53) because gameStatus is not equal to CONTINUE. The program proceeds to the if…else statement in lines 56–59, which prints "Player wins" if gameStatus is equal to WON and "Player loses" if gameStatus is equal to LOST. After the first roll, if the game is not over, the program saves the sum in myPoint (line 37). Execution proceeds with the while statement, because gameStatus is equal to CONTINUE. During each iteration of the while, the program calls rollDice to produce a new sum. If sum matches myPoint, the program sets gameStatus to WON (line 49), the while-test fails, the if…else statement prints "Player wins" and execution terminates. If sum is equal to 7, the program sets gameStatus to LOST (line 52), the while-test fails, the if…else statement prints "Player loses" and execution terminates. The craps program uses two functions—main and rollDice—and the switch, while, if…else, nested if…else and nested if statements. In the exercises, we further investigate of the game of craps.

Common Programming Error 6.7

Assigning the integer equivalent of an enumeration constant (rather than the enumeration constant, itself) to a variable of the enumeration type is a compilation error.

6.9 Storage Classes The programs you’ve seen so far use identifiers for variable names. The attributes of variables include name, type, size and value. This chapter also uses identifiers as names for userdefined functions. Actually, each identifier in a program has other attributes, including storage class, scope and linkage. C++ provides five storage-class specifiers: auto, register, extern, mutable and static. This section discusses storage-class specifiers auto, register, extern and static; mutable (discussed in Chapter 24, Other Topics) is used exclusively with classes.

Storage Class, Scope and Linkage An identifier’s storage class determines the period during which that identifier exists in memory. Some exist briefly, some are repeatedly created and destroyed and others exist for the entire execution of a program. First we discuss the storage classes static and automatic. An identifier’s scope is where the identifier can be referenced in a program. Some identifiers can be referenced throughout a program; others can be referenced from only limited portions of a program. Section 6.10 discusses the scope of identifiers.

216

Chapter 6 Functions and an Introduction to Recursion

An identifier’s linkage determines whether it’s known only in the source file where it’s declared or across multiple files that are compiled, then linked together. An identifier’s storage-class specifier helps determine its storage class and linkage.

Storage Class Categories The storage-class specifiers can be split into two storage classes: automatic storage class and static storage class. Keywords auto and register are used to declare variables of the automatic storage class. Such variables are created when program execution enters the block in which they’re defined, they exist while the block is active and they’re destroyed when the program exits the block. Local Variables Only local variables of a function can be of automatic storage class. A function’s local variables and parameters normally are of automatic storage class. The storage class specifier auto explicitly declares variables of automatic storage class. For example, the following declaration indicates that double variable x is a local variable of automatic storage class— it exists only in the nearest enclosing pair of curly braces within the body of the function in which the definition appears: auto double x;

Local variables are of automatic storage class by default, so keyword auto rarely is used. For this reason, the new C++ standard gives auto a new meaning which we discuss in Section 23.9. For the remainder of the text, we refer to variables of automatic storage class simply as automatic variables.

Performance Tip 6.1

Automatic storage is a means of conserving memory, because automatic storage class variables exist in memory only when the block in which they’re defined is executing.

Software Engineering Observation 6.6

Automatic storage is an example of the principle of least privilege. In the context of an application, the principle states that code should be granted only the amount of privilege and access that it needs to accomplish its designated task, but no more. Why should we have variables stored in memory and accessible when they’re not needed?

Register Variables Data in the machine-language version of a program is normally loaded into registers for calculations and other processing.

Performance Tip 6.2

The storage-class specifier register can be placed before an automatic variable declaration to suggest that the compiler maintain the variable in one of the computer’s high-speed hardware registers rather than in memory. If intensely used variables such as counters or totals are kept in hardware registers, the overhead of repeatedly loading the variables from memory into the registers and storing the results back into memory is eliminated.

The compiler might ignore register declarations. For example, there might not be a sufficient number of registers available. The following definition suggests that the integer

6.9 Storage Classes

217

variable counter be placed in one of the computer’s registers; regardless of whether the compiler does this, counter is initialized to 1: register int counter = 1;

The register keyword can be used only with local variables and function parameters.

Performance Tip 6.3

Often, register is unnecessary. Optimizing compilers can recognize frequently used variables and may place them in registers without needing a register declaration.

Static Storage Class Keywords extern and static declare identifiers for variables of the static storage class and for functions. Static-storage-class variables exist in memory from the point at which the program begins execution and last for the duration of the program. Such a variable is initialized once when its declaration is encountered. For functions, the name of the function exists when the program begins execution, just as for all other functions. However, even though the variables and the function names exist from the start of program execution, this does not mean that these identifiers can be used throughout the program. Storage class and scope (where a name can be used) are separate issues, as we’ll see in Section 6.10. Identifiers with Static Storage Class There are two types of identifiers with static storage class—external identifiers (such as global variables) and local variables declared with the storage-class specifier static. Global variables are created by placing variable declarations outside any class or function definition. Global variables retain their values throughout the execution of the program. Global variables and global functions can be referenced by any function that follows their declarations or definitions in the source file.

Software Engineering Observation 6.7

Declaring a variable as global rather than local allows unintended side effects to occur when a function that does not need access to the variable accidentally or maliciously modifies it. This is another example of the principle of least privilege. In general, except for truly global resources such as cin and cout, the use of global variables should be avoided unless there are unique performance requirements.

Software Engineering Observation 6.8

Variables used only in a particular function should be declared as local variables in that function rather than as global variables.

Local variables declared static are still known only in the function in which they’re declared, but, unlike automatic variables, static local variables retain their values when the function returns to its caller. The next time the function is called, the static local variables contain the values they had when the function last completed execution. The following statement declares local variable count to be static and to be initialized to 1: static int count = 1;

All numeric variables of the static storage class are initialized to zero by default, but it’s nevertheless a good practice to explicitly initialize all variables.

218

Chapter 6 Functions and an Introduction to Recursion

Storage-class specifiers extern and static have special meaning when they’re applied explicitly to external identifiers such as global variables and global function names. In Appendix F, C Legacy Code Topics, we discuss using extern and static with external identifiers and multiple-source-file programs.

6.10 Scope Rules The portion of the program where an identifier can be used is known as its scope. For example, when we declare a local variable in a block, it can be referenced only in that block and in blocks nested within that block. This section discusses four scopes for an identifier—function scope, global namespace scope, local scope and function-prototype scope. Later we’ll see two other scopes—class scope (Chapter 9) and namespace scope (Chapter 24). An identifier declared outside any function or class has global namespace scope. Such an identifier is “known” in all functions from the point at which it’s declared until the end of the file. Global variables, function definitions and function prototypes placed outside a function all have global namespace scope. Labels (identifiers followed by a colon such as start:) are the only identifiers with function scope. Labels can be used anywhere in the function in which they appear, but cannot be referenced outside the function body. Labels are used in goto statements (Appendix F). Identifiers declared inside a block have local scope. Local scope begins at the identifier’s declaration and ends at the terminating right brace (}) of the block in which the identifier is declared. Local variables have local scope, as do function parameters, which are also local variables of the function. Any block can contain variable declarations. When blocks are nested and an identifier in an outer block has the same name as an identifier in an inner block, the identifier in the outer block is “hidden” until the inner block terminates. The inner block “sees” the value of its own local identifier and not the value of the identically named identifier in the enclosing block. Local variables declared static still have local scope, even though they exist from the time the program begins execution. Storage duration does not affect the scope of an identifier. The only identifiers with function prototype scope are those used in the parameter list of a function prototype. As mentioned previously, function prototypes do not require names in the parameter list—only types are required. Names appearing in the parameter list of a function prototype are ignored by the compiler. Identifiers used in a function prototype can be reused elsewhere in the program without ambiguity. In a single prototype, a particular identifier can be used only once.

Common Programming Error 6.8

Accidentally using the same name for an identifier in an inner block that is used for an identifier in an outer block, when in fact you want the identifier in the outer block to be active for the duration of the inner block, is typically a logic error.

Error-Prevention Tip 6.3

Avoid variable names that hide names in outer scopes.

6.10 Scope Rules

219

The program of Fig. 6.12 demonstrates scoping issues with global variables, automatic local variables and static local variables. Line 10 declares and initializes global variable x to 1. This global variable is hidden in any block (or function) that declares a variable named x. In main, line 14 displays the value of global variable x. Line 16 declares a local variable x and initializes it to 5. Line 19 outputs this variable to show that the global x is hidden in main. Next, lines 20–24 define a new block in main in which another local variable x is initialized to 7 (line 21). Line 23 outputs this variable to show that it hides x in the outer block of main. When the block exits, the variable x with value 7 is destroyed automatically. Next, line 26 outputs the local variable x in the outer block of main to show that it’s no longer hidden. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

// Fig. 6.12: fig06_12.cpp // Scoping example. #include using namespace std; void useLocal(); // function prototype void useStaticLocal(); // function prototype void useGlobal(); // function prototype int x = 1; // global variable int main() { cout << "global x in main is " << x << endl; int x = 5; // local variable to main cout << "local x in main's outer scope is " << x << endl; { // start new scope int x = 7; // hides both x in outer scope and global x cout << "local x in main's inner scope is " << x << endl; } // end new scope cout << "local x in main's outer scope is " << x << endl; useLocal(); // useLocal has local x useStaticLocal(); // useStaticLocal has static local x useGlobal(); // useGlobal uses global x useLocal(); // useLocal reinitializes its local x useStaticLocal(); // static local x retains its prior value useGlobal(); // global x also retains its prior value cout << "\nlocal x in main is " << x << endl; } // end main // useLocal reinitializes local variable x during each call void useLocal() {

Fig. 6.12 | Scoping example. (Part 1 of 2.)

220 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

Chapter 6 Functions and an Introduction to Recursion

int x = 25; // initialized each time useLocal is called cout << "\nlocal x is " << x << " on entering useLocal" << endl; ++x; cout << "local x is " << x << " on exiting useLocal" << endl; } // end function useLocal // useStaticLocal initializes static local variable x only the // first time the function is called; value of x is saved // between calls to this function void useStaticLocal() { static int x = 50; // initialized first time useStaticLocal is called cout << "\nlocal static x is " << x << " on entering useStaticLocal" << endl; ++x; cout << "local static x is " << x << " on exiting useStaticLocal" << endl; } // end function useStaticLocal // useGlobal modifies global variable x during each call void useGlobal() { cout << "\nglobal x is " << x << " on entering useGlobal" << endl; x *= 10; cout << "global x is " << x << " on exiting useGlobal" << endl; } // end function useGlobal

global x in main is 1 local x in main's outer scope is 5 local x in main's inner scope is 7 local x in main's outer scope is 5 local x is 25 on entering useLocal local x is 26 on exiting useLocal local static x is 50 on entering useStaticLocal local static x is 51 on exiting useStaticLocal global x is 1 on entering useGlobal global x is 10 on exiting useGlobal local x is 25 on entering useLocal local x is 26 on exiting useLocal local static x is 51 on entering useStaticLocal local static x is 52 on exiting useStaticLocal global x is 10 on entering useGlobal global x is 100 on exiting useGlobal local x in main is 5

Fig. 6.12 | Scoping example. (Part 2 of 2.)

6.11 Function Call Stack and Activation Records

221

To demonstrate other scopes, the program defines three functions, each of which takes no arguments and returns nothing. Function useLocal (lines 39–46) declares automatic variable x (line 41) and initializes it to 25. When the program calls useLocal, the function prints the variable, increments it and prints it again before the function returns program control to its caller. Each time the program calls this function, the function recreates automatic variable x and reinitializes it to 25. Function useStaticLocal (lines 51–60) declares static variable x and initializes it to 50. Local variables declared as static retain their values even when they’re out of scope (i.e., the function in which they’re declared is not executing). When the program calls useStaticLocal, the function prints x, increments it and prints it again before the function returns program control to its caller. In the next call to this function, static local variable x contains the value 51. The initialization in line 53 occurs only once—the first time useStaticLocal is called. Function useGlobal (lines 63–68) does not declare any variables. Therefore, when it refers to variable x, the global x (line 10, preceding main) is used. When the program calls useGlobal, the function prints the global variable x, multiplies it by 10 and prints it again before the function returns program control to its caller. The next time the program calls useGlobal, the global variable has its modified value, 10. After executing functions useLocal, useStaticLocal and useGlobal twice each, the program prints the local variable x in main again to show that none of the function calls modified the value of x in main, because the functions all referred to variables in other scopes.

6.11 Function Call Stack and Activation Records To understand how C++ performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. Think of a stack as analogous to a pile of dishes. When a dish is placed on the pile, it’s normally placed at the top (referred to as pushing the dish onto the stack). Similarly, when a dish is removed from the pile, it’s normally removed from the top (referred to as popping the dish off the stack). Stacks are known as last-in, first-out (LIFO) data structures—the last item pushed (inserted) on the stack is the first item popped (removed) from the stack. One of the most important mechanisms for computer science students to understand is the function call stack (sometimes referred to as the program execution stack). This data structure—working “behind the scenes”—supports the function call/return mechanism. It also supports the creation, maintenance and destruction of each called function’s automatic variables. We explained the last-in, first-out (LIFO) behavior of stacks with our dish-stacking example. As we’ll see in Figs. 6.14–6.16, this LIFO behavior is exactly what a function does when returning to the function that called it. As each function is called, it may, in turn, call other functions, which may, in turn, call other functions—all before any of the functions returns. Each function eventually must return control to the function that called it. So, somehow, we must keep track of the return addresses that each function needs to return control to the function that called it. The function call stack is the perfect data structure for handling this information. Each time a function calls another function, an entry is pushed onto the stack. This entry, called a stack frame or an activation record, contains the return address that the called function needs in order to return to the calling function. It also contains some additional information we’ll soon discuss. If the called function returns, instead of calling another function before

222

Chapter 6 Functions and an Introduction to Recursion

returning, the stack frame for the function call is popped, and control transfers to the return address in the popped stack frame. The beauty of the call stack is that each called function always finds the information it needs to return to its caller at the top of the call stack. And, if a function makes a call to another function, a stack frame for the new function call is simply pushed onto the call stack. Thus, the return address required by the newly called function to return to its caller is now located at the top of the stack. The stack frames have another important responsibility. Most functions have automatic variables—parameters and any local variables the function declares. Automatic variables need to exist while a function is executing. They need to remain active if the function makes calls to other functions. But when a called function returns to its caller, the called function’s automatic variables need to “go away.” The called function’s stack frame is a perfect place to reserve the memory for the called function’s automatic variables. That stack frame exists as long as the called function is active. When that function returns—and no longer needs its local automatic variables—its stack frame is popped from the stack, and those local automatic variables are no longer known to the program. Of course, the amount of memory in a computer is finite, so only a certain amount of memory can be used to store activation records on the function call stack. If more function calls occur than can have their activation records stored on the function call stack, an error known as stack overflow occurs.

Function Call Stack in Action Now let’s consider how the call stack supports the operation of a square function called by main (lines 9–14 of Fig. 6.13). First the operating system calls main—this pushes an activation record onto the stack (shown in Fig. 6.14). The activation record tells main how to return to the operating system (i.e., transfer to return address R1) and contains the space for main’s automatic variable (i.e., a, which is initialized to 10). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

// Fig. 6.13: fig06_13.cpp // square function used to demonstrate the function // call stack and activation records. #include using namespace std; int square( int ); // prototype for function square int main() { int a = 10; // value to square (local automatic variable in main) cout << a << " squared: " << square( a ) << endl; // display a squared } // end main // returns the square of an integer int square( int x ) // x is a local variable { return x * x; // calculate square and return result } // end function square

Fig. 6.13 | Demonstrating the function call stack and activation records. (Part 1 of 2.)

6.11 Function Call Stack and Activation Records

223

10 squared: 100

Fig. 6.13 | Demonstrating the function call stack and activation records. (Part 2 of 2.) Step 1: Operating system invokes main to execute application int main() {

Operating system

int a = 10; cout << a << " squared: " << square( a ) << endl; return 0;

Return location R1

}

Function call stack after Step 1 Top of stack Return location: R1 Activation record for function main

Automatic variables: a

10

Key Lines that represent the operating system executing instructions

Fig. 6.14 | Function call stack after the operating system invokes main to execute the program. Function main—before returning to the operating system—now calls function in line 13 of Fig. 6.13. This causes a stack frame for square (lines 17–20) to be pushed onto the function call stack (Fig. 6.15). This stack frame contains the return address that square needs to return to main (i.e., R2) and the memory for square’s automatic variable (i.e., x). After square calculates the square of its argument, it needs to return to main—and no longer needs the memory for its automatic variable x. So the stack is popped—giving square the return location in main (i.e., R2) and losing square’s automatic variable. Figure 6.16 shows the function call stack after square’s activation record has been popped. square

Step 2: main invokes function square to perform calculation int main() { int a = 10; cout << a << " squared: " << square( a ) << endl; return 0;

Return location R2

int square( int x ) { return x * x; }

}

Fig. 6.15 | Function call stack after main invokes square to perform the calculation. (Part 1 of 2.)

224

Chapter 6 Functions and an Introduction to Recursion

Function call stack after Step 2 Top of stack Return location: R2 Automatic variables:

Activation record for function square

x

10

Return location: R1 Activation record for function main

Automatic variables: a

10

Fig. 6.15 | Function call stack after main invokes square to perform the calculation. (Part 2 of 2.)

Function main now displays the result of calling square (line 13). Reaching the closing right brace of main causes its activation record to be popped from the stack, gives main the address it needs to return to the operating system (i.e., R1 in Fig. 6.14) and causes the memory for main’s automatic variable (i.e., a) to become unavailable. Step 3: square returns its result to main int main() { int a = 10; cout << a << " squared: " << square( a ) << endl; return 0;

Return location R2

int square( int x ) { return x * x; }

}

Function call stack after Step 3

Top of stack Return location: R1 Activation record for function main

Automatic variables: a

10

Fig. 6.16 | Function call stack after function square returns to main.

6.12 Functions with Empty Parameter Lists

225

You’ve now seen how valuable the stack data structure is in implementing a key mechanism that supports program execution. Data structures have many important applications in computer science. We discuss stacks, queues, lists, trees and other data structures in Chapter 20, Custom Templatized Data Structures, and Chapter 22, Standard Template Library (STL).

6.12 Functions with Empty Parameter Lists In C++, an empty parameter list is specified by writing either void or nothing at all in parentheses. The prototype void print();

specifies that function print does not take arguments and does not return a value. Figure 6.17 shows both ways to declare and use functions with empty parameter lists. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

// Fig. 6.17: fig06_17.cpp // Functions that take no arguments. #include using namespace std; void function1(); // function that takes no arguments void function2( void ); // function that takes no arguments int main() { function1(); // call function1 with no arguments function2(); // call function2 with no arguments } // end main // function1 uses an empty parameter list to specify that // the function receives no arguments void function1() { cout << "function1 takes no arguments" << endl; } // end function1 // function2 uses a void parameter list to specify that // the function receives no arguments void function2( void ) { cout << "function2 also takes no arguments" << endl; } // end function2

function1 takes no arguments function2 also takes no arguments

Fig. 6.17 | Functions that take no arguments.

6.13 Inline Functions Implementing a program as a set of functions is good from a software engineering standpoint, but function calls involve execution-time overhead. C++ provides inline functions

226

Chapter 6 Functions and an Introduction to Recursion

to help reduce function call overhead—especially for small functions. Placing the qualifier inline before a function’s return type in the function definition “advises” the compiler to generate a copy of the function’s body code in place (when appropriate) to avoid a function call. The trade-off is that multiple copies of the function code are inserted in the program (often making the program larger) rather than there being a single copy of the function to which control is passed each time the function is called. The compiler can ignore the inline qualifier and typically does so for all but the smallest functions.

Software Engineering Observation 6.9

Any change to an inline function requires all clients of the function to be recompiled.

Figure 6.18 uses inline function cube (lines 9–12) to calculate the volume of a cube. Keyword const in function cube’s parameter list (line 9) tells the compiler that the function does not modify variable side. This ensures that side’s value is not changed by the function during the calculation. (Keyword const is discussed in detail in Chapters 7, 8 and 10.) Notice that the complete definition of function cube appears before it’s used in the program. This is required so that the compiler knows how to expand a cube function call into its inlined code. For this reason, reusable inline functions are typically placed in headers, so that their definitions can be included in each source file that uses them.

Software Engineering Observation 6.10

The const qualifier should be used to enforce the principle of least privilege. Using the principle of least privilege to properly design software can greatly reduce debugging time and improper side effects and can make a program easier to modify and maintain. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 6.18: fig06_18.cpp // Using an inline function to calculate the volume of a cube. #include using namespace std; // Definition of inline function cube. Definition of function appears // before function is called, so a function prototype is not required. // First line of function definition acts as the prototype. inline double cube( const double side ) { return side * side * side; // calculate cube } // end function cube int main() { double sideValue; // stores value entered by user cout << "Enter the side length of your cube: "; cin >> sideValue; // read value from user // calculate cube of sideValue and display result cout << "Volume of cube with side " << sideValue << " is " << cube( sideValue ) << endl; } // end main

Fig. 6.18 |

inline

function that calculates the volume of a cube. (Part 1 of 2.)

6.14 References and Reference Parameters

227

Enter the side length of your cube: 3.5 Volume of cube with side 3.5 is 42.875

Fig. 6.18 |

inline

function that calculates the volume of a cube. (Part 2 of 2.)

6.14 References and Reference Parameters Two ways to pass arguments to functions in many programming languages are pass-byvalue and pass-by-reference. When an argument is passed by value, a copy of the argument’s value is made and passed (on the function call stack) to the called function. Changes to the copy do not affect the original variable’s value in the caller. This prevents the accidental side effects that so greatly hinder the development of correct and reliable software systems. Each argument in this chapter has been passed by value.

Performance Tip 6.4

One disadvantage of pass-by-value is that, if a large data item is being passed, copying that data can take a considerable amount of execution time and memory space.

Reference Parameters This section introduces reference parameters—the first of the two means C++ provides for performing pass-by-reference. With pass-by-reference, the caller gives the called function the ability to access the caller’s data directly, and to modify that data.

Performance Tip 6.5

Pass-by-reference is good for performance reasons, because it can eliminate the pass-by-value overhead of copying large amounts of data.

Software Engineering Observation 6.11

Pass-by-reference can weaken security; the called function can corrupt the caller’s data.

Later, we’ll show how to achieve the performance advantage of pass-by-reference while simultaneously achieving the software engineering advantage of protecting the caller’s data from corruption. A reference parameter is an alias for its corresponding argument in a function call. To indicate that a function parameter is passed by reference, simply follow the parameter’s type in the function prototype by an ampersand (&); use the same convention when listing the parameter’s type in the function header. For example, the following declaration in a function header int &count

when read from right to left is pronounced “count is a reference to an int.” In the function call, simply mention the variable by name to pass it by reference. Then, mentioning the variable by its parameter name in the body of the called function actually refers to the original variable in the calling function, and the original variable can be modified directly by the called function. As always, the function prototype and header must agree.

228

Chapter 6 Functions and an Introduction to Recursion

Passing Arguments by Value and by Reference Figure 6.19 compares pass-by-value and pass-by-reference with reference parameters. The “styles” of the arguments in the calls to function squareByValue and function squareByReference are identical—both variables are simply mentioned by name in the function calls. Without checking the function prototypes or function definitions, it isn’t possible to tell from the calls alone whether either function can modify its arguments. Because function prototypes are mandatory, the compiler has no trouble resolving the ambiguity.

Common Programming Error 6.9

Because reference parameters are mentioned only by name in the body of the called function, you might inadvertently treat reference parameters as pass-by-value parameters. This can cause unexpected side effects if the original variables are changed by the function. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

// Fig. 6.19: fig06_19.cpp // Comparing pass-by-value and pass-by-reference with references. #include using namespace std; int squareByValue( int ); // function prototype (value pass) void squareByReference( int & ); // function prototype (reference pass) int main() { int x = 2; // value to square using squareByValue int z = 4; // value to square using squareByReference // demonstrate squareByValue cout << "x = " << x << " before squareByValue\n"; cout << "Value returned by squareByValue: " << squareByValue( x ) << endl; cout << "x = " << x << " after squareByValue\n" << endl; // demonstrate squareByReference cout << "z = " << z << " before squareByReference" << endl; squareByReference( z ); cout << "z = " << z << " after squareByReference" << endl; } // end main // squareByValue multiplies number by itself, stores the // result in number and returns the new value of number int squareByValue( int number ) { return number *= number; // caller's argument not modified } // end function squareByValue // squareByReference multiplies numberRef by itself and stores the result // in the variable to which numberRef refers in function main void squareByReference( int &numberRef ) { numberRef *= numberRef; // caller's argument modified } // end function squareByReference

Fig. 6.19 | Passing arguments by value and by reference. (Part 1 of 2.)

6.14 References and Reference Parameters

229

x = 2 before squareByValue Value returned by squareByValue: 4 x = 2 after squareByValue z = 4 before squareByReference z = 16 after squareByReference

Fig. 6.19 | Passing arguments by value and by reference. (Part 2 of 2.) Chapter 8 discusses pointers; pointers enable an alternate form of pass-by-reference in which the style of the call clearly indicates pass-by-reference (and the potential for modifying the caller’s arguments).

Performance Tip 6.6

For passing large objects, use a constant reference parameter to simulate the appearance and security of pass-by-value and avoid the overhead of passing a copy of the large object.

To specify a reference to a constant, place the const qualifier before the type specifier in the parameter declaration. Note the placement of & in function squareByReference’s parameter list (line 35, Fig. 6.19). Some C++ programmers prefer to write the equivalent form int& numberRef.

References as Aliases within a Function References can also be used as aliases for other variables within a function (although they typically are used with functions as shown in Fig. 6.19). For example, the code int count = 1; // declare integer variable count int &cRef = count; // create cRef as an alias for count ++cRef; // increment count (using its alias cRef)

increments variable count by using its alias cRef. Reference variables must be initialized in their declarations (see Fig. 6.20 and Fig. 6.21) and cannot be reassigned as aliases to other variables. Once a reference is declared as an alias for another variable, all operations supposedly performed on the alias (i.e., the reference) are actually performed on the original variable. The alias is simply another name for the original variable. Unless it’s a reference to a constant, a reference argument must be an lvalue (e.g., a variable name), not a constant or expression that returns an rvalue (e.g., the result of a calculation). See Section 5.9 for definitions of the terms lvalue and rvalue. 1 2 3 4 5 6 7 8 9

// Fig. 6.20: fig06_20.cpp // Initializing and using a reference. #include using namespace std; int main() { int x = 3; int &y = x; // y refers to (is an alias for) x

Fig. 6.20 | Initializing and using a reference. (Part 1 of 2.)

230 10 11 12 13 14 x y x y

Chapter 6 Functions and an Introduction to Recursion

cout << "x = " << x << endl << "y = " << y << endl; y = 7; // actually modifies x cout << "x = " << x << endl << "y = " << y << endl; } // end main = = = =

3 3 7 7

Fig. 6.20 | Initializing and using a reference. (Part 2 of 2.) 1 2 3 4 5 6 7 8 9 10 11 12 13 14

// Fig. 6.21: fig06_21.cpp // References must be initialized. #include using namespace std; int main() { int x = 3; int &y; // Error: y must be initialized cout << "x = " << x << endl << "y = " << y << endl; y = 7; cout << "x = " << x << endl << "y = " << y << endl; } // end main

Microsoft Visual C++ compiler error message: C:\cpphtp8_examples\ch06\Fig06_21\fig06_21.cpp(9) : error C2530: 'y' : references must be initialized

GNU C++ compiler error message: fig06_21.cpp:9: error: 'y' declared as a reference but not initialized

Fig. 6.21 | Uninitialized reference causes a syntax error. Returning a Reference from a Function Functions can return references, but this can be dangerous. When returning a reference to a variable declared in the called function, unless that variable is declared static, the reference refers to an automatic variable that’s discarded when the function terminates. Such a variable is said to be “undefined,” and the program’s behavior is unpredictable. References to undefined variables are called dangling references.

Common Programming Error 6.10

Returning a reference to an automatic variable in a called function is a logic error. Some compilers issue a warning when this occurs.

6.15 Default Arguments

231

Error Messages for Uninitialized References The C++ standard does not specify the error messages that compilers use to indicate particular errors. For this reason, Fig. 6.21 shows the error messages produced by the Microsoft Visual C++ 2008 compiler and GNU C++ compiler when a reference is not initialized.

6.15 Default Arguments It’s common for a program to invoke a function repeatedly with the same argument value for a particular parameter. In such cases, you can specify that such a parameter has a default argument, i.e., a default value to be passed to that parameter. When a program omits an argument for a parameter with a default argument in a function call, the compiler rewrites the function call and inserts the default value of that argument. Default arguments must be the rightmost (trailing) arguments in a function’s parameter list. When calling a function with two or more default arguments, if an omitted argument is not the rightmost argument in the argument list, then all arguments to the right of that argument also must be omitted. Default arguments must be specified with the first occurrence of the function name—typically, in the function prototype. If the function prototype is omitted because the function definition also serves as the prototype, then the default arguments should be specified in the function header. Default values can be any expression, including constants, global variables or function calls. Default arguments also can be used with inline functions. Figure 6.22 demonstrates using default arguments to calculate a box’s volume. The function prototype for boxVolume (line 7) specifies that all three parameters have been given default values of 1. We provided variable names in the function prototype for readability. As always, variable names are not required in function prototypes. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

// Fig. 6.22: fig06_22.cpp // Using default arguments. #include using namespace std; // function prototype that specifies default arguments int boxVolume( int length = 1, int width = 1, int height = 1 ); int main() { // no arguments--use default values for all dimensions cout << "The default box volume is: " << boxVolume(); // specify length; default width and height cout << "\n\nThe volume of a box with length 10,\n" << "width 1 and height 1 is: " << boxVolume( 10 ); // specify length and width; default height cout << "\n\nThe volume of a box with length 10,\n" << "width 5 and height 1 is: " << boxVolume( 10, 5 );

Fig. 6.22 | Default arguments to a function. (Part 1 of 2.)

232 22 23 24 25 26 27 28 29 30 31 32

Chapter 6 Functions and an Introduction to Recursion

// specify all arguments cout << "\n\nThe volume of a box with length 10,\n" << "width 5 and height 2 is: " << boxVolume( 10, 5, 2 ) << endl; } // end main // function boxVolume calculates the volume of a box int boxVolume( int length, int width, int height ) { return length * width * height; } // end function boxVolume

The default box volume is: 1 The volume of a box with length 10, width 1 and height 1 is: 10 The volume of a box with length 10, width 5 and height 1 is: 50 The volume of a box with length 10, width 5 and height 2 is: 100

Fig. 6.22 | Default arguments to a function. (Part 2 of 2.) The first call to boxVolume (line 12) specifies no arguments, thus using all three default values of 1. The second call (line 16) passes only a length argument, thus using default values of 1 for the width and height arguments. The third call (line 20) passes arguments for only length and width, thus using a default value of 1 for the height argument. The last call (line 24) passes arguments for length, width and height, thus using no default values. Any arguments passed to the function explicitly are assigned to the function’s parameters from left to right. Therefore, when boxVolume receives one argument, the function assigns the value of that argument to its length parameter (i.e., the leftmost parameter in the parameter list). When boxVolume receives two arguments, the function assigns the values of those arguments to its length and width parameters in that order. Finally, when boxVolume receives all three arguments, the function assigns the values of those arguments to its length, width and height parameters, respectively.

Good Programming Practice 6.3

Using default arguments can simplify writing function calls. However, some programmers feel that explicitly specifying all arguments is clearer.

Software Engineering Observation 6.12

If the default values for a function change, all client code using the function must be recompiled.

6.16 Unary Scope Resolution Operator It’s possible to declare local and global variables of the same name. C++ provides the unary scope resolution operator (::) to access a global variable when a local variable of the same

6.16 Unary Scope Resolution Operator

233

name is in scope. The unary scope resolution operator cannot be used to access a local variable of the same name in an outer block. A global variable can be accessed directly without the unary scope resolution operator if the name of the global variable is not the same as that of a local variable in scope. Figure 6.23 shows the unary scope resolution operator with local and global variables of the same name (lines 6 and 10). To emphasize that the local and global versions of variable number are distinct, the program declares one variable int and the other double. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

// Fig. 6.23: fig06_23.cpp // Using the unary scope resolution operator. #include using namespace std; int number = 7; // global variable named number int main() { double number = 10.5; // local variable named number // display values of local and global variables cout << "Local double value of number = " << number << "\nGlobal int value of number = " << ::number << endl; } // end main

Local double value of number = 10.5 Global int value of number = 7

Fig. 6.23 | Unary scope resolution operator. Using the unary scope resolution operator (::) with a given variable name is optional when the only variable with that name is a global variable.

Good Programming Practice 6.4

Always using the unary scope resolution operator (::) to refer to global variables makes programs easier to read and understand, because it makes it clear that you’re intending to access a global variable rather than a nonglobal variable.

Software Engineering Observation 6.13

Always using the unary scope resolution operator (::) to refer to global variables makes programs easier to modify by reducing the risk of name collisions with nonglobal variables.

Error-Prevention Tip 6.4

Always using the unary scope resolution operator (::) to refer to a global variable eliminates logic errors that might occur if a nonglobal variable hides the global variable.

Error-Prevention Tip 6.5

Avoid using variables of the same name for different purposes in a program. Although this is allowed in various circumstances, it can lead to errors.

234

Chapter 6 Functions and an Introduction to Recursion

6.17 Function Overloading C++ enables several functions of the same name to be defined, as long as they have different signatures. This is called function overloading. The C++ compiler selects the proper function to call by examining the number, types and order of the arguments in the call. Function overloading is used to create several functions of the same name that perform similar tasks, but on different data types. For example, many functions in the math library are overloaded for different numeric types—the C++ standard requires float, double and long double overloaded versions of the math library functions discussed in Section 6.3.

Good Programming Practice 6.5

Overloading functions that perform closely related tasks can make programs more readable and understandable.

Overloaded square Functions Figure 6.24 uses overloaded square functions to calculate the square of an int (lines 7– 11) and the square of a double (lines 14–18). Line 22 invokes the int version of function square by passing the literal value 7. C++ treats whole number literal values as type int. Similarly, line 24 invokes the double version of function square by passing the literal value 7.5, which C++ treats as a double value. In each case the compiler chooses the proper function to call, based on the type of the argument. The last two lines of the output window confirm that the proper function was called in each case. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

// Fig. 6.24: fig06_24.cpp // Overloaded functions. #include using namespace std; // function square for int values int square( int x ) { cout << "square of integer " << x << " is "; return x * x; } // end function square with int argument // function square for double values double square( double y ) { cout << "square of double " << y << " is "; return y * y; } // end function square with double argument int main() { cout << square( 7 ); // calls int version cout << endl; cout << square( 7.5 ); // calls double version cout << endl; } // end main

Fig. 6.24 | Overloaded square functions. (Part 1 of 2.)

6.17 Function Overloading

235

square of integer 7 is 49 square of double 7.5 is 56.25

Fig. 6.24 | Overloaded square functions. (Part 2 of 2.) How the Compiler Differentiates Overloaded Functions Overloaded functions are distinguished by their signatures. A signature is a combination of a function’s name and its parameter types (in order). The compiler encodes each function identifier with the number and types of its parameters (sometimes referred to as name mangling or name decoration) to enable type-safe linkage. Type-safe linkage ensures that the proper overloaded function is called and that the types of the arguments conform to the types of the parameters. Figure 6.25 was compiled with GNU C++. Rather than showing the execution output of the program (as we normally would), we show the mangled function names produced in assembly language by GNU C++. Each mangled name (other than main) begins with two underscores (__) followed by the letter Z, a number and the function name. The number that follows Z specifies how many characters are in the function’s name. For example, function square has 6 characters in its name, so its mangled name is prefixed with __Z6. The function name is then followed by an encoding of its parameter list. In the parameter list for function nothing2 (line 25; see the fourth output line), c represents a char, i represents an int, Rf represents a float & (i.e., a reference to a float) and Rd represents a double & (i.e., a reference to a double). In the parameter list for function nothing1, i represents an int, f represents a float, c represents a char and Ri represents an int &. The two square functions are distinguished by their parameter lists; one specifies d for double and the other specifies i for int. The return types of the functions are not specified in the mangled names. Overloaded functions can have different return types, but if they do, they must also have different parameter lists. Again, you cannot have two functions with the same signature and different return types. Function-name mangling is compiler specific. Also, function main is not mangled, because it cannot be overloaded.

Common Programming Error 6.11

Creating overloaded functions with identical parameter lists and different return types is a compilation error. 1 2 3 4 5 6 7 8 9 10 11 12

// Fig. 6.25: fig06_25.cpp // Name mangling. // function square for int values int square( int x ) { return x * x; } // end function square // function square for double values double square( double y ) {

Fig. 6.25 | Name mangling to enable type-safe linkage. (Part 1 of 2.)

236 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

Chapter 6 Functions and an Introduction to Recursion

return y * y; } // end function square // function that receives arguments of types // int, float, char and int & void nothing1( int a, float b, char c, int &d ) { // empty function body } // end function nothing1 // function that receives arguments of types // char, int, float & and double & int nothing2( char a, int b, float &c, double &d ) { return 0; } // end function nothing2 int main() { } // end main

__Z6squarei __Z6squared __Z8nothing1ifcRi __Z8nothing2ciRfRd _main

Fig. 6.25 | Name mangling to enable type-safe linkage. (Part 2 of 2.) The compiler uses only the parameter lists to distinguish between overloaded functions. Such functions need not have the same number of parameters. Use caution when overloading functions with default parameters, because this may cause ambiguity.

Common Programming Error 6.12

A function with default arguments omitted might be called identically to another overloaded function; this is a compilation error. For example, having a program that contains both a function that explicitly takes no arguments and a function of the same name that contains all default arguments results in a compilation error when an attempt is made to use that function name in a call passing no arguments. The compiler cannot determine which version of the function to choose.

Overloaded Operators In Chapter 11, we discuss how to overload operators to define how they should operate on objects of user-defined data types. (In fact, we’ve been using many overloaded operators to this point, including the stream insertion << and the stream extraction >> operators, which are overloaded for all the fundamental types. We say more about overloading << and >> to be able to handle objects of user-defined types in Chapter 11.)

6.18 Function Templates Overloaded functions are normally used to perform similar operations that involve different program logic on different data types. If the program logic and operations are identical

6.18 Function Templates

237

for each data type, overloading may be performed more compactly and conveniently by using function templates. You write a single function template definition. Given the argument types provided in calls to this function, C++ automatically generates separate function template specializations to handle each type of call appropriately. Thus, defining a single function template essentially defines a whole family of overloaded functions. Figure 6.26 defines a maximum function template (lines 3–17) that determines the largest of three values. All function template definitions begin with the template keyword (line 3) followed by a template parameter list to the function template enclosed in angle brackets (< and >). Every parameter in the template parameter list (often referred to as a formal type parameter) is preceded by keyword typename or keyword class (they are synonyms in this context). The formal type parameters are placeholders for fundamental types or user-defined types. These placeholders, in this case, T, are used to specify the types of the function’s parameters (line 4), to specify the function’s return type (line 4) and to declare variables within the body of the function definition (line 6). A function template is defined like any other function, but uses the formal type parameters as placeholders for actual data types. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

// Fig. 6.26: maximum.h // Definition of function template maximum. template < typename T > // or template< typename T > T maximum( T value1, T value2, T value3 ) { T maximumValue = value1; // assume value1 is maximum // determine whether value2 is greater than maximumValue if ( value2 > maximumValue ) maximumValue = value2; // determine whether value3 is greater than maximumValue if ( value3 > maximumValue ) maximumValue = value3; return maximumValue; } // end function template maximum

Fig. 6.26 | Function template maximum header. The function template declares a single formal type parameter T (line 3) as a placeholder for the type of the data to be tested by function maximum. The name of a type parameter must be unique in the template parameter list for a particular template definition. When the compiler detects a maximum invocation in the program source code, the type of the data passed to maximum is substituted for T throughout the template definition, and C++ creates a complete function for determining the maximum of three values of the specified data type—all three must have the same type, since we use only one type parameter in this example. Then the newly created function is compiled. Thus, templates are a means of code generation. Figure 6.27 uses the maximum function template to determine the largest of three int values, three double values and three char values, respectively (lines 17, 27 and 37). Separate functions are created as a result of the calls in lines 17, 27 and 37—expecting three int values, three double values and three char values, respectively. The function template specialization created for type int replaces each occurrence of T with int as follows:

238

Chapter 6 Functions and an Introduction to Recursion int maximum( int value1, int value2, int value3 ) { int maximumValue = value1; // assume value1 is maximum // determine whether value2 is greater than maximumValue if ( value2 > maximumValue ) maximumValue = value2; // determine whether value3 is greater than maximumValue if ( value3 > maximumValue ) maximumValue = value3; return maximumValue; } // end function template maximum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

// Fig. 6.27: fig06_27.cpp // Function template maximum test program. #include #include "maximum.h" // include definition of function template maximum using namespace std; int main() { // demonstrate maximum with int values int int1, int2, int3; cout << "Input three integer values: "; cin >> int1 >> int2 >> int3; // invoke int version of maximum cout << "The maximum integer value is: " << maximum( int1, int2, int3 ); // demonstrate maximum with double values double double1, double2, double3; cout << "\n\nInput three double values: "; cin >> double1 >> double2 >> double3; // invoke double version of maximum cout << "The maximum double value is: " << maximum( double1, double2, double3 ); // demonstrate maximum with char values char char1, char2, char3; cout << "\n\nInput three characters: "; cin >> char1 >> char2 >> char3; // invoke char version of maximum cout << "The maximum character value is: " << maximum( char1, char2, char3 ) << endl; } // end main

Fig. 6.27 | Demonstrating function template maximum. (Part 1 of 2.)

6.19 Recursion

239

Input three integer values: 1 2 3 The maximum integer value is: 3 Input three double values: 3.3 2.2 1.1 The maximum double value is: 3.3 Input three characters: A C B The maximum character value is: C

Fig. 6.27 | Demonstrating function template maximum. (Part 2 of 2.)

6.19 Recursion For some problems, it’s useful to have functions call themselves. A recursive function is a function that calls itself, either directly, or indirectly (through another function). [Note: Although many compilers allow function main to call itself, Section 3.6.1, paragraph 3, and Section 5.2.2, paragraph 9, of the C++ standard document indicate that main should not be called within a program or recursively. Its sole purpose is to be the starting point for program execution.] Recursion is an important topic discussed at length in upper-level computer science courses. This section and the next present simple examples of recursion. Figure 6.33 (at the end of Section 6.21) summarizes the extensive recursion examples and exercises in the book. We first consider recursion conceptually, then examine two programs containing recursive functions. Recursive problem-solving approaches have a number of elements in common. A recursive function is called to solve a problem. The function knows how to solve only the simplest case(s), or so-called base case(s). If the function is called with a base case, the function simply returns a result. If the function is called with a more complex problem, it typically divides the problem into two conceptual pieces—a piece that the function knows how to do and a piece that it does not know how to do. To make recursion feasible, the latter piece must resemble the original problem, but be a slightly simpler or smaller version. This new problem looks like the original, so the function calls a copy of itself to work on the smaller problem—this is referred to as a recursive call and is also called the recursion step. The recursion step often includes the keyword return, because its result will be combined with the portion of the problem the function knew how to solve to form the result passed back to the original caller, possibly main. The recursion step executes while the original call to the function is still “open,” i.e., it has not yet finished executing. The recursion step can result in many more such recursive calls, as the function keeps dividing each new subproblem with which the function is called into two conceptual pieces. In order for the recursion to eventually terminate, each time the function calls itself with a slightly simpler version of the original problem, this sequence of smaller and smaller problems must eventually converge on the base case. At that point, the function recognizes the base case and returns a result to the previous copy of the function, and a sequence of returns ensues up the line until the original call eventually returns the final result to main. This sounds quite exotic compared to the kind of problem solving we’ve been using to this point. As an example of these concepts at work, let’s write a recursive program to perform a popular mathematical calculation.

240

Chapter 6 Functions and an Introduction to Recursion

The factorial of a nonnegative integer n, written n! (and pronounced “n factorial”), is the product n · (n – 1) · (n – 2) · … · 1

with 1! equal to 1, and 0! defined to be 1. For example, 5! is the product 5 · 4 · 3 · 2 · 1, which is equal to 120. The factorial of an integer, number, greater than or equal to 0, can be calculated iteratively (nonrecursively) by using a for statement as follows: factorial = 1; for ( int counter = number; counter >= 1; --counter ) factorial *= counter;

A recursive definition of the factorial function is arrived at by observing the following algebraic relationship: n! = n · (n – 1)!

For example, 5! is clearly equal to 5 * 4! as is shown by the following: 5! = 5 · 4 · 3 · 2 · 1 5! = 5 · (4 · 3 · 2 · 1) 5! = 5 · (4!)

The evaluation of 5! would proceed as shown in Fig. 6.28, which illustrates how the succession of recursive calls proceeds until 1! is evaluated to be 1, terminating the recursion. Figure 6.28(b) shows the values returned from each recursive call to its caller until the final value is calculated and returned. Final value = 120 5!

5!

5! = 5 * 24 = 120 is returned 5 * 4!

5 * 4!

4! = 4 * 6 = 24 is returned 4 * 3!

4 * 3!

3! = 3 * 2 = 6 is returned 3 * 2!

3 * 2!

2! = 2 * 1 = 2 is returned 2 * 1!

2 * 1!

1 returned 1

(a) Procession of recursive calls

Fig. 6.28 | Recursive evaluation of 5!.

1

(b) Values returned from each recursive call

6.19 Recursion

241

Figure 6.29 uses recursion to calculate and print the factorials of the integers 0–10. (The choice of the data type unsigned long is explained momentarily.) The recursive function factorial (lines 18–24) first determines whether the terminating condition number <= 1 (line 20) is true. If number is less than or equal to 1, the factorial function returns 1 (line 21), no further recursion is necessary and the function terminates. If number is greater than 1, line 23 expresses the problem as the product of number and a recursive call to factorial evaluating the factorial of number - 1, which is a slightly simpler problem than the original calculation factorial(number). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 0! 1! 2! 3! 4! 5! 6! 7! 8! 9! 10!

// Fig. 6.29: fig06_29.cpp // Demonstrating the recursive function factorial. #include #include using namespace std; unsigned long factorial( unsigned long ); // function prototype int main() { // calculate the factorials of 0 through 10 for ( int counter = 0; counter <= 10; ++counter ) cout << setw( 2 ) << counter << "! = " << factorial( counter ) << endl; } // end main // recursive definition of function factorial unsigned long factorial( unsigned long number ) { if ( number <= 1 ) // test for base case return 1; // base cases: 0! = 1 and 1! = 1 else // recursion step return number * factorial( number - 1 ); } // end function factorial = = = = = = = = = = =

1 1 2 6 24 120 720 5040 40320 362880 3628800

Fig. 6.29 | Demonstrating the recursive function factorial. Function factorial has been declared to receive a parameter of type unsigned long and return a result of type unsigned long. This is shorthand notation for unsigned long int. The C++ standard requires that a variable of type unsigned long int be at least as big as an int. Typically, an unsigned long int is stored in at least four bytes (32 bits); such a

242

Chapter 6 Functions and an Introduction to Recursion

variable can hold a value in the range 0 to at least 4294967295. (The data type long int is also stored in at least four bytes and can hold a value at least in the range –2147483648 to 2147483647.) As can be seen in Fig. 6.29, factorial values become large quickly. We chose the data type unsigned long so that the program can calculate factorials greater than 7! on computers with small (such as two-byte) integers. Unfortunately, the function factorial produces large values so quickly that even unsigned long does not help us compute many factorial values before even the size of an unsigned long variable is exceeded. Variables of type double could be used to calculate factorials of larger numbers. This points to a weakness in many programming languages, namely, that the languages are not easily extended to handle the unique requirements of various applications. As we’ll see when we discuss object-oriented programming in more depth, C++ is an extensible language that allows us to create classes that can represent arbitrarily large integers if we wish. Such classes already are available in popular class libraries,1 and we work on similar classes of our own in Exercise 9.14 and Exercise 11.9.

Common Programming Error 6.13

Either omitting the base case, or writing the recursion step incorrectly so that it does not converge on the base case, causes “infinite” recursion, eventually exhausting memory. This is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution.

6.20 Example Using Recursion: Fibonacci Series The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, …

begins with 0 and 1 and has the property that each subsequent Fibonacci number is the sum of the previous two Fibonacci numbers. The series occurs in nature and, in particular, describes a form of spiral. The ratio of successive Fibonacci numbers converges on a constant value of 1.618…. This number, too, frequently occurs in nature and has been called the golden ratio or the golden mean. Humans tend to find the golden mean aesthetically pleasing. Architects often design windows, rooms and buildings whose length and width are in the ratio of the golden mean. Postcards are often designed with a golden mean length/width ratio. The Fibonacci series can be defined recursively as follows: fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n) = fibonacci(n – 1) + fibonacci(n – 2)

The program of Fig. 6.30 calculates the nth Fibonacci number recursively by using function fibonacci. Fibonacci numbers tend to become large quickly, although slower than factorials do. Therefore, we chose the data type unsigned long for the parameter type and the return type in function fibonacci. Figure 6.30 shows the execution of the program, which displays the Fibonacci values for several numbers.

1.

Such classes can be found at www.trumphurst.com/cpplibs/datapage.phtml?category='intro'.

6.20 Example Using Recursion: Fibonacci Series 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

243

// Fig. 6.30: fig06_30.cpp // Testing the recursive fibonacci function. #include using namespace std; unsigned long fibonacci( unsigned long ); // function prototype int main() { // calculate the fibonacci values of 0 through 10 for ( int counter = 0; counter <= 10; ++counter ) cout << "fibonacci( " << counter << " ) = " << fibonacci( counter ) << endl; // display higher fibonacci values cout << "fibonacci( 20 ) = " << fibonacci( 20 ) << endl; cout << "fibonacci( 30 ) = " << fibonacci( 30 ) << endl; cout << "fibonacci( 35 ) = " << fibonacci( 35 ) << endl; } // end main // recursive function fibonacci unsigned long fibonacci( unsigned long number ) { if ( ( number == 0 ) || ( number == 1 ) ) // base cases return number; else // recursion step return fibonacci( number - 1 ) + fibonacci( number - 2 ); } // end function fibonacci

fibonacci( fibonacci( fibonacci( fibonacci( fibonacci( fibonacci( fibonacci( fibonacci( fibonacci( fibonacci( fibonacci( fibonacci( fibonacci( fibonacci(

0 ) = 0 1 ) = 1 2 ) = 1 3 ) = 2 4 ) = 3 5 ) = 5 6 ) = 8 7 ) = 13 8 ) = 21 9 ) = 34 10 ) = 55 20 ) = 6765 30 ) = 832040 35 ) = 9227465

Fig. 6.30 | Demonstrating function fibonacci. The application begins with a for statement that calculates and displays the Fibonacci values for the integers 0–10 and is followed by three calls to calculate the Fibonacci values of the integers 20, 30 and 35 (lines 16–18). The calls to fibonacci (lines 13 and 16–18) from main are not recursive calls, but the calls from line 27 of fibonacci are recursive. Each time the program invokes fibonacci (lines 22–28), the function immediately tests the base case to determine whether number is equal to 0 or 1 (line 24). If this is true, line 25 returns number. Interestingly, if number is greater than 1, the recursion step (line 27) generates two recursive calls, each for a slightly smaller problem than the original call to fibonacci.

244

Chapter 6 Functions and an Introduction to Recursion

Figure 6.31 shows how function fibonacci would evaluate fibonacci(3). This figure raises some interesting issues about the order in which C++ compilers evaluate the operands of operators. This is a separate issue from the order in which operators are applied to their operands, namely, the order dictated by the rules of operator precedence and associativity. Figure 6.31 shows that evaluating fibonacci(3) causes two recursive calls, namely, fibonacci(2) and fibonacci(1). In what order are these calls made? fibonacci( 3 )

return

return

fibonacci( 1 )

return 1

fibonacci( 2 )

+

fibonacci( 0 )

+

fibonacci( 1 )

return 1

return 0

Fig. 6.31 | Set of recursive calls to function fibonacci. Order of Evaluation of Operands Most programmers simply assume that the operands are evaluated left to right. C++ does not specify the order in which the operands of most operators (including +) are to be evaluated. Therefore, you must make no assumption about the order in which these calls execute. The calls could in fact execute fibonacci(2) first, then fibonacci(1), or they could execute in the reverse order: fibonacci(1), then fibonacci(2). In this program and in most others, it turns out that the final result would be the same. However, in some programs the evaluation of an operand can have side effects (changes to data values) that could affect the final result of the expression. C++ specifies the order of evaluation of the operands of only four operators—&&, ||, comma (,) and ?:. The first three are binary operators whose two operands are guaranteed to be evaluated left to right. The last operator is C++’s only ternary operator—its leftmost operand is always evaluated first; if it evaluates to nonzero (true), the middle operand evaluates next and the last operand is ignored; if the leftmost operand evaluates to zero (false), the third operand evaluates next and the middle operand is ignored.

Portability Tip 6.2

Programs that depend on the order of evaluation of the operands of operators other than &&, ||, ?: and the comma (,) operator can function differently with different compilers.

Common Programming Error 6.14

Writing programs that depend on the order of evaluation of the operands of operators other than &&, ||, ?: and the comma (,) operator can lead to logic errors.

6.21 Recursion vs. Iteration

245

A word of caution is in order about recursive programs like the one we use here to generate Fibonacci numbers. Each level of recursion in function fibonacci has a doubling effect on the number of function calls; i.e., the number of recursive calls that are required to calculate the nth Fibonacci number is on the order of 2n. This rapidly gets out of hand. Calculating only the 20th Fibonacci number would require on the order of 220 or about a million calls, calculating the 30th Fibonacci number would require on the order of 230 or about a billion calls, and so on. Computer scientists refer to this as exponential complexity. Problems of this nature humble even the world’s most powerful computers! Complexity issues in general, and exponential complexity in particular, are discussed in detail in the upper-level computer science course generally called “Algorithms.”

Performance Tip 6.7

Avoid Fibonacci-style recursive programs that result in an exponential “explosion” of calls.

6.21 Recursion vs. Iteration In the two previous sections, we studied two functions that easily can be implemented recursively or iteratively. This section compares the two approaches and discusses why you might choose one approach over the other in a particular situation. Both iteration and recursion are based on a control statement: Iteration uses a repetition structure; recursion uses a selection structure. Both iteration and recursion involve repetition: Iteration explicitly uses a repetition structure; recursion achieves repetition through repeated function calls. Iteration and recursion both involve a termination test: Iteration terminates when the loop-continuation condition fails; recursion terminates when a base case is recognized. Iteration with counter-controlled repetition and recursion both gradually approach termination: Iteration modifies a counter until the counter assumes a value that makes the loop-continuation condition fail; recursion produces simpler versions of the original problem until the base case is reached. Both iteration and recursion can occur infinitely: An infinite loop occurs with iteration if the loop-continuation test never becomes false; infinite recursion occurs if the recursion step does not reduce the problem during each recursive call in a manner that converges on the base case. To illustrate the differences between iteration and recursion, let’s examine an iterative solution to the factorial problem (Fig. 6.32). A repetition statement is used (lines 23–24 of Fig. 6.32) rather than the selection statement of the recursive solution (lines 20–23 of Fig. 6.29). Both solutions use a termination test. In the recursive solution, line 20 tests for the base case. In the iterative solution, line 23 tests the loop-continuation condition—if the test fails, the loop terminates. Finally, instead of producing simpler versions of the original problem, the iterative solution uses a counter that is modified until the loop-continuation condition becomes false. 1 2 3 4 5

// Fig. 6.32: fig06_32.cpp // Testing the iterative factorial function. #include #include using namespace std;

Fig. 6.32 | Iterative factorial solution. (Part 1 of 2.)

246 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

Chapter 6 Functions and an Introduction to Recursion

unsigned long factorial( unsigned long ); // function prototype int main() { // calculate the factorials of 0 through 10 for ( int counter = 0; counter <= 10; ++counter ) cout << setw( 2 ) << counter << "! = " << factorial( counter ) << endl; } // end main // iterative function factorial unsigned long factorial( unsigned long number ) { unsigned long result = 1; // iterative factorial calculation for ( unsigned long i = number; i >= 1; --i ) result *= i; return result; } // end function factorial

0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800

Fig. 6.32 | Iterative factorial solution. (Part 2 of 2.) Recursion has negatives. It repeatedly invokes the mechanism, and consequently the overhead, of function calls. This can be expensive in both processor time and memory space. Each recursive call causes another copy of the function (actually only the function’s variables) to be created; this can consume considerable memory. Iteration normally occurs within a function, so the overhead of repeated function calls and extra memory assignment is omitted. So why choose recursion?

Software Engineering Observation 6.14

Any problem that can be solved recursively can also be solved iteratively (nonrecursively). A recursive approach is normally chosen when the recursive approach more naturally mirrors the problem and results in a program that’s easier to understand and debug. Another reason to choose a recursive solution is that an iterative solution is not apparent.

Performance Tip 6.8

Avoid using recursion in performance situations. Recursive calls take time and consume additional memory.

6.21 Recursion vs. Iteration

Common Programming Error 6.15

247

Accidentally having a nonrecursive function call itself, either directly or indirectly (through another function), is a logic error.

Figure 6.33 summarizes the recursion examples and exercises in the text. Location in text

Recursion examples and exercises

Chapter 6 Section 6.19, Fig. 6.29 Section 6.20, Fig. 6.30 Exercise 6.36 Exercise 6.38 Exercise 6.40 Exercise 6.41 Exercise 6.45, Exercise 6.46

Factorial function Fibonacci function Raising an integer to an integer power Towers of Hanoi Visualizing recursion Greatest common divisor Mystery “What does this program do?” exercise

Chapter 7 Exercise 7.18 Exercise 7.21 Exercise 7.31 Exercise 7.32 Exercise 7.33 Exercise 7.34 Exercise 7.35 Exercise 7.36 Exercise 7.37

Mystery “What does this program do?” exercise Mystery “What does this program do?” exercise Selection sort Determine whether a string is a palindrome Linear search Eight Queens Print an array Print a string backward Minimum value in an array

Chapter 8 Exercise 8.15 Exercise 8.16 Exercise 8.17

Quicksort Maze traversal Generating mazes randomly

Chapter 19 Section 19.3.3, Figs. 19.5–19.7 Exercise 19.8 Exercise 19.9 Exercise 19.10

Mergesort Linear search Binary search Quicksort

Chapter 20 Section 20.7, Figs. 20.20–20.22 Section 20.7, Figs. 20.20–20.22 Section 20.7, Figs. 20.20–20.22

Binary tree insert Preorder traversal of a binary tree Inorder traversal of a binary tree

Fig. 6.33 | Summary of recursion examples and exercises in the text. (Part 1 of 2.)

248

Chapter 6 Functions and an Introduction to Recursion

Location in text

Recursion examples and exercises

Section 20.7, Figs. 20.20–20.22 Exercise 20.20 Exercise 20.21 Exercise 20.22 Exercise 20.23 Exercise 20.24 Exercise 20.25

Postorder traversal of a binary tree Print a linked list backward Search a linked list Binary tree delete Binary tree search Level order traversal of a binary tree Printing tree

Fig. 6.33 | Summary of recursion examples and exercises in the text. (Part 2 of 2.)

6.22 Wrap-Up In this chapter, you learned more about function declarations, including function prototypes, function signatures, function headers and function bodies. We overviewed the math library functions. You learned about argument coercion, or the forcing of arguments to the appropriate types specified by the parameter declarations of a function. We demonstrated how to use functions rand and srand to generate sets of random numbers that can be used for simulations. We showed how to define sets of constants with enums. You also learned about the scope of variables and storage classes. Two different ways to pass arguments to functions were covered—pass-by-value and pass-by-reference. For pass-by-reference, references are used as an alias to a variable. We showed how to implement inline functions and functions that receive default arguments. You learned that multiple functions in one class can be overloaded by providing functions with the same name and different signatures. Such functions can be used to perform the same or similar tasks, using different types or different numbers of parameters. We demonstrated a simpler way of overloading functions using function templates, where a function is defined once but can be used for several different types. You then studied recursion, where a function calls itself to solve a problem. In Chapter 7, you’ll learn how to maintain lists and tables of data in arrays and objectoriented vectors. You’ll see a more elegant array-based implementation of the dice-rolling application and two enhanced versions of the GradeBook case study we presented in Chapters 3–6 that will use arrays to store the actual grades entered.

Summary Section 6.1 Introduction

• Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces, or components. This technique is called divide and conquer (p. 195).

Section 6.2 Program Components in C++

• C++ programs are typically written by combining new functions and classes you write with “prepackaged” functions and classes available in the C++ Standard Library. • Functions allow you to modularize a program by separating its tasks into self-contained units.

Summary

249

• The statements in the function bodies are written only once, are reused from perhaps several locations in a program and are hidden from other functions.

Section 6.3 Math Library Functions

• Sometimes functions are not members of a class. These are called global functions (p. 197). • The prototypes for global functions are placed in headers, so that they can be reused in any program that includes the header and that can link to the function’s object code.

Section 6.4 Function Definitions with Multiple Parameters

• The compiler refers to the function prototype to check that calls to a function contain the correct number and types of arguments, that the types of the arguments are in the correct order and that the value returned by the function can be used correctly in the expression that called the function. • If a function does not return a result, control returns when the program reaches the functionending right brace, or by execution of the statement return;

If a function does return a result, the statement return

expression;

evaluates expression and returns the value of expression to the caller.

Section 6.5 Function Prototypes and Argument Coercion

• The portion of a function prototype that includes the name of the function and the types of its arguments is called the function signature (p. 203) or simply the signature. • An important feature of function prototypes is argument coercion (p. 203)—i.e., forcing arguments to the appropriate types specified by the parameter declarations. • Arguments can be promoted by the compiler to the parameter types as specified by C++’s promotion rules (p. 204). The promotion rules indicate the implicit conversions that the compiler can perform between fundamental types.

Section 6.6 C++ Standard Library Headers

• The C++ Standard Library is divided into many portions, each with its own header. The headers also contain definitions of various class types, functions and constants. • A header “instructs” the compiler on how to interface with library components.

Section 6.7 Case Study: Random Number Generation

• Calling rand (p. 207) repeatedly produces a sequence of pseudorandom numbers (p. 210). The sequence repeats itself each time the program executes. • To randomize the numbers produced by rand pass an unsigned integer argument (typically from function time; p. 211) to function srand (p. 210), which seeds the rand function. • Random numbers in a range can be generated with number

=

shiftingValue

+ rand() %

scalingFactor;

where shiftingValue (p. 207) is equal to the first number in the desired range of consecutive integers and scalingFactor (p. 207) is equal to the width of the desired range of consecutive integers.

Section 6.8 Case Study: Game of Chance; Introducing enum

• An enumeration, introduced by the keyword enum and followed by a type name (p. 214), is a set of named integer constants (p. 214) that start at 0, unless specified otherwise, and increment by 1.

250

Chapter 6 Functions and an Introduction to Recursion

Section 6.9 Storage Classes

• An identifier’s storage class (p. 215) determines the period during which it exists in memory. • An identifier’s scope is where the identifier can be referenced in a program. • An identifier’s linkage (p. 215) determines whether it’s known only in the source file where it’s declared or across multiple files that are compiled, then linked together. • Keywords auto (p. 215) and register (p. 215) declare variables of the automatic storage class (p. 216). Such variables are created when program execution enters the block in which they’re defined, exist while the block is active and are destroyed when the program exits the block. • Only local variables of a function can be of automatic storage class. • The storage-class specifier auto (p. 215) explicitly declares variables of automatic storage class. Local variables are of automatic storage class by default, so keyword auto is rarely used. • Keywords extern (p. 215) and static declare identifiers for variables of the static storage class (p. 215) and for functions. Static-storage-class variables exist from the point at which the program begins execution and last for the duration of the program. • A static-storage-class variable’s storage is allocated when the program begins execution. Such a variable is initialized once when its declaration is encountered. For functions, the name of the function exists when the program begins execution as for all other functions. • External identifiers (such as global variables) and local variables declared with the storage classspecifier static have static storage class (p. 215). • Global variables (p. 217) declarations are placed outside any class or function definition. Global variables retain their values throughout the program’s execution. Global variables and functions can be referenced by any function that follows their declarations or definitions.

Section 6.10 Scope Rules

• Unlike automatic variables, static local variables retain their values when the function in which they’re declared returns to its caller. • An identifier declared outside any function or class has global namespace scope (p. 218). • Labels are the only identifiers with function scope (p. 218). Labels can be used anywhere in the function in which they appear, but cannot be referenced outside the function body. • Identifiers declared inside a block have local scope (p. 218), which begins at the identifier’s declaration and ends at the terminating right brace (}) of the block in which the identifier is declared. • Identifiers in the parameter list of a function prototype have function-prototype scope (p. 218).

Section 6.11 Function Call Stack and Activation Records

• Stacks (p. 221) are known as last-in, first-out (LIFO) data structures—the last item pushed (inserted; p. 221) on the stack is the first item popped (removed; p. 221) from the stack. • The function call stack (p. 221) supports the function call/return mechanism and the creation, maintenance and destruction of each called function’s automatic variables. • Each time a function calls another function, a stack frame or an activation record (p. 221) is pushed onto the stack containing the return address that the called function needs to return to the calling function, and the function call’s automatic variables and parameters. • The stack frame (p. 221) exists as long as the called function is active. When the called function returns, its stack frame is popped from the stack, and its local automatic variables no longer exist.

Section 6.12 Functions with Empty Parameter Lists

• In C++, an empty parameter list is specified by writing either void or nothing in parentheses.

Summary

251

Section 6.13 Inline Functions

• C++ provides inline functions (p. 225) to help reduce function call overhead—especially for small functions. Placing the qualifier inline before a function’s return type in the function definition “advises” the compiler to generate a copy of the function’s code in place to avoid a function call.

Section 6.14 References and Reference Parameters

• When an argument is passed by value (p. 227), a copy of the argument’s value is made and passed to the called function. Changes to the copy do not affect the original variable’s value in the caller. • With pass-by-reference (p. 227), the caller gives the called function the ability to access the caller’s data directly and to modify it if the called function chooses to do so. • A reference parameter (p. 227) is an alias for its corresponding argument in a function call. • To indicate that a function parameter is passed by reference, follow the parameter’s type in the function prototype and header by an ampersand (&). • All operations performed on a reference are actually performed on the original variable.

Section 6.15 Default Arguments

• When a function is called repeatedly with the same argument for a particular parameter, you can specify that such a parameter has a default argument (p. 231). • When a program omits an argument for a parameter with a default argument, the compiler inserts the default value of that argument to be passed to the function call. • Default arguments must be the rightmost (trailing) arguments in a function’s parameter list. • Default arguments are specified in the function prototype.

Section 6.16 Unary Scope Resolution Operator

• C++ provides the unary scope resolution operator (p. 232), ::, to access a global variable when a local variable of the same name is in scope.

Section 6.17 Function Overloading

• C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters. This capability is called function overloading (p. 234). • When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. • Overloaded functions are distinguished by their signatures. • The compiler encodes each function identifier with the number and types of its parameters to enable type-safe linkage (p. 235). Type-safe linkage ensures that the proper overloaded function is called and that the types of the arguments conform to the types of the parameters.

Section 6.18 Function Templates

• Overloaded functions typically perform similar operations that involve different program logic on different data types. If the program logic and operations are identical for each data type, overloading may be performed more compactly and conveniently using function templates (p. 237). • Given the argument types provided in calls to a function template, C++ automatically generates separate function template specializations (p. 237) to handle each type of call appropriately. • All function template definitions begin with the template keyword (p. 237) followed by a template parameter list (p. 237) to the function template enclosed in angle brackets (< and >). • The formal type parameters (p. 237) are preceded by keyword typename and are placeholders for fundamental types or user-defined types. These placeholders are used to specify the types of the

252

Chapter 6 Functions and an Introduction to Recursion

function’s parameters, to specify the function’s return type and to declare variables within the body of the function definition.

Section 6.19 Recursion

• A recursive function (p. 239) calls itself, either directly or indirectly. • A recursive function knows how to solve only the simplest case(s), or so-called base case(s). If the function is called with a base case (p. 239), the function simply returns a result. • If the function is called with a more complex problem, the function typically divides the problem into two conceptual pieces—a piece that the function knows how to do and a piece that it does not know how to do. To make recursion feasible, the latter piece must resemble the original problem, but be a slightly simpler or slightly smaller version of it. • For recursion to terminate, the sequence of recursive calls (p. 239) must converge on the base case.

Section 6.20 Example Using Recursion: Fibonacci Series

• The ratio of successive Fibonacci numbers converges on a constant value of 1.618…. This number frequently occurs in nature and has been called the golden ratio or the golden mean (p. 242).

Section 6.21 Recursion vs. Iteration

• Iteration (p. 240) and recursion are similar: both are based on a control statement, involve repetition, involve a termination test, gradually approach termination and can occur infinitely. • Recursion repeatedly invokes the mechanism, and overhead, of function calls. This can be expensive in both processor time and memory space. Each recursive call (p. 239) causes another copy of the function’s variables to be created; this can consume considerable memory.

Self-Review Exercises 6.1

Answer each of the following: and . a) Program components in C++ are called b) A function is invoked with a(n) . c) A variable known only within the function in which it’s defined is called a(n) . statement in a called function passes the value of an expression back to d) The the calling function. e) The keyword is used in a function header to indicate that a function does not return a value or to indicate that a function contains no parameters. f) An identifier’s is the portion of the program in which the identifier can be used. g) The three ways to return control from a called function to a caller are , and . h) A(n) allows the compiler to check the number, types and order of the arguments passed to a function. i) Function is used to produce random numbers. j) Function is used to set the random number seed to randomize the number sequence generated by function rand. k) The storage-class specifiers are mutable, , , and . l) Variables declared in a block or in the parameter list of a function are assumed to be of storage class unless specified otherwise. m) Storage-class specifier is a recommendation to the compiler to store a variable in one of the computer’s registers. n) A variable declared outside any block or function is a(n) variable. o) For a local variable in a function to retain its value between calls to the function, it must be declared with the storage-class specifier.

Self-Review Exercises

253

, , , , p) The six possible scopes of an identifier are and . q) A function that calls itself either directly or indirectly (i.e., through another function) function. is a(n) r) A recursive function typically has two components—one that provides a means for the recursion to terminate by testing for a(n) case and one that expresses the problem as a recursive call for a slightly simpler problem than the original call. s) It’s possible to have various functions with the same name that operate on different types or numbers of arguments. This is called function . t) The enables access to a global variable with the same name as a variable in the current scope. qualifier is used to declare read-only variables. u) The enables a single function to be defined to perform a task on many v) A function different data types. 6.2 For the program in Fig. 6.34, state the scope (either function scope, global namespace scope, local scope or function-prototype scope) of each of the following elements: a) The variable x in main. b) The variable y in cube. c) The function cube. d) The function main. e) The function prototype for cube. f) The identifier y in the function prototype for cube. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

// Exercise 6.2: Ex06_02.cpp #include using namespace std; int cube( int y ); // function prototype int main() { int x; for ( x = 1; x <= 10; x++ ) // loop 10 times cout << cube( x ) << endl; // calculate cube of x and output results } // end main // definition of function cube int cube( int y ) { return y * y * y; } // end function cube

Fig. 6.34 | Program for Exercise 6.2. 6.3 Write a program that tests whether the examples of the math library function calls shown in Fig. 6.2 actually produce the indicated results. 6.4

Give the function header for each of the following functions: a) Function hypotenuse that takes two double-precision, floating-point arguments, side1 and side2, and returns a double-precision, floating-point result. b) Function smallest that takes three integers, x, y and z, and returns an integer. c) Function instructions that does not receive any arguments and does not return a value. [Note: Such functions are commonly used to display instructions to a user.]

254

Chapter 6 Functions and an Introduction to Recursion d) Function intToDouble that takes an integer argument, precision, floating-point result.

number,

and returns a double-

6.5

Give the function prototype (without parameter names) for each of the following: a) The function described in Exercise 6.4(a). b) The function described in Exercise 6.4(b). c) The function described in Exercise 6.4(c). d) The function described in Exercise 6.4(d).

6.6

Write a declaration for each of the following: a) Integer count that should be maintained in a register. Initialize count to 0. b) Double-precision, floating-point variable lastVal that is to retain its value between calls to the function in which it’s defined.

6.7 Find the error(s) in each of the following program segments, and explain how the error(s) can be corrected (see also Exercise 6.48): a) int g() { cout << "Inside function g" << endl; int h() { cout << "Inside function h" << endl; }

b)

} int sum( int x, int y ) { int result; result = x + y;

c)

} int sum( int n ) { if ( n == 0 ) return 0; else n + sum( n - 1 );

d)

} void f( double a ); { float a; cout << a << endl;

e)

} void product() { int a; int b; int c; int result; cout << "Enter three integers: "; cin >> a >> b >> c; result = a * b * c; cout << "Result is " << result; return result; }

Answers to Self-Review Exercises

255

6.8

Why would a function prototype contain a parameter type declaration such as double &?

6.9

(True/False) All arguments to function calls in C++ are passed by value.

6.10 Write a complete program that prompts the user for the radius of a sphere, and calculates and prints the volume of that sphere. Use an inline function sphereVolume that returns the result of the following expression: (4.0 / 3.0 * 3.14159 * pow(radius, 3)).

Answers to Self-Review Exercises 6.1 a) functions, classes. b) function call. c) local variable. d) return. e) void. f) scope. g) return;, return expression; or encounter the closing right brace of a function. h) function prototype. i) rand. j) srand. k) auto, register, extern, static. l) auto. m) register. n) global. o) static. p) function scope, global namespace scope, local scope, function-prototype scope, class scope, namespace scope. q) recursive. r) base. s) overloading. t) unary scope resolution operator (::). u) const. v) template. 6.2 a) local scope. b) local scope. c) global namespace scope. d) global namespace scope. e) global namespace scope. f) function-prototype scope. 6.3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

See the following program: // Exercise 6.3: Ex06_03.cpp // Testing the math library functions. #include #include #include using namespace std; int main() { cout << fixed << setprecision( 1 ); cout << "sqrt(" << 900.0 << ") = " << sqrt( 900.0 ) << "\nsqrt(" << 9.0 << ") = " << sqrt( 9.0 ); cout << "\nexp(" << 1.0 << ") = " << setprecision( 6 ) << exp( 1.0 ) << "\nexp(" << setprecision( 1 ) << 2.0 << ") = " << setprecision( 6 ) << exp( 2.0 ); cout << "\nlog(" << 2.718282 << ") = " << setprecision( 1 ) << log( 2.718282 ) << "\nlog(" << setprecision( 6 ) << 7.389056 << ") = " << setprecision( 1 ) << log( 7.389056 ); cout << "\nlog10(" << 1.0 << ") = " << log10( 1.0 ) << "\nlog10(" << 10.0 << ") = " << log10( 10.0 ) << "\nlog10(" << 100.0 << ") = " << log10( 100.0 ) ; cout << "\nfabs(" << 5.1 << ") = " << fabs( 5.1 ) << "\nfabs(" << 0.0 << ") = " << fabs( 0.0 ) << "\nfabs(" << -8.76 << ") = " << fabs( -8.76 ); cout << "\nceil(" << 9.2 << ") = " << ceil( 9.2 ) << "\nceil(" << -9.8 << ") = " << ceil( -9.8 ); cout << "\nfloor(" << 9.2 << ") = " << floor( 9.2 ) << "\nfloor(" << -9.8 << ") = " << floor( -9.8 ); cout << "\npow(" << 2.0 << ", " << 7.0 << ") = " << pow( 2.0, 7.0 ) << "\npow(" << 9.0 << ", " << 0.5 << ") = " << pow( 9.0, 0.5 ); cout << setprecision(3) << "\nfmod(" << 2.6 << ", " << 1.2 << ") = " << fmod( 2.6, 1.2 ) << setprecision( 1 ); cout << "\nsin(" << 0.0 << ") = " << sin( 0.0 );

256

38 39 40

Chapter 6 Functions and an Introduction to Recursion

cout << "\ncos(" << 0.0 << ") = " << cos( 0.0 ); cout << "\ntan(" << 0.0 << ") = " << tan( 0.0 ) << endl; } // end main

sqrt(900.0) = 30.0 sqrt(9.0) = 3.0 exp(1.0) = 2.718282 exp(2.0) = 7.389056 log(2.718282) = 1.0 log(7.389056) = 2.0 log10(1.0) = 0.0 log10(10.0) = 1.0 log10(100.0) = 2.0 fabs(5.1) = 5.1 fabs(0.0) = 0.0 fabs(-8.8) = 8.8 ceil(9.2) = 10.0 ceil(-9.8) = -9.0 floor(9.2) = 9.0 floor(-9.8) = -10.0 pow(2.0, 7.0) = 128.0 pow(9.0, 0.5) = 3.0 fmod(2.600, 1.200) = 0.200 sin(0.0) = 0.0 cos(0.0) = 1.0 tan(0.0) = 0.0

6.4

a) b)

double hypotenuse( double side1, double side2 )

a) b)

double hypotenuse( double, double );

a) b)

register int count = 0;

int smallest( int x, int y, int z ) c) void instructions() d) double intToDouble( int number )

6.5

int smallest( int, int, int ); c) void instructions(); d) double intToDouble( int );

6.6 6.7

static double lastVal;

a) Error: Function h is defined in function g. Correction: Move the definition of h out of the definition of g. b) Error: The function is supposed to return an integer, but does not. Correction: Delete variable result and place the following statement in the function: return x + y;

c) Error: The result of n + sum( n - 1 ) is not returned; sum returns an improper result. Correction: Rewrite the statement in the else clause as return n + sum( n - 1 );

d) Errors: Semicolon after the right parenthesis that encloses the parameter list, and redefining the parameter a in the function definition. Corrections: Delete the semicolon after the right parenthesis of the parameter list, and delete the declaration float a;. e) Error: The function returns a value when it isn’t supposed to. Correction: Eliminate the return statement or change the return type. 6.8 This creates a reference parameter of type “reference to double” that enables the function to modify the original variable in the calling function.

Exercises

257

6.9 False. C++ enables pass-by-reference using reference parameters (and pointers, as we discuss in Chapter 8). 6.10 See the following program: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

// Exercise 6.10 Solution: Ex06_10.cpp // Inline function that calculates the volume of a sphere. #include #include using namespace std; const double PI = 3.14159; // define global constant PI // calculates volume of a sphere inline double sphereVolume( const double radius ) { return 4.0 / 3.0 * PI * pow( radius, 3 ); } // end inline function sphereVolume int main() { double radiusValue; // prompt user for radius cout << "Enter the length of the radius of your sphere: "; cin >> radiusValue; // input radius // use radiusValue to calculate volume of sphere and display result cout << "Volume of sphere with radius " << radiusValue << " is " << sphereVolume( radiusValue ) << endl; } // end main

Exercises 6.11

Show the value of x after each of the following statements is performed: a) x = fabs( 7.5 ) b) x = floor( 7.5 ) c) x = fabs( 0.0 ) d) x = ceil( 0.0 ) e) x = fabs( -6.4 ) f) x = ceil( -6.4 ) g) x = ceil( -fabs( -8 + floor( -5.5 ) ) ) 6.12 (Parking Charges) A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write a program that calculates and prints the parking charges for each of three customers who parked their cars in this garage yesterday. You should enter the hours parked for each customer. Your program should print the results in a neat tabular format and should calculate and print the total of yesterday’s receipts. The program should use the function calculateCharges to determine the charge for each customer. Your outputs should appear in the following format: Car 1 2 3 TOTAL

Hours 1.5 4.0 24.0 29.5

Charge 2.00 2.50 10.00 14.50

258

Chapter 6 Functions and an Introduction to Recursion

6.13 (Rounding Numbers) An application of function floor is rounding a value to the nearest integer. The statement y = floor( x + .5 );

rounds the number x to the nearest integer and assigns the result to y. Write a program that reads several numbers and uses the preceding statement to round each of these numbers to the nearest integer. For each number processed, print both the original number and the rounded number. 6.14 (Rounding Numbers) Function floor can be used to round a number to a specific decimal place. The statement y = floor( x * 10 + .5 ) / 10;

rounds x to the tenths position (the first position to the right of the decimal point). The statement y = floor( x * 100 + .5 ) / 100;

rounds x to the hundredths position (the second position to the right of the decimal point). Write a program that defines four functions to round a number x in various ways: a) roundToInteger( number ) b) roundToTenths( number ) c) roundToHundredths( number ) d) roundToThousandths( number ) For each value read, your program should print the original value, the number rounded to the nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hundredth and the number rounded to the nearest thousandth. 6.15

(Short Answer Questions) Answer each of the following questions: a) What does it mean to choose numbers “at random?” b) Why is the rand function useful for simulating games of chance? c) Why would you randomize a program by using srand? Under what circumstances is it desirable not to randomize? d) Why is it often necessary to scale or shift the values produced by rand? e) Why is computerized simulation of real-world situations a useful technique?

6.16 (Random Numbers) Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤ n ≤ 1112 e) –1 ≤ n ≤ 1 f) –3 ≤ n ≤ 11 6.17 (Random Numbers) Write a single statement that prints a number at random from each of the following sets: a) 2, 4, 6, 8, 10. b) 3, 5, 7, 9, 11. c) 6, 10, 14, 18, 22. 6.18

(Exponentiation) Write a function integerPower(base, exponent) that returns the value of base

exponent

For example, integerPower(3, 4) = 3 * 3 * 3 * 3. Assume that exponent is a positive, nonzero integer and that base is an integer. Do not use any math library functions. 6.19 (Hypotenuse Calculations) Define a function hypotenuse that calculates the hypotenuse of a right triangle when the other two sides are given. The function should take two double arguments

Exercises

259

and return the hypotenuse as a double. Use this function in a program to determine the hypotenuse for each of the triangles shown below.

Triangle

Side 1

Side 2

1 2 3

3.0 5.0 8.0

4.0 12.0 15.0

6.20 (Multiples) Write a function multiple that determines for a pair of integers whether the second is a multiple of the first. The function should take two integer arguments and return true if the second is a multiple of the first, false otherwise. Use this function in a program that inputs a series of pairs of integers. 6.21 (Even Numbers) Write a program that inputs a series of integers and passes them one at a time to function isEven, which uses the modulus operator to determine whether an integer is even. The function should take an integer argument and return true if the integer is even and false otherwise. 6.22 (Square of Asterisks) Write a function that displays at the left margin of the screen a solid square of asterisks whose side is specified in integer parameter side. For example, if side is 4, the function displays the following: **** **** **** ****

6.23 (Square of Any Character) Modify the function created in Exercise 6.22 to form the square out of whatever character is contained in character parameter fillCharacter. Thus, if side is 5 and fillCharacter is #, then this function should print the following: ##### ##### ##### ##### #####

6.24

4

5

(Separating Digits) Write program segments that accomplish each of the following: a) Calculate the integer part of the quotient when integer a is divided by integer b. b) Calculate the integer remainder when integer a is divided by integer b. c) Use the program pieces developed in (a) and (b) to write a function that inputs an integer between 1 and 32767 and prints it as a series of digits, each pair of which is separated by two spaces. For example, the integer 4562 should print as follows: 6

2

6.25 (Calculating Number of Seconds) Write a function that takes the time as three integer arguments (hours, minutes and seconds) and returns the number of seconds since the last time the clock “struck 12.” Use this function to calculate the amount of time in seconds between two times, both of which are within one 12-hour cycle of the clock.

260 6.26

Chapter 6 Functions and an Introduction to Recursion (Celsius and Fahrenheit Temperatures) Implement the following integer functions: a) Function celsius returns the Celsius equivalent of a Fahrenheit temperature. b) Function fahrenheit returns the Fahrenheit equivalent of a Celsius temperature. c) Use these functions to write a program that prints charts showing the Fahrenheit equivalents of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of all Fahrenheit temperatures from 32 to 212 degrees. Print the outputs in a neat tabular format that minimizes the number of lines of output while remaining readable.

6.27 (Find the Minimum) Write a program that inputs three double-precision, floating-point numbers and passes them to a function that returns the smallest number. 6.28 (Perfect Numbers) An integer is said to be a perfect number if the sum of its divisors, including 1 (but not the number itself), is equal to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a function isPerfect that determines whether parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the divisors of each perfect number to confirm that the number is indeed perfect. Challenge the power of your computer by testing numbers much larger than 1000. 6.29 (Prime Numbers) An integer is said to be prime if it’s divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. a) Write a function that determines whether a number is prime. b) Use this function in a program that determines and prints all the prime numbers between 2 and 10,000. How many of these numbers do you really have to test before being sure that you’ve found all the primes? c) Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is prime, but you need only go as high as the square root of n. Why? Rewrite the program, and run it both ways. Estimate the performance improvement. 6.30 (Reverse Digits) Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367. 6.31 (Greatest Common Divisor) The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a function gcd that returns the greatest common divisor of two integers. 6.32 (Quality Points for Numeric Grades) Write a function qualityPoints that inputs a student’s average and returns 4 if a student’s average is 90–100, 3 if the average is 80–89, 2 if the average is 70–79, 1 if the average is 60–69 and 0 if the average is lower than 60. 6.33 (Coin Tossing) Write a program that simulates coin tossing. For each toss of the coin, the program should print Heads or Tails. Let the program toss the coin 100 times and count the number of times each side of the coin appears. Print the results. The program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads. [Note: If the program realistically simulates the coin tossing, then each side of the coin should appear approximately half the time.] 6.34 (Guess-the-Number Game) Write a program that plays the game of “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays the following: I have a number between 1 and 1000. Can you guess my number? Please type your first guess.

The player then types a first guess. The program responds with one of the following:

Exercises

261

1. Excellent! You guessed the number! Would you like to play again (y or n)? 2. Too low. Try again. 3. Too high. Try again.

If the player’s guess is incorrect, your program should loop until the player finally gets the number right. Your program should keep telling the player Too high or Too low to help the player “zero in” on the correct answer. 6.35 (Guess-the-Number Game Modification) Modify the program of Exercise 6.34 to count the number of guesses the player makes. If the number is 10 or fewer, print "Either you know the secret or you got lucky!" If the player guesses the number in 10 tries, then print "Ahah! You know the secret!" If the player makes more than 10 guesses, then print "You should be able to do better!" Why should it take no more than 10 guesses? Well, with each “good guess” the player should be able to eliminate half of the numbers. Now show why any number from 1 to 1000 can be guessed in 10 or fewer tries. 6.36 (Recursive Exponentiation) Write a recursive function power( base, exponent ) that, when invoked, returns base

exponent

For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step would use the relationship base

exponent =

base · base

exponent - 1

and the terminating condition occurs when exponent is equal to 1, because base1 6.37

=

base

(Fibonacci Series) The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, …

begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. (a) Write a nonrecursive function fibonacci(n) that uses type int to calculate the nth Fibonacci number. (b) Determine the largest int Fibonacci number that can be printed on your system. Modify the program of part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified program to repeat part (b). 6.38 (Towers of Hanoi) In this chapter, you studied functions that can be easily implemented both recursively and iteratively. In this exercise, we present a problem whose recursive solution demonstrates the elegance of recursion, and whose iterative solution may not be as apparent. The Towers of Hanoi is one of the most famous classic problems every budding computer scientist must grapple with. Legend has it that in a temple in the Far East, priests are attempting to move a stack of golden disks from one diamond peg to another (Fig. 6.35). The initial stack has 64 disks threaded onto one peg and arranged from bottom to top by decreasing size. The priests are attempting to move the stack from one peg to another under the constraints that exactly one disk is moved at a time and at no time may a larger disk be placed above a smaller disk. Three pegs are provided, one being used for temporarily holding disks. Supposedly, the world will end when the priests complete their task, so there is little incentive for us to facilitate their efforts. Let’s assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that prints the precise sequence of peg-to-peg disk transfers. If we were to approach this problem with conventional methods, we would rapidly find ourselves hopelessly knotted up in managing the disks. Instead, attacking this problem with recursion in mind allows the steps to be simple. Moving n disks can be viewed in terms of moving only n – 1 disks (hence, the recursion), as follows:

262

Chapter 6 Functions and an Introduction to Recursion

peg 1

peg 2

peg 3

Fig. 6.35 | Towers of Hanoi for the case with four disks. a) Move n – 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area. b) Move the last disk (the largest) from peg 1 to peg 3. c) Move the n – 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area. The process ends when the last task involves moving n = 1 disk (i.e., the base case). This task is accomplished by simply moving the disk, without the need for a temporary holding area. Write a program to solve the Towers of Hanoi problem. Use a recursive function with four parameters: a) The number of disks to be moved b) The peg on which these disks are initially threaded c) The peg to which this stack of disks is to be moved d) The peg to be used as a temporary holding area Display the precise instructions for moving the disks from the starting peg to the destination peg. To move a stack of three disks from peg 1 to peg 3, the program displays the following moves: 1 → 3 (This means move one disk from peg 1 to peg 3.) 1 →2 3 →2 1 →3 2 →1 2 →3 1 →3 6.39 (Towers of Hanoi: Iterative Version) Any program that can be implemented recursively can be implemented iteratively, although sometimes with more difficulty and less clarity. Try writing an iterative version of the Towers of Hanoi. If you succeed, compare your iterative version with the recursive version developed in Exercise 6.38. Investigate issues of performance, clarity and your ability to demonstrate the correctness of the programs. 6.40 (Visualizing Recursion) It’s interesting to watch recursion “in action.” Modify the factorial function of Fig. 6.29 to print its local variable and recursive call parameter. For each recursive call, display the outputs on a separate line and add a level of indentation. Do your utmost to make the outputs clear, interesting and meaningful. Your goal here is to design and implement an output format that helps a person understand recursion better. You may want to add such display capabilities to the many other recursion examples and exercises throughout the text. 6.41 (Recursive Greatest Common Divisor) The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd that returns the

Exercises

263

greatest common divisor of x and y, defined recursively as follows: If y is equal to 0, then gcd(x, y) is x; otherwise, gcd(x, y) is gcd(y, x % y), where % is the modulus operator. [Note: For this algorithm, x must be larger than y.] 6.42 (Recursive main) Can main be called recursively on your system? Write a program containing a function main. Include static local variable count and initialize it to 1. Postincrement and print the value of count each time main is called. Compile your program. What happens? 6.43 (Distance Between Points) Write function distance that calculates the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. 6.44 1 2 3 4

5

6 7 8 9 10 11 12 13 14 15

// Exercise 6.44: ex06_44.cpp // What is wrong with this program? #include using namespace std; int main() { int c; if ( ( c = cin.get() ) != EOF ) { main(); cout << c; } // end if } // end main

6.45 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

What’s wrong with the following program?

What does the following program do?

// Exercise 6.45: ex06_45.cpp // What does this program do? #include using namespace std; int mystery( int, int ); // function prototype int main() { int x, y; cout << "Enter two integers: "; cin >> x >> y; cout << "The result is " << mystery( x, y ) << endl; } // end main // Parameter b must be a positive integer to prevent infinite recursion int mystery( int a, int b ) { if ( b == 1 ) // base case return a; else // recursion step return a + mystery( a, b - 1 ); } // end function mystery

6.46 After you determine what the program of Exercise 6.45 does, modify the program to function properly after removing the restriction that the second argument be nonnegative.

264

Chapter 6 Functions and an Introduction to Recursion

6.47 (Math Library Functions) Write a program that tests as many of the math library functions in Fig. 6.2 as you can. Exercise each of these functions by having your program print out tables of return values for a diversity of argument values. 6.48 (Find the Error) Find the error in each of the following program segments and explain how to correct it: a) float cube( float ); // function prototype cube( float number ) // function definition { return number * number * number;

b) c) d)

} register auto int x = 7; int randomNumber = srand(); float y = 123.45678; int x; x = y;

e)

cout << static_cast< float >( x ) << endl; double square( double number ) { double number; return number * number;

f)

} int sum( int n ) { if ( n == 0 ) return 0; else return n + sum( n ); }

6.49 (Craps Game Modification) Modify the craps program of Fig. 6.11 to allow wagering. Package as a function the portion of the program that runs one game of craps. Initialize variable bankBalance to 1000 dollars. Prompt the player to enter a wager. Use a while loop to check that wager is less than or equal to bankBalance and, if not, prompt the user to reenter wager until a valid wager is entered. After a correct wager is entered, run one game of craps. If the player wins, increase bankBalance by wager and print the new bankBalance. If the player loses, decrease bankBalance by wager, print the new bankBalance, check on whether bankBalance has become zero and, if so, print the message "Sorry. You busted!" As the game progresses, print various messages to create some “chatter” such as "Oh, you're going for broke, huh?", "Aw cmon, take a chance!" or "You're up big. Now's the time to cash in your chips!". 6.50 inline

(Circle Area) Write a C++ program that prompts the user for the radius of a circle, then calls function circleArea to calculate the area of that circle.

6.51 (Pass-by-Value vs. Pass-by-Reference) Write a complete C++ program with the two alternate functions specified below, each of which simply triples the variable count defined in main. Then compare and contrast the two approaches. These two functions are a) function tripleByValue that passes a copy of count by value, triples the copy and returns the new value and a) function tripleByReference that passes count by reference via a reference parameter and triples the original value of count through its alias (i.e., the reference parameter).

Making a Difference 6.52

265

What’s the purpose of the unary scope resolution operator?

6.53 (Function Template minimum) Write a program that uses a function template called minimum to determine the smaller of two arguments. Test the program using integer, character and floatingpoint number arguments. 6.54 (Function Template maximum) Write a program that uses a function template called maximum to determine the larger of two arguments. Test the program using integer, character and floatingpoint number arguments. 6.55 (Find the Error) Determine whether the following program segments contain errors. For each error, explain how it can be corrected. [Note: For a particular program segment, it’s possible that no errors are present in the segment.] a) template < class A > int sum( int num1, int num2, int num3 ) { return num1 + num2 + num3;

b)

} void printResults( int x, int y ) { cout << "The sum is " << x + y << '\n'; return x + y;

c)

} template < A > A product( A num1, A num2, A num3 ) { return num1 * num2 * num3;

d)

} double cube( int ); int cube( int );

Making a Difference As computer costs decline, it becomes feasible for every student, regardless of economic circumstance, to have a computer and use it in school. This creates exciting possibilities for improving the educational experience of all students worldwide as suggested by the next five exercises. [Note: Check out initiatives such as the One Laptop Per Child Project (www.laptop.org). Also, research “green” laptops—and note the key “going green” characteristics of these devices? Look into the Electronic Product Environmental Assessment Tool (www.epeat.net) which can help you assess the “greenness” of desktops, notebooks and monitors to help you decide which products to purchase.] 6.56 (Computer-Assisted Instruction) The use of computers in education is referred to as computer-assisted instruction (CAI). Write a program that will help an elementary school student learn multiplication. Use the rand function to produce two positive one-digit integers. The program should then prompt the user with a question, such as How much is 6 times 7?

The student then inputs the answer. Next, the program checks the student’s answer. If it’s correct, display the message "Very good!" and ask another multiplication question. If the answer is wrong, display the message "No. Please try again." and let the student try the same question repeatedly until the student finally gets it right. A separate function should be used to generate each new question. This function should be called once when the application begins execution and each time the user answers the question correctly.

266

Chapter 6 Functions and an Introduction to Recursion

6.57 (Computer-Assisted Instruction: Reducing Student Fatigue) One problem in CAI environments is student fatigue. This can be reduced by varying the computer’s responses to hold the student’s attention. Modify the program of Exercise 6.56 so that various comments are displayed for each answer as follows: Possible responses to a correct answer: Very good! Excellent! Nice work! Keep up the good work!

Possible responses to an incorrect answer: No. Please try again. Wrong. Try once more. Don't give up! No. Keep trying.

Use random-number generation to choose a number from 1 to 4 that will be used to select one of the four appropriate responses to each correct or incorrect answer. Use a switch statement to issue the responses. 6.58 (Computer-Assisted Instruction: Monitoring Student Performance) More sophisticated computer-assisted instruction systems monitor the student’s performance over a period of time. The decision to begin a new topic is often based on the student’s success with previous topics. Modify the program of Exercise 6.57 to count the number of correct and incorrect responses typed by the student. After the student types 10 answers, your program should calculate the percentage that are correct. If the percentage is lower than 75%, display "Please ask your teacher for extra help.", then reset the program so another student can try it. If the percentage is 75% or higher, display "Congratulations, you are ready to go to the next level!", then reset the program so another student can try it. 6.59 (Computer-Assisted Instruction: Difficulty Levels) Exercises 6.56–6.58 developed a computer-assisted instruction program to help teach an elementary school student multiplication. Modify the program to allow the user to enter a difficulty level. At a difficulty level of 1, the program should use only single-digit numbers in the problems; at a difficulty level of 2, numbers as large as two digits, and so on. 6.60 (Computer-Assisted Instruction: Varying the Types of Problems) Modify the program of Exercise 6.59 to allow the user to pick a type of arithmetic problem to study. An option of 1 means addition problems only, 2 means subtraction problems only, 3 means multiplication problems only, 4 means division problems only and 5 means a random mixture of all these types.

7

Arrays and Vectors

Now go, write it before them in a table, and note it in a book. —Isaiah 30:8

Begin at the beginning, … and go on till you come to the end: then stop. —Lewis Carroll

To go beyond is as wrong as to fall short.

—Confucius

Objectives In this chapter you’ll learn: ■





■ ■





To use the array data structure to represent a set of related data items. To use arrays to store, sort and search lists and tables of values. To declare arrays, initialize arrays and refer to the individual elements of arrays. To pass arrays to functions. Basic searching and sorting techniques. To declare and manipulate multidimensional arrays. To use C++ Standard Library class template vector.

268

Chapter 7 Arrays and Vectors

7.1 Introduction 7.2 Arrays 7.3 Declaring Arrays 7.4 Examples Using Arrays 7.4.1 Declaring an Array and Using a Loop to Initialize the Array’s Elements 7.4.2 Initializing an Array in a Declaration with an Initializer List 7.4.3 Specifying an Array’s Size with a Constant Variable and Setting Array Elements with Calculations 7.4.4 Summing the Elements of an Array 7.4.5 Using Bar Charts to Display Array Data Graphically 7.4.6 Using the Elements of an Array as Counters 7.4.7 Using Arrays to Summarize Survey Results

7.4.8 Static Local Arrays and Automatic Local Arrays

7.5 Passing Arrays to Functions 7.6 Case Study: Class GradeBook Using an Array to Store Grades 7.7 Searching Arrays with Linear Search 7.8 Sorting Arrays with Insertion Sort 7.9 Multidimensional Arrays 7.10 Case Study: Class GradeBook Using a Two-Dimensional Array 7.11 Introduction to C++ Standard Library Class Template vector 7.12 Wrap-Up

Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Recursion Exercises | vector Exercises | Making a Difference

7.1 Introduction This chapter introduces the topic of data structures—collections of related data items. Arrays are data structures consisting of related data items of the same type. You learned about classes in Chapter 3. In Chapter 21 we discuss the notion of structures. Structures and classes can each hold related data items of possibly different types. Arrays, structures and classes are “static” entities in that they remain the same size throughout program execution. (They may, of course, be of automatic storage class and hence be created and destroyed each time the blocks in which they’re defined are entered and exited.) After discussing how arrays are declared, created and initialized, we present a series of practical examples that demonstrate several common array manipulations. We present an example of searching arrays to find particular elements. The chapter also introduces one of the most important computing applications—sorting data (i.e., putting the data in some particular order). Two sections of the chapter enhance the GradeBook class by using arrays to enable the class to maintain a set of grades in memory and analyze student grades from multiple exams in a semester. The style of arrays we use throughout most of this chapter are C-style pointer-based arrays. (We’ll study pointers in Chapter 8.) In Section 7.11, and in Chapter 22, Standard Template Library (STL), we’ll cover arrays as full-fledged objects called vectors. We’ll discover that these object-based arrays are safer and more versatile than the C-style, pointerbased arrays that we discuss in the early part of this chapter. As part of the vector example, we introduce the exception-handling mechanism and use it to allow a program to continue executing when the program attempts to access a vector element that does not exist.

7.2 Arrays

269

7.2 Arrays An array is a consecutive group of memory locations that all have the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. Figure 7.1 shows an integer array called c that contains 12 elements. You refer to any one of these elements by giving the array name followed by the particular element’s position number in square brackets ([]). The position number is more formally called a subscript or index (this number specifies the number of elements from the beginning of the array). The first element has subscript 0 (zero) and is sometimes called the zeroth element. Thus, the elements of array c are c[0] (pronounced “c sub zero”), c[1], c[2] and so on. The highest subscript in array c is 11, which is 1 less than the number of elements in the array (12). Array names follow the same conventions as other variable names. Name of the array is c Position number of the element within the array

Name of an individual array element

c[ 0 ]

-45

c[ 1 ]

6

c[ 2 ]

0

c[ 3 ]

72

c[ 4 ]

1543

c[ 5 ]

-89

c[ 6 ]

0

c[ 7 ]

62

c[ 8 ]

-3

c[ 9 ]

1

c[ 10 ]

6453

c[ 11 ]

78

Value

Fig. 7.1 | Array of 12 elements. A subscript must be an integer or integer expression (using any integral type). If a program uses an expression as a subscript, then the program evaluates the expression to determine the subscript. For example, if we assume that variable a is equal to 5 and that variable b is equal to 6, then the statement c[ a + b ] += 2;

adds 2 to array element c[11]. A subscripted array name is an lvalue—it can be used on the left side of an assignment, just as nonarray variable names can. Let’s examine array c in Fig. 7.1 more closely. The name of the entire array is c. Its 12 elements are referred to as c[0] to c[11]. The value of c[0] is -45, the value of c[1] is 6, the value of c[2] is 0, the value of c[7] is 62, and the value of c[11] is 78. To print the sum of the values contained in the first three elements of array c, we’d write cout << c[ 0 ] + c[ 1 ] + c[ 2 ] << endl;

To divide the value of c[6] by 2 and assign the result to the variable x, we would write x = c[ 6 ] / 2;

270

Chapter 7 Arrays and Vectors

Common Programming Error 7.1

Note the difference between the “seventh element of the array” and “array element 7.” Subscripts begin at 0, so the “seventh element of the array” has a subscript of 6, while “array element 7” has a subscript of 7 and is actually the eighth element of the array. This distinction is a frequent source of off-by-one errors. To avoid such errors, we refer to specific array elements explicitly by their array name and subscript number (e.g., c[6] or c[7]).

The brackets that enclose a subscript are actually an operator that has the same precedence as parentheses. Figure 7.2 shows the precedence and associativity of the operators introduced so far. The operators are shown top to bottom in decreasing order of precedence with their associativity and type. Operators

Associativity

Type

::

()

scope resolution

()

[]

++

--

++

--

+

*

/

%

+

-

<<

>>

[See parentheses caution in Fig. 2.10] left to right left to right right to left left to right left to right left to right left to right left to right left to right left to right right to left right to left left to right

<

<=

==

!=

static_cast(operand) -

>

>=

-=

*=

!

&& || ?: =

+=

/=

%=

,

function call/array access unary (postfix) unary (prefix) multiplicative additive insertion/extraction relational equality logical AND logical OR conditional assignment comma

Fig. 7.2 | Operator precedence and associativity.

7.3 Declaring Arrays Arrays occupy space in memory. To specify the type of the elements and the number of elements required by an array use a declaration of the form: type arrayName[ arraySize ];

The compiler reserves the appropriate amount of memory. (Recall that a declaration which reserves memory is more properly known as a definition.) The arraySize must be an integer constant greater than zero. For example, to tell the compiler to reserve 12 elements for integer array c, use the declaration int c[ 12 ]; // c is an array of 12 integers

7.4 Examples Using Arrays

271

Arrays can be declared to contain values of any nonreference data type. For example, an array of type string can be used to store character strings.

7.4 Examples Using Arrays This section presents many examples that demonstrate how to declare, initialize and manipulate arrays.

7.4.1 Declaring an Array and Using a Loop to Initialize the Array’s Elements The program in Fig. 7.3 declares 10-element integer array n (line 9). Lines 12–13 use a for statement to initialize the array elements to zeros. Like other automatic variables, automatic arrays are not implicitly initialized to zero although static arrays are. The first output statement (line 15) displays the column headings for the columns printed in the subsequent for statement (lines 18–19), which prints the array in tabular format. Remember that setw specifies the field width in which only the next value is to be output. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

// Fig. 7.3: fig07_03.cpp // Initializing an array's elements to zeros and printing the array. #include #include using namespace std; int main() { int n[ 10 ]; // n is an array of 10 integers // initialize elements of array n to 0 for ( int i = 0; i < 10; ++i ) n[ i ] = 0; // set element at location i to 0 cout << "Element" << setw( 13 ) << "Value" << endl; // output each array element's value for ( int j = 0; j < 10; ++j ) cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl; } // end main

Element 0 1 2 3 4 5 6 7 8 9

Value 0 0 0 0 0 0 0 0 0 0

Fig. 7.3 | Initializing an array’s elements to zeros and printing the array.

272

Chapter 7 Arrays and Vectors

7.4.2 Initializing an Array in a Declaration with an Initializer List The elements of an array also can be initialized in the array declaration by following the array name with an equals sign and a brace-delimited comma-separated list of initializers. The program in Fig. 7.4 uses an initializer list to initialize an integer array with 10 values (line 10) and prints the array in tabular format (lines 12–16). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

// Fig. 7.4: fig07_04.cpp // Initializing an array in a declaration. #include #include using namespace std; int main() { // use initializer list to initialize array n int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; cout << "Element" << setw( 13 ) << "Value" << endl; // output each array element's value for ( int i = 0; i < 10; ++i ) cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; } // end main

Element 0 1 2 3 4 5 6 7 8 9

Value 32 27 64 18 95 14 90 70 60 37

Fig. 7.4 | Initializing an array in a declaration. If there are fewer initializers than array elements, the remaining array elements are initialized to zero. For example, the elements of array n in Fig. 7.3 could have been initialized to zero with the declaration int n[ 10 ] = {}; // initialize elements of array n to 0

which initializes the elements to zero, because there are fewer initializers (none in this case) than array elements. This technique can be used only in the array’s declaration, whereas the initialization technique shown in Fig. 7.3 can be used repeatedly during program execution to “reinitialize” an array’s elements. If the array size is omitted from a declaration with an initializer list, the compiler sizes the array to the number of elements in the initializer list. For example, int n[] = { 1, 2, 3, 4, 5 };

creates a five-element array.

7.4 Examples Using Arrays

273

If the array size and an initializer list are specified in an array declaration, the number of initializers must be less than or equal to the array size. The array declaration int n[ 5 ] = { 32, 27, 64, 18, 95, 14 };

causes a compilation error, because there are six initializers and only five array elements.

7.4.3 Specifying an Array’s Size with a Constant Variable and Setting Array Elements with Calculations Figure 7.5 sets the elements of a 10-element array s to the even integers 2, 4, 6, …, 20 (lines 14–15) and prints the array in tabular format (lines 17–21). These numbers are generated (line 15) by multiplying each successive value of the loop counter by 2 and adding 2. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

// Fig. 7.5: fig07_05.cpp // Set array s to the even integers from 2 to 20. #include #include using namespace std; int main() { // constant variable can be used to specify array size const int arraySize = 10; int s[ arraySize ]; // array s has 10 elements for ( int i = 0; i < arraySize; ++i ) // set the values s[ i ] = 2 + 2 * i; cout << "Element" << setw( 13 ) << "Value" << endl; // output contents of array s in tabular format for ( int j = 0; j < arraySize; ++j ) cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl; } // end main

Element 0 1 2 3 4 5 6 7 8 9

Value 2 4 6 8 10 12 14 16 18 20

Fig. 7.5 | Generating values to be placed into elements of an array. Line 10 uses the const qualifier to declare a so-called constant variable arraySize with the value 10. Constant variables must be initialized with a constant expression when

274

Chapter 7 Arrays and Vectors

they’re declared and cannot be modified thereafter (as shown in Fig. 7.6 and Fig. 7.7). Constant variables are also called named constants or read-only variables.

Common Programming Error 7.2

Not initializing a constant variable when it’s declared is a compilation error.

Common Programming Error 7.3

Assigning a value to a constant variable in an executable statement is a compilation error.

1 2 3 4 5 6 7 8 9 10 11

// Fig. 7.6: fig07_06.cpp // Using a properly initialized constant variable. #include using namespace std; int main() { const int x = 7; // initialized constant variable cout << "The value of constant variable x is: " << x << endl; } // end main

The value of constant variable x is: 7

Fig. 7.6 | Using a properly initialized constant variable. 1 2 3 4 5 6 7 8 9

// Fig. 7.7: fig07_07.cpp // A const variable must be initialized. int main() { const int x; // Error: x must be initialized x = 7; // Error: cannot modify a const variable } // end main

Microsoft Visual C++ compiler error message: C:\cpphtp8_examples\ch07\fig07_07.cpp(6) : error C2734: 'x' : const object must be initialized if not extern C:\cpphtp8_examples\ch07\fig07_07.cpp(8) : error C3892: 'x' : you cannot assign to a variable that is const

GNU C++ compiler error message: fig07_07.cpp:6: error: uninitialized const ’x' fig07_07.cpp:8: error: assignment of read-only variable ’x'

Fig. 7.7 | A const variable must be initialized.

7.4 Examples Using Arrays

275

In Fig. 7.7, the compilation error produced by Microsoft Visual C++ refers to the int variable x as a “const object.” The ISO/IEC C++ standard defines an “object” as any “region of storage.” Like objects of classes, fundamental-type variables also occupy space in memory, so they’re often referred to as “objects.” Constant variables can be placed anywhere a constant expression is expected. In Fig. 7.5, constant variable arraySize specifies the size of array s in line 12.

Common Programming Error 7.4

Only constants can be used to declare the size of automatic and static arrays. Not using a constant for this purpose is a compilation error.

Using constant variables to specify array sizes makes programs more scalable. In Fig. 7.5, the first for statement could fill a 1000-element array by simply changing the value of arraySize in its declaration from 10 to 1000. If the constant variable arraySize had not been used, we would have to change lines 12, 14 and 20 of the program to scale the program to handle 1000 array elements. As programs get larger, this technique becomes more useful for writing clearer, easier-to-modify programs.

Software Engineering Observation 7.1

Defining the size of each array as a constant variable instead of a literal constant can make programs more scalable.

Good Programming Practice 7.1

Defining the size of an array as a constant variable instead of a literal constant makes programs clearer. This technique eliminates so-called magic numbers. For example, repeatedly mentioning the size 10 in array-processing code for a 10-element array gives the number 10 an artificial significance and can be confusing when the program includes other 10s that have nothing to do with the array size.

7.4.4 Summing the Elements of an Array Often, the elements of an array represent a series of values to be used in a calculation. For example, if the elements of an array represent exam grades, a professor may wish to total the elements of the array and use that sum to calculate the class average for the exam. The program in Fig. 7.8 sums the values contained in the 10-element integer array a. The program declares, creates and initializes the array in line 9. The for statement (lines 13–14) performs the calculations. The values being supplied as initializers for array a also could be read into the program from the user at the keyboard, or from a file on disk (see Chapter 17, File Processing). For example, the for statement for ( int j = 0; j < arraySize; ++j ) cin >> a[ j ];

reads one value at a time from the keyboard and stores the value in element a[j]. 1 2 3

// Fig. 7.8: fig07_08.cpp // Compute the sum of the elements of the array. #include

Fig. 7.8 | Computing the sum of the elements of an array. (Part 1 of 2.)

276 4 5 6 7 8 9 10 11 12 13 14 15 16 17

Chapter 7 Arrays and Vectors

using namespace std; int main() { const int arraySize = 10; // constant variable indicating size of array int a[ arraySize ] = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 }; int total = 0; // sum contents of array a for ( int i = 0; i < arraySize; ++i ) total += a[ i ]; cout << "Total of array elements: " << total << endl; } // end main

Total of array elements: 849

Fig. 7.8 | Computing the sum of the elements of an array. (Part 2 of 2.)

7.4.5 Using Bar Charts to Display Array Data Graphically Many programs present data to users in a graphical manner. For example, numeric values are often displayed as bars in a bar chart. In such a chart, longer bars represent proportionally larger numeric values. One simple way to display numeric data graphically is with a bar chart that shows each numeric value as a bar of asterisks (*). Professors often like to examine the distribution of grades on an exam. A professor might graph the number of grades in each of several categories to visualize the grade distribution. Suppose the grades were 87, 68, 94, 100, 83, 78, 85, 91, 76 and 87. There was one grade of 100, two grades in the 90s, four grades in the 80s, two grades in the 70s, one grade in the 60s and no grades below 60. Our next program (Fig. 7.9) stores this grade distribution data in an array of 11 elements, each corresponding to a category of grades. For example, n[0] indicates the number of grades in the range 0–9, n[7] indicates the number of grades in the range 70–79 and n[10] indicates the number of grades of 100. The two versions of class GradeBook later in the chapter (Figs. 7.15–7.16 and Figs. 7.22– 7.23) contain code that calculates these grade frequencies based on a set of grades. For now, we manually create the array by looking at the set of grades. 1 2 3 4 5 6 7 8 9 10 11

// Fig. 7.9: fig07_09.cpp // Bar chart printing program. #include #include using namespace std; int main() { const int arraySize = 11; int n[ arraySize ] = { 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 };

Fig. 7.9 | Bar chart printing program. (Part 1 of 2.)

7.4 Examples Using Arrays 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

277

cout << "Grade distribution:" << endl; // for each element of array n, output a bar of the chart for ( int i = 0; i < arraySize; ++i ) { // output bar labels ("0-9:", ..., "90-99:", "100:" ) if ( i == 0 ) cout << " 0-9: "; else if ( i == 10 ) cout << " 100: "; else cout << i * 10 << "-" << ( i * 10 ) + 9 << ": "; // print bar of asterisks for ( int stars = 0; stars < n[ i ]; ++stars ) cout << '*'; cout << endl; // start a new line of output } // end outer for } // end main

Grade distribution: 0-9: 10-19: 20-29: 30-39: 40-49: 50-59: 60-69: * 70-79: ** 80-89: **** 90-99: ** 100: *

Fig. 7.9 | Bar chart printing program. (Part 2 of 2.) The program reads the numbers from the array and graphs the information as a bar chart, displaying each grade range followed by a bar of asterisks indicating the number of grades in that range. To label each bar, lines 18–23 output a grade range (e.g., "70-79: ") based on the current value of counter variable i. The nested for statement (lines 26–27) outputs the bars. Note the loop-continuation condition in line 26 (stars < n[i]). Each time the program reaches the inner for, the loop counts from 0 up to n[i], thus using a value in array n to determine the number of asterisks to display. In this example, n[0]– n[5] contain zeros because no students received a grade below 60. Thus, the program displays no asterisks next to the first six grade ranges.

7.4.6 Using the Elements of an Array as Counters Sometimes, programs use counter variables to summarize data, such as the results of a survey. In Fig. 6.9, we used separate counters in our die-rolling program to track the number of occurrences of each side of a die as the program rolled the die 6,000,000 times. An array version of this program is shown in Fig. 7.10.

278 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Chapter 7 Arrays and Vectors

// Fig. 7.10: fig07_10.cpp // Roll a six-sided die 6,000,000 times. #include #include #include #include using namespace std; int main() { const int arraySize = 7; // ignore element zero int frequency[ arraySize ] = {}; // initialize elements to 0 srand( time( 0 ) ); // seed random number generator // roll die 6,000,000 times; use die value as frequency index for ( int roll = 1; roll <= 6000000; ++roll ) ++frequency[ 1 + rand() % 6 ] ; cout << "Face" << setw( 13 ) << "Frequency" << endl; // output each array element's value for ( int face = 1; face < arraySize; ++face ) cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ] << endl; } // end main

Face 1 2 3 4 5 6

Frequency 1000167 1000149 1000152 998748 999626 1001158

Fig. 7.10 | Die-rolling program using an array instead of switch. Figure 7.10 uses the array frequency (line 12) to count the occurrences of each side of the die. The single statement in line 18 of this program replaces the switch statement in lines 25–47 of Fig. 6.9. Line 18 uses a random value to determine which frequency element to increment during each iteration of the loop. The calculation in line 18 produces a random subscript from 1 to 6, so array frequency must be large enough to store six counters. However, we use a seven-element array in which we ignore frequency[0]—it’s more logical to have the die face value 1 increment frequency[1] than frequency[0]. Thus, each face value is used as a subscript for array frequency. We also replace lines 51–56 of Fig. 6.9 by looping through array frequency to output the results (lines 23–25).

7.4.7 Using Arrays to Summarize Survey Results Our next example uses arrays to summarize the results of data collected in a survey. Consider the following problem statement:

7.4 Examples Using Arrays

279

Twenty students were asked to rate on a scale of 1 to 5 the quality of the food in the student cafeteria, with 1 being “awful” and 5 being “excellent.” Place the 20 responses in an integer array and determine the frequency of each rating.

This is a typical array-processing application (Fig. 7.11). We wish to summarize the number of responses of each type (that is, 1–5). The array responses (lines 14–15) is a 20element integer array of the students’ responses to the survey. The array responses is declared const, as its values do not (and should not) change. We use a six-element array frequency (line 18) to count the number of occurrences of each response. Each element of the array is used as a counter for one of the survey responses and is initialized to zero. As in Fig. 7.10, we ignore frequency[0]. The first for statement (lines 22–23) takes the responses one at a time from the array responses and increments one of the five counters in the frequency array (frequency[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

// Fig. 7.11: fig07_11.cpp // Poll analysis program. #include #include using namespace std; int main() { // define array sizes const int responseSize = 20; // size of array responses const int frequencySize = 6; // size of array frequency // place survey responses in array responses const int responses[ responseSize ] = { 1, 2, 5, 4, 3, 5, 2, 1, 3, 1, 4, 3, 3, 3, 2, 3, 3, 2, 2, 5 }; // initialize frequency counters to 0 int frequency[ frequencySize ] = {}; // for each answer, select responses element and use that value // as frequency subscript to determine element to increment for ( int answer = 0; answer < responseSize; ++answer ) ++frequency[ responses[ answer ] ] ; cout << "Rating" << setw( 17 ) << "Frequency" << endl; // output each array element's value for ( int rating = 1; rating < frequencySize; ++rating ) cout << setw( 6 ) << rating << setw( 17 ) << frequency[ rating ] << endl; } // end main

Rating 1 2 3 4 5

Frequency 3 5 7 2 3

Fig. 7.11 | Poll analysis program.

280

Chapter 7 Arrays and Vectors

to frequency[5]). The key statement in the loop is line 23, which increments the appropriate frequency counter, depending on the value of responses[answer]. Let’s consider several iterations of the for loop. When control variable answer is 0, the value of responses[answer] is the value of responses[0] (i.e., 1 in line 14), so the program interprets ++frequency[responses[answer]] as ++frequency[ 1 ]

which increments the value in array element 1. To evaluate the expression, start with the value in the innermost set of square brackets (answer). Once you know answer’s value (which is the value of the loop control variable in line 23), plug it into the expression and evaluate the next outer set of square brackets (i.e., responses[answer], which is a value selected from the responses array in lines 14–17). Then use the resulting value as the subscript for the frequency array to specify which counter to increment. When answer is 1, responses[answer] is the value of responses[1], which is 2, so the program interprets ++frequency[responses[answer]] as ++frequency[ 2 ]

which increments array element 2. When answer is 2, responses[answer] is the value of responses[2], which is 5, so the program interprets ++frequency[responses[answer]] as ++frequency[ 5 ]

which increments array element 5, and so on. Regardless of the number of responses processed in the survey, the program requires only an six-element array (ignoring element zero) to summarize the results, because all the response values are between 1 and 5 and the subscript values for an six-element array are 0 through 5. If the data in responses contained an invalid value, such as 13, the program would have attempted to add 1 to frequency[13], which is outside the bounds of the array. C++ has no array bounds checking to prevent the computer from referring to an element that does not exist. Thus, an executing program can “walk off” either end of an array without warning. You should ensure that all array references remain within the bounds of the array.

Common Programming Error 7.5

Referring to an element outside the array bounds is an execution-time logic error. It isn’t a syntax error.

Error-Prevention Tip 7.1

When looping through an array, the index should never go below 0 and should always be less than the total number of array elements (one less than the size of the array). Make sure that the loop-termination condition prevents accessing elements outside this range.

C++ is an extensible language. Section 7.11 presents C++ Standard Library class template vector, which enables you to perform many operations that are not available for built-in arrays. For example, we’ll be able to compare vectors directly and assign one vector to another. In Chapter 11, we extend C++ further by implementing an array as a class of our own. This new array definition will enable us to input and output entire arrays with cin and cout, initialize arrays when they’re created and prevent access to out-of-range array elements. We’ll even be able to use noninteger subscripts.

7.4 Examples Using Arrays

281

Error-Prevention Tip 7.2

In Chapter 11, we’ll see how to develop a class representing a “smart array,” which checks that all subscript references are in bounds at runtime. Using such smart data types helps eliminate bugs.

7.4.8 Static Local Arrays and Automatic Local Arrays Chapter 6 discussed the storage-class specifier static. A static local variable in a function definition exists for the program’s duration but is visible only in the function’s body.

Performance Tip 7.1

We can apply static to a local array declaration so that it not created and initialized each time the program calls the function and is not destroyed each time the function terminates. This can improve performance, especially when using large arrays.

A program initializes static local arrays when their declarations are first encountered. If a static array is not initialized explicitly by you, each element of that array is initialized to zero by the compiler when the array is created. Recall that C++ does not perform such default initialization for automatic variables. Figure 7.12 demonstrates function staticArrayInit (lines 23–39) with a static local array (line 26) and function automaticArrayInit (lines 42–58) with an automatic local array (line 45). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

// Fig. 7.12: fig07_12.cpp // Static arrays are initialized to zero. #include using namespace std; void staticArrayInit( void ); // function prototype void automaticArrayInit( void ); // function prototype const int arraySize = 3; int main() { cout << "First call to each function:\n"; staticArrayInit(); automaticArrayInit(); cout << "\n\nSecond call to each function:\n"; staticArrayInit(); automaticArrayInit(); cout << endl; } // end main // function to demonstrate a static local array void staticArrayInit( void ) { // initializes elements to 0 first time function is called static int array1[ arraySize ]; // static local array

Fig. 7.12 |

static

array initialization and automatic array initialization. (Part 1 of 2.)

282 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

Chapter 7 Arrays and Vectors

cout << "\nValues on entering staticArrayInit:\n"; // output contents of array1 for ( int i = 0; i < arraySize; ++i ) cout << "array1[" << i << "] = " << array1[ i ] << "

";

cout << "\nValues on exiting staticArrayInit:\n"; // modify and output contents of array1 for ( int j = 0; j < arraySize; ++j ) cout << "array1[" << j << "] = " << ( array1[ j ] += 5 ) << " } // end function staticArrayInit

";

// function to demonstrate an automatic local array void automaticArrayInit( void ) { // initializes elements each time function is called int array2[ arraySize ] = { 1, 2, 3 }; // automatic local array cout << "\n\nValues on entering automaticArrayInit:\n"; // output contents of array2 for ( int i = 0; i < arraySize; ++i ) cout << "array2[" << i << "] = " << array2[ i ] << "

";

cout << "\nValues on exiting automaticArrayInit:\n"; // modify and output contents of array2 for ( int j = 0; j < arraySize; ++j ) cout << "array2[" << j << "] = " << ( array2[ j ] += 5 ) << " } // end function automaticArrayInit

First call to each function: Values on array1[0] Values on array1[0]

entering staticArrayInit: = 0 array1[1] = 0 array1[2] = 0 exiting staticArrayInit: = 5 array1[1] = 5 array1[2] = 5

Values on array2[0] Values on array2[0]

entering automaticArrayInit: = 1 array2[1] = 2 array2[2] = 3 exiting automaticArrayInit: = 6 array2[1] = 7 array2[2] = 8

Second call to each function: Values on array1[0] Values on array1[0]

entering staticArrayInit: = 5 array1[1] = 5 array1[2] = 5 exiting staticArrayInit: = 10 array1[1] = 10 array1[2] = 10

Values on array2[0] Values on array2[0]

entering automaticArrayInit: = 1 array2[1] = 2 array2[2] = 3 exiting automaticArrayInit: = 6 array2[1] = 7 array2[2] = 8

Fig. 7.12 |

static

array initialization and automatic array initialization. (Part 2 of 2.)

";

7.5 Passing Arrays to Functions

283

Function staticArrayInit is called twice (lines 13 and 17). The static local array is initialized to zero by the compiler the first time the function is called. The function prints the array, adds 5 to each element and prints the array again. The second time the function is called, the static array contains the modified values stored during the first function call. Function automaticArrayInit also is called twice (lines 14 and 18). The elements of the automatic local array are initialized (line 45) with the values 1, 2 and 3. The function prints the array, adds 5 to each element and prints the array again. The second time the function is called, the array elements are reinitialized to 1, 2 and 3. The array has automatic storage class, so the array is recreated and reinitialized during each call to automaticArrayInit.

Common Programming Error 7.6

Assuming that elements of a function’s local static array are initialized every time the function is called can lead to logic errors in a program.

7.5 Passing Arrays to Functions To pass an array argument to a function, specify the name of the array without any brackets. For example, if array hourlyTemperatures has been declared as int hourlyTemperatures[ 24 ];

the function call modifyArray( hourlyTemperatures, 24 );

passes array hourlyTemperatures and its size to function modifyArray. When passing an array to a function, the array size is normally passed as well, so the function can process the specific number of elements in the array. Otherwise, we would need to build this knowledge into the called function itself or, worse yet, place the array size in a global variable. In Section 7.11, when we present C++ Standard Library class template vector to represent a more robust type of array, you’ll see that the size of a vector is built in—every vector object “knows” its own size, which can be obtained by invoking the vector object’s size member function. Thus, when we pass a vector object into a function, we won’t have to pass the size of the vector as an argument. C++ passes arrays to functions by reference—the called functions can modify the element values in the callers’ original arrays. The value of the name of the array is the address in the computer’s memory of the first element of the array. Because the starting address of the array is passed, the called function knows precisely where the array is stored in memory. Therefore, when the called function modifies array elements in its function body, it’s modifying the actual elements of the array in their original memory locations.

Performance Tip 7.2

Passing arrays by reference makes sense for performance reasons. Passing by value would require copying each element. For large, frequently passed arrays, this would be time consuming and would require considerable storage for the copies of the array elements.

Software Engineering Observation 7.2

It’s possible to pass an array by value by simply embedding it as a data member of a class and passing an object of the class, which defaults to pass-by-value.

284

Chapter 7 Arrays and Vectors

Although entire arrays are passed by reference, individual array elements are passed by value exactly as simple variables are. To pass an element of an array to a function, use the subscripted name of the array element as an argument in the function call. In Chapter 6, we showed how to pass individual variables and array elements by reference with references—in Chapter 8, we show how to pass them by reference with pointers. For a function to receive an array through a function call, the function’s parameter list must specify that the function expects to receive an array. For example, the function header for function modifyArray might be written as void modifyArray( int b[], int arraySize )

indicating that modifyArray expects to receive the address of an array of integers in parameter b and the number of array elements in parameter arraySize. The array’s size is not required in the array brackets. If it’s included, the compiler ignores it; thus, arrays of any size can be passed to the function. C++ passes arrays to functions by reference—when the called function uses the array name b, it refers to the actual array in the caller (i.e., array hourlyTemperatures discussed at the beginning of this section). Note the strange appearance of the function prototype for modifyArray void modifyArray( int [], int );

This prototype could have been written (for documentation purposes) void modifyArray( int anyArrayName[], int anyVariableName );

but, as we learned in Chapter 3, C++ compilers ignore variable names in prototypes. Remember, the prototype tells the compiler the number of arguments and the type of each argument (in the order in which the arguments are expected to appear). The program in Fig. 7.13 demonstrates the difference between passing an entire array and passing an array element. Lines 19–20 print the five original elements of integer array a. Line 25 passes a and its size to function modifyArray (lines 40–45), which multiplies each of a’s elements by 2 (through parameter b). Then, lines 29–30 print array a again in main. As the output shows, the elements of a are indeed modified by modifyArray. Next, line 33 prints the value of a[3], then line 35 passes element a[3] to function modifyElement (lines 49–53), which multiplies its parameter by 2 and prints the new value. When line 36 prints a[3] again in main, the value has not been modified, because individual array elements are passed by value. 1 2 3 4 5 6 7 8 9 10 11

// Fig. 7.13: fig07_13.cpp // Passing arrays and individual array elements to functions. #include #include using namespace std; void modifyArray( int [], int ); // appears strange; array and size void modifyElement( int ); // receive array element value int main() {

Fig. 7.13 | Passing arrays and individual array elements to functions. (Part 1 of 2.)

7.5 Passing Arrays to Functions 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

285

const int arraySize = 5; // size of array a int a[ arraySize ] = { 0, 1, 2, 3, 4 }; // initialize array a cout << "Effects of passing entire array by reference:" << "\nThe values of the original array are:\n"; // output original array elements for ( int i = 0; i < arraySize; ++i ) cout << setw( 3 ) << a[ i ]; cout << endl; // pass array a to modifyArray by reference modifyArray( a, arraySize ); cout << "The values of the modified array are:\n"; // output modified array elements for ( int j = 0; j < arraySize; ++j ) cout << setw( 3 ) << a[ j ]; cout << "\n\nEffects of passing array element by value:" << "\na[3] before modifyElement: " << a[ 3 ] << endl; modifyElement( a[ 3 ] ); // pass array element a[ 3 ] by value cout << "a[3] after modifyElement: " << a[ 3 ] << endl; } // end main // in function modifyArray, "b" points to the original array "a" in memory void modifyArray( int b[], int sizeOfArray ) { // multiply each array element by 2 for ( int k = 0; k < sizeOfArray; ++k ) b[ k ] *= 2; } // end function modifyArray // in function modifyElement, "e" is a local copy of // array element a[ 3 ] passed from main void modifyElement( int e ) { // multiply parameter by 2 cout << "Value of element in modifyElement: " << ( e *= 2 ) << endl; } // end function modifyElement

Effects of The values 0 1 2 The values 0 2 4

passing entire array by reference: of the original array are: 3 4 of the modified array are: 6 8

Effects of passing array element by value: a[3] before modifyElement: 6 Value of element in modifyElement: 12 a[3] after modifyElement: 6

Fig. 7.13 | Passing arrays and individual array elements to functions. (Part 2 of 2.)

286

Chapter 7 Arrays and Vectors

There may be situations in your programs in which a function should not be allowed to modify array elements. The type qualifier const can be used to prevent modification of array values in the caller by code in a called function. When a function specifies an array parameter that’s preceded by the const qualifier, the elements of the array become constant in the function body, and any attempt to modify an element of the array in the function body results in a compilation error. This enables you to prevent accidental modification of array elements in the function’s body. Figure 7.14 demonstrates the const qualifier applied to an array parameter. Function tryToModifyArray (lines 18–21) is defined with parameter const int b[], which specifies that array b is constant and cannot be modified. The attempt by the function to modify array b’s element 0 (line 20) results in a compilation error. Some compilers, for example, produce an error like “Cannot modify a const object.” This message indicates that using a const object (b[0]) as an lvalue is an error—you cannot assign a new value to a const object.

Software Engineering Observation 7.3

Applying the const type qualifier to an array parameter in a function definition to prevent the original array from being modified in the function body is another example of the principle of least privilege. Functions should not be given the capability to modify an array unless it’s absolutely necessary. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

// Fig. 7.14: fig07_14.cpp // Demonstrating the const type qualifier. #include using namespace std; void tryToModifyArray( const int [] ); // function prototype int main() { int a[] = { 10, 20, 30 }; tryToModifyArray( a ); cout << a[ 0 ] << ' ' << a[ 1 ] << ' ' << a[ 2 ] << '\n'; } // end main // In function tryToModifyArray, "b" cannot be used // to modify the original array "a" in main. void tryToModifyArray( const int b[] ) { b[ 0 ] /= 2; // compilation error } // end function tryToModifyArray

Microsoft Visual C++ compiler error message: c:\cpphtp8_examples\ch07\fig07_14\fig07_14.cpp(20) : error C3892: 'b' : you cannot assign to a variable that is const

GNU C++ compiler error message: fig07_14.cpp:20: error: assignment of read-only location

Fig. 7.14 |

const

type qualifier applied to an array parameter.

7.6 Case Study: Class GradeBook Using an Array to Store Grades

287

7.6 Case Study: Class GradeBook Using an Array to Store Grades This section further evolves class GradeBook, introduced in Chapter 3 and expanded in Chapters 4–6. Recall that this class represents a grade book used by a professor to store and analyze student grades. Previous versions of the class process grades entered by the user, but do not maintain the individual grade values in the class’s data members. Thus, repeat calculations require the user to reenter the grades. One way to solve this problem would be to store each grade entered in an individual data member of the class. For example, we could create data members grade1, grade2, …, grade10 in class GradeBook to store 10 student grades. However, the code to total the grades and determine the class average would be cumbersome. In this section, we solve this problem by storing grades in an array.

Storing Student Grades in an Array in Class GradeBook The version of class GradeBook (Figs. 7.15–7.16) presented here uses an array of integers to store the grades of several students on a single exam. This eliminates the need to repeatedly input the same set of grades. Array grades is declared as a data member in line 28 of Fig. 7.15—therefore, each GradeBook object maintains its own set of grades. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

// Fig. 7.15: GradeBook.h // Definition of class GradeBook that uses an array to store test grades. // Member functions are defined in GradeBook.cpp #include // program uses C++ Standard Library string class using namespace std; // GradeBook class definition class GradeBook { public: // constant -- number of students who took the test static const int students = 10; // note public data // constructor initializes course name and array of grades GradeBook( string, const int [] ); void setCourseName( string ); // function to set the course name string getCourseName(); // function to retrieve the course name void displayMessage(); // display a welcome message void processGrades(); // perform various operations on the grade data int getMinimum(); // find the minimum grade for the test int getMaximum(); // find the maximum grade for the test double getAverage(); // determine the average grade for the test void outputBarChart(); // output bar chart of grade distribution void outputGrades(); // output the contents of the grades array private: string courseName; // course name for this grade book int grades[ students ]; // array of student grades }; // end class GradeBook

Fig. 7.15 | Definition of class GradeBook that uses an array to store test grades.

288 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

Chapter 7 Arrays and Vectors

// Fig. 7.16: GradeBook.cpp // Member-function definitions for class GradeBook that // uses an array to store test grades. #include #include #include "GradeBook.h" // GradeBook class definition using namespace std; // constructor initializes courseName and grades array GradeBook::GradeBook( string name, const int gradesArray[] ) { setCourseName( name ); // initialize courseName // copy grades from gradesArray to grades data member for ( int grade = 0; grade < students; ++grade ) grades[ grade ] = gradesArray[ grade ]; } // end GradeBook constructor // function to set the course name void GradeBook::setCourseName( string name ) { courseName = name; // store the course name } // end function setCourseName // function to retrieve the course name string GradeBook::getCourseName() { return courseName; } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { // this statement calls getCourseName to get the // name of the course this GradeBook represents cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } // end function displayMessage // perform various operations on the data void GradeBook::processGrades() { outputGrades(); // output grades array // display average of all grades and minimum and maximum grades cout << "\nClass average is " << setprecision( 2 ) << fixed << getAverage() << "\nLowest grade is " << getMinimum() << "\nHighest grade is " << getMaximum() << endl; outputBarChart(); // print grade distribution chart } // end function processGrades

Fig. 7.16 |

GradeBook

class member functions manipulating an array of grades. (Part 1 of 3.)

7.6 Case Study: Class GradeBook Using an Array to Store Grades 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 101 102 103 104 105

289

// find minimum grade int GradeBook::getMinimum() { int lowGrade = 100; // assume lowest grade is 100 // loop through grades array for ( int grade = 0; grade < students; ++grade ) { // if current grade lower than lowGrade, assign it to lowGrade if ( grades[ grade ] < lowGrade ) lowGrade = grades[ grade ]; // new lowest grade } // end for return lowGrade; // return lowest grade } // end function getMinimum // find maximum grade int GradeBook::getMaximum() { int highGrade = 0; // assume highest grade is 0 // loop through grades array for ( int grade = 0; grade < students; ++grade ) { // if current grade higher than highGrade, assign it to highGrade if ( grades[ grade ] > highGrade ) highGrade = grades[ grade ]; // new highest grade } // end for return highGrade; // return highest grade } // end function getMaximum // determine average grade for test double GradeBook::getAverage() { int total = 0; // initialize total // sum grades in array for ( int grade = 0; grade < students; ++grade ) total += grades[ grade ]; // return average of grades return static_cast< double >( total ) / students; } // end function getAverage // output bar chart displaying grade distribution void GradeBook::outputBarChart() { cout << "\nGrade distribution:" << endl; // stores frequency of grades in each range of 10 grades const int frequencySize = 11; int frequency[ frequencySize ] = {}; // initialize elements to 0

Fig. 7.16 |

GradeBook

class member functions manipulating an array of grades. (Part 2 of 3.)

290 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

Chapter 7 Arrays and Vectors

// for each grade, increment the appropriate frequency for ( int grade = 0; grade < students; ++grade ) ++frequency[ grades[ grade ] / students ]; // for each grade frequency, print bar in chart for ( int count = 0; count < frequencySize; ++count ) { // output bar labels ("0-9:", ..., "90-99:", "100:" ) if ( count == 0 ) cout << " 0-9: "; else if ( count == 10 ) cout << " 100: "; else cout << count * 10 << "-" << ( count * 10 ) + 9 << ": "; // print bar of asterisks for ( int stars = 0; stars < frequency[ count ]; ++stars ) cout << '*'; cout << endl; // start a new line of output } // end outer for } // end function outputBarChart // output the contents of the grades array void GradeBook::outputGrades() { cout << "\nThe grades are:\n\n"; // output each student's grade for ( int student = 0; student < students; ++student ) cout << "Student " << setw( 2 ) << student + 1 << ": " << setw( 3 ) << grades[ student ] << endl; } // end function outputGrades

Fig. 7.16 |

GradeBook

class member functions manipulating an array of grades. (Part 3 of 3.)

The size of the array in line 28 of Fig. 7.15 is specified by public static const data member students (declared in line 12), which is public so that it’s accessible to the class’s clients. We’ll soon see an example of a client program using this constant. Declaring students with the const qualifier indicates that this data member is constant—its value cannot be changed after being initialized. Keyword static in this variable declaration indicates that the data member is shared by all objects of the class—all GradeBook objects store grades for the same number of students. Recall from Section 3.4 that when each object of a class maintains its own copy of an attribute, the variable that represents the attribute is known as a data member—each object (instance) of the class has a separate copy of the variable in memory. There are variables for which each object of a class does not have a separate copy. That is the case with static data members, which are also known as class variables. When objects of a class containing static data members are created, all the objects share one copy of the class’s static data members. A static data member can be accessed within the class definition and the member-function definitions like any other data member. As you’ll soon see, a public static data member can also be accessed outside of the class, even when no objects of the class

7.6 Case Study: Class GradeBook Using an Array to Store Grades

291

exist, using the class name followed by the scope resolution operator (::) and the name of the data member. You’ll learn more about static data members in Chapter 10.

Constructor The class’s constructor (declared in line 15 of Fig. 7.15 and defined in lines 10–17 of Fig. 7.16) has two parameters—the course name and an array of grades. When a program creates a GradeBook object (e.g., lines 12–13 of fig07_17.cpp), the program passes an existing int array to the constructor, which copies the array’s values into the data member grades (lines 15–16 of Fig. 7.16). The grade values in the passed array could have been input from a user or read from a file on disk (as we discuss in Chapter 17, File Processing). In our test program, we simply initialize an array with a set of grade values (Fig. 7.17, lines 9–10). Once the grades are stored in data member grades of class GradeBook, all the class’s member functions can access the grades array as needed to perform various calculations. Member Function processGrades Member function processGrades (declared in line 20 of Fig. 7.15 and defined in lines 41–51 of Fig. 7.16) contains a series of member function calls that output a report summarizing the grades. Line 43 calls member function outputGrades to print the contents of the array grades. Lines 136–138 in member function outputGrades use a for statement to output each student’s grade. Although array indices start at 0, a professor would typically number students starting at 1. Thus, lines 137–138 output student + 1 as the student number to produce grade labels "Student 1: ", "Student 2: ", and so on. Member Function getAverage Member function processGrades next calls member function getAverage (line 47) to obtain the average of the grades. Member function getAverage (declared in line 23 of Fig. 7.15 and defined in lines 86–96 of Fig. 7.16) totals the values in array grades before calculating the average. The averaging calculation in line 95 uses static const data member students to determine the number of grades being averaged. Member Functions getMinimum and getMaximum Lines 47–48 in processGrades call member functions getMinimum and getMaximum to determine the lowest and highest grades of any student on the exam, respectively. Let’s examine how member function getMinimum finds the lowest grade. Because the highest grade allowed is 100, we begin by assuming that 100 is the lowest grade (line 56). Then, we compare each of the elements in the array to the lowest grade, looking for smaller values. Lines 59–64 in member function getMinimum loop through the array, and line 62 compares each grade to lowGrade. If a grade is less than lowGrade, lowGrade is set to that grade. When line 66 executes, lowGrade contains the lowest grade in the array. Member function getMaximum (lines 70–83) works similarly to member function getMinimum. Member Function outputBarChart Finally, line 50 in member function processGrades calls member function outputBarChart to print a distribution chart of the grade data using a technique similar to that in Fig. 7.9. In that example, we manually calculated the number of grades in each category (i.e., 0–9, 10– 19, …, 90–99 and 100) by simply looking at a set of grades. In this example, lines 108–109 use a technique similar to that in Fig. 7.10 and Fig. 7.11 to calculate the frequency of grades in each category. Line 105 declares and creates array frequency of 11 ints to store the fre-

292

Chapter 7 Arrays and Vectors

quency of grades in each grade category. For each grade in array grades, lines 108–109 increment the appropriate element of the frequency array. To determine which element to increment, line 109 divides the current grade by 10 using integer division. For example, if grade is 85, line 109 increments frequency[8] to update the count of grades in the range 80–89. Lines 112–127 next print the bar chart (see Fig. 7.17) based on the values in array frequency. Like lines 26–27 of Fig. 7.9, lines 123–124 of Fig. 7.16 use a value in array frequency to determine the number of asterisks to display in each bar.

Testing Class GradeBook The program of Fig. 7.17 creates an object of class GradeBook (Figs. 7.15–7.16) using the int array gradesArray (declared and initialized in lines 9–10). The scope resolution operator (::) is used in the expression “GradeBook::students” (line 9) to access class GradeBook’s static constant students. We use this constant here to create an array that is the same size as array grades stored as a data member in class GradeBook. Lines 12–13 pass a course name and gradesArray to the GradeBook constructor. Line 14 displays a welcome message, and line 15 invokes the GradeBook object’s processGrades member function. The output reveals the summary of the 10 grades in myGradeBook. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

// Fig. 7.17: fig07_17.cpp // Creates GradeBook object using an array of grades. #include "GradeBook.h" // GradeBook class definition // function main begins program execution int main() { // array of student grades int gradesArray[ GradeBook::students ] = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 }; GradeBook myGradeBook( "CS101 Introduction to C++ Programming", gradesArray ); myGradeBook.displayMessage(); myGradeBook.processGrades(); } // end main

Welcome to the grade book for CS101 Introduction to C++ Programming! The grades are: Student 1: 87 Student 2: 68 Student 3: 94 Student 4: 100 Student 5: 83 Student 6: 78 Student 7: 85 Student 8: 91 Student 9: 76 Student 10: 87

Fig. 7.17 | Creates a GradeBook object using an array of grades, then invokes member function processGrades

to analyze them. (Part 1 of 2.)

7.7 Searching Arrays with Linear Search

293

Class average is 84.90 Lowest grade is 68 Highest grade is 100 Grade distribution: 0-9: 10-19: 20-29: 30-39: 40-49: 50-59: 60-69: * 70-79: ** 80-89: **** 90-99: ** 100: *

Fig. 7.17 | Creates a GradeBook object using an array of grades, then invokes member function processGrades

to analyze them. (Part 2 of 2.)

7.7 Searching Arrays with Linear Search Often it may be necessary to determine whether an array contains a value that matches a certain key value. The process of finding a particular element of an array is called searching. In this section we discuss the simple linear search. Exercise 7.33 at the end of this chapter asks you to implement a recursive version of the linear search. In Chapter 19, Searching and Sorting, we present the more complex, yet more efficient, binary search.

Linear Search The linear search (Fig. 7.18, lines 33–40) compares each element of an array with a search key (line 36). Because the array is not in any particular order, it’s just as likely that the value will be found in the first element as the last. On average, therefore, the program must compare the search key with half the elements of the array. To determine that a value is not in the array, the program must compare the search key to every element of the array. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

// Fig. 7.18: fig07_18.cpp // Linear search of an array. #include using namespace std; int linearSearch( const int [], int, int ); // prototype int main() { const int arraySize = 100; // size of array a int a[ arraySize ]; // create array a int searchKey; // value to locate in array a for ( int i = 0; i < arraySize; ++i ) a[ i ] = 2 * i; // create some data

Fig. 7.18 | Linear search of an array. (Part 1 of 2.)

294 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

Chapter 7 Arrays and Vectors

cout << "Enter integer search key: "; cin >> searchKey; // attempt to locate searchKey in array a int element = linearSearch( a, searchKey, arraySize ); // display results if ( element != -1 ) cout << "Found value in element " << element << endl; else cout << "Value not found" << endl; } // end main // compare key to every element of array until location is // found or until end of array is reached; return subscript of // element if key is found or -1 if key not found int linearSearch( const int array[], int key, int sizeOfArray ) { for ( int j = 0; j < sizeOfArray; ++j ) if ( array[ j ] == key ) // if found, return j; // return location of key return -1; // key not found } // end function linearSearch

Enter integer search key: 36 Found value in element 18

Enter integer search key: 37 Value not found

Fig. 7.18 | Linear search of an array. (Part 2 of 2.) The linear searching method works well for small arrays or for unsorted arrays (i.e., arrays whose elements are in no particular order). However, for large arrays, linear searching is inefficient. If the array is sorted (e.g., its elements are in ascending order), you can use the high-speed binary search technique that you’ll learn about in Chapter 19.

7.8 Sorting Arrays with Insertion Sort Sorting data (i.e., placing the data into some particular order such as ascending or descending) is one of the most important computing applications. A bank sorts all checks by account number so that it can prepare individual bank statements at the end of each month. Telephone companies sort their phone directories by last name and, within that, by first name to make it easy to find phone numbers. Virtually every organization must sort some data and, in many cases, massive amounts of it. Sorting data is an intriguing problem that has attracted some of the most intense research efforts in the field of computer science. In this chapter, we discuss a simple sorting scheme. In Chapter 19, we investigate more complex schemes that yield superior performance, and we introduce Big O

7.8 Sorting Arrays with Insertion Sort

295

(pronounced “Big Oh”) notation for characterizing how hard each scheme must work to accomplish its task.

Insertion Sort The program in Fig. 7.19 sorts the values of the 10-element array data into ascending order. The technique we use is called insertion sort—a simple, but inefficient, sorting algorithm. The first iteration of this algorithm takes the second element and, if it’s less than the first element, swaps it with the first element (i.e., the program inserts the second element in front of the first element). The second iteration looks at the third element and inserts it into the correct position with respect to the first two elements, so all three elements are in order. At the ith iteration of this algorithm, the first i elements in the original array will be sorted. Line 10 of Fig. 7.19 declares and initializes array data with the following values: 34

56

4

10

77

51

93

30

5

52

The program first looks at data[0] and data[1], whose values are 34 and 56, respectively. These two elements are already in order, so the program continues—if they were out of order, the program would swap them. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

// Fig. 7.19: fig07_19.cpp // This program sorts an array's values into ascending order. #include #include using namespace std; int main() { const int arraySize = 10; // size of array a int data[ arraySize ] = { 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 }; int insert; // temporary variable to hold element to insert cout << "Unsorted array:\n"; // output original array for ( int i = 0; i < arraySize; ++i ) cout << setw( 4 ) << data[ i ]; // insertion sort // loop over the elements of the array for ( int next = 1; next < arraySize; ++next ) { insert = data[ next ]; // store the value in the current element int moveItem = next; // initialize location to place element // search for the location in which to put the current element while ( ( moveItem > 0 ) && ( data[ moveItem - 1 ] > insert ) ) {

Fig. 7.19 | Sorting an array with insertion sort. (Part 1 of 2.)

296 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

Chapter 7 Arrays and Vectors

// shift element one slot to the right data[ moveItem ] = data[ moveItem - 1 ]; moveItem--; } // end while data[ moveItem ] = insert; // place inserted element into the array } // end for cout << "\nSorted array:\n"; // output sorted array for ( int i = 0; i < arraySize; ++i ) cout << setw( 4 ) << data[ i ]; cout << endl; } // end main

Unsorted array: 34 56 4 10 Sorted array: 4 5 10 30

77

51

93

30

5

52

34

51

52

56

77

93

Fig. 7.19 | Sorting an array with insertion sort. (Part 2 of 2.) In the second iteration, the program looks at the value of data[2], 4. This value is less than 56, so the program stores 4 in a temporary variable and moves 56 one element to the right. The program then checks and determines that 4 is less than 34, so it moves 34 one element to the right. The program has now reached the beginning of the array, so it places 4 in data[0]. The array now is 4

34

56

10

77

51

93

30

5

52

In the third iteration, the program stores the value of data[3], 10, in a temporary variable. Then the program compares 10 to 56 and moves 56 one element to the right because it’s larger than 10. The program then compares 10 to 34, moving 34 right one element. When the program compares 10 to 4, it observes that 10 is larger than 4 and places 10 in data[1]. The array now is 4

10

34

56

77

51

93

30

5

52

Using this algorithm, at the i th iteration, the first i elements of the original array are sorted.

They may not be in their final locations, however, because smaller values may be located later in the array. The sorting is performed by the for statement in lines 21–36 that loops over the elements of the array. In each iteration, line 23 temporarily stores in variable insert (declared in line 11) the value of the element that will be inserted into the sorted portion of the array. Line 25 declares and initializes the variable moveItem, which keeps track of where to insert the element. Lines 28–33 loop to locate the correct position where the element should be inserted. The loop terminates either when the program reaches the front of the array or when it reaches an element that’s less than the value to be inserted. Line 31 moves an element to the right, and line 32 decrements the position at which to insert the

7.9 Multidimensional Arrays

297

next element. After the while loop ends, line 35 inserts the element into place. When the for statement in lines 21–36 terminates, the elements of the array are sorted. The chief virtue of the insertion sort is that it’s easy to program; however, it runs slowly. This becomes apparent when sorting large arrays. In the exercises, we’ll investigate some alternate algorithms for sorting an array. We investigate sorting and searching in greater depth in Chapter 19.

7.9 Multidimensional Arrays Arrays with two dimensions (i.e., subscripts) often represent tables of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two subscripts. By convention, the first identifies the element’s row and the second identifies the element’s column. Arrays that require two subscripts to identify a particular element are called two-dimensional arrays or 2-D arrays. Arrays with two or more dimensions are known as multidimensional arrays and can have more than two dimensions. Figure 7.20 illustrates a two-dimensional array, a. The array contains three rows and four columns, so it’s said to be a 3-by-4 array. In general, an array with m rows and n columns is called an m-by-n array. Column 0

Column 1

Column 2

Column 3

Row 0 a[ 0 ][ 0 ]

a[ 0 ][ 1 ]

a[ 0 ][ 2 ]

a[ 0 ][ 3 ]

Row 1

a[ 1 ][ 0 ]

a[ 1 ][ 1 ]

a[ 1 ][ 2 ]

a[ 1 ][ 3 ]

Row 2

a[ 2 ][ 0 ]

a[ 2 ][ 1 ]

a[ 2 ][ 2 ]

a[ 2 ][ 3 ]

Column subscript Row subscript Array name

Fig. 7.20 | Two-dimensional array with three rows and four columns. Every element in array a is identified in Fig. 7.20 by an element name of the form where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a. Notice that the names of the elements in row 0 all have a first subscript of 0; the names of the elements in column 3 all have a second subscript of 3. a[i][j],

Common Programming Error 7.7

Referencing a two-dimensional array element a[x][y] incorrectly as a[x, y] is an error. Actually, a[x, y] is treated as a[y], because C++ evaluates the expression x, y (containing a comma operator) simply as y (the last of the comma-separated expressions).

A multidimensional array can be initialized in its declaration much like a one-dimensional array. For example, a two-dimensional array b with values 1 and 2 in its row 0 elements and values 3 and 4 in its row 1 elements could be declared and initialized with int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

298

Chapter 7 Arrays and Vectors

The values are grouped by row in braces. So, 1 and 2 initialize b[0][0] and b[0][1], respectively, and 3 and 4 initialize b[1][0] and b[1][1], respectively. If there are not enough initializers for a given row, the remaining elements of that row are initialized to 0. Thus, the declaration int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };

initializes b[0][0] to 1, b[0][1] to 0, b[1][0] to 3 and b[1][1] to 4. Figure 7.21 demonstrates initializing two-dimensional arrays in declarations. Lines 12–14 declare three arrays, each with two rows and three columns. The declaration of array1 (line 12) provides six initializers in two sublists. The first sublist initializes row 0 of the array to the values 1, 2 and 3; and the second sublist initializes row 1 of the array to 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

// Fig. 7.21: fig07_21.cpp // Initializing multidimensional arrays. #include using namespace std; void printArray( const int [][ 3 ] ); // prototype const int rows = 2; const int columns = 3; int main() { int array1[ rows ][ columns ] = { { 1, 2, 3 }, { 4, 5, 6 } }; int array2[ rows ][ columns ] = { 1, 2, 3, 4, 5 }; int array3[ rows ][ columns ] = { { 1, 2 }, { 4 } }; cout << "Values in array1 by row are:" << endl; printArray( array1 ); cout << "\nValues in array2 by row are:" << endl; printArray( array2 ); cout << "\nValues in array3 by row are:" << endl; printArray( array3 ); } // end main // output array with two rows and three columns void printArray( const int a[][ columns ] ) { // loop through array's rows for ( int i = 0; i < rows; ++i ) { // loop through columns of current row for ( int j = 0; j < columns; ++j ) cout << a[ i ][ j ] << ' '; cout << endl; // start new line of output } // end outer for } // end function printArray

Fig. 7.21 | Initializing multidimensional arrays. (Part 1 of 2.)

7.9 Multidimensional Arrays

299

Values in array1 by row are: 1 2 3 4 5 6 Values in array2 by row are: 1 2 3 4 5 0 Values in array3 by row are: 1 2 0 4 0 0

Fig. 7.21 | Initializing multidimensional arrays. (Part 2 of 2.) the values 4, 5 and 6. If the braces around each sublist are removed from the array1 initializer list, the compiler initializes the elements of row 0 followed by the elements of row 1, yielding the same result. The declaration of array2 (line 13) provides only five initializers. The initializers are assigned to row 0, then row 1. Any elements that do not have an explicit initializer are initialized to zero, so array2[1][2] is initialized to zero. The declaration of array3 (line 14) provides three initializers in two sublists. The sublist for row 0 explicitly initializes the first two elements of row 0 to 1 and 2; the third element is implicitly initialized to zero. The sublist for row 1 explicitly initializes the first element to 4 and implicitly initializes the last two elements to zero. The program calls function printArray to output each array’s elements. Notice that the function prototype (line 6) and definition (lines 27–38) specify the parameter const int a[][columns]. When a function receives a one-dimensional array as an argument, the array brackets are empty in the function’s parameter list. The size of a two-dimensional array’s first dimension (i.e., the number of rows) is not required either, but all subsequent dimension sizes are required. The compiler uses these sizes to determine the locations in memory of elements in multidimensional arrays. All array elements are stored consecutively in memory, regardless of the number of dimensions. In a two-dimensional array, row 0 is stored in memory followed by row 1. Each row is a one-dimensional array. To locate an element in a particular row, the function must know exactly how many elements are in each row so it can skip the proper number of memory locations when accessing the array. Thus, when accessing a[1][2], the function knows to skip row 0’s three elements in memory to get to row 1. Then, the function accesses element 2 of that row. Many common array manipulations use for statements. For example, the following for statement sets all the elements in row 2 of array a in Fig. 7.20 to zero: for ( int column = 0; column < 4; ++column ) a[ 2 ][ column ] = 0;

The for statement varies only the second subscript (i.e., the column subscript). The preceding for statement is equivalent to the following assignment statements: a[ a[ a[ a[

2 2 2 2

][ ][ ][ ][

0 1 2 3

] ] ] ]

= = = =

0; 0; 0; 0;

300

Chapter 7 Arrays and Vectors

The following nested for statement determines the total of all the elements in array a: total = 0; for ( int row = 0; row < 3; ++row ) for ( int column = 0; column < 4; ++column ) total += a[ row ][ column ];

The for statement totals the elements of the array one row at a time. The outer for statement begins by setting row (i.e., the row subscript) to 0, so the elements of row 0 may be totaled by the inner for statement. The outer for statement then increments row to 1, so the elements of row 1 can be totaled. Then, the outer for statement increments row to 2, so the elements of row 2 can be totaled. When the nested for statement terminates, total contains the sum of all the array elements.

7.10 Case Study: Class GradeBook Using a TwoDimensional Array In Section 7.6, we presented class GradeBook (Figs. 7.15–7.16), which used a one-dimensional array to store student grades on a single exam. In most semesters, students take several exams. Professors are likely to want to analyze grades across the entire semester, both for a single student and for the class as a whole.

Storing Student Grades in a Two-Dimensional Array in Class GradeBook Figures 7.22–7.23 contain a version of class GradeBook that uses a two-dimensional array grades to store the grades of a number of students on multiple exams. Each row of the array represents a single student’s grades for the entire course, and each column represents all the grades the students earned for one particular exam. A client program, such as Fig. 7.24, passes the array as an argument to the GradeBook constructor. In this example, we use a ten-by-three array containing ten students’ grades on three exams. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

// Fig. 7.22: GradeBook.h // Definition of class GradeBook that uses a // two-dimensional array to store test grades. // Member functions are defined in GradeBook.cpp #include // program uses C++ Standard Library string class using namespace std; // GradeBook class definition class GradeBook { public: // constants static const int students = 10; // number of students static const int tests = 3; // number of tests // constructor initializes course name and array of grades GradeBook( string, const int [][ tests ] );

Fig. 7.22 | Definition of class GradeBook that uses a two-dimensional array to store test grades. (Part 1 of 2.)

7.10 Case Study: Class GradeBook Using a Two-Dimensional Array 18 19 20 21 22 23 24 25 26 27 28 29 30 31

301

void setCourseName( string ); // function to set the course name string getCourseName(); // function to retrieve the course name void displayMessage(); // display a welcome message void processGrades(); // perform various operations on the grade data int getMinimum(); // find the minimum grade in the grade book int getMaximum(); // find the maximum grade in the grade book double getAverage( const int [], const int ); // get student’s average void outputBarChart(); // output bar chart of grade distribution void outputGrades(); // output the contents of the grades array private: string courseName; // course name for this grade book int grades[ students ][ tests ]; // two-dimensional array of grades }; // end class GradeBook

Fig. 7.22 | Definition of class GradeBook that uses a two-dimensional array to store test grades. (Part 2 of 2.)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

// Fig. 7.23: GradeBook.cpp // Member-function definitions for class GradeBook that // uses a two-dimensional array to store grades. #include #include // parameterized stream manipulators using namespace std; // include definition of class GradeBook from GradeBook.h #include "GradeBook.h" // two-argument constructor initializes courseName and grades array GradeBook::GradeBook( string name, const int gradesArray[][ tests ] ) { setCourseName( name ); // initialize courseName // copy grades from gradeArray to grades for ( int student = 0; student < students; ++student ) for ( int test = 0; test < tests; ++test ) grades[ student ][ test ] = gradesArray[ student ][ test ]; } // end two-argument GradeBook constructor // function to set the course name void GradeBook::setCourseName( string name ) { courseName = name; // store the course name } // end function setCourseName // function to retrieve the course name string GradeBook::getCourseName() {

Fig. 7.23 | Member-function definitions for class GradeBook that uses a two-dimensional array to store grades. (Part 1 of 4.)

302 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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

Chapter 7 Arrays and Vectors

return courseName; } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { // this statement calls getCourseName to get the // name of the course this GradeBook represents cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } // end function displayMessage // perform various operations on the data void GradeBook::processGrades() { outputGrades(); // output grades array // call functions getMinimum and getMaximum cout << "\nLowest grade in the grade book is " << getMinimum() << "\nHighest grade in the grade book is " << getMaximum() << endl; outputBarChart(); // display distribution chart of grades on all tests } // end function processGrades // find minimum grade in the entire gradebook int GradeBook::getMinimum() { int lowGrade = 100; // assume lowest grade is 100 // loop through rows of grades array for ( int student = 0; student < students; ++student ) { // loop through columns of current row for ( int test = 0; test < tests; ++test ) { // if current grade less than lowGrade, assign it to lowGrade if ( grades[ student ][ test ] < lowGrade ) lowGrade = grades[ student ][ test ]; // new lowest grade } // end inner for } // end outer for return lowGrade; // return lowest grade } // end function getMinimum // find maximum grade in the entire gradebook int GradeBook::getMaximum() { int highGrade = 0; // assume highest grade is 0 // loop through rows of grades array for ( int student = 0; student < students; ++student ) {

Fig. 7.23 | Member-function definitions for class GradeBook that uses a two-dimensional array to store grades. (Part 2 of 4.)

7.10 Case Study: Class GradeBook Using a Two-Dimensional Array 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 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

303

// loop through columns of current row for ( int test = 0; test < tests; ++test ) { // if current grade greater than highGrade, assign to highGrade if ( grades[ student ][ test ] > highGrade ) highGrade = grades[ student ][ test ]; // new highest grade } // end inner for } // end outer for return highGrade; // return highest grade } // end function getMaximum // determine average grade for particular set of grades double GradeBook::getAverage( const int setOfGrades[], const int grades ) { int total = 0; // initialize total // sum grades in array for ( int grade = 0; grade < grades; ++grade ) total += setOfGrades[ grade ]; // return average of grades return static_cast< double >( total ) / grades; } // end function getAverage // output bar chart displaying grade distribution void GradeBook::outputBarChart() { cout << "\nOverall grade distribution:" << endl; // stores frequency of grades in each range of 10 grades const int frequencySize = 11; int frequency[ frequencySize ] = {}; // initialize elements to 0 // for each grade, increment the appropriate frequency for ( int student = 0; student < students; ++student ) for ( int test = 0; test < tests; ++test ) ++frequency[ grades[ student ][ test ] / 10 ]; // for each grade frequency, print bar in chart for ( int count = 0; count < frequencySize; ++count ) { // output bar label ("0-9:", ..., "90-99:", "100:" ) if ( count == 0 ) cout << " 0-9: "; else if ( count == 10 ) cout << " 100: "; else cout << count * 10 << "-" << ( count * 10 ) + 9 << ": ";

Fig. 7.23 | Member-function definitions for class GradeBook that uses a two-dimensional array to store grades. (Part 3 of 4.)

304 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

Chapter 7 Arrays and Vectors

// print bar of asterisks for ( int stars = 0; stars < frequency[ count ]; ++stars ) cout << '*'; cout << endl; // start a new line of output } // end outer for } // end function outputBarChart // output the contents of the grades array void GradeBook::outputGrades() { cout << "\nThe grades are:\n\n"; cout << " "; // align column heads // create a column heading for each of the tests for ( int test = 0; test < tests; ++test ) cout << "Test " << test + 1 << " "; cout << "Average" << endl; // student average column heading // create rows/columns of text representing array grades for ( int student = 0; student < students; ++student ) { cout << "Student " << setw( 2 ) << student + 1; // output student's grades for ( int test = 0; test < tests; ++test ) cout << setw( 8 ) << grades[ student ][ test ]; // call member function getAverage to calculate // pass row of grades and the value of tests as double average = getAverage( grades[ student ], cout << setw( 9 ) << setprecision( 2 ) << fixed } // end outer for } // end function outputGrades

student's average; the arguments tests ); << average << endl;

Fig. 7.23 | Member-function definitions for class GradeBook that uses a two-dimensional array to store grades. (Part 4 of 4.)

Five member functions (declared in lines 23–27 of Fig. 7.22) perform array manipulations to process the grades. Each of these member functions is similar to its counterpart in the earlier one-dimensional array version of class GradeBook (Figs. 7.15–7.16). Member function getMinimum (defined in lines 57–74 of Fig. 7.23) determines the lowest grade of all students for the semester. Member function getMaximum (defined in lines 77–94 of Fig. 7.23) determines the highest grade of all students for the semester. Member function getAverage (lines 97–107 of Fig. 7.23) determines a particular student’s semester average. Member function outputBarChart (lines 110–141 of Fig. 7.23) outputs a bar chart of the distribution of all student grades for the semester. Member function outputGrades (lines 144–169 of Fig. 7.23) outputs the two-dimensional array in a tabular format, along with each student’s semester average. Member functions getMinimum, getMaximum, outputBarChart and outputGrades each loop through array grades by using nested for statements. For example, consider the

7.10 Case Study: Class GradeBook Using a Two-Dimensional Array

305

nested for statement in member function getMinimum (lines 62–71). The outer for statement begins by setting student (i.e., the row subscript) to 0, so the elements of row 0 can be compared with variable lowGrade in the body of the inner for statement. The inner for statement loops through the grades of a particular row and compares each grade with lowGrade. If a grade is less than lowGrade, lowGrade is set to that grade. The outer for statement then increments the row subscript to 1. The elements of row 1 are compared with variable lowGrade. The outer for statement then increments the row subscript to 2, and the elements of row 2 are compared with variable lowGrade. This repeats until all rows of grades have been traversed. When execution of the nested statement is complete, lowGrade contains the smallest grade in the two-dimensional array. Member function getMaximum works similarly to member function getMinimum. Member function outputBarChart in Fig. 7.23 is nearly identical to the one in Fig. 7.16. However, to output the overall grade distribution for a whole semester, the function uses a nested for statement (lines 119–122) to create the one-dimensional array frequency based on all the grades in the two-dimensional array. The rest of the code in each of the two outputBarChart member functions that displays the chart is identical. Member function outputGrades (lines 144–169) also uses nested for statements to output values of the array grades, in addition to each student’s semester average. The output in Fig. 7.24 shows the result, which resembles the tabular format of a professor’s physical grade book. Lines 150–151 print the column headings for each test. We use a counter-controlled for statement so that we can identify each test with a number. Similarly, the for statement in lines 156–168 first outputs a row label using a counter variable to identify each student (line 158). Although array indices start at 0, lines 151 and 158 output test + 1 and student + 1, respectively, to produce test and student numbers starting at 1 (see Fig. 7.24). The inner for statement in lines 161–162 uses the outer for statement’s counter variable student to loop through a specific row of array grades and output each student’s test grade. Finally, line 166 obtains each student’s semester average by passing the current row of grades (i.e., grades[student]) to member function getAverage. Member function getAverage (lines 97–107) takes two arguments—a one-dimensional array of test results for a particular student and the number of test results in the array. When line 166 calls getAverage, the first argument is grades[student], which specifies that a particular row of the two-dimensional array grades should be passed to getAverage. For example, based on the array created in Fig. 7.24, the argument grades[1] represents the three values (a one-dimensional array of grades) stored in row 1 of the two-dimensional array grades. A two-dimensional array can be considered an array whose elements are one-dimensional arrays. Member function getAverage calculates the sum of the array elements, divides the total by the number of test results and returns the floating-point result as a double value (line 106).

Testing Class GradeBook The program in Fig. 7.24 creates an object of class GradeBook (Figs. 7.22–7.23) using the two-dimensional array of ints named gradesArray (declared and initialized in lines 10– 20). Line 10 accesses class GradeBook’s static constants students and tests to indicate the size of each dimension of array gradesArray. Lines 22–23 pass a course name and gradesArray to the GradeBook constructor. Lines 24–25 then invoke myGradeBook’s displayMessage and processGrades member functions to display a welcome message and obtain a report summarizing the students’ grades for the semester, respectively.

306 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Chapter 7 Arrays and Vectors

// Fig. 7.24: fig07_24.cpp // Creates GradeBook object using a two-dimensional array of grades. #include "GradeBook.h" // GradeBook class definition // function main begins program execution int main() { // two-dimensional array of student grades int gradesArray[ GradeBook::students ][ GradeBook::tests ] = { { 87, 96, 70 }, { 68, 87, 90 }, { 94, 100, 90 }, { 100, 81, 82 }, { 83, 65, 85 }, { 78, 87, 65 }, { 85, 75, 83 }, { 91, 94, 100 }, { 76, 72, 84 }, { 87, 93, 73 } }; GradeBook myGradeBook( "CS101 Introduction to C++ Programming", gradesArray ); myGradeBook.displayMessage(); myGradeBook.processGrades(); } // end main

Welcome to the grade book for CS101 Introduction to C++ Programming! The grades are: Student 1 Student 2 Student 3 Student 4 Student 5 Student 6 Student 7 Student 8 Student 9 Student 10

Test 1 87 68 94 100 83 78 85 91 76 87

Test 2 96 87 100 81 65 87 75 94 72 93

Test 3 70 90 90 82 85 65 83 100 84 73

Average 84.33 81.67 94.67 87.67 77.67 76.67 81.00 95.00 77.33 84.33

Lowest grade in the grade book is 65 Highest grade in the grade book is 100 Overall grade distribution: 0-9: 10-19: 20-29: 30-39:

Fig. 7.24 | Creates a GradeBook object using a two-dimensional array of grades, then invokes member function processGrades to analyze them. (Part 1 of 2.)

7.11 Introduction to C++ Standard Library Class Template vector

40-49: 50-59: 60-69: 70-79: 80-89: 90-99: 100:

307

*** ****** *********** ******* ***

Fig. 7.24 | Creates a GradeBook object using a two-dimensional array of grades, then invokes member function processGrades to analyze them. (Part 2 of 2.)

7.11 Introduction to C++ Standard Library Class Template vector We now introduce C++ Standard Library class template vector, which represents a more robust type of array featuring many additional capabilities. As you’ll see in later chapters, C-style pointer-based arrays (i.e., the type of arrays presented thus far) have great potential for errors. For example, as mentioned earlier, a program can easily “walk off” either end of an array, because C++ does not check whether subscripts fall outside the range of an array. Two arrays cannot be meaningfully compared with equality operators or relational operators. As you’ll learn in Chapter 8, pointer variables (known more commonly as pointers) contain memory addresses as their values. Array names are simply pointers to where the arrays begin in memory, and, of course, two arrays will always be at different memory locations. When an array is passed to a general-purpose function designed to handle arrays of any size, the size of the array must be passed as an additional argument. Furthermore, one array cannot be assigned to another with the assignment operator(s)—array names are const pointers, and, as you’ll learn in Chapter 8, a constant pointer cannot be used on the left side of an assignment operator. These and other capabilities certainly seem like “naturals” for dealing with arrays, but C++ does not provide such capabilities. However, the C++ Standard Library provides class template vector to allow you to create a more powerful and less error-prone alternative to arrays. In Chapter 11, we present the means to implement such array capabilities as those provided by vector. You’ll learn how to customize operators for use with your own classes (a technique known as operator overloading). The vector class template is available to anyone building applications with C++. The notations that the vector example uses might be unfamiliar to you, because vectors use template notation. Recall that Section 6.18 discussed function templates. In Chapter 14, we discuss class templates. For now, you should feel comfortable using class template vector by mimicking the syntax in the example we show in this section. You’ll deepen your understanding as we study class templates in Chapter 14. Chapter 22 presents class template vector (and several other standard C++ container classes) in detail. The program of Fig. 7.25 demonstrates capabilities provided by C++ Standard Library class template vector that are not available for C-style pointer-based arrays. Standard class template vector provides many of the same features as the Array class that we construct in Chapter 11. Standard class template vector is defined in header (line 5) and belongs to namespace std. Chapter 22 discusses the full functionality of vector. At the end of this section, we’ll demonstrate class vector’s bounds checking capa-

308

Chapter 7 Arrays and Vectors

bilities and introduce C++’s exception-handling mechanism, which can be used to detect and handle an out-of-bounds vector index. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

// Fig. 7.25: fig07_25.cpp // Demonstrating C++ Standard Library class template vector. #include #include #include using namespace std; void outputVector( const vector< int > & ); // display the vector void inputVector( vector< int > & ); // input values into the vector int main() { vector< int > integers1( 7 ); // 7-element vector< int > vector< int > integers2( 10 ); // 10-element vector< int > // print integers1 size and contents cout << "Size of vector integers1 is " << integers1.size() << "\nvector after initialization:" << endl; outputVector( integers1 ); // print integers2 size and contents cout << "\nSize of vector integers2 is " << integers2.size() << "\nvector after initialization:" << endl; outputVector( integers2 ); // input and print integers1 and integers2 cout << "\nEnter 17 integers:" << endl; inputVector( integers1 ); inputVector( integers2 ); cout << "\nAfter input, the vectors contain:\n" << "integers1:" << endl; outputVector( integers1 ); cout << "integers2:" << endl; outputVector( integers2 ); // use inequality (!=) operator with vector objects cout << "\nEvaluating: integers1 != integers2" << endl; if ( integers1 != integers2 ) cout << "integers1 and integers2 are not equal" << endl; // create vector integers3 using integers1 as an // initializer; print size and contents vector< int > integers3( integers1 ); // copy constructor cout << "\nSize of vector integers3 is " << integers3.size() << "\nvector after initialization:" << endl; outputVector( integers3 );

Fig. 7.25 | Demonstrating C++ Standard Library class template vector. (Part 1 of 4.)

7.11 Introduction to C++ Standard Library Class Template vector 50 51 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 101 102

// use overloaded assignment (=) operator cout << "\nAssigning integers2 to integers1:" << endl; integers1 = integers2; // assign integers2 to integers1 cout << "integers1:" << outputVector( integers1 cout << "integers2:" << outputVector( integers2

endl; ); endl; );

// use equality (==) operator with vector objects cout << "\nEvaluating: integers1 == integers2" << endl; if ( integers1 == integers2 ) cout << "integers1 and integers2 are equal" << endl; // use square brackets to create rvalue cout << "\nintegers1[5] is " << integers1[ 5 ]; // use square brackets to create lvalue cout << "\n\nAssigning 1000 to integers1[5]" << endl; integers1[ 5 ] = 1000; cout << "integers1:" << endl; outputVector( integers1 ); // attempt to use out-of-range subscript try { cout << "\nAttempt to display integers1.at( 15 )" << endl; cout << integers1.at( 15 ) << endl; // ERROR: out of range } // end try catch ( out_of_range &ex ) { cout << "An exception occurred: " << ex.what() << endl; } // end catch } // end main // output vector contents void outputVector( const vector< int > &array ) { size_t i; // declare control variable for ( i = 0; i < array.size(); ++i ) { cout << setw( 12 ) << array[ i ]; if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output cout << endl; } // end for if ( i % 4 != 0 ) cout << endl; } // end function outputVector

Fig. 7.25 | Demonstrating C++ Standard Library class template vector. (Part 2 of 4.)

309

310 103 104 105 106 107 108 109

Chapter 7 Arrays and Vectors

// input vector contents void inputVector( vector< int > &array ) { for ( size_t i = 0; i < array.size(); ++i ) cin >> array[ i ]; } // end function inputVector

Size of vector integers1 is 7 vector after initialization: 0 0 0 0 Size of vector integers2 is 10 vector after initialization: 0 0 0 0 0 0

0 0

0

0 0

0 0

Enter 17 integers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 After input, the vectors contain: integers1: 1 2 3 5 6 7 integers2: 8 9 10 12 13 14 16 17

4 11 15

Evaluating: integers1 != integers2 integers1 and integers2 are not equal Size of vector integers3 is 7 vector after initialization: 1 2 5 6 Assigning integers2 to integers1: integers1: 8 9 12 13 16 17 integers2: 8 9 12 13 16 17

3 7

4

10 14

11 15

10 14

11 15

Evaluating: integers1 == integers2 integers1 and integers2 are equal integers1[5] is 13

Fig. 7.25 | Demonstrating C++ Standard Library class template vector. (Part 3 of 4.)

7.11 Introduction to C++ Standard Library Class Template vector Assigning 1000 to integers1[5] integers1: 8 9 12 1000 16 17

10 14

311

11 15

Attempt to display integers1.at( 15 ) An exception occurred: invalid vector subscript

Fig. 7.25 | Demonstrating C++ Standard Library class template vector. (Part 4 of 4.) Creating vector Objects Lines 13–14 create two vector objects that store values of type int—integers1 contains seven elements, and integers2 contains 10 elements. By default, all the elements of each vector object are set to 0. Note that vectors can be defined to store any data type, by replacing int in vector with the appropriate data type. This notation, which specifies the type stored in the vector, is similar to the template notation that Section 6.18 introduced with function templates. Member Function size; Function outputVector Line 17 uses vector member function size to obtain the size (i.e., the number of elements) of integers1. Line 19 passes integers1 to function outputVector (lines 88–102), which uses square brackets, [] (line 94), to obtain the value in each element of the vector for output. Note the resemblance of this notation to that used to access the value of an array element. Lines 22 and 24 perform the same tasks for integers2. Member function size of class template vector returns the number of elements in a vector as a value of type size_t (which represents the type unsigned int on many systems). As a result, line 90 declares the control variable i to be of type size_t, too. On some compilers, declaring i as an int causes the compiler to issue a warning message, since the loop-continuation condition (line 92) would compare a signed value (i.e., int i) and an unsigned value (i.e., a value of type size_t returned by function size).

vector

Function inputVector Lines 28–29 pass integers1 and integers2 to function inputVector (lines 105–109) to read values for each vector’s elements from the user. The function uses square brackets ([]) to form lvalues that are used to store the input values in each vector element. Comparing vector Objects for Inequality Line 40 demonstrates that vector objects can be compared with one another using the != operator. If the contents of two vectors are not equal, the operator returns true; otherwise, it returns false. Initializing One vector with the Contents of Another The C++ Standard Library class template vector allows you to create a new vector object that is initialized with the contents of an existing vector. Line 45 creates a vector object integers3 and initializes it with a copy of integers1. This invokes vector’s so-called copy constructor to perform the copy operation. You’ll learn about copy constructors in detail in Chapter 11. Lines 47–49 output the size and contents of integers3 to demonstrate that it was initialized correctly.

312

Chapter 7 Arrays and Vectors

Assigning vectors and Comparing vectors for Equality Line 53 assigns integers2 to integers1, demonstrating that the assignment (=) operator can be used with vector objects. Lines 55–58 output the contents of both objects to show that they now contain identical values. Line 63 then compares integers1 to integers2 with the equality (==) operator to determine whether the contents of the two objects are equal after the assignment in line 53 (which they are). Using the [] Operator to Access and Modify vector Elements Lines 67 and 71 use square brackets ([]) to obtain a vector element as an rvalue and as an lvalue, respectively. Recall from Section 5.9 that an rvalue cannot be modified, but an lvalue can. As is the case with C-style pointer-based arrays, C++ does not perform any bounds checking when vector elements are accessed with square brackets. Therefore, you must ensure that operations using [] do not accidentally attempt to manipulate elements outside the bounds of the vector. Standard class template vector does, however, provide bounds checking in its member function at, which we use at line 79 and discuss shortly. Exception Handling: Processing an Out-of-Range Subscript An exception indicates a problem that occurs while a program executes. The name “exception” suggests that the problem occurs infrequently—if the “rule” is that a statement normally executes correctly, then the problem represents the “exception to the rule.” Exception handling enables you to create fault-tolerant programs that can resolve (or handle) exceptions. In many cases, this allows a program to continue executing as if no problems were encountered. For example, Fig. 7.25 still runs to completion, even though an attempt was made to access an out-of-range subscript. More severe problems might prevent a program from continuing normal execution, instead requiring the program to notify the user of the problem, then terminate. When a function detects a problem, such as an invalid array subscript or an invalid argument, it throws an exception—that is, an exception occurs. Here we introduce exception handling briefly. We’ll discuss it in detail in Chapter 16, Exception Handling: A Deeper Look. The try Statement To handle an exception, place any code that might throw an exception in a try statement (lines 76–84). The try block (lines 76–80) contains the code that might throw an exception, and the catch block (lines 81–84) contains the code that handles the exception if one occurs. You can have many catch blocks to handle different types of exceptions that might be thrown in the corresponding try block. If the code in the try block executes successfully, lines 81–84 are ignored. The braces that delimit try and catch blocks’ bodies are required. The vector member function at provides bounds checking and throws an exception if its argument is an invalid subscript. By default, this causes a C++ program to terminate. If the subscript is valid, function at returns the element at the specified location as a modifiable lvalue or an unmodifiable lvalue, depending on the context in which the call appears. An unmodifiable lvalue is an expression that identifies an object in memory (such as an element in a vector), but cannot be used to modify that object. Executing the catch Block When the program calls vector member function at with the argument 15 (line 79), the function attempts to access the element at location 15, which is outside the vector’s bounds—integers1 has only 10 elements at this point. Because bounds checking is per-

7.12 Wrap-Up

313

formed at execution time, vector member function at generates an exception—specifically line 79 throws an out_of_range exception (from header ) to notify the program of this problem. At this point, the try block terminates immediately and the catch block begins executing—if you declared any variables in the try block, they’re now out of scope and are not accessible in the catch block. [Note: To avoid compilation errors with GNU C++, you may need to include header to use class out_of_range.] The catch block declares a type (out_of_range) and an exception parameter (ex) that it receives as a reference. The catch block can handle exceptions of the specified type. Inside the block, you can use the parameter’s identifier to interact with a caught exception object.

Member Function of the Exception Parameter When lines 81–84 catch the exception, the program displays a message indicating the problem that occurred. Line 83 calls the exception object’s what member function to get the error message that is stored in the exception object and display it. Once the message is displayed in this example, the exception is considered handled and the program continues with the next statement after the catch block’s closing brace. In this example, the end of the program is reached, so the program terminates. We use exception handling again in Chapters 9–13 and Chapter 16 presents a deeper look at exception handling. what

Summary of This Example In this section, we demonstrated the C++ Standard Library class template vector, a robust, reusable class that can replace C-style pointer-based arrays. In Chapter 11, you’ll see that vector achieves many of its capabilities by “overloading” C++’s built-in operators, and you’ll learn how to customize operators for use with your own classes in similar ways. For example, we create an Array class that, like class template vector, improves upon basic array capabilities. Our Array class also provides additional features, such as the ability to input and output entire arrays with operators >> and <<, respectively.

7.12 Wrap-Up This chapter began our introduction to data structures, exploring the use of arrays and to store data in and retrieve data from lists and tables of values. The chapter examples demonstrated how to declare an array, initialize an array and refer to individual elements of an array. We also illustrated how to pass arrays to functions and how to use the const qualifier to enforce the principle of least privilege. Chapter examples also presented basic searching and sorting techniques. You learned how to declare and manipulate multidimensional arrays. Finally, we demonstrated the capabilities of C++ Standard Library class template vector, which provides a more robust alternative to arrays. In that example, we also discussed basic exception-handling concepts. We continue our coverage of data structures in Chapter 14, Templates, where we build a stack class template and in Chapter 20, Custom Templatized Data Structures, which introduces other dynamic data structures, such as lists, queues, stacks and trees, that can grow and shrink as programs execute. Chapter 22 introduces several of the C++ Standard Library’s predefined data structures, which you can use instead of building their own. Chapter 22 presents the full functionality of class template vector and discusses many additional data structure classes, including list and deque, which are array-like data structures that can grow and shrink in response to a program’s changing storage requirements. vectors

314

Chapter 7 Arrays and Vectors

We’ve now introduced the basic concepts of classes, objects, control statements, functions and arrays. In Chapter 8, we present one of C++’s most powerful features—the pointer. Pointers keep track of where data and functions are stored in memory, which allows us to manipulate those items in interesting ways. After introducing basic pointer concepts, we examine in detail the close relationship among arrays, pointers and strings.

Summary Section 7.1 Introduction

• Data structures (p. 268) are collections of related data items. Arrays (p. 268) are data structures consisting of related data items of the same type. Arrays are “static” entities in that they remain the same size throughout program execution. (They may, of course, be of automatic storage class and hence be created and destroyed each time the blocks in which they’re defined are entered and exited.)

Section 7.2 Arrays

• An array is a consecutive group of memory locations that share the same type. • To refer to a particular location or element in an array, we specify the name of the array (p. 269) and the position number of the particular element in the array. • A program refers to any one of an array’s elements by giving the name of the array followed by the index (p. 269) of the particular element in square brackets ([]). • The first element in every array has index zero (p. 269) and is sometimes called the zeroth element. • An index must be an integer or integer expression (using any integral type). • The brackets used to enclose the index are an operator with the same precedence as parentheses.

Section 7.3 Declaring Arrays

• Arrays occupy space in memory. You specify the type of each element and the number of elements required by an array as follows: type arrayName[ arraySize

];

and the compiler reserves the appropriate amount of memory. • Arrays can be declared to contain any data type. For example, an array of type char can be used to store a character string.

Section 7.4 Examples Using Arrays

• The elements of an array can be initialized in the array declaration by following the array name with an equals sign and an initializer list (p. 272)—a comma-separated list (enclosed in braces) of constant initializers (p. 272). When initializing an array with an initializer list, if there are fewer initializers than elements in the array, the remaining elements are initialized to zero. • If the array size is omitted from a declaration with an initializer list, the compiler determines the number of elements in the array by counting the number of elements in the initializer list. • If the array size and an initializer list are specified in an array declaration, the number of initializers must be less than or equal to the array size. • Constants must be initialized with a constant expression when they’re declared and cannot be modified thereafter. Constants can be placed anywhere a constant expression is expected. • C++ has no array bounds checking (p. 280). You should ensure that all array references remain within the bounds of the array.

Summary

315

• A static local variable in a function definition exists for the duration of the program but is visible only in the function body. • A program initializes static local arrays when their declarations are first encountered. If a static array is not initialized explicitly by you, each element of that array is initialized to zero by the compiler when the array is created.

Section 7.5 Passing Arrays to Functions

• To pass an array argument to a function, specify the name of the array without any brackets. To pass an element of an array to a function, use the subscripted name of the array element as an argument in the function call. • Arrays are passed to functions by reference—the called functions can modify the element values in the callers’ original arrays. The value of the name of the array is the address in the computer’s memory of the first element of the array. Because the starting address of the array is passed, the called function knows precisely where the array is stored in memory. • Individual array elements are passed by value exactly as simple variables are. • To receive an array argument, a function’s parameter list must specify that the function expects to receive an array. The size of the array is not required between the array brackets. • The type qualifier const (p. 273) can be used to prevent modification of array values in the caller by code in a called function. When an array parameter is preceded by the const qualifier, the elements of the array become constant in the function body, and any attempt to modify an element of the array in the function body results in a compilation error.

Section 7.6 Case Study: Class GradeBook Using an Array to Store Grades

• Class variables (static data members; p. 290) are shared by all objects of the class in which the variables are declared. • A static data member can be accessed within the class definition and the member-function definitions like any other data member. • A public static data member can also be accessed outside of the class, even when no objects of the class exist, using the class name followed by the scope resolution operator (::) and the name of the data member.

Section 7.7 Searching Arrays with Linear Search

• The linear search (p. 293) compares each array element with a search key (p. 293). Because the array is not in any particular order, it’s just as likely that the value will be found in the first element as the last. On average, a program must compare the search key with half the array elements. To determine that a value is not in the array, the program must compare the search key to every element in the array.

Section 7.8 Sorting Arrays with Insertion Sort

• An array can be sorted using insertion sort (p. 295). The first iteration of this algorithm takes the second element and, if it’s less than the first element, swaps it with the first element (i.e., the program inserts the second element in front of the first element). The second iteration looks at the third element and inserts it into the correct position with respect to the first two elements, so all three elements are in order. At the i th iteration of this algorithm, the first i elements in the original array will be sorted. For small arrays, the insertion sort is acceptable, but for larger arrays it’s inefficient compared to other more sophisticated sorting algorithms.

Section 7.9 Multidimensional Arrays

• Multidimensional arrays (p. 297) with two dimensions are often used to represent tables of values (p. 297) consisting of information arranged in rows and columns.

316

Chapter 7 Arrays and Vectors

• Arrays that require two subscripts to identify a particular element are called two-dimensional arrays (p. 297). An array with m rows and n columns is called an m-by-n array (p. 297).

Section 7.11 Introduction to C++ Standard Library Class Template vector

• C++ Standard Library class template vector (p. 307) represents a more robust alternative to arrays featuring many capabilities that are not provided for C-style pointer-based arrays. • By default, all the elements of an integer vector object are set to 0. • A vector can be defined to store any data type using a declaration of the form vector<

type

>

name( size );

• Member function size (p. 311) of class template vector returns the number of elements in the vector on which it’s invoked. • The value of an element of a vector can be accessed or modified using square brackets ([]). • Objects of standard class template vector can be compared directly with the equality (==) and inequality (!=) operators. The assignment (=) operator can also be used with vector objects. • An unmodifiable lvalue is an expression that identifies an object in memory (such as an element in a vector), but cannot be used to modify that object. A modifiable lvalue also identifies an object in memory, but can be used to modify the object. • An exception (p. 312) indicates a problem that occurs while a program executes. The name “exception” suggests that the problem occurs infrequently—if the “rule” is that a statement normally executes correctly, then the problem represents the “exception to the rule.” • Exception handling (p. 312) enables you to create fault-tolerant programs (p. 312) that can resolve exceptions. • To handle an exception, place any code that might throw an exception (p. 312) in a try statement. • The try block (p. 312) contains the code that might throw an exception, and the catch block (p. 312) contains the code that handles the exception if one occurs. • When a try block terminates any variables declared in the try block go out of scope. • A catch block (p. 312) declares a type and an exception parameter. Inside the catch block, you can use the parameter’s identifier to interact with a caught exception object. • An exception object’s what method (p. 313) returns the exception’s error message.

Self-Review Exercises 7.1

(Fill in the Blanks) Answer each of the following: a) Lists and tables of values can be stored in or . b) The elements of an array are related by the fact that they have the same and . c) The number used to refer to a particular element of an array is called its . d) A(n) should be used to declare the size of an array, because it makes the program more scalable. e) The process of placing the elements of an array in order is called the array. f) The process of determining if an array contains a particular key value is called the array. g) An array that uses two subscripts is referred to as a(n) array.

7.2

(True or False) State whether the following are true or false. If the answer is false, explain why. a) An array can store many different types of values. b) An array subscript should normally be of data type float.

Answers to Self-Review Exercises

317

c) If there are fewer initializers in an initializer list than the number of elements in the array, the remaining elements are initialized to the last value in the initializer list. d) It’s an error if an initializer list has more initializers than there are elements in the array. e) An individual array element that is passed to a function and modified in that function will contain the modified value when the called function completes execution. 7.3 (Write C++ Statements) Write one or more statements that perform the following tasks for an array called fractions: a) Define a constant integer variable arraySize initialized to 10. b) Declare an array with arraySize elements of type double, and initialize the elements to 0. c) Name the fourth element of the array. d) Refer to array element 4. e) Assign the value 1.667 to array element 9. f) Assign the value 3.333 to the seventh element of the array. g) Print array elements 6 and 9 with two digits of precision to the right of the decimal point, and show the output that is actually displayed on the screen. h) Print all the array elements using a for statement. Define the integer variable i as a control variable for the loop. Show the output. 7.4

(Double Array Questions) Answer the following questions regarding an array called table: a) Declare the array to be an integer array and to have 3 rows and 3 columns. Assume that the constant variable arraySize has been defined to be 3. b) How many elements does the array contain? c) Use a for statement to initialize each element of the array to the sum of its subscripts. Assume that the integer variables i and j are declared as control variables. d) Write a program segment to print the values of each element of array table in tabular format with 3 rows and 3 columns. Assume that the array was initialized with the declaration int table[ arraySize ][ arraySize ] = { { 1, 8 }, { 2, 4, 6 }, { 5 } };

and the integer variables i and j are declared as control variables. Show the output. 7.5 error:

(Find the Error) Find the error in each of the following program segments and correct the a) #include ; b) arraySize = 10; // arraySize was c) Assume that int b[ 10 ] = {};

declared const

for ( int i = 0; i <= 10; ++i ) b[ i ] = 1;

d) Assume that int

a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };

a[ 1, 1 ] = 5;

Answers to Self-Review Exercises 7.1 a) arrays, vectors. b) array name, type. c) subscript or index. d) constant variable. e) sorting. f) searching. g) two-dimensional. 7.2

a) b) c) d) e)

False. An array can store only values of the same type. False. An array subscript should be an integer or an integer expression. False. The remaining elements are initialized to zero. True. False. Individual elements of an array are passed by value. If the entire array is passed to a function, then any modifications to the elements will be reflected in the original.

318 7.3

Chapter 7 Arrays and Vectors a) b) c) d) e) f) g)

const int arraySize = 10; double fractions[ arraySize ] = { 0.0 }; fractions[ 3 ] fractions[ 4 ] fractions[ 9 ] = 1.667; fractions[ 6 ] = 3.333; cout << fixed << setprecision( 2 ); cout << fractions[ 6 ] << ' ' << fractions[ 9 ] << endl;

h)

Output:

3.33 1.67.

for ( int i = 0; i < arraySize; ++i ) cout << "fractions[" << i << "] = " << fractions[ i ] << endl;

Output:

fractions[ 0 ] = 0.0 fractions[ 1 ] = 0.0 fractions[ 2 ] = 0.0 fractions[ 3 ] = 0.0 fractions[ 4 ] = 0.0 fractions[ 5 ] = 0.0 fractions[ 6 ] = 3.333 fractions[ 7 ] = 0.0 fractions[ 8 ] = 0.0 fractions[ 9 ] = 1.667

7.4

a) int table[ arraySize ][ arraySize b) Nine. c) for ( i = 0; i < arraySize; ++i )

];

for ( j = 0; j < arraySize; ++j )

d)

table[ i ][ j ] = i + j; cout << "

[0]

[1]

[2]" << endl;

for ( int i = 0; i < arraySize; ++i ) { cout << '[' << i << "] "; for ( int j = 0; j < arraySize; ++j ) cout << setw( 3 ) << table[ i ][ j ] << "

";

cout << endl; }

Output:

7.5

[0]

[1]

[2]

[0]

1

8

0

[1]

2

4

6

[2]

5

0

0

a) Error: Semicolon at end of #include preprocessor directive. Correction: Eliminate semicolon. b) Error: Assigning a value to a constant variable using an assignment statement. Correction: Initialize the constant variable in a const int arraySize declaration. c) Error: Referencing an array element outside the bounds of the array (b[10]). Correction: Change the final value of the control variable to 9 or change <= to <. d) Error: Array subscripting done incorrectly. Correction: Change the statement to a[ 1 ][ 1 ] = 5;

Exercises

319

Exercises 7.6

(Fill in the Blanks) Fill in the blanks in each of the following: , , a) The names of the four elements of array p (int p[4];) are and . b) Naming an array, stating its type and specifying the number of elements in the array is called the array. c) By convention, the first subscript in a two-dimensional array identifies an element’s and the second subscript identifies an element’s . rows, columns and elements. d) An m-by-n array contains e) The name of the element in row 3 and column 5 of array d is .

7.7

(True or False) Determine whether each of the following is true or false. If false, explain why. a) To refer to a particular location or element within an array, we specify the name of the array and the value of the particular element. b) An array definition reserves space for an array. c) To indicate reserve 100 locations for integer array p, you write the declaration p[ 100 ];

d) A for statement must be used to initialize the elements of a 15-element array to zero. e) Nested for statements must be used to total the elements of a two-dimensional array. 7.8

(Write C++ Statements) Write C++ statements to accomplish each of the following: a) Display the value of element 6 of character array f. b) Input a value into element 4 of one-dimensional floating-point array b. c) Initialize each of the 5 elements of one-dimensional integer array g to 8. d) Total and print the elements of floating-point array c of 100 elements. e) Copy array a into the first portion of array b. Assume double a[ 11 ], b[ 34 ]; f) Determine and print the smallest and largest values contained in 99-element floatingpoint array w.

7.9

(Double Array Questions) Consider a 2-by-3 integer array t. a) Write a declaration for t. b) How many rows does t have? c) How many columns does t have? d) How many elements does t have? e) Write the names of all the elements in row 1 of t. f) Write the names of all the elements in column 2 of t. g) Write a statement that sets the element of t in the first row and second column to zero. h) Write a series of statements that initialize each element of t to zero. Do not use a loop. i) Write a nested for statement that initializes each element of t to zero. j) Write a statement that inputs the values for the elements of t from the keyboard. k) Write a series of statements that determine and print the smallest value in array t. l) Write a statement that displays the elements in row 0 of t. m) Write a statement that totals the elements in column 3 of t. n) Write a series of statements that prints the array t in neat, tabular format. List the column subscripts as headings across the top and list the row subscripts at the left of each row.

7.10 (Salesperson Salary Ranges) Use a one-dimensional array to solve the following problem. A company pays its salespeople on a commission basis. The salespeople each receive $200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9 percent of $5000, or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):

320

Chapter 7 Arrays and Vectors a) b) c) d) e) f) g) h) i)

$200–299 $300–399 $400–499 $500–599 $600–699 $700–799 $800–899 $900–999 $1000 and over

7.11 (Bubble Sort) In the bubble sort algorithm, smaller values gradually “bubble” their way upward to the top of the array like air bubbles rising in water, while the larger values sink to the bottom. The bubble sort makes several passes through the array. On each pass, successive pairs of elements are compared. If a pair is in increasing order (or the values are identical), we leave the values as they are. If a pair is in decreasing order, their values are swapped in the array. The comparisons on each pass proceed as follows—the 0th element value is compared to the 1st, the 1st is compared to the 2nd, the 2nd is compared to the third, ..., the second-to-last element is compared to the last element. Write a program that sorts an array of 10 integers using bubble sort. 7.12 (Bubble Sort Enhancements) The bubble sort described in Exercise 7.11 is inefficient for large arrays. Make the following simple modifications to improve the performance of the bubble sort: a) After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are “in place,” and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass, and so on. b) The data in the array may already be in the proper order or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass if any swaps have been made. If none have been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed. 7.13 (Single Array Questions) Write single statements that perform the following one-dimensional array operations: a) Initialize the 10 elements of integer array counts to zero. b) Add 1 to each of the 15 elements of integer array bonus. c) Read 12 values for double array monthlyTemperatures from the keyboard. d) Print the 5 values of integer array bestScores in column format. 7.14

(Find the Errors) Find the error(s) in each of the following statements: a) Assume that: int a[ 3 ]; cout << a[ 1 ] << " " << a[ 2 ] << " " << a[ 3 ] << endl;

b) double f[ 3 ] = { 1.1, 10.01, c) Assume that: double d[ 2 ][ 10

100.001, 1000.0001 }; ];

d[ 1, 9 ] = 2.345;

7.15 (Duplicate Elimination) Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it isn’t a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem. 7.16 (Double Array Initialization) Label the elements of a 3-by-5 two-dimensional array sales to indicate the order in which they’re set to zero by the following program segment:

Exercises

321

for ( row = 0; row < 3; ++row ) for ( column = 0; column < 5; ++column ) sales[ row ][ column ] = 0;

7.17 (Dice Rolling) Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] Figure 7.26 shows the 36 possible combinations of the two dice. Your program should roll the two dice 36,000 times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable (i.e., there are six ways to roll a 7, so approximately one-sixth of all the rolls should be 7). 1

2

3

4

5

6

1

2

3

4

5

6

7

2

3

4

5

6

7

8

3

4

5

6

7

8

9

4

5

6

7

8

9 10

5

6

7

8

9 10 11

6

7

8

9 10 11 12

Fig. 7.26 | The 36 possible outcomes of rolling two dice. 7.18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

(What Does This Code Do?) What does the following program do?

// Ex. 7.18: Ex07_18.cpp // What does this program do? #include using namespace std; int whatIsThis( int [], int ); // function prototype int main() { const int arraySize = 10; int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int result = whatIsThis( a, arraySize ); cout << "Result is " << result << endl; } // end main // What does this function do? int whatIsThis( int b[], int size ) { if ( size == 1 ) // base case return b[ 0 ]; else // recursive step return b[ size - 1 ] + whatIsThis( b, size - 1 ); } // end function whatIsThis

322

Chapter 7 Arrays and Vectors

7.19 (Craps Game Modification) Modify the program of Fig. 6.11 to play 1000 games of craps. The program should keep track of the statistics and answer the following questions: a) How many games are won on the 1st roll, 2nd roll, …, 20th roll, and after the 20th roll? b) How many games are lost on the 1st roll, 2nd roll, …, 20th roll, and after the 20th roll? c) What are the chances of winning at craps? [Note: You should discover that craps is one of the fairest casino games. What do you suppose this means?] d) What’s the average length of a game of craps? e) Do the chances of winning improve with the length of the game? 7.20 (Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. You’ve been asked to program the new system. You are to write a program to assign seats on each flight of the airline’s only plane (capacity: 10 seats). Your program should display the following menu of alternatives—Please type 1 for "First Class" and Please type 2 for "Economy". If the person types 1, your program should assign a seat in the first class section (seats 1–5). If the person types 2, your program should assign a seat in the economy section (seats 6–10). Your program should print a boarding pass indicating the person’s seat number and whether it’s in the first class or economy section of the plane. Use a one-dimensional array to represent the seating chart of the plane. Initialize all the elements of the array to false to indicate that all seats are empty. As each seat is assigned, set the corresponding elements of the array to true to indicate that the seat is no longer available. Your program should, of course, never assign a seat that has already been assigned. When the first class section is full, your program should ask the person if it’s acceptable to be placed in the economy section (and vice versa). If yes, then make the appropriate seat assignment. If no, then print the message "Next flight leaves in 3 hours." 7.21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

(What Does This Code Do?) What does the following program do?

// Ex. 7.21: Ex07_21.cpp // What does this program do? #include using namespace std; void someFunction( int [], int, int ); // function prototype int main() { const int arraySize = 10; int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; cout << "The values in the array are:" << endl; someFunction( a, 0, arraySize ); cout << endl; } // end main // What does this function do? void someFunction( int b[], int current, int size ) { if ( current < size ) { someFunction( b, current + 1, size ); cout << b[ current ] << " "; } // end if } // end function someFunction

Exercises

323

7.22 (Sales Summary) Use a two-dimensional array to solve the following problem. A company has four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each different type of product sold. Each slip contains the following: a) The salesperson number b) The product number c) The total dollar value of that product sold that day Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Write a program that will read all this information for last month’s sales (one salesperson’s data at a time) and summarize the total sales by salesperson by product. All totals should be stored in the two-dimensional array sales. After processing all the information for last month, print the results in tabular format with each of the columns representing a particular salesperson and each of the rows representing a particular product. Cross total each row to get the total sales of each product for last month; cross total each column to get the total sales by salesperson for last month. Your tabular printout should include these cross totals to the right of the totaled rows and to the bottom of the totaled columns. 7.23 (Turtle Graphics) The Logo language, which is popular among elementary school children, made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a C++ program. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this problem, you’ll simulate the operation of the turtle and create a computerized sketchpad as well. Use a 20-by-20 array floor that is initialized to false. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its pen up. The set of turtle commands your program must process are shown in Fig. 7.27.

Command

Meaning

1 2 3 4 5,10

Pen up Pen down Turn right Turn left Move forward 10 spaces (or a number other than 10) Print the 20-by-20 array End of data (sentinel)

6 9

Fig. 7.27 | Turtle graphics commands. Suppose that the turtle is somewhere near the center of the floor. The following “program” would draw and print a 12-by-12 square and end with the pen in the up position: 2 5,12 3 5,12 3 5,12 3

324

Chapter 7 Arrays and Vectors 5,12 1 6 9

As the turtle moves with the pen down, set the appropriate elements of array floor to true. When the 6 command (print) is given, wherever there is a true in the array, display an asterisk or some other character you choose. Wherever there is a zero, display a blank. Write a program to implement the turtle graphics capabilities discussed here. Write several turtle graphics programs to draw interesting shapes. Add other commands to increase the power of your turtle graphics language. 7.24 (Knight’s Tour) One of the more interesting puzzlers for chess buffs is the Knight’s Tour problem. The question is this: Can the chess piece called the knight move around an empty chessboard and touch each of the 64 squares once and only once? We study this intriguing problem in depth in this exercise. The knight makes L-shaped moves (over two in one direction then over one in a perpendicular direction). Thus, from a square in the middle of an empty chessboard, the knight can make eight different moves (numbered 0 through 7) as shown in Fig. 7.28. a) Draw an 8-by-8 chessboard on a sheet of paper and attempt a Knight’s Tour by hand. Put a 1 in the first square you move to, a 2 in the second square, a 3 in the third, etc. Before starting the tour, estimate how far you think you’ll get, remembering that a full tour consists of 64 moves. How far did you get? Was this close to your estimate? b) Now let’s develop a program that will move the knight around a chessboard. The board is represented by an 8-by-8 two-dimensional array board. Each of the squares is initialized to zero. We describe each of the eight possible moves in terms of both their horizontal and vertical components. For example, a move of type 0, as shown in Fig. 7.28, consists of moving two squares horizontally to the right and one square vertically upward. Move 2 consists of moving one square horizontally to the left and two squares vertically upward. Horizontal moves to the left and vertical moves upward are indicated with negative numbers. The eight moves may be described by two one-dimensional arrays, horizontal and vertical, as follows: horizontal[ horizontal[ horizontal[ horizontal[ horizontal[ horizontal[ horizontal[ horizontal[

0 1 2 3 4 5 6 7

] ] ] ] ] ] ] ]

= = = = = = = =

2 1 -1 -2 -2 -1 1 2

0

1

vertical[ vertical[ vertical[ vertical[ vertical[ vertical[ vertical[ vertical[

2

3

4

5

0 1 2 3 4 5 6 7

6

0 1 2

2

0 K

3 4 5

1

3 4

7 5

6

6 7

Fig. 7.28 | The eight possible moves of the knight.

] ] ] ] ] ] ] ]

= = = = = = = =

7

-1 -2 -2 -1 1 2 2 1

Exercises

325

Let the variables currentRow and currentColumn indicate the row and column of the knight’s current position. To make a move of type moveNumber, where moveNumber is between 0 and 7, your program uses the statements currentRow += vertical[ moveNumber ]; currentColumn += horizontal[ moveNumber ];

Keep a counter that varies from 1 to 64. Record the latest count in each square the knight moves to. Remember to test each potential move to see if the knight has already visited that square, and, of course, test every potential move to make sure that the knight does not land off the chessboard. Now write a program to move the knight around the chessboard. Run the program. How many moves did the knight make? c) After attempting to write and run a Knight’s Tour program, you’ve probably developed some valuable insights. We’ll use these to develop a heuristic (or strategy) for moving the knight. Heuristics do not guarantee success, but a carefully developed heuristic greatly improves the chance of success. You may have observed that the outer squares are more troublesome than the squares nearer the center of the board. In fact, the most troublesome, or inaccessible, squares are the four corners. Intuition may suggest that you should attempt to move the knight to the most troublesome squares first and leave open those that are easiest to get to, so when the board gets congested near the end of the tour, there will be a greater chance of success. We may develop an “accessibility heuristic” by classifying each square according to how accessible it’s then always moving the knight to the square (within the knight’s Lshaped moves, of course) that is most inaccessible. We label a two-dimensional array accessibility with numbers indicating from how many squares each particular square is accessible. On a blank chessboard, each center square is rated as 8, each corner square is rated as 2 and the other squares have accessibility numbers of 3, 4 or 6 as follows: 2 3 4 4 4 4 3 2

3 4 6 6 6 6 4 3

4 6 8 8 8 8 6 4

4 6 8 8 8 8 6 4

4 6 8 8 8 8 6 4

4 6 8 8 8 8 6 4

3 4 6 6 6 6 4 3

2 3 4 4 4 4 3 2

Now write a version of the Knight’s Tour program using the accessibility heuristic. At any time, the knight should move to the square with the lowest accessibility number. In case of a tie, the knight may move to any of the tied squares. Therefore, the tour may begin in any of the four corners. [Note: As the knight moves around the chessboard, your program should reduce the accessibility numbers as more and more squares become occupied. In this way, at any given time during the tour, each available square’s accessibility number will remain equal to precisely the number of squares from which that square may be reached.] Run this version of your program. Did you get a full tour? Now modify the program to run 64 tours, one starting from each square of the chessboard. How many full tours did you get? d) Write a version of the Knight’s Tour program which, when encountering a tie between two or more squares, decides what square to choose by looking ahead to those squares reachable from the “tied” squares. Your program should move to the square for which the next move would arrive at a square with the lowest accessibility number. 7.25 (Knight’s Tour: Brute Force Approaches) In Exercise 7.24, we developed a solution to the Knight’s Tour problem. The approach used, called the “accessibility heuristic,” generates many solutions and executes efficiently.

326

Chapter 7 Arrays and Vectors

As computers continue increasing in power, we’ll be able to solve more problems with sheer computer power and relatively unsophisticated algorithms. This is the “brute force” approach to problem solving. a) Use random number generation to enable the knight to walk around the chessboard (in its legitimate L-shaped moves, of course) at random. Your program should run one tour and print the final chessboard. How far did the knight get? b) Most likely, the preceding program produced a relatively short tour. Now modify your program to attempt 1000 tours. Use a one-dimensional array to keep track of the number of tours of each length. When your program finishes attempting the 1000 tours, it should print this information in neat tabular format. What was the best result? c) Most likely, the preceding program gave you some “respectable” tours, but no full tours. Now “pull all the stops out” and simply let your program run until it produces a full tour. [Caution: This version of the program could run for hours on a powerful computer.] Once again, keep a table of the number of tours of each length, and print this table when the first full tour is found. How many tours did your program attempt before producing a full tour? How much time did it take? d) Compare the brute force version of the Knight’s Tour with the accessibility heuristic version. Which required a more careful study of the problem? Which algorithm was more difficult to develop? Which required more computer power? Could we be certain (in advance) of obtaining a full tour with the accessibility heuristic approach? Could we be certain (in advance) of obtaining a full tour with the brute force approach? Argue the pros and cons of brute force problem solving in general. 7.26 (Eight Queens) Another puzzler for chess buffs is the Eight Queens problem. Simply stated: Is it possible to place eight queens on an empty chessboard so that no queen is “attacking” any other, i.e., no two queens are in the same row, the same column, or along the same diagonal? Use the thinking developed in Exercise 7.24 to formulate a heuristic for solving the Eight Queens problem. Run your program. [Hint: It’s possible to assign a value to each square of the chessboard indicating how many squares of an empty chessboard are “eliminated” if a queen is placed in that square. Each of the corners would be assigned the value 22, as in Fig. 7.29. Once these “elimination numbers” are placed in all 64 squares, an appropriate heuristic might be: Place the next queen in the square with the smallest elimination number. Why is this strategy intuitively appealing?]

*

*

*

*

* * * * * *

*

*

*

*

*

*

* * * * * *

Fig. 7.29 | The 22 squares eliminated by placing a queen in the upper-left corner. 7.27 (Eight Queens: Brute Force Approaches) In this exercise, you’ll develop several brute-force approaches to solving the Eight Queens problem introduced in Exercise 7.26. a) Solve the Eight Queens exercise, using the random brute force technique developed in Exercise 7.25.

Exercises

327

b) Use an exhaustive technique, i.e., try all possible combinations of eight queens on the chessboard. c) Why do you suppose the exhaustive brute force approach may not be appropriate for solving the Knight’s Tour problem? d) Compare and contrast the random brute force and exhaustive brute force approaches in general. 7.28 (Knight’s Tour: Closed-Tour Test) In the Knight’s Tour, a full tour occurs when the knight makes 64 moves touching each square of the chess board once and only once. A closed tour occurs when the 64th move is one move away from the location in which the knight started the tour. Modify the Knight’s Tour program you wrote in Exercise 7.24 to test for a closed tour if a full tour has occurred. 7.29 (The Sieve of Eratosthenes) A prime integer is any integer that is evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows: a) Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain 1. All other array elements will eventually be set to zero. You’ll ignore elements 0 and 1 in this exercise. b) Starting with array subscript 2, every time an array element is found whose value is 1, loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the element with value 1. For array subscript 2, all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4, 6, 8, 10, etc.); for array subscript 3, all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts 6, 9, 12, 15, etc.); and so on. When this process is complete, the array elements that are still set to one indicate that the subscript is a prime number. These subscripts can then be printed. Write a program that uses an array of 1000 elements to determine and print the prime numbers between 2 and 999. Ignore element 0 of the array. 7.30 (Bucket Sort) A bucket sort begins with a one-dimensional array of positive integers to be sorted and a two-dimensional array of integers with rows subscripted from 0 to 9 and columns subscripted from 0 to n – 1, where n is the number of values in the array to be sorted. Each row of the two-dimensional array is referred to as a bucket. Write a function bucketSort that takes an integer array and the array size as arguments and performs as follows: a) Place each value of the one-dimensional array into a row of the bucket array based on the value’s ones digit. For example, 97 is placed in row 7, 3 is placed in row 3 and 100 is placed in row 0. This is called a “distribution pass.” b) Loop through the bucket array row by row, and copy the values back to the original array. This is called a “gathering pass.” The new order of the preceding values in the onedimensional array is 100, 3 and 97. c) Repeat this process for each subsequent digit position (tens, hundreds, thousands, etc.). On the second pass, 100 is placed in row 0, 3 is placed in row 0 (because 3 has no tens digit) and 97 is placed in row 9. After the gathering pass, the order of the values in the one-dimensional array is 100, 3 and 97. On the third pass, 100 is placed in row 1, 3 is placed in row zero and 97 is placed in row zero (after the 3). After the last gathering pass, the original array is now in sorted order. Note that the two-dimensional array of buckets is 10 times the size of the integer array being sorted. This sorting technique provides better performance than an insertion sort, but requires much more memory. The insertion sort requires space for only one additional element of data. This is an example of the space-time trade-off: The bucket sort uses more memory than the insertion sort, but performs better. This version of the bucket sort requires copying all the data back to the original array on each pass. Another possibility is to create a second two-dimensional bucket array and repeatedly swap the data between the two bucket arrays.

328

Chapter 7 Arrays and Vectors

Recursion Exercises 7.31 (Selection Sort) A selection sort searches an array looking for the smallest element. Then, the smallest element is swapped with the first element of the array. The process is repeated for the subarray beginning with the second element of the array. Each pass of the array results in one element being placed in its proper location. This sort performs comparably to the insertion sort—for an array of n elements, n – 1 passes must be made, and for each subarray, n – 1 comparisons must be made to find the smallest value. When the subarray being processed contains one element, the array is sorted. Write recursive function selectionSort to perform this algorithm. 7.32 (Palindromes) A palindrome is a string that is spelled the same way forward and backward. Examples of palindromes include “radar” and “able was i ere i saw elba.” Write a recursive function testPalindrome that returns true if a string is a palindrome, and false otherwise. Note that like an array, the square brackets ([]) operator can be used to iterate through the characters in a string. 7.33 (Linear Search) Modify the program in Fig. 7.18 to use recursive function linearSearch to perform a linear search of the array. The function should receive an integer array and the size of the array as arguments. If the search key is found, return the array subscript; otherwise, return –1. 7.34 (Eight Queens) Modify the Eight Queens program you created in Exercise 7.26 to solve the problem recursively. 7.35 (Print an Array) Write a recursive function printArray that takes an array, a starting subscript and an ending subscript as arguments, returns nothing and prints the array. The function should stop processing and return when the starting subscript equals the ending subscript. 7.36 (Print a String Backward) Write a recursive function stringReverse that takes a string and a starting subscript as arguments, prints the string backward and returns nothing. The function should stop processing and return when the end of the string is encountered. Note that like an array the square brackets ([]) operator can be used to iterate through the characters in a string. 7.37 (Find the Minimum Value in an Array) Write a recursive function recursiveMinimum that takes an integer array, a starting subscript and an ending subscript as arguments, and returns the smallest element of the array. The function should stop processing and return when the starting subscript equals the ending subscript.

vector Exercises 7.38 (Salesperson Salary Ranges with vector) Use a vector of integers to solve the problem described in Exercise 7.10. 7.39 (Dice Rolling with vector) Modify the dice-rolling program you created in Exercise 7.17 to use a vector to store the numbers of times each possible sum of the two dice appears. 7.40 (Find the Minimum Value in a vector) Modify your solution to Exercise 7.37 to find the minimum value in a vector instead of an array.

Making a Difference 7.41 (Polling) The Internet and the web are enabling more people to network, join a cause, voice opinions, and so on. The presidential candidates in 2008 used the Internet intensively to get out their messages and raise money for their campaigns. In this exercise, you’ll write a simple polling program that allows users to rate five social-consciousness issues from 1 (least important) to 10 (most important). Pick five causes that are important to you (e.g., political issues, global environmental issues). Use a one-dimensional array topics (of type string) to store the five causes. To summarize the survey responses, use a 5-row, 10-column two-dimensional array responses (of type int), each row corresponding to an element in the topics array. When the program runs, it should

Making a Difference

329

ask the user to rate each issue. Have your friends and family respond to the survey. Then have the program display a summary of the results, including: a) A tabular report with the five topics down the left side and the 10 ratings across the top, listing in each column the number of ratings received for each topic. b) To the right of each row, show the average of the ratings for that issue. c) Which issue received the highest point total? Display both the issue and the point total. d) Which issue received the lowest point total? Display both the issue and the point total.

8 Addresses are given to us to conceal our whereabouts. —Saki (H. H. Munro)

By indirection find direction out. —William Shakespeare

Many things, having full reference To one consent, may work contrariously.

—William Shakespeare

You will find it a very good practice always to verify your references, sir! —Dr. Routh

Objectives In this chapter you’ll learn: ■ ■





■ ■



What pointers are. The similarities and differences between pointers and references, and when to use each. To use pointers to pass arguments to functions by reference. The close relationships between pointers and arrays. To use arrays of pointers. Basic pointer-based string processing. To use pointers to functions.

Pointers

8.1 Introduction 8.1 Introduction 8.2 Pointer Variable Declarations and Initialization 8.3 Pointer Operators 8.4 Pass-by-Reference with Pointers 8.5 Using const with Pointers 8.6 Selection Sort Using Pass-byReference 8.7 sizeof Operator

331

8.8 Pointer Expressions and Pointer Arithmetic 8.9 Relationship Between Pointers and Arrays 8.10 Pointer-Based String Processing 8.11 Arrays of Pointers 8.12 Function Pointers 8.13 Wrap-Up

Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Special Section: Building Your Own Computer

8.1 Introduction This chapter discusses one of the most powerful features of the C++ programming language, the pointer. In Chapter 6, we saw that references can be used to perform pass-byreference. Pointers also enable pass-by-reference and can be used to create and manipulate dynamic data structures that can grow and shrink, such as linked lists, queues, stacks and trees. This chapter explains basic pointer concepts and reinforces the intimate relationship among arrays and pointers. The view of arrays as pointers derives from the C programming language. As we saw in Chapter 7, the C++ Standard Library class vector provides an implementation of arrays as full-fledged objects. Similarly, C++ actually offers two types of strings—string class objects (which we’ve been using since Chapter 3) and C-style, pointer-based strings. This chapter on pointers briefly introduces C strings to deepen your knowledge of pointers. C strings are widely used in legacy C and C++ systems. We discuss C strings in depth in Chapter 21. In new software development projects, you should favor string class objects. We’ll examine the use of pointers with classes in Chapter 13, Object-Oriented Programming: Polymorphism, where we’ll see that the so-called “polymorphic processing” associated with object-oriented programming is performed with pointers and references. Chapter 20, Custom Templatized Data Structures, presents examples of creating and using dynamic data structures that are implemented with pointers.

8.2 Pointer Variable Declarations and Initialization Pointer variables contain memory addresses as their values. Normally, a variable directly contains a specific value. A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value (Fig. 8.1). Referencing a value through a pointer is called indirection. Diagrams typically represent a pointer as an arrow from the variable that contains an address to the variable located at that address in memory. Pointers, like any other variables, must be declared before they can be used. For example, for the pointer in Fig. 8.1, the declaration int *countPtr, count;

332

Chapter 8 Pointers

count 7 countPtr

count 7

count directly references a variable that contains the value 7

Pointer countPtr indirectly references a variable that contains the value 7

Fig. 8.1 | Directly and indirectly referencing a variable. declares the variable countPtr to be of type int * (i.e., a pointer to an int value) and is read (right to left), “countPtr is a pointer to int.” Also, variable count in the preceding declaration is declared to be an int, not a pointer to an int. The * in the declaration applies only to countPtr. Each variable being declared as a pointer must be preceded by an asterisk (*). For example, the declaration double *xPtr, *yPtr;

indicates that both xPtr and yPtr are pointers to double values. When * appears in a declaration, it isn’t an operator; rather, it indicates that the variable being declared is a pointer. Pointers can be declared to point to objects of any data type.

Common Programming Error 8.1

Assuming that the * used to declare a pointer distributes to all names in a declaration’s comma-separated list of variables can lead to errors. Each pointer must be declared with the * prefixed to the name (with or without spaces in between). Declaring only one variable per declaration helps avoid these types of errors and improves program readability.

Good Programming Practice 8.1

Although it isn’t a requirement, including the letters Ptr in a pointer variable name makes it clear that the variable is a pointer and that it must be handled accordingly.

Pointers should be initialized to 0, NULL or an address of the corresponding type either when they’re declared or in an assignment. A pointer with the value 0 or NULL “points to nothing” and is known as a null pointer. Symbolic constant NULL is defined in several standard library headers to represent the value 0. Initializing a pointer to NULL is equivalent to initializing a pointer to 0, but in C++, 0 is used by convention. When 0 is assigned, it’s converted to a pointer of the appropriate type. The value 0 is the only integer value that can be assigned directly to a pointer variable without first casting the integer to a pointer type. [Note: In the new standard, you should use the constant nullptr to initialize a pointer instead of 0 or NULL. Several C++ compilers already implement this constant.]

Error-Prevention Tip 8.1

Initialize pointers to prevent pointing to unknown or uninitialized areas of memory.

8.3 Pointer Operators The address operator (&) is a unary operator that obtains the memory address of its operand. For example, assuming the declarations

8.3 Pointer Operators

333

int y = 5; // declare variable y int *yPtr; // declare pointer variable yPtr

the statement yPtr = &y; // assign address of y to yPtr

assigns the address of the variable y to pointer variable yPtr. Then variable yPtr is said to “point to” y. Now, yPtr indirectly references variable y’s value. The use of the & in the preceding statement is not the same as the use of the & in a reference variable declaration, which is always preceded by a data-type name. When declaring a reference, the & is part of the type. In an expression like &y, the & is the address operator. Figure 8.2 shows a schematic representation of memory after the preceding assignment. The “pointing relationship” is indicated by drawing an arrow from the box that represents the pointer yPtr in memory to the box that represents the variable y in memory. yPtr

y 5

Fig. 8.2 | Graphical representation of a pointer pointing to a variable in memory. Figure 8.3 shows another pointer representation in memory with integer variable y stored at memory location 600000 and pointer variable yPtr stored at memory location 500000. The operand of the address operator must be an lvalue; the address operator cannot be applied to constants or to expressions that do not result in references. yPtr location 500000

600000

y location 600000

5

Fig. 8.3 | Representation of y and yPtr in memory. The * operator, commonly referred to as the indirection operator or dereferencing operator, returns a synonym (i.e., an alias or a nickname) for the object to which its pointer operand points. For example (referring again to Fig. 8.2), the statement cout << *yPtr << endl;

prints the value of variable y, namely, 5, just as the statement cout << y << endl;

would. Using * in this manner is called dereferencing a pointer. A dereferenced pointer may also be used on the left side of an assignment statement, as in *yPtr = 9;

which would assign 9 to y in Fig. 8.3. The dereferenced pointer may also be used to receive an input value as in cin >> *yPtr;

which places the input value in y. The dereferenced pointer is an lvalue.

334

Chapter 8 Pointers

Common Programming Error 8.2

Dereferencing an uninitialized pointer could cause a fatal execution-time error, or it could accidentally modify important data and allow the program to run to completion, possibly with incorrect results.

Common Programming Error 8.3

An attempt to dereference a variable that is not a pointer is a compilation error.

Common Programming Error 8.4

Dereferencing a null pointer is often a fatal execution-time error.

The program in Fig. 8.4 demonstrates the & and * pointer operators. Memory locations are output by << in this example as hexadecimal (i.e., base-16) integers. (See Appendix D, Number Systems, for more information on hexadecimal integers.) The hexadecimal memory addresses output by this program are compiler and operating-system dependent, so you may get different results when you run the program. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

// Fig. 8.4: fig08_04.cpp // Pointer operators & and *. #include using namespace std; int main() { int a; // a is an integer int *aPtr; // aPtr is an int * which is a pointer to an integer a = 7; // assigned 7 to a aPtr = &a; // assign the address of a to aPtr cout << "The address of a is " << &a << "\nThe value of aPtr is " << aPtr; cout << "\n\nThe value of a is " << a << "\nThe value of *aPtr is " << *aPtr; cout << "\n\nShowing that * and & are inverses of " << "each other.\n&*aPtr = " << &*aPtr << "\n*&aPtr = " << *&aPtr << endl; } // end main

The address of a is 0012F580 The value of aPtr is 0012F580 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 0012F580 *&aPtr = 0012F580

Fig. 8.4 | Pointer operators & and *.

8.4 Pass-by-Reference with Pointers

335

The address of a (line 14) and the value of aPtr (line 15) are identical in the output, confirming that the address of a is indeed assigned to the pointer variable aPtr. The & and * operators are inverses of one another—when they’re applied consecutively to aPtr in either order, they “cancel one another out” yielding the same result (the value in aPtr). Figure 8.5 lists the precedence and associativity of the operators introduced to this point. The address (&) and dereferencing operator (*) are unary operators on the third level. Operators

Associativity

Type

::

()

()

[]

++

--

static_cast(operand)

++

--

+

*

/

%

+

-

<<

>>

<

<=

==

!=

[See caution in Fig. 2.10] left to right left to right right to left left to right left to right left to right left to right left to right left to right left to right right to left right to left left to right

highest function call/array access unary (postfix) unary (prefix) multiplicative additive insertion/extraction relational equality logical AND logical OR conditional assignment comma

-

>

>=

-=

*=

!

&

/=

%=

&& || ?: = ,

+=

*

Fig. 8.5 | Operator precedence and associativity.

8.4 Pass-by-Reference with Pointers There are three ways in C++ to pass arguments to a function—pass-by-value, pass-by-reference with reference arguments and pass-by-reference with pointer arguments. Chapter 6 compared and contrasted pass-by-value and pass-by-reference with reference arguments. In this section, we explain pass-by-reference with pointer arguments. As we saw in Chapter 6, return can be used to return one value from a called function to a caller (or to simply return control). We also saw that arguments can be passed to a function using reference arguments. Such arguments enable the called function to modify the original values of the arguments in the caller. Reference arguments also enable programs to pass large data objects to a function and avoid the overhead of passing the objects by value (which, of course, requires making a copy of the object). Pointers, like references, also can be used to modify one or more variables in the caller or to pass pointers to large data objects to avoid the overhead of passing the objects by value. You can use pointers and the indirection operator (*) to accomplish pass-by-reference (exactly as pass-by-reference is done in C programs—C does not have references). When calling a function with an argument that should be modified, the address of the argument

336

Chapter 8 Pointers

is passed. This is normally accomplished by applying the address operator (&) to the name of the variable whose value will be modified. As we saw in Chapter 7, arrays are not passed using operator &, because the name of the array is the starting location in memory of the array (i.e., an array name is already a pointer). The name of the array arrayName is equivalent to &arrayName[0]. When the address of a variable is passed to a function, the indirection operator (*) can be used in the function to form a synonym for the name of the variable (i.e., an lvalue)—this in turn can be used to modify the variable’s value at that location in the caller’s memory. Figure 8.6 and Fig. 8.7 present two versions of a function that cubes an integer— cubeByValue and cubeByReference. Figure 8.6 passes variable number by value to function cubeByValue (line 14). Function cubeByValue (lines 19–22) cubes its argument and passes the new value back to main using a return statement (line 21). The new value is assigned to number (line 14) in main. The calling function has the opportunity to examine the function call’s result before modifying variable number’s value. For example, we could have stored the result of cubeByValue in another variable, examined its value and assigned the result to number only after determining that the returned value was reasonable. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

// Fig. 8.6: fig08_06.cpp // Pass-by-value used to cube a variable’s value. #include using namespace std; int cubeByValue( int ); // prototype int main() { int number = 5; cout << "The original value of number is " << number; number = cubeByValue( number ); // pass number by value to cubeByValue cout << "\nThe new value of number is " << number << endl; } // end main // calculate and return cube of integer argument int cubeByValue( int n ) { return n * n * n; // cube local variable n and return result } // end function cubeByValue

The original value of number is 5 The new value of number is 125

Fig. 8.6 | Pass-by-value used to cube a variable’s value. Figure 8.7 passes the variable number to function cubeByReference using pass-by-reference with a pointer argument (line 15)—the address of number is passed to the function. Function cubeByReference (lines 21–24) specifies parameter nPtr (a pointer to int) to receive its argument. The function dereferences the pointer and cubes the value to which nPtr points (line 23). This directly changes the value of number in main.

8.4 Pass-by-Reference with Pointers

337

Common Programming Error 8.5

Not dereferencing a pointer when it’s necessary to do so to obtain the value to which the pointer points is an error. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

// Fig. 8.7: fig08_07.cpp // Pass-by-reference with a pointer argument used to cube a // variable’s value. #include using namespace std; void cubeByReference( int * ); // prototype int main() { int number = 5; cout << "The original value of number is " << number; cubeByReference( &number ); // pass number address to cubeByReference cout << "\nThe new value of number is " << number << endl; } // end main // calculate cube of *nPtr; modifies variable number in main void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr } // end function cubeByReference

The original value of number is 5 The new value of number is 125

Fig. 8.7 | Pass-by-reference with a pointer argument used to cube a variable’s value. A function receiving an address as an argument must define a pointer parameter to receive the address. For example, the header for function cubeByReference (line 21) specifies that cubeByReference receives the address of an int variable (i.e., a pointer to an int) as an argument, stores the address locally in nPtr and does not return a value. The function prototype for cubeByReference (line 7) contains int * in parentheses. As with other variable types, it isn’t necessary to include the names of pointer parameters in function prototypes. Parameter names included for documentation purposes are ignored by the compiler. Figures 8.8–8.9 analyze graphically the execution of the programs in Fig. 8.6 and Fig. 8.7, respectively.

Software Engineering Observation 8.1

Use pass-by-value to pass arguments to a function unless the caller explicitly requires that the called function directly modify the value of the argument variable in the caller. This is another example of the principle of least privilege.

338

Chapter 8 Pointers

Step 1: Before main calls cubeByValue: int main() { int number = 5;

number 5

int cubeByValue( int n ) { return n * n * n; }

number = cubeByValue( number );

n

undefined

}

Step 2: After cubeByValue receives the call: number

int main() { int number = 5;

5

int cubeByValue( int n ) { return n * n * n; }

n

number = cubeByValue( number ); }

5

Step 3: After cubeByValue cubes parameter n and before cubeByValue returns to main: number

int main() { int number = 5;

5

int cubeByValue( int n ) { 125 return n * n * n; n

}

number = cubeByValue( number );

5

}

Step 4: After cubeByValue returns to main and before assigning the result to number: int main() { int number = 5;

number 5 125

int cubeByValue( int n ) { return n * n * n; }

number = cubeByValue( number );

n

undefined

}

Step 5: After main completes the assignment to number: number

int main() { int number = 5; 125

125 125

int cubeByValue( int n ) { return n * n * n; }

number = cubeByValue( number ); }

Fig. 8.8 | Pass-by-value analysis of the program of Fig. 8.6.

n

undefined

8.5 Using const with Pointers

339

Step 1: Before main calls cubeByReference: int main() { int number = 5;

number 5

cubeByReference( &number );

void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; } nPtr

undefined

}

Step 2: After cubeByReference receives the call and before *nPtr is cubed: int main() { int number = 5;

number 5

cubeByReference( &number );

void cubeByReference( int *nPtr ) { *nPtr = *nPtr * *nPtr * *nPtr; } nPtr

call establishes this pointer

}

Step 3: After *nPtr is cubed and before program control returns to main: int main() { int number = 5;

number 125

cubeByReference( &number ); }

void cubeByReference( int *nPtr ) { 125 *nPtr = *nPtr * *nPtr * *nPtr; }

called function modifies caller’s variable

nPtr

Fig. 8.9 | Pass-by-reference analysis (with a pointer argument) of the program of Fig. 8.7. In the function header and in the prototype for a function that expects a one-dimensional array as an argument, the pointer notation in the parameter list of cubeByReference may be used. The compiler does not differentiate between a function that receives a pointer and a function that receives a one-dimensional array. This, of course, means that the function must “know” when it’s receiving an array or simply a single variable which is being passed by reference. When the compiler encounters a function parameter for a one-dimensional array of the form int b[], the compiler converts the parameter to the pointer notation int *b (that is, “b is a pointer to an integer”). Both forms of declaring a function parameter as a one-dimensional array are interchangeable.

8.5 Using const with Pointers Recall that const enables you to inform the compiler that the value of a particular variable should not be modified. Many possibilities exist for using (or not using) const with function parameters. How do you choose the most appropriate of these possibilities? Let the principle of least privilege be your guide. Always give a function enough access to the data in its parameters to accomplish its specified task, but no more. This section discusses how to combine const with pointer declarations to enforce the principle of least privilege.

340

Chapter 8 Pointers

Chapter 6 explained that when an argument is passed by value, a copy of the argument in the function call is made and passed to the function. If the copy is modified in the function, the original value in the caller does not change. In many cases, a value passed to a function is modified in that function. However, in some instances, the value should not be altered in the called function, even though the called function manipulates only a copy of the original value. Consider a function that takes a one-dimensional array and its size as arguments and subsequently prints the array. Such a function should loop through the array and output each element individually. The size of the array is used in the function body to determine the array’s highest subscript so the loop can terminate when the printing completes. The array’s size does not need to change in the function body, so it should be declared const to ensure that it will not change. Because the array is only being printed, it, too, should be declared const. This is especially important because arrays are always passed by reference and could easily be changed in the called function. If an attempt is made to modify a const value, an error occurs.

Software Engineering Observation 8.2

If a value does not (or should not) change in the body of a function to which it’s passed, the parameter should be declared const.

Error-Prevention Tip 8.2

Before using a function, check its function prototype to determine the parameters that it can and cannot modify.

There are four ways to pass a pointer to a function: a nonconstant pointer to nonconstant data, a nonconstant pointer to constant data (Fig. 8.10), a constant pointer to nonconstant data (Fig. 8.11) and a constant pointer to constant data (Fig. 8.12). Each combination provides a different level of access privilege.

Nonconstant Pointer to Nonconstant Data The highest access is granted by a nonconstant pointer to nonconstant data—the data can be modified through the dereferenced pointer, and the pointer can be modified to point to other data. Such a pointer’s declaration (e.g., int *countPtr) does not include const. Nonconstant Pointer to Constant Data A nonconstant pointer to constant data is a pointer that can be modified to point to any data item of the appropriate type, but the data to which it points cannot be modified through that pointer. Such a pointer might be used to receive an array argument to a function that will process each array element, but should not be allowed to modify the data. Any attempt to modify the data in the function results in a compilation error. The declaration for such a pointer places const to the left of the pointer’s type, as in const int *countPtr;

The declaration is read from right to left as “countPtr is a pointer to an integer constant.” Figure 8.10 demonstrates the compilation error messages produced when attempting to compile a function that receives a nonconstant pointer to constant data, then tries to use that pointer to modify the data.

8.5 Using const with Pointers 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

341

// Fig. 8.10: fig08_10.cpp // Attempting to modify data through a // nonconstant pointer to constant data. void f( const int * ); // prototype int main() { int y; f( &y ); // f attempts illegal modification } // end main // xPtr cannot modify the value of constant variable to which it points void f( const int *xPtr ) { *xPtr = 100; // error: cannot modify a const object } // end function f

Microsoft Visual C++ compiler error message: c:\cpphtp8_examples\ch08\Fig08_10\fig08_10.cpp(17) : error C3892: 'xPtr' : you cannot assign to a variable that is const

GNU C++ compiler error message: fig08_10.cpp: In function `void f(const int*)': fig08_10.cpp:17: error: assignment of read-only location

Fig. 8.10 | Attempting to modify data through a nonconstant pointer to constant data. As you know, arrays are aggregate data types that store related data items of the same type under one name. When a function is called with an array as an argument, the array is passed to the function by reference. However, by default, objects are passed by value—a copy of the entire object is passed. This requires the execution-time overhead of making a copy of each data item in the object and storing it on the function call stack. When a pointer to an object is passed, only a copy of the address of the object must be made—the object itself is not copied.

Performance Tip 8.1

If they do not need to be modified by the called function, pass large objects using pointers to constant data or references to constant data, to obtain the performance benefits of passby-reference.

Software Engineering Observation 8.3

Pass large objects using pointers to constant data, or references to constant data, to obtain the security of pass-by-value.

Constant Pointer to Nonconstant Data A constant pointer to nonconstant data is a pointer that always points to the same memory location; the data at that location can be modified through the pointer. An example of

342

Chapter 8 Pointers

such a pointer is an array name, which is a constant pointer to the beginning of the array. All data in the array can be accessed and changed by using the array name and array subscripting. A constant pointer to nonconstant data can be used to receive an array as an argument to a function that accesses array elements using array subscript notation. Pointers that are declared const must be initialized when they’re declared, but if the pointer is a function parameter, it’s initialized with the pointer that’s passed to the function.

Common Programming Error 8.6

Not initializing a pointer that’s declared const is a compilation error.

The program of Fig. 8.11 attempts to modify a constant pointer. Line 11 declares pointer ptr to be of type int * const. The declaration is read from right to left as “ptr is a constant pointer to a nonconstant integer.” The pointer is initialized with the address of integer variable x. Line 14 attempts to assign the address of y to ptr, but the compiler generates an error message. No error occurs when line 13 assigns the value 7 to *ptr—the nonconstant value to which ptr points can be modified using the dereferenced ptr, even though ptr itself has been declared const. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

// Fig. 8.11: fig08_11.cpp // Attempting to modify a constant pointer to nonconstant data. int main() { int x, y; // ptr is a constant pointer to an integer that can // be modified through ptr, but ptr always points to the // same memory location. int * const ptr = &x; // const pointer must be initialized *ptr = 7; // allowed: *ptr is not const ptr = &y; // error: ptr is const; cannot assign to it a new address } // end main

Microsoft Visual C++ compiler error message: c:\cpphtp8_examples\ch08\Fig08_11\fig08_11.cpp(14) : error C3892: 'ptr' : you cannot assign to a variable that is const

GNU C++ compiler error message: fig08_11.cpp: In function `int main()': fig08_11.cpp:14: error: assignment of read-only variable `ptr'

Fig. 8.11 | Attempting to modify a constant pointer to nonconstant data. Constant Pointer to Constant Data The minimum access privilege is granted by a constant pointer to constant data. Such a pointer always points to the same memory location, and the data at that location cannot be

8.6 Selection Sort Using Pass-by-Reference

343

modified via the pointer. This is how an array should be passed to a function that only reads the array, using array subscript notation, and does not modify the array. The program of Fig. 8.12 declares pointer variable ptr to be of type const int * const (line 13). This declaration is read from right to left as “ptr is a constant pointer to an integer constant.” The figure shows the error messages generated when an attempt is made to modify the data to which ptr points (line 17) and when an attempt is made to modify the address stored in the pointer variable (line 18). No errors occur when the program attempts to dereference ptr (line 15), or when the program attempts to output the value to which ptr points, because neither the pointer nor the data it points to is being modified in this statement. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

// Fig. 8.12: fig08_12.cpp // Attempting to modify a constant pointer to constant data. #include using namespace std; int main() { int x = 5, y; // ptr is a constant pointer to a constant integer. // ptr always points to the same location; the integer // at that location cannot be modified. const int *const ptr = &x; cout << *ptr << endl; *ptr = 7; // error: *ptr is const; cannot assign new value ptr = &y; // error: ptr is const; cannot assign new address } // end main

Microsoft Visual C++ compiler error message: c:\cpphtp8_examples\ch08\Fig08_12\fig08_12.cpp(17) : error C3892: 'ptr' : you cannot assign to a variable that is const c:\cpphtp8_examples\ch08\Fig08_12\fig08_12.cpp(18) : error C3892: 'ptr' : you cannot assign to a variable that is const

GNU C++ compiler error message: fig08_12.cpp: In function `int main()': fig08_12.cpp:17: error: assignment of read-only location fig08_12.cpp:18: error: assignment of read-only variable `ptr'

Fig. 8.12 | Attempting to modify a constant pointer to constant data.

8.6 Selection Sort Using Pass-by-Reference In this section, we define a sorting program to demonstrate passing arrays and individual array elements by reference. We use the selection sort algorithm, which is an easy-to-program, but unfortunately inefficient, sorting algorithm. The first iteration of the algorithm

344

Chapter 8 Pointers

selects the smallest element in the array and swaps it with the first element. The second iteration selects the second-smallest element (which is the smallest element of the remaining elements) and swaps it with the second element. The algorithm continues until the last iteration selects the second-largest element and swaps it with the second-to-last index, leaving the largest element in the last index. After the i th iteration, the smallest i items of the array will be sorted into increasing order in the first i elements of the array. As an example, consider the array 34

56

4

10

77

51

93

30

5

52

A program that implements the selection sort first determines the smallest value (4) in the array, which is contained in element 2. The program swaps the 4 with the value in element 0 (34), resulting in 4

56

34

10

77

51

93

30

5

52

The program then determines the smallest value of the remaining elements (all elements except 4), which is 5, contained in element 8. The program swaps the 5 with the 56 in element 1, resulting in 4

5

34

10

77

51

93

30

56

52

On the third iteration, the program determines the next smallest value, 10, and swaps it with the value in element 2 (34). 4

5

10

34

77

51

93

30

56

52

77

93

The process continues until the array is fully sorted. 4

5

10

30

34

51

52

56

After the first iteration, the smallest element is in the first position. After the second iteration, the two smallest elements are in order in the first two positions. After the third iteration, the three smallest elements are in order in the first three positions. Figure 8.13 implements selection sort using functions selectionSort and swap. Function selectionSort (lines 32–49) sorts the array. Line 34 declares the variable smallest, which will store the index of the smallest element in the remaining array. Lines 37–48 loop size - 1 times. Line 39 sets the smallest element’s index to the current index. Lines 42–45 loop over the remaining array elements. For each element, line 44 compares its value to the value of the smallest element. If the current element is smaller than the smallest element, line 45 assigns the current element’s index to smallest. When this loop finishes, smallest will contain the index of the smallest element in the remaining array. Line 47 calls function swap (lines 53–58) to place the smallest remaining element in the next spot in the array (i.e., exchange the array elements array[i] and array[smallest]). 1 2 3 4 5 6

// Fig. 8.13: fig08_13.cpp // Selection sort with pass-by-reference. This program puts values into an // array, sorts them into ascending order and prints the resulting array. #include #include using namespace std;

Fig. 8.13 | Selection sort with pass-by-reference. (Part 1 of 3.)

8.6 Selection Sort Using Pass-by-Reference 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

void selectionSort( int * const, const int ); // prototype void swap( int * const, int * const ); // prototype int main() { const int arraySize = 10; int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; cout << "Data items in original order\n"; for ( int i = 0; i < arraySize; ++i ) cout << setw( 4 ) << a[ i ]; selectionSort( a, arraySize ); // sort the array cout << "\nData items in ascending order\n"; for ( int j = 0; j < arraySize; ++j ) cout << setw( 4 ) << a[ j ]; cout << endl; } // end main // function to sort an array void selectionSort( int * const array, const int size ) { int smallest; // index of smallest element // loop over size - 1 elements for ( int i = 0; i < size - 1; ++i ) { smallest = i; // first index of remaining array // loop to find index of smallest element for ( int index = i + 1; index < size; ++index ) if ( array[ index ] < array[ smallest ] ) smallest = index; swap( &array[ i ], &array[ smallest ] ); } // end if } // end function selectionSort // swap values at memory locations to which // element1Ptr and element2Ptr point void swap( int * const element1Ptr, int * const element2Ptr ) { int hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold; } // end function swap

Fig. 8.13 | Selection sort with pass-by-reference. (Part 2 of 3.)

345

346

Chapter 8 Pointers

Data items in original order 2 6 4 8 10 12 89 68 Data items in ascending order 2 4 6 8 10 12 37 45

45

37

68

89

Fig. 8.13 | Selection sort with pass-by-reference. (Part 3 of 3.) Let’s now look more closely at function swap. Remember that information hiding is enforced between functions, so swap does not have access to individual array elements in selectionSort. Because selectionSort wants swap to have access to the array elements to be swapped, selectionSort passes each of these elements to swap by reference—the address of each array element is passed explicitly. Although entire arrays are passed by reference, individual array elements are ordinarily passed by value. Therefore, selectionSort uses the address operator (&) on each array element in the swap call (line 47) to effect passby-reference. Function swap (lines 53–58) receives &array[ i ] in pointer variable element1Ptr. Information hiding prevents swap from “knowing” the name array[ i ], but swap can use *element1Ptr as a synonym for array[ i ]. Thus, when swap references *element1Ptr, it’s actually referencing array[ i ] in selectionSort. Similarly, when swap references *element2Ptr, it’s actually referencing array[ smallest ] in selectionSort. Even though swap is not allowed to use the statements hold = array[ i ]; array[ i ] = array[ smallest ]; array[ smallest ] = hold;

precisely the same effect is achieved by int hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold;

in the swap function of Fig. 8.13. Several features of function selectionSort should be noted. The function header (line 32) declares array as int * const array, rather than int array[], to indicate that the function receives a one-dimensional array as an argument. Both parameter array’s pointer and the parameter size are declared const to enforce the principle of least privilege. Although parameter size receives a copy of a value in main and modifying the copy cannot change the value in main, selectionSort does not need to alter size to accomplish its task—the array size remains fixed during the execution of selectionSort. Therefore, size is declared const to ensure that it isn’t modified. If the size of the array were to be modified during the sorting process, the sorting algorithm would not run correctly. Function selectionSort receives the size of the array as a parameter, because the function must have that information to sort the array. When an array is passed to a function, only the memory address of the first element of the array is received by the function; the array size must be passed separately to the function. By defining function selectionSort to receive the array size as a parameter, we enable the function to be used by any program that sorts one-dimensional int arrays of arbitrary size. The size of the array could have been programmed directly into the function, but this would restrict the function to processing an array of a specific size and reduce the

8.7 sizeof Operator function’s reusability—only programs processing one-dimensional cific size “hard coded” into the function could use the function.

int

347

arrays of the spe-

Software Engineering Observation 8.4

When passing an array to a function, also pass the size of the array (rather than building into the function knowledge of the array size)—this makes the function more reusable.

8.7 sizeof Operator The compile time unary operator sizeof determines the size of an array (or of any other data type, variable or constant) in bytes during program compilation. When applied to the name of an array, as in Fig. 8.14 (line 13), the sizeof operator returns the total number of bytes in the array as a value of type size_t (an unsigned integer type that is at least as big as unsigned int). This is different from the size of a vector, for example, which is the number of integer elements in the vector. The computer we used to compile this program stores variables of type double in 8 bytes of memory, and array is declared to have 20 elements (line 11), so array uses 160 bytes in memory. When applied to a pointer parameter (line 22) in a function that receives an array as an argument, the sizeof operator returns the size of the pointer in bytes (4 on the system we used)—not the size of the array.

Common Programming Error 8.7

Using the sizeof operator in a function to find the size in bytes of an array parameter results in the size in bytes of a pointer, not the size in bytes of the array. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

// Fig. 8.14: fig08_14.cpp // Sizeof operator when used on an array name // returns the number of bytes in the array. #include using namespace std; size_t getSize( double * ); // prototype int main() { double array[ 20 ]; // 20 doubles; occupies 160 bytes on our system cout << "The number of bytes in the array is " << sizeof( array ); cout << "\nThe number of bytes returned by getSize is " << getSize( array ) << endl; } // end main // return size of ptr size_t getSize( double *ptr ) { return sizeof( ptr ); } // end function getSize

Fig. 8.14 |

sizeof

array. (Part 1 of 2.)

operator when applied to an array name returns the number of bytes in the

348

Chapter 8 Pointers

The number of bytes in the array is 160 The number of bytes returned by getSize is 4

Fig. 8.14 |

sizeof

operator when applied to an array name returns the number of bytes in the

array. (Part 2 of 2.)

The number of elements in an array also can be determined using the results of two operations. For example, consider the following array declaration:

sizeof

double realArray[ 22 ];

If variables of data type double are stored in eight bytes of memory, array realArray contains a total of 176 bytes. To determine the number of elements in the array, the following expression (which is evaluated at compile time) can be used: sizeof realArray / sizeof( realArray[ 0 ] )

The expression determines the number of bytes in array realArray (176) and divides that value by the number of bytes used in memory to store the array’s first element (typically 8 for a double value)—the result is the number of elements in realArray (22).

Determining the Sizes of the Fundamental Types, an Array and a Pointer Figure 8.15 uses sizeof to calculate the number of bytes used to store most of the standard data types. The output shows that the types double and long double have the same size. Types may have different sizes based on the platform running the program. On another system, for example, double and long double may be of different sizes. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

// Fig. 8.15: fig08_15.cpp // Demonstrating the sizeof operator. #include using namespace std; int main() { char c; // variable of type char short s; // variable of type short int i; // variable of type int long l; // variable of type long float f; // variable of type float double d; // variable of type double long double ld; // variable of type long double int array[ 20 ]; // array of int int *ptr = array; // variable of type int * cout << "sizeof c = " << sizeof c << "\tsizeof(char) = " << sizeof( char ) << "\nsizeof s = " << sizeof s << "\tsizeof(short) = " << sizeof( short ) << "\nsizeof i = " << sizeof i

Fig. 8.15 |

sizeof

operator used to determine standard data type sizes. (Part 1 of 2.)

8.8 Pointer Expressions and Pointer Arithmetic 23 24 25 26 27 28 29 30 31 32 33 34

<< << << << << << << << << << << } // end

sizeof sizeof sizeof sizeof sizeof sizeof sizeof sizeof sizeof

349

"\tsizeof(int) = " << sizeof( int ) "\nsizeof l = " << sizeof l "\tsizeof(long) = " << sizeof( long ) "\nsizeof f = " << sizeof f "\tsizeof(float) = " << sizeof( float ) "\nsizeof d = " << sizeof d "\tsizeof(double) = " << sizeof( double ) "\nsizeof ld = " << sizeof ld "\tsizeof(long double) = " << sizeof( long double ) "\nsizeof array = " << sizeof array "\nsizeof ptr = " << sizeof ptr << endl; main

c = 1 sizeof(char) = 1 s = 2 sizeof(short) = 2 i = 4 sizeof(int) = 4 l = 4 sizeof(long) = 4 f = 4 sizeof(float) = 4 d = 8 sizeof(double) = 8 ld = 8 sizeof(long double) = 8 array = 80 ptr = 4

Fig. 8.15 |

sizeof

operator used to determine standard data type sizes. (Part 2 of 2.)

Portability Tip 8.1

The number of bytes used to store a particular data type may vary among systems. When writing programs that depend on data type sizes, and that will run on several computer systems, use sizeof to determine the number of bytes used to store the data types.

Operator sizeof can be applied to any expression or type name. When sizeof is applied to a variable name (which is not an array name) or other expression, the number of bytes used to store the specific type of the expression’s value is returned. The parentheses used with sizeof are required only if a type name (e.g., int) is supplied as its operand. The parentheses used with sizeof are not required when sizeof’s operand is an expression. Remember that sizeof is an operator, not a function, and that it has its effect at compile time, not execution time.

8.8 Pointer Expressions and Pointer Arithmetic This section describes the operators that can have pointers as operands and how these operators are used with pointers. C++ enables pointer arithmetic—certain arithmetic operations may be performed on pointers. A pointer may be incremented (++) or decremented (--), an integer may be added to a pointer (+ or +=), an integer may be subtracted from a pointer (- or -=), or one pointer may be subtracted from another of the same type. Assume that array int v[5] has been declared and that its first element is at memory location 3000. Assume that pointer vPtr has been initialized to point to v[0] (i.e., the value of vPtr is 3000). Figure 8.16 diagrams this situation for a machine with four-byte integers. Variable vPtr can be initialized to point to array v with either of the following statements (because the name of an array is equivalent to the address of its first element):

350

Chapter 8 Pointers int *vPtr = v; int *vPtr = &v[ 0 ];

Portability Tip 8.2

Most computers today have two-byte or four-byte integers. Some of the newer machines use eight-byte integers. Because the results of pointer arithmetic depend on the size of the objects a pointer points to, pointer arithmetic is machine dependent.

location 3000

3004

v[0]

3008

v[1]

3012

v[2]

3016

v[3]

v[4]

pointer variable vPtr

Fig. 8.16 | Array v and a pointer variable int *vPtr that points to v. In conventional arithmetic, the addition 3000 + 2 yields the value 3002. This is normally not the case with pointer arithmetic. When an integer is added to, or subtracted from, a pointer, the pointer is not simply incremented or decremented by that integer, but by that integer times the size of the object to which the pointer refers. The number of bytes depends on the object’s data type. For example, the statement vPtr += 2;

would produce 3008 (from the calculation 3000 + 2 * 4), assuming that an int is stored in four bytes of memory. In the array v, vPtr would now point to v[2] (Fig. 8.17). If an integer is stored in two bytes of memory, then the preceding calculation would result in memory location 3004 (3000 + 2 * 2). If the array elements were of a different data type, the preceding statement would increment the pointer by twice the number of bytes it takes to store an object of that data type.

location 3000

3004

v[0]

3008

v[1]

pointer variable vPtr

Fig. 8.17 | Pointer vPtr after pointer arithmetic.

3012

v[2]

3016

v[3]

v[4]

8.8 Pointer Expressions and Pointer Arithmetic

351

If vPtr had been incremented to 3016, which points to v[4], the statement vPtr -= 4;

would set vPtr back to 3000—the beginning of the array. If a pointer is being incremented or decremented by one, the increment (++) and decrement (--) operators can be used. Each of the statements ++vPtr; vPtr++;

increments the pointer to point to the next element of the array. Each of the statements --vPtr; vPtr--;

decrements the pointer to point to the previous element of the array. Pointer variables pointing to the same array may be subtracted from one another. For example, if vPtr contains the address 3000 and v2Ptr contains the address 3008, the statement x = v2Ptr - vPtr;

would assign to x the number of array elements from vPtr to v2Ptr—in this case, 2. Pointer arithmetic is meaningless unless performed on a pointer that points to an array. We cannot assume that two variables of the same type are stored contiguously in memory unless they’re adjacent elements of an array.

Common Programming Error 8.8

Subtracting or comparing two pointers that do not refer to elements of the same array is a logic error.

A pointer can be assigned to another pointer if both pointers are of the same type. Otherwise, a cast operator (normally a reinterpret_cast; discussed in Section 17.7) must be used to convert the value of the pointer on the right of the assignment to the pointer type on the left of the assignment. The exception to this rule is the pointer to void (i.e., void *), which is a generic pointer capable of representing any pointer type. All pointer types can be assigned to a pointer of type void * without casting. However, a pointer of type void * cannot be assigned directly to a pointer of another type—the pointer of type void * must first be cast to the proper pointer type.

Software Engineering Observation 8.5

Nonconstant pointer arguments can be passed to constant pointer parameters.

A void * pointer cannot be dereferenced. For example, the compiler “knows” that a pointer to int refers to four bytes of memory on a machine with four-byte integers, but a pointer to void simply contains a memory address for an unknown data type—the precise number of bytes to which the pointer refers and the type of the data are not known by the compiler. The compiler must know the data type to determine the number of bytes to be dereferenced for a particular pointer—for a pointer to void, this number of bytes cannot be determined.

352

Chapter 8 Pointers

Common Programming Error 8.9

Assigning a pointer of one type to a pointer of another (other than void *) without using a cast (normally a reinterpret_cast) is a compilation error.

Common Programming Error 8.10

All operations on a void * pointer are compilation errors, except comparing void * pointers with other pointers, casting void * pointers to valid pointer types and assigning addresses to void * pointers.

Pointers can be compared using equality and relational operators. Comparisons using relational operators are meaningless unless the pointers point to members of the same array. Pointer comparisons compare the addresses stored in the pointers. A comparison of two pointers pointing to the same array could show, for example, that one pointer points to a higher numbered element of the array than the other pointer does. A common use of pointer comparison is determining whether a pointer is 0 (i.e., the pointer is a null pointer—it does not point to anything).

8.9 Relationship Between Pointers and Arrays Arrays and pointers are intimately related in C++ and may be used almost interchangeably. An array name can be thought of as a constant pointer. Pointers can be used to do any operation involving array subscripting. Assume the following declarations: int b[ 5 ]; // create 5-element int array b int *bPtr; // create int pointer bPtr

Because the array name (without a subscript) is a (constant) pointer to the first element of the array, we can set bPtr to the address of the first element in array b with the statement bPtr = b; // assign address of array b to bPtr

This is equivalent to assigning the address of the first element of the array as follows: bPtr = &b[ 0 ]; // also assigns address of array b to bPtr

Array element b[3] can alternatively be referenced with the pointer expression *( bPtr + 3 )

The 3 in the preceding expression is the offset to the pointer. When the pointer points to the beginning of an array, the offset indicates which array element should be referenced, and the offset value is identical to the subscript. This notation is referred to as pointer/ offset notation. The parentheses are necessary, because the precedence of * is higher than that of +. Without the parentheses, the preceding expression would add 3 to a copy *bPtr’s value (i.e., 3 would be added to b[0], assuming that bPtr points to the beginning of the array). Just as the array element can be referenced with a pointer expression, the address &b[ 3 ]

can be written with the pointer expression bPtr + 3

8.9 Relationship Between Pointers and Arrays

353

The array name (which is implicitly const) can be treated as a pointer and used in pointer arithmetic. For example, the expression *( b + 3 )

also refers to the array element b[3]. In general, all subscripted array expressions can be written with a pointer and an offset. In this case, pointer/offset notation was used with the name of the array as a pointer. The preceding expression does not modify the array name in any way; b still points to the first element in the array. Pointers can be subscripted exactly as arrays can. For example, the expression bPtr[ 1 ]

refers to the array element b[1]; this expression uses pointer/subscript notation. Remember that an array name is a constant pointer; it always points to the beginning of the array. Thus, the expression b += 3

causes a compilation error, because it attempts to modify the value of the array name (a constant) with pointer arithmetic.

Common Programming Error 8.11

Although array names are pointers to the beginning of the array, array names cannot be modified in arithmetic expressions, because array names are constant pointers.

Good Programming Practice 8.2

For clarity, use array notation instead of pointer notation when manipulating arrays.

Figure 8.18 uses the four notations discussed in this section for referring to array elements—array subscript notation, pointer/offset notation with the array name as a pointer, pointer subscript notation and pointer/offset notation with a pointer—to accomplish the same task, namely printing the four elements of the integer array b. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

// Fig. 8.18: fig08_18.cpp // Using subscripting and pointer notations with arrays. #include using namespace std; int main() { int b[] = { 10, 20, 30, 40 }; // create 4-element array b int *bPtr = b; // set bPtr to point to array b // output array b using array subscript notation cout << "Array b printed with:\n\nArray subscript notation\n"; for ( int i = 0; i < 4; ++i ) cout << "b[" << i << "] = " << b[ i ] << '\n';

Fig. 8.18 | Referencing array elements with the array name and with pointers. (Part 1 of 2.)

354 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

Chapter 8 Pointers

// output array b using the array name and pointer/offset notation cout << "\nPointer/offset notation where " << "the pointer is the array name\n"; for ( int offset1 = 0; offset1 < 4; ++offset1 ) cout << "*(b + " << offset1 << ") = " << *( b + offset1 ) << '\n'; // output array b using bPtr and array subscript notation cout << "\nPointer subscript notation\n"; for ( int j = 0; j < 4; ++j ) cout << "bPtr[" << j << "] = " << bPtr[ j ] << '\n'; cout << "\nPointer/offset notation\n"; // output array b using bPtr and pointer/offset notation for ( int offset2 = 0; offset2 < 4; ++offset2 ) cout << "*(bPtr + " << offset2 << ") = " << *( bPtr + offset2 ) << '\n'; } // end main

Array b printed with: Array subscript notation b[0] = 10 b[1] = 20 b[2] = 30 b[3] = 40 Pointer/offset notation where the pointer is the array name *(b + 0) = 10 *(b + 1) = 20 *(b + 2) = 30 *(b + 3) = 40 Pointer bPtr[0] bPtr[1] bPtr[2] bPtr[3]

subscript notation = 10 = 20 = 30 = 40

Pointer/offset notation *(bPtr + 0) = 10 *(bPtr + 1) = 20 *(bPtr + 2) = 30 *(bPtr + 3) = 40

Fig. 8.18 | Referencing array elements with the array name and with pointers. (Part 2 of 2.)

8.10 Pointer-Based String Processing We’ve already used the C++ Standard Library string class to represent strings as fullfledged objects. For example, the GradeBook class case study in Chapters 3–7 represents a course name using a string object. Chapter 18 presents class string in detail. This section introduces C-style, pointer-based strings. C++’s string class is preferred for use in new programs, because it eliminates many of the security problems and bugs that can be caused by

8.10 Pointer-Based String Processing

355

manipulating C strings. We cover C strings here for a deeper understanding of arrays. Also, if you work with legacy C and C++ programs, you’re likely to encounter these pointerbased strings. We cover C-style, pointer-based strings in detail in Chapter 21.

Characters and Character Constants Characters are the fundamental building blocks of C++ source programs. Every program is composed of a sequence of characters that—when grouped together meaningfully—is interpreted by the compiler as a series of instructions used to accomplish a task. A program may contain character constants. A character constant is an integer value represented as a character in single quotes. The value of a character constant is the integer value of the character in the machine’s character set. For example, 'z' represents the integer value of z (122 in the ASCII character set; see Appendix B), and '\n' represents the integer value of newline (10 in the ASCII character set). Strings A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, /and $. String literals, or string constants, in C++ are written in double quotation marks as follows: "John Q. Doe" "9999 Main Street" "Maynard, Massachusetts" "(201) 555-1212"

(a name) (a street address) (a city and state) (a telephone number)

Pointer-Based Strings A pointer-based string is an array of characters ending with a null character ('\0'), which marks where the string terminates in memory. A string is accessed via a pointer to its first character. The value of a string literal is the address of its first character, but the sizeof a string literal is the length of the string including the terminating null character. Pointerbased strings are like arrays—an array name is also a pointer to its first element. String Literals as Initializers A string literal may be used as an initializer in the declaration of either a character array or a variable of type char *. The declarations char color[] = "blue"; const char *colorPtr = "blue";

each initialize a variable to the string "blue". The first declaration creates a five-element array color containing the characters 'b', 'l', 'u', 'e' and '\0'. The second declaration creates pointer variable colorPtr that points to the letter b in the string "blue" (which ends in '\0') somewhere in memory. String literals have static storage class (they exist for the duration of the program) and may or may not be shared if the same string literal is referenced from multiple locations in a program. The effect of modifying a string literal is undefined; thus, you should always declare a pointer to a string literal as const char *.

Character Constants as Initializers The declaration char color[] = "blue"; could also be written char color[] = { 'b', 'l', 'u', 'e', '\0' };

356

Chapter 8 Pointers

which uses character constants in single quotes (') as initializers for each element of the array. When declaring a character array to contain a string, the array must be large enough to store the string and its terminating null character. The compiler determines the size of the array in the preceding declaration, based on the number of initializers in the initializer list.

Common Programming Error 8.12

Not allocating sufficient space in a character array to store the null character that terminates a string is a logic error.

Common Programming Error 8.13

Creating or using a C-style string that does not contain a terminating null character can lead to logic errors.

Error-Prevention Tip 8.3

When storing a string of characters in a character array, be sure that the array is large enough to hold the largest string that will be stored. C++ allows strings of any length. If a string is longer than the character array in which it’s to be stored, characters beyond the end of the array will overwrite data in memory following the array, leading to logic errors and potential security breaches.

Accessing Characters in a C-String Because a C-style string is an array of characters, we can access individual characters in a string directly with array subscript notation. For example, in the preceding declaration, color[0] is the character 'b', color[2] is 'u' and color[4] is the null character. Reading Strings into char Arrays with cin A string can be read into a character array using stream extraction with cin. For example, the following statement reads a string into character array word[20]: cin >> word;

The string entered by the user is stored in word. The preceding statement reads characters until a white-space character or end-of-file indicator is encountered. The string should be no longer than 19 characters to leave room for the terminating null character. The setw stream manipulator can be used to ensure that the string read into word does not exceed the size of the array. For example, the statement cin >> setw( 20 ) >> word;

specifies that cin should read a maximum of 19 characters into array word and save the 20th location in the array to store the terminating null character for the string. The setw stream manipulator applies only to the next value being input. If more than 19 characters are entered, the remaining characters are not saved in word, but they will be in the input stream and can be read by the next input operation.

Reading Lines of Text into char Arrays with cin.getline In some cases, it’s desirable to input an entire line of text into a character array. For this purpose, the cin object provides the member function getline, which takes three arguments—a character array in which the line of text will be stored, a length and a delimiter character. For example, the statements

8.11 Arrays of Pointers

357

char sentence[ 80 ]; cin.getline( sentence, 80, '\n' );

declare array sentence of 80 characters and read a line of text from the keyboard into the array. The function stops reading characters when the delimiter character '\n' is encountered, when the end-of-file indicator is entered or when the number of characters read so far is one less than the length specified in the second argument. The last character in the array is reserved for the terminating null character. If the delimiter character is encountered, it’s read and discarded. The third argument to cin.getline has '\n' as a default value, so the preceding function call could have been written as: cin.getline( sentence, 80 );

Chapter 15, Stream Input/Output, provides a detailed discussion of other input/output functions.

cin.getline

and

Displaying C-Style Strings A character array representing a null-terminated string can be output with cout and <<. The statement cout << sentence;

prints the array sentence. Note that cout <<, like cin >>, does not care how large the character array is. The characters of the string are output until a terminating null character is encountered; the null character is not printed. [Note: cin and cout assume that character arrays should be processed as strings terminated by null characters; cin and cout do not provide similar input and output processing capabilities for other array types.]

8.11 Arrays of Pointers Arrays may contain pointers. A common use of such a data structure is to form an array of pointer-based strings, referred to simply as a string array. Each entry in the array is a string, but in C++ a string is essentially a pointer to its first character, so each entry in an array of strings is simply a pointer to the first character of a string. Consider the declaration of string array suit that might be useful in representing a deck of cards: const char * const suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };

The

portion of the declaration indicates an array of four elements. The const portion of the declaration indicates that each element of array suit is of type “pointer to char constant data.” The four values to be placed in the array are "Hearts", "Diamonds", "Clubs" and "Spades". Each is stored in memory as a null-terminated character string that is one character longer than the number of characters between quotes. The four strings are seven, nine, six and seven characters long (including their terminating null characters), respectively. Although it appears as though these strings are being placed in the suit array, only pointers are actually stored in the array, as shown in Fig. 8.19. Each pointer points to the first character of its corresponding string. Thus, even though the suit array is fixed in size, it provides access to character strings of any length. This flexibility is one example of C++’s powerful data-structuring capabilities. suit[4]

char *

358

Chapter 8 Pointers

suit[0]

'H'

'e'

'a'

'r'

't'

's'

'\0'

suit[1]

'D'

'i'

'a'

'm'

'o'

'n'

'd'

suit[2]

'C'

'l'

'u'

'b'

's'

'\0'

suit[3]

'S'

'p'

'a'

'd'

'e'

's'

's'

'\0'

'\0'

Fig. 8.19 | Graphical representation of the suit array. The suit strings could be placed into a two-dimensional array, in which each row represents one suit and each column represents one of the letters of a suit name. Such a data structure must have a fixed number of columns per row, and that number must be as large as the largest string. Therefore, considerable memory is wasted when we store a large number of strings, of which most are shorter than the longest string. We use arrays of strings to help represent a deck of cards in the next section. String arrays are commonly used with command-line arguments that are passed to function main when a program begins execution. Such arguments follow the program name when a program is executed from the command line. A typical use of command-line arguments is to pass options to a program. For example, from the command line on a Windows computer, the user can type dir /p

to list the contents of the current directory and pause after each screen of information. When the dir command executes, the option /p is passed to dir as a command-line argument. Such arguments are placed in a string array that main receives as an argument. We discuss command-line arguments in Appendix F, C Legacy Code Topics.

8.12 Function Pointers A pointer to a function contains the function’s address in memory. We know that an array’s name is actually the address in memory of the first element. Similarly, a function’s name is actually the starting address in memory of the code that performs the function’s task. Pointers to functions can be passed to functions, returned from functions, stored in arrays, assigned to other function pointers and used to call the underlying function.

Multipurpose Selection Sort Using Function Pointers To illustrate the use of pointers to functions, Fig. 8.20 modifies the selection sort program of Fig. 8.13. Figure 8.20 consists of main (lines 13–50) and the functions selectionSort (lines 54–71), swap (lines 75–80), ascending (lines 84–87) and descending (lines 91– 94). Function selectionSort receives a pointer to a function—either function ascending or function descending—as an argument in addition to the integer array to sort and the size of the array. Functions ascending and descending determine the sorting order. The program prompts the user to choose whether the array should be sorted in ascending order or in descending order (lines 20–22). If the user enters 1, a pointer to function ascending is passed to function selectionSort (line 33), causing the array to be sorted into increas-

8.12 Function Pointers

359

ing order. If the user enters 2, a pointer to function descending is passed to function se(line 41), causing the array to be sorted into decreasing order.

lectionSort

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

// Fig. 8.20: fig08_20.cpp // Multipurpose sorting program using function pointers. #include #include using namespace std; // prototypes void selectionSort( int [], const int, bool (*)( int, int ) ); void swap( int * const, int * const ); bool ascending( int, int ); // implements ascending order bool descending( int, int ); // implements descending order int main() { const int arraySize = 10; int order; // 1 = ascending, 2 = descending int counter; // array index int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; cout << "Enter 1 to sort in ascending order,\n" << "Enter 2 to sort in descending order: "; cin >> order; cout << "\nData items in original order\n"; // output original array for ( counter = 0; counter < arraySize; ++counter ) cout << setw( 4 ) << a[ counter ]; // sort array in ascending order; pass function ascending // as an argument to specify ascending sorting order if ( order == 1 ) { selectionSort( a, arraySize, ascending ); cout << "\nData items in ascending order\n"; } // end if // sort array in descending order; pass function descending // as an argument to specify descending sorting order else { selectionSort( a, arraySize, descending ); cout << "\nData items in descending order\n"; } // end else part of if...else // output sorted array for ( counter = 0; counter < arraySize; ++counter ) cout << setw( 4 ) << a[ counter ]; cout << endl; } // end main

Fig. 8.20 | Multipurpose sorting program using function pointers. (Part 1 of 3.)

360 51 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

Chapter 8 Pointers

// multipurpose selection sort; the parameter compare is a pointer to // the comparison function that determines the sorting order void selectionSort( int work[], const int size, bool (*compare)( int, int ) ) { int smallestOrLargest; // index of smallest (or largest) element // loop over size - 1 elements for ( int i = 0; i < size - 1; ++i ) { smallestOrLargest = i; // first index of remaining vector // loop to find index of smallest (or largest) element for ( int index = i + 1; index < size; ++index ) if ( !(*compare)( work[ smallestOrLargest ], work[ index ] ) ) smallestOrLargest = index; swap( &work[ smallestOrLargest ], &work[ i ] ); } // end if } // end function selectionSort // swap values at memory locations to which // element1Ptr and element2Ptr point void swap( int * const element1Ptr, int * const element2Ptr ) { int hold = *element1Ptr; *element1Ptr = *element2Ptr; *element2Ptr = hold; } // end function swap // determine whether element a is less than // element b for an ascending order sort bool ascending( int a, int b ) { return a < b; // returns true if a is less than b } // end function ascending // determine whether element a is greater than // element b for a descending order sort bool descending( int a, int b ) { return a > b; // returns true if a is greater than b } // end function descending

Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 1 Data items in original order 2 6 4 8 10 12 89 68 Data items in ascending order 2 4 6 8 10 12 37 45

45

37

68

89

Fig. 8.20 | Multipurpose sorting program using function pointers. (Part 2 of 3.)

8.13 Wrap-Up

361

Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 2 Data items in original order 2 6 4 8 10 12 89 68 Data items in descending order 89 68 45 37 12 10 8 6

45

37

4

2

Fig. 8.20 | Multipurpose sorting program using function pointers. (Part 3 of 3.) The following parameter appears in line 55 of selectionSort’s function header: bool ( *compare )( int, int )

This parameter specifies a pointer to a function. The keyword bool indicates that the function being pointed to returns a bool value. The text (*compare) indicates the name of the pointer to the function (the * indicates that parameter compare is a pointer). The text (int, int) indicates that the function pointed to by compare takes two integer arguments. Parentheses are needed around *compare to indicate that compare is a pointer to a function. If we had not included the parentheses, the declaration would have been bool *compare( int, int )

which declares a function that receives two ints and returns a pointer to a bool value. The corresponding parameter in the function prototype of selectionSort (line 8) is bool (*)( int, int )

Only types have been included. As always, for documentation purposes, you can include names that the compiler will ignore. The function passed to selectionSort is called in line 66 as follows: ( *compare )( work[ smallestOrLargest ], work[ index ] )

Just as a pointer to a variable is dereferenced to access the value of the variable, a pointer to a function is dereferenced to execute the function. The parentheses around *compare are necessary—if they were left out, the * operator would attempt to dereference the value returned from the function call. The call to the function could have been made without dereferencing the pointer, as in compare( work[ smallestOrLargest ], work[ index ] )

which uses the pointer directly as the function name. We prefer the first method of calling a function through a pointer, because it explicitly illustrates that compare is a pointer to a function that is dereferenced to call the function. The second method of calling a function through a pointer makes it appear as though compare is the name of an actual function in the program. This may be confusing to a user of the program who would like to see the definition of function compare and finds that it isn’t defined in the file. Chapter 22, Standard Template Library (STL), presents many common uses of function pointers.

8.13 Wrap-Up In this chapter we provided a detailed introduction to pointers—variables that contain memory addresses as their values. We began by demonstrating how to declare and initial-

362

Chapter 8 Pointers

ize pointers. You saw how to use the address operator (&) to assign the address of a variable to a pointer and the indirection operator (*) to access the data stored in the variable indirectly referenced by a pointer. We discussed passing arguments by reference using both pointer arguments and reference arguments. You learned how to use const with pointers to enforce the principle of least privilege. We demonstrated using nonconstant pointers to nonconstant data, nonconstant pointers to constant data, constant pointers to nonconstant data, and constant pointers to constant data. We then used selection sort to demonstrate passing arrays and individual array elements by reference. We discussed the compile-time sizeof operator, which can be used to determine the sizes of data types and variables in bytes during program compilation. We demonstrated how to use pointers in arithmetic and comparison expressions. You saw that pointer arithmetic can be used to jump from one element of an array to another. You learned how to use arrays of pointers, and more specifically string arrays (arrays of strings). We discussed function pointers, which enable you to pass functions as parameters. We briefly introduced pointer-based strings. In the next chapter, we begin our deeper treatment of classes. You’ll learn about the scope of a class’s members, and how to keep objects in a consistent state. You’ll also learn about using special member functions called constructors and destructors, which execute when an object is created and destroyed, respectively, and we’ll discuss when constructors and destructors are called. In addition, we’ll demonstrate using default arguments with constructors and using default memberwise assignment to assign one object of a class to another object of the same class. We’ll also discuss the danger of returning a reference to a private data member of a class.

Summary Section 8.2 Pointer Variable Declarations and Initialization

• Pointers are variables that contain as their values memory addresses of other variables. • The declaration int *ptr;

declares ptr to be a pointer to a variable of type int and is read, “ptr is a pointer to int.” The * as used here in a declaration indicates that the variable is a pointer. • There are three values that can be used to initialize a pointer: 0, NULL or an address of an object of the same type. The new C++ standard also provides the nullptr constant, which is preferred. • The only integer that can be assigned to a pointer without casting is zero.

Section 8.3 Pointer Operators

• The & (address) operator (p. 332) obtains the memory address of its operand. • The operand of the address operator must be a variable name (or another lvalue); the address operator cannot be applied to constants or to expressions that do not return a reference. • The * indirection (or dereferencing) operator (p. 333) returns a synonym for the name of the object that its operand points to in memory. This is called dereferencing the pointer (p. 333).

Section 8.4 Pass-by-Reference with Pointers

• When calling a function with an argument that the caller wants the called function to modify, the address of the argument may be passed. The called function then uses the indirection operator (*) to dereference the pointer and modify the value of the argument in the calling function.

Summary

363

• A function receiving an address as an argument must have a pointer as its corresponding parameter.

Section 8.5 Using const with Pointers

• The const qualifier enables you to inform the compiler that the value of a particular variable cannot be modified through the specified identifier. • There are four ways to pass a pointer to a function—a nonconstant pointer to nonconstant data (p. 340), a nonconstant pointer to constant data (p. 340), a constant pointer to nonconstant data (p. 341), and a constant pointer to constant data (p. 341). • The value of the array name is the address of the array’s first element. • To pass a single array element by reference using pointers, pass the element’s address.

Section 8.6 Selection Sort Using Pass-by-Reference

• The selection sort algorithm (p. 343) is an easy-to-program, but inefficient, sorting algorithm. The first iteration of the algorithm selects the smallest element in the array and swaps it with the first element. The second iteration selects the second-smallest element (which is the smallest element of the remaining elements) and swaps it with the second element. The algorithm continues until the last iteration selects the second-largest element and swaps it with the second-to-last index, leaving the largest element in the last index. After the ith iteration, the smallest i items of the array will be sorted into increasing order in the first i elements of the array.

Section 8.7 sizeof Operator

• sizeof (p. 347) determines the size in bytes of a type, variable or constant at compile time. • When applied to an array name, sizeof returns the total number of bytes in the array.

Section 8.8 Pointer Expressions and Pointer Arithmetic

• The arithmetic operations that may be performed on pointers are incrementing (++) a pointer, decrementing (--) a pointer, adding (+ or +=) an integer to a pointer, subtracting (- or -=) an integer from a pointer and subtracting one pointer from another. • When an integer is added or subtracted from a pointer, the pointer is incremented or decremented by that integer times the size of the object to which the pointer refers. • Pointers can be assigned to one another if they are of the same type. Otherwise, a cast must be used. The exception to this is a void * pointer, which is a generic pointer type that can hold pointer values of any type. • The only valid operations on a void * pointer are comparing void * pointers with other pointers, assigning addresses to void * pointers and casting void * pointers to valid pointer types. • Pointers can be compared using the equality and relational operators. Comparisons using relational operators are meaningful only if the pointers point to members of the same array.

Section 8.9 Relationship Between Pointers and Arrays

• Pointers that point to arrays can be subscripted exactly as array names can (p. 353). • In pointer/offset notation (p. 352), if the pointer points to the first element of the array, the offset is the same as an array subscript. • All subscripted array expressions can be written with a pointer and an offset (p. 352), using either the name of the array as a pointer or using a separate pointer that points to the array.

Section 8.10 Pointer-Based String Processing

• A character constant (p. 355) is an integer value represented as a character in single quotes. The value of a character constant is the integer value of the character in the machine’s character set.

364

Chapter 8 Pointers

• A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, /and $. • String literals, or string constants, in C++ are written in double quotation marks (p. 355). • A pointer-based string is an array of characters ending with a null character ('\0'; p. 355), which marks where the string terminates in memory. A string is accessed via a pointer to its first character. • The value of a string literal is the address of its first character, but the sizeof a string literal is the length of the string including the terminating null character. • A string literal may be used as an initializer for a character array or a variable of type char *. • String literals have static storage class and may or may not be shared if the same string literal is referenced from multiple locations in a program. • The effect modifying a string literal is undefined; thus, you should always declare a pointer to a string literal as const char *. • When declaring a character array to contain a string, the array must be large enough to store the string and its terminating null character. • If a string is longer than the character array in which it’s to be stored, characters beyond the end of the array will overwrite data in memory following the array, leading to logic errors. • You can access individual characters in a string directly with array subscript notation. • A string can be read into a character array using stream extraction with cin. • The setw stream manipulator can be used to ensure that the string read into a character array does not exceed the size of the array. • The cin object provides the member function getline (p. 356) to input an entire line of text into a character array. The function takes three arguments—a character array in which the line of text will be stored, a length and a delimiter character. The third argument has '\n' as a default value. • A character array representing a null-terminated string can be output with cout and <<. The characters of the string are output until a terminating null character is encountered.

Section 8.11 Arrays of Pointers

• Arrays may contain pointers. • Such a data structure can be used to form an array of pointer-based strings, referred to as a string array (p. 357). Each entry in the array is a string, but in C++ a string is essentially a pointer to its first character, so each entry in an array of strings is simply a pointer to the first character of a string. • String arrays are commonly used with command-line arguments (p. 358) that are passed to main when a program begins execution.

Section 8.12 Function Pointers

• A pointer to a function (p. 358) is the address where the code for the function resides. • Pointers to functions can be used to call the functions they point to, passed to functions, returned from functions, stored in arrays, assigned to other pointers.

Self-Review Exercises 8.1

Answer each of the following: a) A pointer is a variable that contains as its value the of another variable. b) The three values that can be used to initialize a pointer are , and . c) The only integer that can be assigned directly to a pointer is .

Self-Review Exercises 8.2

365

State whether the following are true or false. If the answer is false, explain why. a) The address operator & can be applied only to constants and to expressions. b) A pointer that is declared to be of type void * can be dereferenced. c) A pointer of one type can’t be assigned to one of another type without a cast operation. 8.3 For each of the following, write C++ statements that perform the specified task. Assume that double-precision, floating-point numbers are stored in eight bytes and that the starting address of the array is at location 1002500 in memory. Each part of the exercise should use the results of previous parts where appropriate. a) Declare an array of type double called numbers with 10 elements, and initialize the elements to the values 0.0, 1.1, 2.2, …, 9.9. Assume that the symbolic constant SIZE has been defined as 10. b) Declare a pointer nPtr that points to a variable of type double. c) Use a for statement to print the elements of array numbers using array subscript notation. Print each number with one position of precision to the right of the decimal point. d) Write two separate statements that each assign the starting address of array numbers to the pointer variable nPtr. e) Use a for statement to print the elements of array numbers using pointer/offset notation with pointer nPtr. f) Use a for statement to print the elements of array numbers using pointer/offset notation with the array name as the pointer. g) Use a for statement to print the elements of array numbers using pointer/subscript notation with pointer nPtr. h) Refer to the fourth element of array numbers using array subscript notation, pointer/offset notation with the array name as the pointer, pointer subscript notation with nPtr and pointer/offset notation with nPtr. i) Assuming that nPtr points to the beginning of array numbers, what address is referenced by nPtr + 8? What value is stored at that location? j) Assuming that nPtr points to numbers[5], what address is referenced by nPtr after nPtr -= 4 is executed? What’s the value stored at that location? 8.4 For each of the following, write a single statement that performs the specified task. Assume that floating-point variables number1 and number2 have been declared and that number1 has been initialized to 7.3. Assume that variable ptr is of type char *. Assume that arrays s1 and s2 are each 100-element char arrays that are initialized with string literals. a) Declare the variable fPtr to be a pointer to an object of type double. b) Assign the address of variable number1 to pointer variable fPtr. c) Print the value of the object pointed to by fPtr. d) Assign the value of the object pointed to by fPtr to variable number2. e) Print the value of number2. f) Print the address of number1. g) Print the address stored in fPtr. Is the value printed the same as the address of number1? 8.5 Perform the task specified by each of the following statements: a) Write the function header for a function called exchange that takes two pointers to double-precision, floating-point numbers x and y as parameters and does not return a value. b) Write the function prototype for the function in part (a). c) Write the function header for a function called evaluate that returns an integer and that takes as parameters integer x and a pointer to function poly. Function poly takes an integer parameter and returns an integer. d) Write the function prototype for the function in part (c). e) Write two statements that each initialize character array vowel with the string of vowels, "AEIOU".

366

Chapter 8 Pointers

Find the error in each of the following program segments. Assume the following declara8.6 tions and statements: int *zPtr; // zPtr will reference array z void *sPtr = 0; int number; int z[ 5 ] = { 1, 2, 3, 4, 5 };

a) b) c) d)

++zPtr; // use pointer to get first value of array number = zPtr; // assign array element 2 (the value 3) to number number = *zPtr[ 2 ]; // print entire array z for ( int i = 0; i <= 5; ++i )

e) f)

cout << zPtr[ i ] << endl; // assign the value pointed to by sPtr to number number = *sPtr; ++z;

Answers to Self-Review Exercises 8.1

a) address. b) 0, NULL, an address. c) 0.

8.2

a) False. The operand of the address operator must be an lvalue; the address operator cannot be applied to constants or to expressions that do not result in references. b) False. A pointer to void cannot be dereferenced. Such a pointer does not have a type that enables the compiler to determine the number of bytes of memory to dereference and the type of the data to which the pointer points. c) False. Pointers of any type can be assigned to void pointers. Pointers of type void can be assigned to pointers of other types only with an explicit type cast.

8.3

a) b) c)

double numbers[ SIZE ] = { 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9 }; double *nPtr; cout << fixed << showpoint << setprecision( 1 ); for ( int i = 0; i < SIZE; ++i )

d) e)

cout << numbers[ i ] << ' '; nPtr = numbers; nPtr = &numbers[ 0 ]; cout << fixed << showpoint << setprecision( 1 ); for ( int j = 0; j < SIZE; ++j )

f)

cout << *( nPtr + j ) << ' '; cout << fixed << showpoint << setprecision( 1 ); for ( int k = 0; k < SIZE; ++k )

g)

cout << *( numbers + k ) <<

' ';

cout << fixed << showpoint << setprecision( 1 ); for ( int m = 0; m < SIZE; ++m )

h)

cout << nPtr[ m ] <<

' ';

numbers[ 3 ] *( numbers + 3 ) nPtr[ 3 ] *( nPtr + 3 )

i) The address is 1002500

+ 8 * 8 = 1002564.

The value is 8.8.

Exercises

367

j) The address of numbers[ 5 ] is 1002500 + 5 * 8 = 1002540. The address of nPtr -= 4 is 1002540 - 4 * 8 = 1002508. The value at that location is 1.1. 8.4

8.5

a) b) c) d) e) f) g) a) b) c) d) e)

double *fPtr; fPtr = &number1; cout << "The value of *fPtr is " << *fPtr << endl; number2 = *fPtr; cout << "The value of number2 is " << number2 << endl; cout << "The address of number1 is " << &number1 << endl; cout << "The address stored in fPtr is " << fPtr << endl;

Yes, the value is the same.

void exchange( double *x, double *y ) void exchange( double *, double * ); int evaluate( int x, int (*poly)( int ) ) int evaluate( int, int (*)( int ) ); char vowel[] = "AEIOU"; char vowel[] = { 'A', 'E', 'I', 'O', 'U', '\0' };

8.6

a) Error: zPtr has not been initialized. Correction: Initialize zPtr with zPtr = z; b) Error: The pointer is not dereferenced. Correction: Change the statement to number = *zPtr; c) Error: zPtr[ 2 ] is not a pointer and should not be dereferenced. Correction: Change *zPtr[ 2 ] to zPtr[ 2 ]. d) Error: Referring to an array element outside the array bounds with pointer subscripting. Correction: To prevent this, change the relational operator in the for statement to < or change the 5 to a 4. e) Error: Dereferencing a void pointer. Correction: To dereference the void pointer, it must first be cast to an integer pointer. Change the statement to number = *static_cast< int * >( sPtr ); f) Error: Trying to modify an array name with pointer arithmetic. Correction: Use a pointer variable instead of the array name to accomplish pointer arithmetic, or subscript the array name to refer to a specific element.

Exercises 8.7

(True or False) State whether the following are true or false. If false, explain why. a) Two pointers that point to different arrays cannot be compared meaningfully. b) Because the name of an array is a pointer to the first element of the array, array names can be manipulated in precisely the same manner as pointers.

8.8 (Write C++ Statements) For each of the following, write C++ statements that perform the specified task. Assume that unsigned integers are stored in two bytes and that the starting address of the array is at location 1002500 in memory. a) Declare an array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. Assume that the symbolic constant SIZE has been defined as 5. b) Declare a pointer vPtr that points to an object of type unsigned int. c) Use a for statement to print the elements of array values using array subscript notation. d) Write two separate statements that assign the starting address of array values to pointer variable vPtr. e) Use a for statement to print the elements of array values using pointer/offset notation.

368

Chapter 8 Pointers f) Use a for statement to print the elements of array values using pointer/offset notation with the array name as the pointer. g) Use a for statement to print the elements of array values by subscripting the pointer to the array. h) Refer to the fifth element of values using array subscript notation, pointer/offset notation with the array name as the pointer, pointer subscript notation and pointer/offset notation. i) What address is referenced by vPtr + 3? What value is stored at that location? j) Assuming that vPtr points to values[ 4 ], what address is referenced by vPtr -= 4? What value is stored at that location?

8.9 (Write C++ Statements) For each of the following, write a single statement that performs the specified task. Assume that long variables value1 and value2 have been declared and value1 has been initialized to 200000. a) Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Print the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Print the value of value2. f) Print the address of value1. g) Print the address stored in longPtr. Is the value printed the same as value1’s address? 8.10 (Function Headers and Prototypes) Perform the task specified by each of the following statements: a) Write the function header for function zero that takes a long integer array parameter bigIntegers and does not return a value. b) Write the function prototype for the function in part (a). c) Write the function header for function add1AndSum that takes an integer array parameter oneTooSmall and returns an integer. d) Write the function prototype for the function described in part (c). 8.11 (Find the Code Errors) Find the error in each of the following segments. If the error can be corrected, explain how. a) int *number; b)

cout << number << endl; double *realPtr; long *integerPtr;

c) d)

integerPtr = realPtr; int * x, y; x = y; char s[] = "this is a character array"; for ( ; *s != '\0'; ++s)

e)

cout << *s << ' '; short *numPtr, result; void *genericPtr = numPtr;

f)

result = *genericPtr + 7; double x

= 19.34;

double xPtr = &x; cout << xPtr << endl;

8.12 (Simulation: The Tortoise and the Hare) In this exercise, you’ll re-create the classic race of the tortoise and the hare. You’ll use random number generation to develop a simulation of this memorable event.

Exercises

369

Our contenders begin the race at “square 1” of 70 squares. Each square represents a possible position along the race course. The finish line is at square 70. The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce. The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground. There is a clock that ticks once per second. With each tick of the clock, your program should use function moveTortoise and moveHare to adjust the position of the animals according to the rules in Fig. 8.21. These functions should use pointer-based pass-by-reference to modify the position of the tortoise and the hare.

Animal

Move type

Percentage of the time

Actual move

Tortoise

Fast plod Slip Slow plod Sleep Big hop Big slip Small hop Small slip

50% 20% 30% 20% 20% 10% 30% 20%

3 squares to the right 6 squares to the left 1 square to the right No move at all 9 squares to the right 12 squares to the left 1 square to the right 2 squares to the left

Hare

Fig. 8.21 | Rules for moving the tortoise and the hare. Use variables to keep track of the positions of the animals (i.e., position numbers are 1–70). Start each animal at position 1 (i.e., the “starting gate”). If an animal slips left before square 1, move the animal back to square 1. Generate the percentages in the preceding table by producing a random integer i in the range 1 ≤i ≤10. For the tortoise, perform a “fast plod” when 1 ≤ i ≤ 5, a “slip” when 6 ≤ i ≤ 7 or a “slow plod” when 8 ≤ i ≤ 10. Use a similar technique to move the hare. Begin the race by printing BANG !!!!! AND THEY'R