C++ dem

281 133 0
C++ dem

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Table of Contents BackCover C++ Demystified Introduction Who Should Read this Book What this Book Covers How to Read this Book Special Features Contacting the Author Chapter 1: How a C++ Program Works What Is a Computer Program? What Is a Programming Language? Anatomy of a C++ Program Translating the Code for the Computer Using an IDE to Create and Run the " Hello World! " Project Summary Quiz Chapter 2: Memory and Data Types Memory Data Types Project: Determining the Size of Data Types Summary Quiz Chapter 3: Variables Declaring Variables Assigning Values to Variables Summary Quiz Chapter 4: Arithmetic Operators Arithmetic Operators The Change Machine Project Summary Quiz Chapter 5: Making Decisions: if and switch Statements Relational Operators Flowcharting The if Statement The if / else Statement The if /else if /else Statement The switch Statement Summary Quiz Chapter 6: Nested if Statements and Logical Operators Nested if Statements Logical Operators Using the switch Statement with Logical Operators Summary Quiz Chapter 7: The For Loop Increment and Decrement Operators The For Loop Summary Quiz Chapter 8: While and Do While Loops The While Loop The Do While Loop Summary Quiz Chapter 9: Functions Defining and Calling a Function Variable Scope and Lifetime Sending Information to a Function Returning a Value from a Function Summary Quiz Chapter 10: Arrays Declaring an Array Initialization Assigning and Displaying Array Values Passing Arrays as Function Arguments Summary Quiz Chapter 11: What's the Address? Pointers Declaring a Pointer Assigning a Value to a Pointer Indirection Operator and Dereferencing The Pointer as a Variable or a Constant Pointer Arithmetic Pointers as Function Arguments Dynamic Memory Allocation Returning Pointers from Functions Summary Quiz Chapter 12: Character, C-String, and C++ String Class Functions Reading a Character Useful Character Functions Useful C-String and C++ String Functions Summary Quiz Chapter 13: Persistent Data: File Input and Output Text vs Binary Files The fstream Standard Library The File Access Life Cycle Opening a File Closing a File Writing to a File Reading from a File File Stream Objects as Function Arguments Summary Quiz Chapter 14: The Road Ahead: Structures and Classes Your Reasons for Reading This Book? Object-Oriented Programming Structures Classes Summary Quiz Final Exam Answers to Quizzes and Final Exam Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Final Exam Index Index_A Index_B Index_C Index_D Index_E Index_F Index_G Index_H Index_I Index_K Index_L Index_M Index_N Index_O Index_P Index_R Index_S Index_T Index_U Index_V Index_W List of Figures List of Tables C++ Demystified: A Self-Teaching Guide ISBN:0072253703 by Jeff Kent McGraw-Hill/Osborne © 2004 This hands-on, step-by-step resource will guide you through each phase of C++ programming, providing you with the foundation to discover how computer programs and programming languages work Table of Contents C++ Demystified Introduction Chapter - How a C++ Program Works Chapter - Memory and Data Types Chapter - Variables Chapter - Arithmetic Operators Chapter - Making Decisions: if and switch Statements Chapter - Nested if Statements and Logical Operators Chapter - The For Loop Chapter - While and Do While Loops Chapter - Functions Chapter 10 - Arrays Chapter 11 - What’s the Address? Pointers Chapter 12 - Character, C-String, and C++ String Class Functions Chapter 13 - Persistent Data: File Input and Output Chapter 14 - The Road Ahead: Structures and Classes Final Exam Answers to Quizzes and Final Exam Index List of Figures List of Tables Back Cover If you’re looking for an easy way to learn C++ and want to immediately start writing your own programs, this is the resource you need The hands-on approach and step-by-step instruction guide you through each phase of C++ programming with easy-tounderstand language from start to finish Whether or not you have previous C++ experience, you’ll get an excellent foundation here, discovering how computer programs and programming languages work Next, you’ll learn the basics of the language—what data types, variables, and operators are and what they do, then on to functions, arrays, loops, and beyond With no unnecessary, time-consuming material included, plus quizzes at the end of each chapter and a final exam, you’ll emerge a C++ pro, completing and running your very own complex programs in no time About the Author Jeff Kent is an Associate Professor of Computer Science at Los Angeles Valley College in Valley Glen, California He teaches a number of programming languages, including Visual Basic, C++, Java and, when he’s feeling masochistic, Assembler, but mostly he teaches C++ He also manages a network for a Los Angeles law firm whose employees are guinea pigs for his applications, and as an attorney gives advice to young attorneys whether they want it or not He also has written several books on computer programming, including the recent Visual Basic.NET A Beginner’s Guide for McGraw-Hill/Osborne Jeff has had a varied career—or careers He graduated from UCLA with a Bachelor of Science degree in economics, then obtained a Juris Doctor degree from Loyola (Los Angeles) School of Law, and went on to practice law C++ Demystified Jeff Kent McGraw-Hill/Osborne New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto McGraw-Hill/Osborne 2100 Powell Street, 10th Floor Emeryville, California 94608 U.S.A To arrange bulk purchase discounts for sales promotions, premiums, or fund-raisers, please contact McGraw-Hill/Osborne at the above address For information on translations or book distributors outside the U.S.A., please see the International Contact Information page immediately following the index of this book Copyright © 2004 by The McGraw-Hill Companies All rights reserved Printed in the United States of America Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication 1234567890 FGR FGR 01987654 ISBN 0-07-225370-3 Publisher Brandon A Nordin Vice President & Associate Publisher Scott Rogers Editorial Director Wendy Rinaldi Project Editor Lisa Wolters-Broder Acquisitions Coordinator Athena Honore Technical Editor Jim Keogh Copy Editor Mike McGee Proofreader Susie Elkind Indexer Irv Hershman Composition Apollo Publishing Services, Lucie Ericksen Illustrators Kathleen Edwards, Melinda Lytle Cover Series Design Margaret Webster-Shapiro Cover Illustration Lance Lekander This book was composed with Corel VENTURA™ Publisher Information has been obtained by McGraw-Hill/Osborne from sources believed to be reliable However, because of the possibility of human or mechanical error by our sources, McGraw-Hill/Osborne, or others, McGraw-Hill/Osborne does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information About the Author Jeff Kent is an Associate Professor of Computer Science at Los Angeles Valley College in Valley Glen, California He teaches a number of programming languages, including Visual Basic, C++, Java and, when he’s feeling masochistic, Assembler, but mostly he teaches C++ He also manages a network for a Los Angeles law firm whose employees are guinea pigs for his applications, and as an attorney gives advice to young attorneys whether they want it or not He also has written several books on computer programming, including the recent Visual Basic.NET A Beginner’s Guide for McGraw-Hill/Osborne Jeff has had a varied career—or careers He graduated from UCLA with a Bachelor of Science degree in economics, then obtained a Juris Doctor degree from Loyola (Los Angeles) School of Law, and went on to practice law During this time, when personal computers still were a gleam in Bill Gates’s eye, Jeff was also a professional chess master, earning a third-place finish in the United States Under-21 Championship and, later, an international title Jeff does find time to spend with his wife, Devvie, which is not difficult since she also is a computer science professor at Valley College He also acts as personal chauffeur for his teenaged daughter, Emily (his older daughter, Elise, now has her own driver’s license) and in his remaining spare time enjoys watching international chess tournaments on the Internet His goal is to resume running marathons, since otherwise, given his losing battle to lose weight, his next book may be Sumo Wrestling Demystified I would like to dedicate this book to my wife, Devvie Schneider Kent There is not room here to describe how she has helped me in my personal and professional life, though I mention several ways in the Acknowledgments She also has been my computer programming teacher in more ways than one; I wouldn’t be writing this and other computer programming books if it wasn’t for her —Jeff Kent Acknowledgments It seems obligatory in acknowledgments for authors to thank their publishers (especially if they want to write for them again), but I really mean it This is my fourth book for McGrawHill/Osborne, and I hope there will be many more It truly is a pleasure to work with professionals who are nice people as well as very good at what they (even when what they are good at is keeping accurate track of the deadlines I miss) I first want to thank Wendy Rinaldi, who got me started with McGraw-Hill/Osborne back in 1998 (has it been that long?) Wendy was also my first Acquisitions Editor Indeed, I got started on this book through a telephone call with Wendy at the end of a vacation with my wife, Devvie, who, being in earshot, and with an “are you insane” tone in her voice, asked incredulously, “You’re writing another book?” I also must thank my Acquisitions Coordinator, Athena Honore, and my Project Editor, Lisa Wolters-Broder Both were unfailingly helpful and patient, while still keeping me on track in this deadline-sensitive business (e.g., “I’m so sorry you broke both your arms and legs; you’ll still have the next chapter turned in by this Friday, right?”) Mike McGee did the copyediting, together with Lisa They were kind about my obvious failure during my school days to pay attention to my grammar lessons They improved what I wrote while still keeping it in my words (that way, if something is wrong, it is still my fault) Mike also indicated he liked some of my stale jokes, which makes him a friend for life Jim Keogh was my technical editor Jim and I had a balance of terror going between us, in that while he was tech editing this book, I was tech editing two books on which he was the main author, Data Structures Demystified and OOP Demystified Seriously, Jim’s The significance of dynamic memory allocation is not scope, but lifetime Like a global variable or a static local variable, the lifetime of a dynamically created variable is as long as that of the program’s execution However, if before the end of the program the pointer that points to a dynamically created variable goes out of scope, you no longer have any way of accessing the dynamically created memory Therefore, the dynamically created variable still takes up memory, but is inaccessible This is called a memory leak Having programs that dynamically allocate memory but never release it is akin to a library where patrons check out books but never return them Sooner or later the library will run out of books, and the computer will run out of memory A memory leak is not a particular concern in the preceding program since the pointer that points to the dynamically allocated memory does not go out of scope until immediately before the program ends However, if you dynamically allocate memory inside a function using a local pointer (as in a program in the next section), then when the function terminates, the pointer will be destroyed but the memory will remain, orphaned since there is no longer a way of accessing it for the remainder of the program You release dynamically allocated memory with the delete operator Just as the new operator is used to create dynamically allocated memory, the delete operator is used to return dynamically allocated memory to the operating system The syntax is the delete operator followed by the pointer that points to the dynamically created memory Additionally, if the dynamically created memory is an array as opposed to a single variable, then empty subscripts [] are placed between the delete operator and the pointer, as in the following statement from the program: delete [] iPtr; While the delete operator operates on a pointer, the delete operator does not delete the pointer Instead, the delete operator deletes the memory at the address pointed to by the pointer You should only use the delete operator with a pointer that points to dynamically Note created memory Using the delete operator with a pointer that points to memory created on the stack rather than from the heap can lead to unpredictable results Finally, since the pointer is the only way to which you can refer to the dynamically allocated variable, you should not change the value of the pointer to point to a different address unless you first assign a different pointer to the dynamically allocated memory Otherwise, you no longer have a way of accessing the dynamically created memory The result would be a memory leak Returning Pointers from Functions In Chapter 10, you learned several ways to initialize a character array The following program shows you an additional way: #include using namespace std; char * setName(); int main (void) { char* str = "Jeff Kent"; cout

Ngày đăng: 25/03/2019, 15:47

Từ khóa liên quan

Mục lục

  • C++ Demystified Jeff Kent McGraw-Hill/Osborne New York €€Chicago €€San Francisco €€Lisbon €€London €€Madrid €€Mexico City €€Milan €€New Delhi €€San Juan €€Seoul €€Singapore €€Sydney €€Toronto McGraw-Hill/Osborne2100 Powell Street, 10th FloorEmeryville, California €94608U.S.A. To arrange bulk purchase discounts for sales promotions, premiums, or fund-raisers, please contact McGraw-Hill/Osborne at the above address. For information on translations or book distributors outside the U.S.A., please see the International Contact Information page immediately following the index of this book. Copyright © 2004 by The McGraw-Hill Companies. All rights reserved. Printed in the United States of America. Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of publisher, with the exception that the program listings may be entered,

  • IntroductionC++ was my first programming language. While I’ve since learned others, I’ve always thought C++ was the “best” programming language, perhaps because of the power it gives the programmer. Of course, this power is a double-edged sword, being also the power to hang yourself if you are not careful. Nonetheless, C++ has always been my favorite programming language. C++ also has been the first choice of others, not just in the business world because of its power, but also in academia. Additionally, many other programming languages, including Java and C#, are based on C++. Indeed, the Java programming language was written using C++. Therefore, knowing C++ also makes learning other programming languages easier. Why Did I Write this Book? Not as a road to riches, fame, or beautiful women. I may be misguided, but I’m not completely delusional. To be sure, there are many introductory level books on C++. Nevertheless, I wrote this book because I believe I bring a different and, I hope,

  • Who Should Read this BookAnyone who will pay for it! Just kidding, though no buyers will be turned away. It is hardly news that publishers and authors want the largest possible audience for their books. Therefore, this section of the introduction usually tells you this book is for you whoever you may be and whatever you do. However, no programming book is for everyone. For example, if you exclusively create game programs using Java, this book may not be for you ⠀琀栀漀甀最栀 戀攀椀渀最 愀 挀漀洀洀甀渀椀琀礀 挀漀氀氀攀最攀 琀攀愀挀栀攀爀 䤀 洀愀礀 戀攀 礀漀甀爀 渀攀砀琀 挀甀猀琀漀洀攀爀 椀昀 礀漀甀 挀爀攀愀琀攀 愀 猀瀀愀挀攀 戀攀愀猀琀猀 瘀猀⸀ 挀漀洀洀甀渀椀琀礀 挀漀氀氀攀最攀 愀搀洀椀渀椀猀琀爀愀琀漀爀猀 最愀洀攀). While this book is, of course, not for everyone, it very well may be for you. Many people need or want to learn C++, either as part of a degree program, job training, or even as a hobby. C++ is not the easiest subject to learn, and unfortunately many books don’t make learning C++ any easier, throwing at you a veritable telephone book of complexity and jargon. By contrast, this book, as it

  • What this Book CoversI strongly believe that the best way to learn programming is to write programs. The concepts covered by the chapters are illustrated by clearly and thoroughly explained code. You can run this code yourself, or use the code as the basis for writing further programs that expand on the covered concepts. Chapter 1 gets you started. This chapter answers questions such as what is a computer program and what is a programming language. It then discusses the anatomy of a basic C++ program, including both the code you see and what happens “under the hood,” explaining how the preprocessor, compiler, and linker work together to translate your code into instructions the computer can understand. Finally, the chapter tells you how to use an integrated development environment ⠀䤀䐀䔀) to create and run a project. Being able to create and run a program that outputs “Hello World!” as in Chapter 1 is a good start. However, most programs require the storing of information of different ty

  • What this Book Covers

  • How to Read this BookI have organized this book to be read from beginning to end. While this may seem patently obvious, my students often express legitimate frustration about books (or teachers) that, in discussing a programming concept, mention other concepts that are covered several chapters later or, even worse, not at all. Therefore, I have endeavored to present the material in a linear, logical progression. This not only avoids the frustration of material that is out of order, but also enables you in each succeeding chapter to build on the skills you learned in the preceding chapters.

  • Special FeaturesThroughout each chapter are Notes, Tips, and Cautions, as well as detailed code listings. To provide you with additional opportunities to review, there is a Quiz at the end of each chapter and a Final Exam ⠀昀漀甀渀搀 椀渀 琀栀攀 昀椀爀猀琀 愀瀀瀀攀渀搀椀砀) at the end of this book. Answers to both are contained in the following appendix. The overall objective is to get you up to speed quickly, without a lot of dry theory or unnecessary detail. So let’s get started. It’s easy and fun to write C++ programs.

  • Contacting the AuthorHmmm… it depends why. Just kidding. While I always welcome gushing praise and shameless flattery, comments, suggestions, and yes, even criticism also can be valuable. The best way to contact me is via e-mail; you can use jkent@genghiskhent.com ⠀琀栀攀 搀漀洀愀椀渀 渀愀洀攀 椀猀 戀愀猀攀搀 漀渀 洀礀 猀琀甀搀攀渀琀猠ᤀ 昀漀渀搀 渀椀挀欀渀愀洀攀 昀漀爀 洀攀). Alternately, you can visit my web site, http://www.genghiskhent.com/. Don’t be thrown off by the entry page; I use this site primarily to support the online classes and online components of other classes that I teach at the college, but there will be a link to the section that supports this book. I hope you enjoy this book as much as I enjoyed writing it.

  • Chapter 1: How a C++ Program Works Overview You probably interact with computer programs many times during an average day. When you arrive at work and find out your computer doesn’t work, you call tech support. At the other end of the telephone line, a computer program forces you to navigate a voicemail menu maze and then tortures you while you are on perpetual hold with repeated insincere messages about how important your call is, along with false promises about how soon you will get through. When you’re finally done with tech support, you decide to take a break and log on to your now-working computer to do battle with giant alien insects from the planet Megazoid. Unfortunately, the network administrator catches you goofing off using yet another computer program which monitors employee computer usage. Assuming you are still employed, an accounts payable program then generates your payroll check. On your way home, you decide you need some cash and stop at an ATM, where a computer progr

  • Chapter 1: How a C++ Program Works

  • What Is a Computer Program?Computers are so widespread in our society because they have three advantages over us humans. First, computers can store huge amounts of information. Second, they can recall that information quickly and accurately. Third, computers can perform calculations with lightning speed and perfect accuracy. The advantages that computers have over us even extend to thinking sports like chess. In 1997, the computer Deep Blue beat the world chess champion, Garry Kasparov, in a chess match. In 2003, Kasparov was out for revenge against another computer, Deep Junior, but only drew the match. Kasparov, while perhaps the best chess player ever, is only human, and therefore no match for the computer’s ability to calculate and remember prior games. However, we have one very significant advantage over computers. We think on our own, while computers don’t, at least not yet anyway. Indeed, computers fundamentally are far more brawn than brain. A computer cannot do anything withou

  • What Is a Programming Language?When you enter a darkened room and want to see what is inside, you turn on a light switch. When you leave the room, you turn the light switch off. The first computers were not too different than that light switch. These early computers consisted of wires and switches in which the electrical current followed a path dependent on which switches were in the on ⠀漀渀攀) or off ⠀稀攀爀漀) position. Indeed, I built such a simple computer when I was a kid ⠀眀栀椀挀栀 愀挀挀漀爀搀椀渀最 琀漀 洀礀 漀眀渀 挀栀椀氀搀爀攀渀 眀愀猀 戀愀挀欀 眀栀攀渀 搀椀渀漀猀愀甀爀猀 猀琀椀氀氀 爀甀氀攀搀 琀栀攀 攀愀爀琀栀). Each switch’s position could be expressed as a number: 1 for the on position, 0 for the off position. Thus, the instructions given to these first computers, in the form of the switches’ positions, essentially were a series of ones and zeroes. Today’s computers, of course, are far more powerful and sophisticated than these early computers. However, the language that computers understand, called machine language, remains the same, essenti

  • Anatomy of a C++ ProgramIt seems to be a tradition in C++ programming books for the first code example to output to a console window the message “Hello World!” ⠀猀栀漀眀渀 椀渀 䘀椀最甀爀攀 ㄀ⴀ㄀). Figure 1-1: C++ program outputting “Hello World!” to the screen Note  The term “console” goes back to the days before Windows when the screen did not have menus and toolbars but just text. If you have typed commands using DOS or UNIX, you likely did so in a console window. The text “Press any key to continue” immediately following “Hello World!” is not part of the program, but instead is a cue for how to close the console window. Unfortunately, all too often the “Hello World!” example is followed quickly by many other program examples without the book or teacher first stopping to explain how the “Hello World!” program works. The result soon is a confused reader or student who’s ready to say “Goodbye, Cruel World.” While the “Hello World!” program looks simple, there actually is a lot going on behind the sc

  • Translating the Code for the Computer While you now understand the “Hello World!” code, the computer won’t. Computers don’t understand C++ or any other programming language. They understand only machine language. Three programs are used to translate your source code into an executable file that the computer can run. These programs are, in their order of appearance: Preprocessor Compiler Linker Preprocessor The preprocessor is a program that scans the source code for preprocessor directives such as include directives. The preprocessor inserts into the source code all files included by the include directives. In this example, the iostream standard library file is included by an include directive. Therefore, the preprocessor directive inserts the contents of that standard library file, including its definition of the cout object, into the source code file. Compiler The compiler is another program that translates the preprocessed source code ⠀琀栀攀 猀漀甀爀挀攀 挀漀搀攀 愀昀琀攀爀 琀栀攀 椀渀猀攀爀琀椀漀渀猀 洀愀搀攀 戀礀 琀栩ਾ㸊敮摯扪ਊ㘵㠠〠潢樊㰼ਯ䑥獴⁛㄰㜠〠删⽘奚‵″㤲〠湵汬崊⽎數琠㘵㤠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘵㜠〠刊⽔楴汥
Using an IDE to Create and Run the “Hello World!” ProjectYou can use any plain-text editor such as Notepad to write the source code. You also can download a free compiler, which usually includes a preprocessor and linker. You then can compile and run your code from the command line. The command line may be, for example, a DOS prompt at which you type a command that specifies the action you want, such as compiling, followed by the name of the file you want to compile. While there is nothing wrong with using a plain-text editor and command line tools, many programmers, including me, prefer to create, compile, and run their programs in a C++ Integrated Development Environment, known by the acronym IDE. The term “integrated” in IDE means that the text editor, preprocessor, compiler, and linker are all together under one ⠀猀漀昀琀眀愀爀攀) roof. Thus, the IDE enables you to create, compile, and run your code using one program rather than separate programs. Additionally, most IDEs have a graphical u⤊㸾੥湤潢樊ਸ਼㔹‰扪਼㰊⽄敳琠嬱㈲‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘶〠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘵㠠〠刊⽔楴汥
Summary Computers can store huge amounts of information, recall that information quickly and accurately, and perform calculations with lightning speed and perfect accuracy. However, computers cannot think by themselves, and need step-by-step instructions from us telling them what to do. These instructions are called a computer program, written by a human computer programmer in a programming language such as C++. A compiler, together with a preprocessor and a linker, translates the computer program into machine language that a computer understands. We then analyzed a C++ program, which outputs “Hello World!” to the screen. The program looks simple, but much is going on behind the scenes. We analyzed that code, line by line. You then created and ran your own “Hello World!” C++ application.⤊㸾੥湤潢樊ਸ਼㘰‰扪਼㰊⽄敳琠嬱㈴‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘶ㄠ〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘵㤠〠刊⽔楴汥
兵楺⁗桡琠楳⁡⁣潭灵瑥爠灲潧牡洿⁎慭攠獥癥牡氠慤癡湴慧敳⁡⁣潭灵瑥爠桡猠潶敲⁨畭慮猠楮⁰牯捥獳楮朠楮景牭慴楯渿⁗桡琠楳⁡⁰牯杲慭浩湧慮杵慧政⁗桹⁩猠䌫⬠愠杯潤⁰牯杲慭浩湧慮杵慧攠瑯敡牮㼠坨慴⁩猠愠晵湣瑩潮㼠䡯眠浡湹慩渠晵湣瑩潮猠獨潵汤⁡⁃⬫⁰牯杲慭⁨慶政⁗桡琠楳⁡⁳瑡湤慲搠汩扲慲礠晩汥㼠坨慴⁩猠瑨攠灵牰潳攠潦⁡渠楮捬畤攠摩牥捴楶政⁗桡琠摯敳⁡⁰牥灲潣敳獯爠摯㼠坨慴⁤潥猠愠捯浰楬敲⁤漿⁗桡琠摯敳⁡楮步爠摯㼩ਾ㸊敮摯扪ਊ㘶ㄠ〠潢樊㰼ਯ䑥獴⁛ㄲ㘠〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㘲‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㘰‰⁒ਯ呩瑬攠⣾＀䌀栀愀瀀琀攀爀 ㈀㨀 䴀攀洀漀爀礀 愀渀搀 䐀愀琀愀 吀礀瀀攀猀 伀瘀攀爀瘀椀攀眀 䄀昀琀攀爀 䤀 眀爀漀琀攀 洀礀 昀椀爀猀琀 戀漀漀欀Ⰰ 䤀 攀砀瀀攀挀琀愀渀琀氀礀 眀愀椀琀攀搀 攀瘀攀爀礀 搀愀礀 昀漀爀 洀礀 洀愀椀氀Ⰰ 栀漀瀀椀渀最 琀漀 爀攀挀攀椀瘀攀 爀攀焀甀攀猀琀猀 昀漀爀 洀礀 愀甀琀漀最爀愀瀀栀⸀ 吀栀攀 爀攀猀甀氀琀 眀愀猀 瀀爀漀漀昀 漀昀 琀栀攀 愀搀愀最攀†ᰀ戀攀 挀愀爀攀昀甀氀 眀栀愀琀 礀漀甀 愀猀欀 昀漀爀⸠ᴀ 䴀礀 洀愀椀氀戀漀砀 眀愀猀 猀琀甀昀昀攀搀 眀椀琀栀 渀甀洀攀爀漀甀猀 爀攀焀甀攀猀琀猀 昀漀爀 洀礀 愀甀琀漀最爀愀瀀栀⸀ 䄀氀愀猀Ⰰ 琀栀攀猀攀 爀攀焀甀攀猀琀猀 挀愀洀攀 昀爀漀洀 琀栀漀猀攀 眀栀漀 眀愀渀琀攀搀 琀漀 猀栀愀爀攀 洀礀 洀漀渀攀礀Ⰰ 渀漀琀 洀礀 昀愀洀攀⸀ 䴀礀 愀甀琀漀最爀愀瀀栀 眀愀猀 爀攀焀甀攀猀琀攀搀 漀渀 挀栀攀挀欀猀 琀漀 瀀愀礀 洀礀 洀漀爀琀最愀最攀Ⰰ 挀爀攀搀椀琀 挀愀爀搀猀Ⰰ 椀渀猀甀爀愀渀挀攀Ⰰ 瀀栀漀渀攀 猀攀爀瘀椀挀攀Ⰰ 攀氀攀挀琀爀椀挀椀琀礀㬀 眀攀氀氀Ⰰ 礀漀甀 最攀琀 琀栀攀 瀀椀挀琀甀爀攀⸀ 吀栀攀猀攀 挀漀洀瀀愀渀椀攀猀 眀栀漀 氀漀瘀攀 猀攀渀搀椀渀最 洀攀 戀椀氀氀猀 挀漀甀氀搀 渀漀琀 瀀漀猀猀椀戀氀礀 欀攀攀瀀 琀爀愀挀欀 漀昀 琀栀攀椀爀 琀栀漀甀猀愀渀搀猀 漀昀 挀甀猀琀漀洀攀爀猀 戀礀 甀猀椀渀最 瀀攀渀挀椀氀 愀渀搀 瀀愀瀀攀爀⸀ 䤀渀猀琀攀愀搀Ⰰ 琀栀攀礀 甀猀攀 挀漀洀瀀甀琀攀爀 瀀爀漀最爀愀洀猀Ⰰ 眀栀椀挀栀 栀愀爀渀攀猀猀 琀栀攀 挀漀洀瀀甀琀攀爠ᤀ猀 愀戀椀氀椀琀礀 琀漀 猀琀漀爀攀 瘀攀爀礀 氀愀爀最攀 愀洀漀甀渀琀猀 漀昀 椀渀昀漀爀洀愀琀椀漀渀 愀渀搀 琀漀 爀攀琀爀椀攀瘀攀 琀栀愀琀 猀琀漀爀攀搀 椀渀昀漀爀洀愀琀椀漀渀 瘀攀爀礀 焀甀椀挀欀氀礀⸀ 圀攀 甀猀攀 漀甀爀 洀攀洀漀爀礀 琀漀 猀琀漀爀攀 愀渀搀 爀攀挀愀氀氀 椀渀昀漀爀洀愀琀椀漀渀⸀ 匀漀 搀漀 挀漀洀瀀甀琀攀爀猀⸀ 䠀漀眀攀瘀攀爀Ⰰ 愀 挀漀洀瀀甀琀攀爠ᤀ猀 洀攀洀漀爀礀 椀猀 瘀攀爀礀 搀椀昀昀攀爀攀渀琀 昀爀漀洀 漀甀爀猀⸀ 吀栀椀猀 挀栀愀瀀琀攀爀 眀椀氀氀 攀砀瀀氀愀椀渀 栀漀眀 愀 挀漀洀瀀甀琀攀爠ᤀ猀 洀攀洀漀爀礀 眀漀爀欀猀⸀ 䤀渀昀漀爀洀愀琀椩ਾ㸊敮摯扪ਊ㘶㈠〠潢樊㰼ਯ䑥獴⁛ㄲ㘠〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㘳‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㘱‰⁒ਯ呩瑬攠⡃桡灴敲′㨠䵥浯特⁡湤⁄慴愠呹灥猩ਾ㸊敮摯扪ਊ㘶㌠〠潢樊㰼ਯ䑥獴⁛ㄳ㔠〠删⽘奚‵′㌵㐠湵汬崊⽎數琠㘶㐠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘶㈠〠刊⽔楴汥
MemoryComputer programs consist of instructions and data. As discussed in Chapter 1, instructions, written in a programming language such as C++ and then translated by the compiler and linker into machine language, give the computer step-by-step directions on what to do. The data is the information that is the subject of the program. For example, if the user of your computer program wants a list of all students with a GPA of 4.0, the data could be a list of all students and their GPAs. The program then would follow instructions to determine and output the list of all students with a GPA of 4.0. The computer program’s instructions and data have to be in the computer’s memory for the program to work. This section will explain the different types of computer memory, as well as how and where instructions and data are stored in computer memory. Types of Memory There are three principal memory locations on your computer. The central processing unit ⠀䌀倀唀) Random access memory ⠀刀䄀䴀) Persistent⤊㸾੥湤潢樊ਸ਼㘴‰扪਼㰊⽄敳琠嬱㔴‰⁒ 塙娠㔠㌹㈰畬汝ਯ乥硴‶㘵‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㘳‰⁒ਯ呩瑬攠⣾＀䐀愀琀愀 吀礀瀀攀猀 吀栀攀 漀渀攀猀 愀渀搀 稀攀爀漀攀猀 琀栀愀琀 洀愀礀 戀攀 猀琀漀爀攀搀 愀琀 愀 洀攀洀漀爀礀 愀搀搀爀攀猀猀 洀愀礀 爀攀瀀爀攀猀攀渀琀 琀攀砀琀Ⰰ 猀甀挀栀 愀猀 洀礀 渀愀洀攀Ⰰ 䨀攀昀昀 䬀攀渀琀⸀ 吀栀攀猀攀 漀渀攀猀 愀渀搀 稀攀爀漀攀猀 椀渀猀琀攀愀搀 洀愀礀 爀攀瀀爀攀猀攀渀琀 愀 眀栀漀氀攀 渀甀洀戀攀爀Ⰰ 猀甀挀栀 愀猀 洀礀 栀攀椀最栀琀 椀渀 椀渀挀栀攀猀Ⰰ 㜀㈀Ⰰ 漀爀 愀 渀甀洀戀攀爀 眀椀琀栀 搀椀最椀琀猀 琀漀 琀栀攀 爀椀最栀琀 漀昀 琀栀攀 搀攀挀椀洀愀氀 瀀漀椀渀琀Ⰰ 猀甀挀栀 愀猀 洀礀 䜀倀䄀 椀渀 栀椀最栀 猀挀栀漀漀氀Ⰰ 眀栀椀挀栀 䤠ᤀ氀氀 猀愀礀 眀愀猀 ㌀⸀㜀㔀 (I honestly don’t remember, it was too long ago⤀⸀ 䄀氀琀攀爀渀愀琀椀瘀攀氀礀Ⰰ 琀栀攀 漀渀攀猀 愀渀搀 稀攀爀漀攀猀 洀愀礀 爀攀瀀爀攀猀攀渀琀 攀椀琀栀攀爀 琀爀甀攀 漀爀 昀愀氀猀攀Ⰰ 猀甀挀栀 愀猀 眀栀攀琀栀攀爀 䤀 愀洀 愀 唀⸀匀⸀ 挀椀琀椀稀攀渀⸀ 䐀愀琀愀 挀漀洀攀猀 椀渀 洀愀渀礀 昀漀爀洀猀Ⰰ 愀渀搀 椀猀 最攀渀攀爀愀氀氀礀 攀椀琀栀攀爀 渀甀洀攀爀椀挀 漀爀 琀攀砀琀甀愀氀⸀ 䄀搀搀椀琀椀漀渀愀氀氀礀Ⰰ 猀漀洀攀 渀甀洀攀爀椀挀 搀愀琀愀 甀猀攀猀 眀栀漀氀攀 渀甀洀戀攀爀猀Ⰰ 猀甀挀栀 愀猀 㘀Ⰰ  Ⰰ 漀爀†ጀ㜀Ⰰ 眀栀椀氀攀 漀琀栀攀爀 渀甀洀攀爀椀挀 搀愀琀愀 甀猀攀猀 昀氀漀愀琀椀渀最ⴀ瀀漀椀渀琀 渀甀洀戀攀爀猀Ⰰ 猀甀挀栀 愀猀 ⸀㘀Ⰰ 㜀⸀㌀Ⰰ 愀渀搀†ጀ㘀⸀㄀⸀ 吀栀攀爀攀 愀爀攀 搀椀昀昀攀爀攀渀琀 搀愀琀愀 琀礀瀀攀猀 昀漀爀 攀愀挀栀 漀昀 琀栀攀 洀愀渀礀 昀漀爀洀猀 漀昀 搀愀琀愀⸀ 吀栀攀 搀愀琀愀 琀礀瀀攀 礀漀甀 挀栀漀漀猀攀 眀椀氀氀 愀昀昀攀挀琀 渀漀琀 漀渀氀礀 琀栀攀 昀漀爀洀 椀渀 眀栀椀挀栀 琀栀攀 搀愀琀愀 椀猀 猀琀漀爀攀搀Ⰰ 戀甀琀 愀氀猀漀 琀栀攀 愀洀漀甀渀琀 漀昀 洀攀洀漀爀礀 爀攀焀甀椀爀攀搀 琀漀 猀琀漀爀攀 琀栀攀 搀愀琀愀⸀ 䰀攀琠ᤀ猀 渀漀眀 琀愀欀攀 愀 氀漀漀欀 愀琀 琀栀攀猀攀 搀椀昀昀攀爀攀渀琀 搀愀琀愀 琀礀瀀攀猀⸀ 圀栀漀氀攀 一甀洀戀攀爀 䐀愀琀愀 吀礀瀀攀猀 圀攀 搀攀愀氩ਾ㸊敮摯扪ਊ㘶㔠〠潢樊㰼ਯ䑥獴⁛ㄷ㌠〠删⽘奚‵′㌵㐠湵汬崊⽎數琠㘶㘠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘶㐠〠刊⽔楴汥
Project: Determining the Size of Data TypesAs discussed in the previous Data Types section, the size of each data type depends on the compiler and operating system you are using. In this project, you will find out the size of each data type on your system by using the sizeof operator. The sizeof Operator The sizeof operator is followed by parentheses, in which you place a data type. It returns the size in bytes of that data type. For example, on my computer, the expression sizeof⠀椀渀琀) returns 4. This means that on my compiler and operating system, the size of an int data type is 4 bytes. Changing the Source File of Your Project Try creating and running the next program using the steps you followed in Chapter 1 to create the “Hello World!” program. While you could start a new project, in this example, you will reuse the project you used in Chapter 1. It is good to know both how to create a new project and how to reuse an existing one. Start Visual C++. Use the File | Open Solution menu⤊㸾੥湤潢樊ਸ਼㘶‰扪਼㰊⽄敳琠嬱㠱‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘶㜠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘶㔠〠刊⽔楴汥
SummaryA computer program’s instructions and data have to be in the computer’s memory for the program to work. There are three principal memory locations on your computer: the central processing unit ⠀䌀倀唀), random access memory ⠀刀䄀䴀), and persistent storage. Computer programs usually use RAM to store instructions and data. Instructions and data are stored at addresses, represented by a sequential series of numbers. A computer stores information in a series of ones and zeroes. Each one or zero is a bit. However, a computer cannot process information as small as a single bit. Eight bits, or one byte, is the smallest unit of information that a computer can process. Therefore, each address stores one byte of information. Some information is numeric; other data is textual. Each type of information is referred to as a data type. The principal data type categories are whole numbers, floating-point numbers, and text. However, all data types have in common a characteristic of size, which is the⤊㸾੥湤潢樊ਸ਼㘷‰扪਼㰊⽄敳琠嬱㠳‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘶㠠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘶㘠〠刊⽔楴汥
兵楺⁆牯洠睨楣栠潦⁴桥⁦潬汯睩湧⁴祰敳映浥浯特⁣慮⁴桥⁃偕潳琠煵楣歬礠慣捥獳⁩湳瑲畣瑩潮猠潲⁤慴愺⁣慣桥敭潲礬⁒䅍Ⱐ潲⁰敲獩獴敮琠獴潲慧政⁗桩捨映瑨攠景汬潷楮朠瑹灥猠潦敭潲礠楳潴⁴敭灯牡特㨠捡捨攠浥浯特Ⱐ剁䴬爠灥牳楳瑥湴⁳瑯牡来㼠坨慴⁩猠瑨攠慭潵湴映楮景牭慴楯渠瑨慴慹⁢攠獴潲敤⁡琠愠灡牴楣畬慲敭潲礠慤摲敳猿⁉猠瑨攠獩穥映愠摡瑡⁴祰攠慬睡祳⁴桥⁳慭攠湯慴瑥爠睨楣栠捯浰畴敲⁹潵慹⁢攠睯牫楮朠潮㼠坨慴⁩猠浥慮琠批⁴桥⁲慮来映愠摡瑡⁴祰政⁗桡琠楳⁴桥⁤楦晥牥湣攠扥瑷敥渠慮⁵湳楧湥搠慮搠獩杮敤⁤慴愠瑹灥㼠坨慴⁤散業慬畭扥爠楳⁲数牥獥湴敤⁢礠㔮ㅅⴳ⁩渠䔠湯瑡瑩潮㼠坨慴⁩猠慮⁁千䥉⁶慬略㼠坨慴⁤潥猠瑨攠獩穥潦灥牡瑯爠摯㼠坨慴⁩猠愠汩瑥牡氠獴物湧㼠坨慴⁩猠慮⁥硰牥獳楯渿⤊㸾੥湤潢樊ਸ਼㘸‰扪਼㰊⽄敳琠嬱㠵‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘶㤠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘶㜠〠刊⽔楴汥
Chapter 3: Variables Overview Recently, while in a crowded room, someone yelled “Hey, you!” I and a number of other people looked up, because none of us could tell to whom the speaker was referring. Had the speaker instead yelled “Hey, Jeff Kent!,” I would have known he was calling me ⠀甀渀氀攀猀猀 漀昀 挀漀甀爀猀攀 琀栀攀爀攀 栀愀瀀瀀攀渀攀搀 琀漀 戀攀 愀渀漀琀栀攀爀 䨀攀昀昀 䬀攀渀琀 椀渀 琀栀攀 爀漀漀洀). We use names to refer to each other. Similarly, when you need to refer in code to a particular item of information among perhaps thousands of items of information, you do so by referring to the name of that information item. You name information by creating a variable. A variable not only gives you a way of referring later to particular information, but also reserves the amount of memory necessary to store that information. This chapter will show you how to create variables, store information in them, and retrieve information from them.⤊㸾੥湤潢樊ਸ਼㘹‰扪਼㰊⽄敳琠嬱㠵‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘷〠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘶㠠〠刊⽔楴汥
䍨慰瑥爠㌺⁖慲楡扬敳⤊㸾੥湤潢樊ਸ਼㜰‰扪਼㰊⽄敳琠嬱㤳‰⁒ 塙娠㔠㈳㔴畬汝ਯ乥硴‶㜱‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㘹‰⁒ਯ呩瑬攠⣾＀䐀攀挀氀愀爀椀渀最 嘀愀爀椀愀戀氀攀猀夀漀甀 氀攀愀爀渀攀搀 椀渀 䌀栀愀瀀琀攀爀 ㈀ 琀栀愀琀 琀栀攀 椀渀昀漀爀洀愀琀椀漀渀 愀 瀀爀漀最爀愀洀 甀猀攀猀 眀栀椀氀攀 椀琀 椀猀 爀甀渀渀椀渀最 昀椀爀猀琀 渀攀攀搀猀 琀漀 戀攀 猀琀漀爀攀搀 椀渀 洀攀洀漀爀礀⸀ 夀漀甀 渀攀攀搀 琀漀 爀攀猀攀爀瘀攀 洀攀洀漀爀礀 戀攀昀漀爀攀 礀漀甀 挀愀渀 猀琀漀爀攀 椀渀昀漀爀洀愀琀椀漀渀 琀栀攀爀攀⸀ 夀漀甀 爀攀猀攀爀瘀攀 洀攀洀漀爀礀 戀礀 搀攀挀氀愀爀椀渀最 愀 瘀愀爀椀愀戀氀攀⸀ 䐀攀挀氀愀爀椀渀最 愀 瘀愀爀椀愀戀氀攀 渀漀琀 漀渀氀礀 爀攀猀攀爀瘀攀猀 洀攀洀漀爀礀Ⰰ 戀甀琀 愀氀猀漀 最椀瘀攀猀 礀漀甀 愀 挀漀渀瘀攀渀椀攀渀琀 眀愀礀 漀昀 爀攀昀攀爀爀椀渀最 琀漀 琀栀愀琀 爀攀猀攀爀瘀攀搀 洀攀洀漀爀礀 眀栀攀渀 礀漀甀 渀攀攀搀 琀漀 搀漀 猀漀 椀渀 礀漀甀爀 瀀爀漀最爀愀洀⸀ 夀漀甀 愀氀猀漀 氀攀愀爀渀攀搀 椀渀 䌀栀愀瀀琀攀爀 ㈀ 琀栀愀琀 洀攀洀漀爀礀 愀搀搀爀攀猀猀攀猀 栀愀瘀攀 栀攀砀愀搀攀挀椀洀愀氀 瘀愀氀甀攀猀 猀甀挀栀 愀猀   ㄀㈀䘀䔀䐀㐀⸀ 吀栀攀猀攀 瘀愀氀甀攀猀 愀爀攀 栀愀爀搀 琀漀 爀攀洀攀洀戀攀爀⸀ 䤀琀 椀猀 洀甀挀栀 攀愀猀椀攀爀 琀漀 爀攀洀攀洀戀攀爀 椀渀昀漀爀洀愀琀椀漀渀 琀栀愀琀Ⰰ 昀漀爀 攀砀愀洀瀀氀攀Ⰰ 爀攀氀愀琀攀猀 琀漀 愀 琀攀猀琀 猀挀漀爀攀 戀礀 琀栀攀 渀愀洀攀 琀攀猀琀匀挀漀爀攀⸀ 䈀礀 搀攀挀氀愀爀椀渀最 愀 瘀愀爀椀愀戀氀攀Ⰰ 礀漀甀 挀愀渀 爀攀昀攀爀 琀漀 琀栀攀 爀攀猀攀爀瘀攀搀 洀攀洀漀爀礀 戀礀 琀栀攀 瘀愀爀椀愀戀氀攠ᤀ猀 渀愀洀攀Ⰰ 眀栀椀挀栀 椀猀 洀甀挀栀 攀愀猀椀攀爀 琀漀 爀攀洀攀洀戀攀爀 愀渀搀 椀搀攀渀琀椀昀礀 眀椀琀栀 琀栀攀 猀琀漀爀攀搀 椀渀昀漀爀洀愀琀椀漀渀 琀栀愀渀 椀猀 琀栀攀 栀攀砀愀搀攀挀椀洀愀氀 愀搀搀爀攀猀猀⸀ 圀栀椀氀攀 搀攀挀氀愀爀椀渀最 愀 瘀愀爀椀愀戀氀攀 椀猀 爀攀氀愀琀椀瘀攀氀礀 猀椀洀瀀氀攀Ⰰ 爀攀焀甀椀爀椀渀最 漀渀氀礀 漀渀攀 氀椀渀攀 漀昀 挀漀搀攀Ⰰ 洀甀挀栀 椀猀 栀愀瀀瀀攀渀椀渀最 戀攀栀椀渀搀 琀栀攀 猀挀攀渀攀猀⸀ 吀栀攀 瀀爀漀最爀愀洀 愀琀 琀栀攀 攀渀搀 漀昀 琀栀椀猀 猀攀挀琀椀漀渀 眀椀氀氩ਾ㸊敮摯扪ਊ㘷ㄠ〠潢樊㰼ਯ䑥獴⁛㈱㐠〠删⽘奚‵‴㜰㌠湵汬崊⽎數琠㘷㈠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘷〠〠刊⽔楴汥
Assigning Values to VariablesThe purpose of a variable is to store information. Therefore, after you have created a variable, the next logical step is to specify the information that the variable will store. This is called assigning a value to a variable. A variable can be assigned a value supplied by the programmer in code. A variable also can be assigned a value by the user, usually via the keyboard, when the program is running. You may use the assignment operator, which is discussed in the next section, to specify the value to be stored in a variable. You use the cin object ⠀搀椀猀挀甀猀猀攀搀 椀渀 琀栀攀 甀瀀挀漀洀椀渀最 猀攀挀琀椀漀渀†ᰀ唀猀椀渀最 琀栀攀 挀椀渀 伀戀樀攀挀琠ᴀ)  after the assignment operator, to obtain the user’s input, usually from the keyboard, and then store that input in a variable. Assignment Operator You use the assignment operator to assign a value to a variable. The syntax is [variable name] = [value]; The assignment operator looks like the equal sign. However, in C++ the = sign is not used to test for e⤊㸾੥湤潢樊ਸ਼㜲‰扪਼㰊⽄敳琠嬲㈶‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘷㌠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘷ㄠ〠刊⽔楴汥
卵浭慲祁⁶慲楡扬攠獥牶敳⁴睯⁰畲灯獥献⁉琠灲潶楤敳⁹潵⁷楴栠愠睡礠潦⁲敦敲物湧⁴漠灡牴楣畬慲⁩湦潲浡瑩潮Ⱐ慮搠慬獯⁲敳敲癥猠瑨攠慭潵湴映浥浯特散敳獡特⁴漠獴潲攠瑨慴⁩湦潲浡瑩潮⸠奯甠浵獴⁣牥慴攠愠癡物慢汥⁢敦潲攠祯甠捡渠獴慲琠畳楮朠楴⸠奯甠捲敡瑥⁡⁶慲楡扬攠批⁤散污物湧⁩琮⁙潵慹⁤散污牥畬瑩灬攠癡物慢汥猠潦⁴桥⁳慭攠瑹灥⁩渠潮攠獴慴敭敮琮⁙潵⁣慮⁵獥⁴桥⁡摤牥獳灥牡瑯爬…Ⱐ瑯⁤整敲浩湥⁴桥⁡摤牥獳映愠癡物慢汥Ⱐ慮搠瑨攠獩穥潦灥牡瑯爠瑯⁤整敲浩湥⁴桥⁳楺攠潦⁡⁶慲楡扬攮⁔桥⁰畲灯獥映愠癡物慢汥⁩猠瑯⁳瑯牥⁩湦潲浡瑩潮⸠周敲敦潲攬⁡晴敲⁹潵⁨慶攠捲敡瑥搠愠癡物慢汥Ⱐ瑨攠湥硴潧楣慬⁳瑥瀠楳⁴漠獰散楦礠瑨攠楮景牭慴楯渠瑨慴⁴桥⁶慲楡扬攠睩汬⁳瑯牥⸠周楳⁩猠捡汬敤⁡獳楧湩湧⁡⁶慬略⁴漠愠癡物慢汥⸠䄠癡物慢汥⁣慮⁢攠慳獩杮敤⁡⁶慬略⁥楴桥爠批⁴桥⁰牯杲慭浥爠楮⁣潤攠潲⁢礠瑨攠畳敲Ⱐ畳畡汬礠癩愠瑨攠步祢潡牤Ⱐ睨敮⁴桥⁰牯杲慭⁩猠牵湮楮朮⁙潵⁵獥⁴桥⁡獳楧湭敮琠潰敲慴潲⁴漠慳獩杮⁡⁶慬略⁳異灬楥搠批⁣潤攮⁙潵⁵獥⁴桥⁣楮扪散琠瑯⁡獳楧渠愠癡汵攠獵灰汩敤⁢礠瑨攠畳敲⸠䥮⁴桥數琠捨慰⤊㸾੥湤潢樊ਸ਼㜳‰扪਼㰊⽄敳琠嬲㈸‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘷㐠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘷㈠〠刊⽔楴汥
Quiz What is the effect of declaring a variable? Can you refer to a variable before declaring it as long as you declare it later? Can you declare several variables in the same statement? What is a “naming convention” with respect to variables? What is the difference between the address and sizeof operators? What is initialization? What is overflow? What is the consequence of using an assignment operator to assign a string value to an integer variable? Do you use the cin object for compile time or run-time assignment of values to variables? Can you use one cin statement to assign values to several variables of different data types?⤊㸾੥湤潢樊ਸ਼㜴‰扪਼㰊⽄敳琠嬲㌰‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘷㔠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘷㌠〠刊⽔楴汥
Chapter 4: Arithmetic Operators Overview When I went to elementary school, which as far as my kids are concerned was when dinosaurs roamed the earth, I had to perform arithmetic calculations by hand or in my head. There were no calculators, only slide rules. ⠀圀愀爀渀椀渀最㨀 夀漀甀 洀愀礀 搀愀琀攀 礀漀甀爀猀攀氀昀 戀礀 攀瘀攀渀 愀搀洀椀琀琀椀渀最 礀漀甀 欀渀漀眀 眀栀愀琀 愀 猀氀椀搀攀 爀甀氀攀 椀猀℀) When it was my kids’ turn to go to school, and I’d ask them to perform an arithmetic calculation while going over their homework or tests, they would whip out a calculator. When I asked them to perform the calculation by hand or in their head, they would look at me with mixed amazement and pity and exclaim “Aw, Dad, no one does it that way anymore.” Maybe my kids were right. When I write computer programs, I don’t do it “that way” anymore either. I let the fastest, most accurate calculator I own do the work: my computer. Many computer programs need to perform calculations. Computers, in addition to being able to store vast amounts of data, also can ca⤊㸾੥湤潢樊ਸ਼㜵‰扪਼㰊⽄敳琠嬲㌰‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘷㘠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘷㐠〠刊⽔楴汥
䍨慰瑥爠㐺⁁物瑨浥瑩挠佰敲慴潲猩ਾ㸊敮摯扪ਊ㘷㘠〠潢樊㰼ਯ䑥獴⁛㈴㐠〠删⽘奚‵‴㜰㌠湵汬崊⽎數琠㘷㜠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘷㔠〠刊⽔楴汥
Arithmetic OperatorsAn operator is a symbol that represents a specific action. We have discussed and used operators in prior chapters, including the assignment operator, =. C++ also supports operators for arithmetic, specifically addition, subtraction, multiplication, and division. Operators used for arithmetic are called, naturally enough, arithmetic operators. Table 4-1 summarizes them. Table 4-1: Arithmetic Operators Operator Purpose Example Result + Addition 5 + 2 7 - Subtraction 5 – 2 3 * Multiplication 5 * 2 10 / Division ⠀儀甀漀琀椀攀渀琀) 5 / 2 2 % Division ⠀刀攀洀愀椀渀搀攀爀) 5 % 2 1 The % operator, also called the modulus operator, may look unfamiliar. It returns the remainder in division, and will be explained in the “Division Operators” section later in this chapter. Arithmetic operators are binary operators because they operate on two operands, binary being a reference to 2, and operand referring to each of the two values that is in the arithmetic expression. For example, in the expressio⤊㸾੥湤潢樊ਸ਼㜷‰扪਼㰊⽄敳琠嬲㘳‰⁒ 塙娠㔠ㄵ㜱畬汝ਯ乥硴‶㜸‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㜶‰⁒ਯ呩瑬攠⡔桥⁃桡湧攠䵡捨楮攠偲潪散瑍礠浯瑨敲⁷慳潴⁡扯癥⁵獩湧⁡⁣桡湧攠浡捨楮攠瑯⁤楳瑲慣琠捲慮歹爠浩獣桩敶潵猠祯畮朠杲慮摣桩汤牥渮⁔桥⁹潵湧獴敲猠灯畲敤⁨畮摲敤猠潦⁰敮湩敳⁩湴漠瑨攠瑯瀠潦⁴桥慣桩湥Ⱐ慮搠睡瑣桥搠睩瑨⁦慳捩湡瑩潮
景牴畮慴敬礬⁹潵湧獴敲猠慲攠敡獩汹⁦慳捩湡瑥搩⁡猠瑨攠浡捨楮攠獯牴敤⁴桥⁰敮湩敳⁩湴漠慭潵湴猠潦⁣桡湧攠瑨慴⁣潵汤⁢攠瑡步渠瑯⁴桥⁢慮欠慮搠數捨慮来搠景爠摯汬慲猬ⁱ畡牴敲猬⁡湤⁢楧来爠汯潴⸠周攠祯畮杳瑥牳⁷敲攠浯瑩癡瑥搠慳⁷敬氠慳⁦慳捩湡瑥搬⁳楮捥⁧略獳⁷桯⁧潴⁴漠步数⁴桥ⁱ畡牴敲猿⁐牯杲慭⁄敳捲楰瑩潮⁔桩猠灲潧牡洠睩汬⁡獫⁴桥⁵獥爠瑯⁩湰畴⁴桥畭扥爠潦⁰敮湩敳⸠奯甠浡礠慳獵浥⁴桥⁵獥爠睩汬⁩湰畴⁡⁰潳楴楶攠睨潬攠湵浢敲⸠周攠捯摥⁴桥渠睩汬畴灵琠瑨攠湵浢敲映摯汬慲猬ⁱ畡牴敲猬⁤業敳Ⱐ湩捫敬猬⁡湤⁰敮湩敳⸠周攠楮灵琠慮搠潵瑰畴⁣潵汤⁢攠䕮瑥爠湵浢敲映灥湮楥猠瑯慫攠捨慮来⁦潲㨠㌸㜠䑯汬慲猺″⁑畡牴敲猺″⁄業敳㨠ㄠ乩捫敬猺‰⁐敮湩敳㨠㈠周攠湥硴⁳散瑩潮⁷楬氠牥灲潤畣攠瑨攠捯摥Ⱐ慮搠瑨攠獥捴楯渠景汬潷楮朠睩汬⁥硰污楮⁴桥⁣潤攮⁈潷敶敲Ⱐ慳⁡⁰牯杲慭浩湧⁣桡汬攩ਾ㸊敮摯扪ਊ㘷㠠〠潢樊㰼ਯ䑥獴⁛㈶㜠〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㜹‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㜷‰⁒ਯ呩瑬攠⣾＀匀甀洀洀愀爀礀䴀愀渀礀 挀漀洀瀀甀琀攀爀 瀀爀漀最爀愀洀猀 渀攀攀搀 琀漀 瀀攀爀昀漀爀洀 挀愀氀挀甀氀愀琀椀漀渀猀⸀ 䌀漀洀瀀甀琀攀爀猀Ⰰ 椀渀 愀搀搀椀琀椀漀渀 琀漀 戀攀椀渀最 愀戀氀攀 琀漀 猀琀漀爀攀 瘀愀猀琀 愀洀漀甀渀琀猀 漀昀 搀愀琀愀Ⰰ 愀氀猀漀 挀愀渀 挀愀氀挀甀氀愀琀攀 昀愀爀 昀愀猀琀攀爀 愀渀搀 洀漀爀攀 愀挀挀甀爀愀琀攀氀礀 琀栀愀渀 眀攀 挀愀渀⸀ 夀漀甀 甀猀攀 愀爀椀琀栀洀攀琀椀挀 漀瀀攀爀愀琀漀爀猀 琀漀 栀愀爀渀攀猀猀 琀栀攀 挀漀洀瀀甀琀攀爠ᤀ猀 挀愀氀挀甀氀愀琀椀渀最 愀戀椀氀椀琀礀⸀ 䌀⬀⬀ 猀甀瀀瀀漀爀琀猀 愀爀椀琀栀洀攀琀椀挀 漀瀀攀爀愀琀漀爀猀 昀漀爀 愀搀搀椀琀椀漀渀Ⰰ 猀甀戀琀爀愀挀琀椀漀渀Ⰰ 洀甀氀琀椀瀀氀椀挀愀琀椀漀渀Ⰰ 愀渀搀 搀椀瘀椀猀椀漀渀⸀ 圀栀椀氀攀 愀搀搀椀琀椀漀渀Ⰰ 猀甀戀琀爀愀挀琀椀漀渀Ⰰ 愀渀搀 洀甀氀琀椀瀀氀椀挀愀琀椀漀渀 攀愀挀栀 栀愀瘀攀 漀渀攀 漀瀀攀爀愀琀漀爀Ⰰ 搀椀瘀椀猀椀漀渀 栀愀猀 琀眀漀⸀ 吀栀攀 ⼀ 漀瀀攀爀愀琀漀爀 最椀瘀攀猀 礀漀甀 琀栀攀 焀甀漀琀椀攀渀琀Ⰰ 眀栀椀氀攀 琀栀攀 ─ (or modulus operator⤀ 最椀瘀攀猀 礀漀甀 琀栀攀 爀攀洀愀椀渀搀攀爀⸀ 吀栀攀 愀爀椀琀栀洀攀琀椀挀 漀瀀攀爀愀琀漀爀猀 愀氀氀 眀漀爀欀 眀椀琀栀 眀栀漀氀攀 渀甀洀戀攀爀 漀瀀攀爀愀渀搀猀⸀ 䄀氀氀 戀甀琀 琀栀攀 洀漀搀甀氀甀猀 漀瀀攀爀愀琀漀爀 愀氀猀漀 眀漀爀欀 眀椀琀栀 昀氀漀愀琀椀渀最 渀甀洀戀攀爀 漀瀀攀爀愀渀搀猀⸀ 吀栀攀 愀搀搀椀琀椀漀渀 漀瀀攀爀愀琀漀爀 愀氀猀漀 眀漀爀欀猀 眀椀琀栀 猀琀爀椀渀最 漀瀀攀爀愀渀搀猀Ⰰ 愀瀀瀀攀渀搀椀渀最 漀渀攀 猀琀爀椀渀最 琀漀 愀渀漀琀栀攀爀⸀ 䌀⬀⬀Ⰰ 甀渀氀椀欀攀 猀漀洀攀 漀琀栀攀爀 瀀爀漀最爀愀洀洀椀渀最 氀愀渀最甀愀最攀猀Ⰰ 搀漀攀猀 渀漀琀 栀愀瘀攀 愀渀 攀砀瀀漀渀攀渀琀 漀瀀攀爀愀琀漀爀⸀ 䤀渀猀琀攀愀搀Ⰰ 椀琀 栀愀猀 愀 戀甀椀氀琀ⴀ椀渀 昀甀渀挀琀椀漀渀 渀愀洀攀搀 瀀漀眀 眀栀椀挀栀 椀猀 搀攀昀椀渀攀搀 椀渀 琀栀攀 猀琀愀渀搀愀爀搀 氀椀戀爀愀爀礀 挀洀愀琀栀⸀ 䤀渀 琀栀攀 渀攀砀琀 挀栀愀瀀琀攀爀Ⰰ 礀漀甀 眀椀氀氀 氀攀愀爀渀 愀戀漀甀琀 爀攀氀愀琀椀漀渀愀氀 愀渀搀 氩ਾ㸊敮摯扪ਊ㘷㤠〠潢樊㰼ਯ䑥獴⁛㈶㤠〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㠰‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㜸‰⁒ਯ呩瑬攠⡑畩稠坨楣栠潦⁴桥⁦潵爠慲楴桭整楣灥牡瑩潮猠桡猠浯牥⁴桡渠潮攠潰敲慴潲㼠坨楣栠潦⁴桥⁡物瑨浥瑩挠潰敲慴潲猠捡渠潰敲慴攠潮⁳瑲楮朠慳⁷敬氠慳畭敲楣灥牡湤猿⁗桩捨映瑨攠慲楴桭整楣灥牡瑯牳⁣慮湯琠桡癥⁡⁦汯慴楮朠灯楮琠潰敲慮搿⁗桩捨映瑨攠慲楴桭整楣灥牡瑯牳⁣慮湯琠桡癥⁡⁺敲漠慳⁡⁳散潮搠潰敲慮搿⁁獳畭楮朠瑯瑡氠楳⁡⁶慲楡扬攬⁨潷⁥汳攠捯畬搠祯甠數灲敳猠楮⁣潤攠瑯瑡氠㴠瑯瑡氠⬠㈿⁗桡琠楳⁴桥⁲敳畬琠潦′‫″‪‴㼠坨慴⁩猠瑨攠牥獵汴映瑨攠數灲敳獩潮‸ ′‪‴㼠坨慴⁩猠瑨攠牥獵汴映瑨攠數灲敳獩潮‱〠⼠㐿⁗桡琠潰敲慴潲爠晵湣瑩潮⁤漠祯甠畳攠瑯⁲慩獥⁡畭扥爠瑯⁡⁣敲瑡楮⁰潷敲㼠坨慴⁩猠慮⁡汧潲楴桭㼩ਾ㸊敮摯扪ਊ㘸〠〠潢樊㰼ਯ䑥獴⁛㈷ㄠ〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㠱‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㜹‰⁒ਯ呩瑬攠⣾＀䌀栀愀瀀琀攀爀 㔀㨀 䴀愀欀椀渀最 䐀攀挀椀猀椀漀渀猀㨀 椀昀 愀渀搀 猀眀椀琀挀栀 匀琀愀琀攀洀攀渀琀猀 伀瘀攀爀瘀椀攀眀 吀栀攀 昀愀洀漀甀猀 瀀漀攀洀†ᰀ吀栀攀 刀漀愀搀 一漀琀 吀愀欀攀渠ᴀ 戀礀 刀漀戀攀爀琀 䘀爀漀猀琀 戀攀最椀渀猀㨀†ᰀ吀眀漀 爀漀愀搀猀 搀椀瘀攀爀最攀搀 椀渀 愀 礀攀氀氀漀眀 眀漀漀搀Ⰰ 愀渀搀 猀漀爀爀礀 䤀 挀漀甀氀搀 渀漀琀 琀爀愀瘀攀氀 戀漀琀栀⸠ᴀ 吀栀椀猀 瀀漀攀洀 椀氀氀甀猀琀爀愀琀攀猀 琀栀愀琀 氀椀昀攀Ⰰ 椀昀 渀漀琀栀椀渀最 攀氀猀攀Ⰰ 瀀爀攀猀攀渀琀猀 甀猀 眀椀琀栀 挀栀漀椀挀攀猀⸀ 匀椀洀椀氀愀爀氀礀Ⰰ 挀漀洀瀀甀琀攀爀 瀀爀漀最爀愀洀猀 瀀爀攀猀攀渀琀 琀栀攀椀爀 甀猀攀爀猀 眀椀琀栀 挀栀漀椀挀攀猀⸀ 匀漀 昀愀爀Ⰰ 昀漀爀 琀栀攀 猀愀欀攀 漀昀 猀椀洀瀀氀椀挀椀琀礀Ⰰ 琀栀攀 昀氀漀眀 漀昀 攀愀挀栀 瀀爀漀最爀愀洀 栀愀猀 昀漀氀氀漀眀攀搀 愀 爀攀氀愀琀椀瘀攀氀礀 猀琀爀愀椀最栀琀 氀椀渀攀Ⰰ 琀愀欀椀渀最 愀 瀀爀攀搀攀琀攀爀洀椀渀攀搀 瀀愀琀栀 昀爀漀洀 戀攀最椀渀渀椀渀最 琀漀 攀渀搀⸀ 䠀漀眀攀瘀攀爀Ⰰ 愀猀 瀀爀漀最爀愀洀猀 戀攀挀漀洀攀 洀漀爀攀 猀漀瀀栀椀猀琀椀挀愀琀攀搀Ⰰ 琀栀攀礀 漀昀琀攀渀 戀爀愀渀挀栀 椀渀 琀眀漀 漀爀 洀漀爀攀 搀椀爀攀挀琀椀漀渀猀 戀愀猀攀搀 漀渀 愀 挀栀漀椀挀攀 愀 甀猀攀爀 洀愀欀攀猀⸀ 䘀漀爀 攀砀愀洀瀀氀攀Ⰰ 眀栀攀渀 䤀 愀洀 戀甀礀椀渀最 戀漀漀欀猀 漀渀氀椀渀攀Ⰰ 䤀 愀洀 瀀爀攀猀攀渀琀攀搀 眀椀琀栀 挀栀漀椀挀攀猀 猀甀挀栀 愀猀 愀搀搀椀渀最 愀渀漀琀栀攀爀 椀琀攀洀 琀漀 洀礀 猀栀漀瀀瀀椀渀最 挀愀爀琀Ⰰ 爀攀挀愀氀挀甀氀愀琀椀渀最 洀礀 琀漀琀愀氀Ⰰ 漀爀 挀栀攀挀欀椀渀最 漀甀琀⸀ 吀栀攀 瀀爀漀最爀愀洀 搀漀攀猀 猀漀洀攀琀栀椀渀最 搀椀昀昀攀爀攀渀琀 椀昀 䤀 愀搀搀 愀渀漀琀栀攀爀 椀琀攀洀 琀漀 洀礀 猀栀漀瀀瀀椀渀最 挀愀爀琀 爀愀琀栀攀爀 琀栀愀渀 挀栀攀挀欀 漀甀琀⸀ 吀栀攀 瀀爀漀最爀愀洀 搀攀琀攀爀洀椀渀攀猀 琀栀攀 愀挀琀椀漀渀 椀琀 琀愀欀攀猀 戀礀 挀漀洀瀀愀爀椀渀最 洀礀 挀栀漀椀挀攀 眀椀琀栀 琀栀攀 瘀愀爀椀漀甀猀 愀氀琀攀爀渀愀琀椀瘀攀猀⸀ 吀栀愀琀 挀漀洀瀀愀爀椀猀漀渀 椀猀 洀愀搀攀 甩ਾ㸊敮摯扪ਊ㘸ㄠ〠潢樊㰼ਯ䑥獴⁛㈷ㄠ〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㠲‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㠰‰⁒ਯ呩瑬攠⡃桡灴敲‵㨠䵡歩湧⁄散楳楯湳㨠楦⁡湤⁳睩瑣栠却慴敭敮瑳⤊㸾੥湤潢樊ਸ਼㠲‰扪਼㰊⽄敳琠嬲㜷‰⁒ 塙娠㔠ㄵ㜱畬汝ਯ乥硴‶㠳‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㠱‰⁒ਯ呩瑬攠⣾＀刀攀氀愀琀椀漀渀愀氀 伀瀀攀爀愀琀漀爀猀圀攀 洀愀欀攀 挀漀洀瀀愀爀椀猀漀渀猀 愀氀氀 琀栀攀 琀椀洀攀Ⰰ 愀渀搀 猀漀 搀漀 瀀爀漀最爀愀洀猀⸀ 䄀 瀀爀漀最爀愀洀 洀愀礀 渀攀攀搀 琀漀 搀攀琀攀爀洀椀渀攀 眀栀攀琀栀攀爀 漀渀攀 瘀愀氀甀攀 椀猀 攀焀甀愀氀 琀漀Ⰰ 最爀攀愀琀攀爀 琀栀愀渀Ⰰ 漀爀 氀攀猀猀 琀栀愀渀 愀渀漀琀栀攀爀 瘀愀氀甀攀⸀ 䘀漀爀 攀砀愀洀瀀氀攀Ⰰ 椀昀 愀 瀀爀漀最爀愀洀 挀愀氀挀甀氀愀琀攀猀 琀栀攀 挀漀猀琀 漀昀 愀 琀椀挀欀攀琀 琀漀 愀 洀漀瘀椀攀 椀渀 眀栀椀挀栀 挀栀椀氀搀爀攀渀 氀攀猀猀 琀栀愀渀 ㄀㈀ 最攀琀 椀渀 昀爀攀攀Ⰰ 椀琀 渀攀攀搀猀 琀漀 昀椀渀搀 漀甀琀 椀昀 琀栀攀 挀甀猀琀漀洀攀爠ᤀ猀 愀最攀 椀猀 氀攀猀猀 琀栀愀渀 ㄀㈀⸀ 倀爀漀最爀愀洀猀 挀漀洀瀀愀爀攀 瘀愀氀甀攀猀 戀礀 甀猀椀渀最 愀 爀攀氀愀琀椀漀渀愀氀 漀瀀攀爀愀琀漀爀⸀ 吀愀戀氀攀 㔀ⴀ㄀ 氀椀猀琀猀 琀栀攀 爀攀氀愀琀椀漀渀愀氀 漀瀀攀爀愀琀漀爀猀 猀甀瀀瀀漀爀琀攀搀 戀礀 䌀⬀⬀㨀 吀愀戀氀攀 㔀ⴀ㄀㨀 刀攀氀愀琀椀漀渀愀氀 伀瀀攀爀愀琀漀爀猀 伀瀀攀爀愀琀漀爀 䴀攀愀渀椀渀最 㸀 䜀爀攀愀琀攀爀 琀栀愀渀 㰀 䰀攀猀猀 琀栀愀渀 㸀㴀 䜀爀攀愀琀攀爀 琀栀愀渀 漀爀 攀焀甀愀氀 琀漀 㰀㴀 䰀攀猀猀 琀栀愀渀 漀爀 攀焀甀愀氀 琀漀 㴀㴀 䔀焀甀愀氀 琀漀 ℀㴀 一漀琀 攀焀甀愀氀 琀漀 刀攀氀愀琀椀漀渀愀氀 䔀砀瀀爀攀猀猀椀漀渀猀 䰀椀欀攀 琀栀攀 愀爀椀琀栀洀攀琀椀挀 漀瀀攀爀愀琀漀爀猀 搀椀猀挀甀猀猀攀搀 椀渀 琀栀攀 氀愀猀琀 挀栀愀瀀琀攀爀Ⰰ 琀栀攀 爀攀氀愀琀椀漀渀愀氀 漀瀀攀爀愀琀漀爀猀 愀爀攀 戀椀渀愀爀礠᐀琀栀愀琀 椀猀Ⰰ 琀栀攀礀 挀漀洀瀀愀爀攀 琀眀漀 漀瀀攀爀愀渀搀猀⸀ 䄀 猀琀愀琀攀洀攀渀琀 眀椀琀栀 琀眀漀 漀瀀攀爀愀渀搀猀 愀渀搀 愀 爀攀氀愀琀椀漀渀愀氀 漀瀀攀爀愀琀漀爀 戀攀琀眀攀攀渀 琀栀攀洀 椀猀 挀愀氀氀攀搀 愀 爀攀氀愀琀椀漀渀愀氀 攀砀瀀爀攀猀猀椀漀渀⸀ 吀栀攀 爀攀猀甀氀琀 漀昀 愀 爀攀氀愀琀椀漀渀愀氀 攀砀瀀爀攀猀猀椀漀渀 椀猀 愀 䈀漀漀氀攀愀渀 瘀愀氀甀攀Ⰰ 搀攀瀀椀挀琀攀搀 愀猀 攀椀琀栀攀爀 琀爀甀攀 漀爀 昀愀氀猀攀⸀ 吀愀戀氀攀 㔀ⴀ㈀ 氀椀猀琀猀 猀攀瘀攀爀愀氩ਾ㸊敮摯扪ਊ㘸㌠〠潢樊㰼ਯ䑥獴⁛㈸㈠〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㠴‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㠲‰⁒ਯ呩瑬攠⣾＀䘀氀漀眀挀栀愀爀琀椀渀最䄀 瀀爀漀最爀愀洀Ⰰ 氀椀欀攀 愀 爀椀瘀攀爀Ⰰ 昀氀漀眀猀 昀爀漀洀 戀攀最椀渀渀椀渀最 琀漀 攀渀搀⸀ 倀爀漀最爀愀洀洀攀爀猀 洀愀礀 昀椀渀搀 椀琀 栀攀氀瀀昀甀氀Ⰰ 戀漀琀栀 椀渀 眀爀椀琀椀渀最 挀漀搀攀 愀渀搀 椀渀 甀渀搀攀爀猀琀愀渀搀椀渀最 猀漀洀攀漀渀攀 攀氀猀攠ᤀ猀 挀漀搀攀Ⰰ 琀漀 瘀椀猀甀愀氀氀礀 搀攀瀀椀挀琀 琀栀攀 昀氀漀眀 漀昀 琀栀攀 瀀爀漀最爀愀洀⸀ 䄀昀琀攀爀 愀氀氀Ⰰ 愀猀 琀栀攀 愀搀愀最攀 最漀攀猀Ⰰ 愀 瀀椀挀琀甀爀攀 椀猀 眀漀爀琀栀 愀 琀栀漀甀猀愀渀搀 眀漀爀搀猀⸀ 吀栀攀 愀戀椀氀椀琀礀 琀漀 瘀椀猀甀愀氀椀稀攀 琀栀攀 昀氀漀眀 漀昀 愀 瀀爀漀最爀愀洀 戀攀挀漀洀攀猀 攀瘀攀渀 洀漀爀攀 栀攀氀瀀昀甀氀 愀猀 眀攀 琀爀愀渀猀椀琀椀漀渀 昀爀漀洀 爀攀氀愀琀椀瘀攀氀礀 猀椀洀瀀氀攀 瀀爀漀最爀愀洀猀 琀栀愀琀 昀氀漀眀 椀渀 愀 猀琀爀愀椀最栀琀 氀椀渀攀 琀漀 洀漀爀攀 挀漀洀瀀氀攀砀 瘀愀爀椀攀琀椀攀猀 琀栀愀琀 戀爀愀渀挀栀 椀渀 搀椀昀昀攀爀攀渀琀 搀椀爀攀挀琀椀漀渀猀 戀愀猀攀搀 漀渀 琀栀攀 瘀愀氀甀攀 漀昀 愀 爀攀氀愀琀椀漀渀愀氀 攀砀瀀爀攀猀猀椀漀渀⸀ 倀爀漀最爀愀洀洀攀爀猀 甀猀攀 愀 昀氀漀眀挀栀愀爀琀 琀漀 瘀椀猀甀愀氀氀礀 搀攀瀀椀挀琀 琀栀攀 昀氀漀眀 漀昀 愀 瀀爀漀最爀愀洀⸀ 䘀氀漀眀挀栀愀爀琀猀 甀猀攀 猀琀愀渀搀愀爀搀椀稀攀搀 猀礀洀戀漀氀猀 瀀爀攀猀挀爀椀戀攀搀 戀礀 琀栀攀 䄀洀攀爀椀挀愀渀 一愀琀椀漀渀愀氀 匀琀愀渀搀愀爀搀 䤀渀猀琀椀琀甀琀攀 (ANSI⤀Ⰰ 眀栀椀挀栀 瀀爀攀猀挀爀椀戀攀猀 漀琀栀攀爀 猀琀愀渀搀愀爀搀猀 眀攀 眀椀氀氀 戀攀 甀猀椀渀最 椀渀 琀栀椀猀 戀漀漀欀⸀ 吀栀攀猀攀 昀氀漀眀挀栀愀爀琀椀渀最 猀礀洀戀漀氀猀 爀攀瀀爀攀猀攀渀琀 搀椀昀昀攀爀攀渀琀 愀猀瀀攀挀琀猀 漀昀 愀 瀀爀漀最爀愀洀Ⰰ 猀甀挀栀 愀猀 琀栀攀 猀琀愀爀琀 漀爀 攀渀搀 漀昀 愀 瀀爀漀最爀愀洀Ⰰ 甀猀攀爀 椀渀瀀甀琀Ⰰ 栀漀眀 椀琀 搀椀猀瀀氀愀礀猀 漀渀 愀 洀漀渀椀琀漀爀Ⰰ 愀渀搀 猀漀 漀渀⸀ 吀栀攀猀攀 猀礀洀戀漀氀猀 愀爀攀 樀漀椀渀攀搀 戀礀 愀爀爀漀眀猀 愀渀搀 漀琀栀攀爀 挀漀渀渀攀挀琀漀爀猀 眀栀椀挀栀 猀栀漀眀 琀栀攀 挀漩ਾ㸊敮摯扪ਊ㘸㐠〠潢樊㰼ਯ䑥獴⁛㈹〠〠删⽘奚‵‱㔷ㄠ湵汬崊⽎數琠㘸㔠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘸㌠〠刊⽔楴汥
The if Statement The if statement is used to execute code only when the value of a relational expression is true. The syntax of an if statement is if ⠀䈀漀漀氀攀愀渀 瘀愀氀甀攀)   statement; Both lines together are called an if statement. The first line consists of the if keyword followed by an expression, such as a relational expression, that evaluates to a Boolean value, true or false. The relational ⠀漀爀 漀琀栀攀爀 䈀漀漀氀攀愀渀) expression must be in parentheses, and should not be terminated with a semicolon. The next line is called a conditional statement. As you may recall from Chapter 1, a statement is an instruction to the computer, directing it to perform a specific action. The statement is conditional because it executes only if the value of the relational expression is true. If the value of the relational expression is false, then the conditional statement is not executed—meaning, it’s essentially skipped. The following program, which tests if a whole number entered by the user is even, illustrates⤊㸾੥湤潢樊ਸ਼㠵‰扪਼㰊⽄敳琠嬲㤹‰⁒ 塙娠㔠ㄵ㜱畬汝ਯ乥硴‶㠶‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㠴‰⁒ਯ呩瑬攠⣾＀吀栀攀 椀昀 ⼀ 攀氀猀攀 匀琀愀琀攀洀攀渀琀 伀渀攀 瀀爀漀戀氀攀洀 眀椀琀栀 琀栀攀 瀀爀漀最爀愀洀 琀栀愀琀 琀攀猀琀猀 眀栀攀琀栀攀爀 愀 渀甀洀戀攀爀 椀猀 攀瘀攀渀 椀猀 琀栀愀琀 琀栀攀爀攀 椀猀 渀漀 漀甀琀瀀甀琀 椀昀 琀栀攀 渀甀洀戀攀爀 椀猀 漀搀搀⸀ 圀栀椀氀攀 琀栀攀爀攀 椀猀 愀 挀漀渀搀椀琀椀漀渀愀氀 猀琀愀琀攀洀攀渀琀 椀昀 琀栀攀 爀攀氀愀琀椀漀渀愀氀 攀砀瀀爀攀猀猀椀漀渀 椀猀 琀爀甀攀Ⰰ 琀栀攀爀攀 椀猀 渀漀 挀漀爀爀攀猀瀀漀渀搀椀渀最 挀漀渀搀椀琀椀漀渀愀氀 猀琀愀琀攀洀攀渀琀 (cout << “The number is odd”⤀ 椀昀 琀栀攀 爀攀氀愀琀椀漀渀愀氀 攀砀瀀爀攀猀猀椀漀渀 椀猀 昀愀氀猀攀⸀ 吀栀攀 猀漀氀甀琀椀漀渀 椀猀 琀漀 愀搀搀 愀渀 攀氀猀攀 瀀愀爀琀 琀漀 琀栀攀 椀昀 猀琀愀琀攀洀攀渀琀⸀ 吀栀攀 爀攀猀甀氀琀 椀猀 愀渀 椀昀 ⼀ 攀氀猀攀 猀琀愀琀攀洀攀渀琀⸀ 吀栀攀 猀礀渀琀愀砀 漀昀 愀渀 椀昀 ⼀ 攀氀猀攀 猀琀愀琀攀洀攀渀琀 椀猀 椀昀 (relational expression⤀ ꀀꀀ挀漀渀搀椀琀椀漀渀愀氀 猀琀愀琀攀洀攀渀琀㬀 攀氀猀攀 ꀀꀀ挀漀渀搀椀琀椀漀渀愀氀 猀琀愀琀攀洀攀渀琀㬀 䄀挀挀漀爀搀椀渀最氀礀Ⰰ 琀栀攀 瀀爀漀最爀愀洀 洀愀礀 戀攀 洀漀搀椀昀椀攀搀 琀漀 愀搀搀 愀渀 攀氀猀攀 瀀愀爀琀 琀漀 琀栀攀 椀昀 猀琀愀琀攀洀攀渀琀㨀 ⌀椀渀挀氀甀搀攀 㰀椀漀猀琀爀攀愀洀㸀 甀猀椀渀最 渀愀洀攀猀瀀愀挀攀 猀琀搀㬀 椀渀琀 洀愀椀渀(void⤀ 笀 ꀀꀀ椀渀琀 渀甀洀㬀 ꀀꀀ挀漀甀琀 㰀㰀 ∀䔀渀琀攀爀 愀 眀栀漀氀攀 渀甀洀戀攀爀㨀 ∀㬀 ꀀꀀ挀椀渀 㸀㸀 渀甀洀㬀 ꀀꀀ椀昀 ( num % 2 == 0 ⤀ ꀀꀀꀀꀀꀀ挀漀甀琀 㰀㰀 ∀吀栀攀 渀甀洀戀攀爀 椀猀 攀瘀攀渀∀ 㰀㰀 攀渀搀氀㬀 ꀀꀀ攀氀猀攀 ꀀꀀꀀꀀꀀ挀漀甀琀 㰀㰀 ∀吀栀攀 渀甀洀戀攀爀 椀猀 漀搀搀∀ 㰀㰀 攀渀搀氀㬀 ꀀꀀ爀攀琀甀爀渀  㬀 紀 刀甀渀 琀栀椀猀 挀漀搀攀⸀ 䤀昀 琀栀攀 椀渀瀀甀琀琀攀搀 渀甀洀戀攀爀 椀猀 攀瘀攀渀Ⰰ 琀栀攀渀 琀栀攀 漀甀琀瀀甀琀 漀渀挀攀 愀最愀椀渀 椀猀†ᰀ吀栀攀 渀甀洀戀攀爀 椀猀 攀瘀攀渀⸠ᴀ 䠀漀眀攀瘀攀爀Ⰰ 椀昀 琀栀攀 渀甀洀戀攀爩ਾ㸊敮摯扪ਊ㘸㘠〠潢樊㰼ਯ䑥獴⁛㌰㠠〠删⽘奚‵‱㔷ㄠ湵汬崊⽎數琠㘸㜠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘸㔠〠刊⽔楴汥
The if /else if /else StatementThe program we used to illustrate the if/else statement involved only two alternatives. Additionally, these alternatives were mutually exclusive; only one could be chosen, not both. A whole number is either even or odd; it can’t be both and there is no third alterative. There are many other examples of only two mutually exclusive alternatives. For example, a person is either dead or alive, male or female, child or adult. However, there are other scenarios where there are more than two, mutually exclusive alternatives. For example, if you take a test, your grade may be one of five types: A, B, C, D, or F. Additionally, these grades are mutually exclusive; you can’t get an A and a C on the same test. Since you can have only one if expression and only one else expression in an if statement, you need another expression for the third and additional alternatives. That expression is else if. You use the if / else if / else statement when there are three or more⤊㸾੥湤潢樊ਸ਼㠷‰扪਼㰊⽄敳琠嬳ㄹ‰⁒ 塙娠㔠㌱㌷畬汝ਯ乥硴‶㠸‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㠶‰⁒ਯ呩瑬攠⡔桥⁳睩瑣栠却慴敭敮瑔桥⁳睩瑣栠獴慴敭敮琠楳⁳業楬慲⁴漠慮⁩映⽥汳攠楦 敬獥⁳瑡瑥浥湴⸠䥴⁥癡汵慴敳⁴桥⁶慬略映慮⁩湴敧敲⁥硰牥獳楯渠慮搠瑨敮⁣潭灡牥猠瑨慴⁶慬略⁴漠瑷漠潲潲攠潴桥爠癡汵敳⁴漠摥瑥牭楮攠睨楣栠捯摥⁴漠數散畴攮⁔桥⁦潬汯睩湧⁰牯杲慭⁳桯睳⁡⁳睩瑣栠獴慴敭敮琠楮⁡捴楯渠楮⁡⁰牯杲慭⁴桡琠摥瑥牭楮敳⁹潵爠慶敲慧攠扡獥搠潮⁹潵爠杲慤攺‣楮捬畤攠㱩潳瑲敡派⁵獩湧慭敳灡捥⁳瑤㬠楮琠浡楮⡶潩搩⁻₠ꁣ桡爠杲慤攻₠ꁣ潵琠㰼•䕮瑥爠祯畲⁧牡摥㨠∻₠ꁣ楮‾㸠杲慤攻₠ꁳ睩瑣栠⡧牡摥⤠ꂠ笠ꂠ捡獥‧䄧㨠ꂠꂠꁣ潵琠㰼•奯畲⁡癥牡来畳琠扥⁢整睥敮‹〠ⴠ㄰〢₠ꂠꂠꂠꂠꀼ㰠敮摬㬠ꂠꂠꁢ牥慫㬠ꂠ捡獥‧䈧㨠ꂠꂠꁣ潵琠㰼•奯畲⁡癥牡来畳琠扥⁢整睥敮‸〠ⴠ㠹∠ꂠꂠꂠꂠꂠ㰼⁥湤氻₠ꂠꂠ扲敡欻₠ꁣ慳攠❃✺₠ꂠꂠ捯畴‼㰠≙潵爠慶敲慧攠浵獴⁢攠扥瑷敥渠㜰‭‷㤢₠ꂠꂠꂠꂠꀼ㰠敮摬㬠ꂠꂠꁢ牥慫㬠ꂠ捡獥‧䐧㨠ꂠꂠꁣ潵琠㰼•奯畲⁡癥牡来畳琠扥⁢整睥敮‶〠ⴠ㘹∠ꂠꂠꂠꂠꂠ㰼⁥湤氻₠ꂠꂠ扲敡欻₠ꁤ敦慵汴㨠ꂠꂠꁣ潵琠㰼•奯畲⁡癥牡来畳琠扥⁢敬潷‶〢‼㰠敮摬㬠ꂠ素ꂠ牥瑵牮‰㬠素䡥牥⁡牥⁳敶敲慬⁳慭灬攠牵湳Ⱐ敡捨⁳数慲慴敤⁢礩ਾ㸊敮摯扪ਊ㘸㠠〠潢樊㰼ਯ䑥獴⁛㌲㜠〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㠹‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㠷‰⁒ਯ呩瑬攠⣾＀匀甀洀洀愀爀礀 䌀漀洀瀀甀琀攀爀 瀀爀漀最爀愀洀猀 甀猀甀愀氀氀礀 搀漀 渀漀琀 琀愀欀攀 愀 瀀爀攀漀爀搀愀椀渀攀搀 瀀愀琀栀 昀爀漀洀 戀攀最椀渀渀椀渀最 琀漀 攀渀搀⸀ 䤀渀猀琀攀愀搀Ⰰ 搀椀昀昀攀爀攀渀琀 挀漀搀攀 攀砀攀挀甀琀攀猀 戀愀猀攀搀 漀渀 挀栀漀椀挀攀猀 洀愀搀攀 戀礀 琀栀攀 甀猀攀爀⸀ 刀攀氀愀琀椀漀渀愀氀 漀瀀攀爀愀琀漀爀猀 愀爀攀 甀猀攀搀 琀漀 挀漀洀瀀愀爀攀 琀栀攀 甀猀攀爠ᤀ猀 挀栀漀椀挀攀 眀椀琀栀 瘀愀爀椀漀甀猀 愀氀琀攀爀渀愀琀椀瘀攀猀⸀ 吀栀攀 椀昀Ⰰ 椀昀⼀攀氀猀攀Ⰰ 椀昀 ⼀攀氀猀攀 椀昀 ⼀攀氀猀攀Ⰰ 愀渀搀 猀眀椀琀挀栀 猀琀愀琀攀洀攀渀琀猀 愀爀攀 甀猀攀搀 琀漀 猀琀爀甀挀琀甀爀攀 琀栀攀 挀漀搀攀 猀漀 搀椀昀昀攀爀攀渀琀 挀漀搀攀 攀砀攀挀甀琀攀猀 搀攀瀀攀渀搀椀渀最 漀渀 眀栀椀挀栀 挀栀漀椀挀攀 眀愀猀 洀愀搀攀⸀ 夀漀甀 愀氀猀漀 氀攀愀爀渀攀搀 愀戀漀甀琀 昀氀漀眀挀栀愀爀琀猀Ⰰ 眀栀椀挀栀 栀攀氀瀀 洀愀欀攀 瀀爀漀最爀愀洀猀 洀漀爀攀 甀渀搀攀爀猀琀愀渀搀愀戀氀攀 戀礀 瘀椀猀甀愀氀氀礀 搀攀瀀椀挀琀椀渀最 琀栀攀 瀀爀漀最爀愀洀 挀漀洀瀀漀渀攀渀琀猀 愀渀搀 昀氀漀眀⸀ 䤀渀 琀栀椀猀 挀栀愀瀀琀攀爀Ⰰ 漀渀氀礀 漀渀攀 挀漀洀瀀愀爀椀猀漀渀 眀愀猀 洀愀搀攀 愀琀 愀 琀椀洀攀⸀ 䠀漀眀攀瘀攀爀Ⰰ 猀漀洀攀琀椀洀攀猀 洀漀爀攀 琀栀愀渀 漀渀攀 挀漀洀瀀愀爀椀猀漀渀 渀攀攀搀猀 琀漀 戀攀 洀愀搀攀⸀ 䘀漀爀 攀砀愀洀瀀氀攀Ⰰ 礀漀甀 愀爀攀 攀氀椀最椀戀氀攀 琀漀 瘀漀琀攀 椀渀 琀栀攀 唀⸀匀⸀ 漀渀氀礀 椀昀 礀漀甀 愀爀攀 愀 挀椀琀椀稀攀渀 愀渀搀 愀爀攀 愀琀 氀攀愀猀琀 ㄀㠀 礀攀愀爀猀 漀氀搀⸀ 夀漀甀 挀愀渀渀漀琀 瘀漀琀攀 甀渀氀攀猀猀 戀漀琀栀 愀爀攀 琀爀甀攀⸀ 䠀漀眀攀瘀攀爀Ⰰ 礀漀甀 洀愀礀 最攀琀 椀渀琀漀 愀 洀漀瘀椀攀 昀爀攀攀 椀昀 礀漀甀 愀爀攀 攀椀琀栀攀爀 愀 猀攀渀椀漀爀 挀椀琀椀稀攀渀 (65 years or older⤀ 漀爀 愀 挀栀椀氀搀 (12 or under⤀⸀ 吀栀甀猀Ⰰ 礀漀甀 最攀琀 椀渀 昀爀攀攀 椀昀 攀椀琀栀攀爀 椀猀 琀爀甀攀⸀ 䤀渀 琀栀攀 渀攀砀琀 挀栀愀瀀琀攀爀Ⰰ 礀漀甀 眀椀氀氀 氀攀愀爀渀 愀戀漀甀琀 栀漀眩ਾ㸊敮摯扪ਊ㘸㤠〠潢樊㰼ਯ䑥獴⁛㌲㤠〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㤰‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㠸‰⁒ਯ呩瑬攠⡑畩稠䡯眠浡湹灥牡湤猠慲攠楮⁡⁲敬慴楯湡氠數灲敳獩潮㼠坨慴⁩猠瑨攠灵牰潳攠潦⁡⁦汯督桡牴㼠坨慴⁩猠瑨攠摡瑡⁴祰攠潦⁴桥⁥硰牥獳楯渠景汬潷楮朠瑨攠楦敹睯牤㼠䥮⁡渠楦 敬獥⁩映⽥汳攠獴慴敭敮琬⁷桩捨⁰慲琠浵獴⁹潵⁨慶攠潮攬⁢畴湬礠潮攬昿⁉渠慮⁩映⽥汳攠楦 敬獥⁳瑡瑥浥湴Ⱐ睨楣栠灡牴慹⁹潵⁨慶攠浯牥⁴桡渠潮攠潦㼠䥮⁡渠楦 敬獥⁩映⽥汳攠獴慴敭敮琬⁷桩捨⁰慲琠浡礠祯甠潭楴㼠䥮⁡⁳睩瑣栠獴慴敭敮琬⁷桡琠楳⁴桥⁲敱畩牥搠摡瑡⁴祰攠潦⁥硰牥獳楯渠景汬潷楮朠瑨攠獷楴捨敹睯牤㼠䥮⁡⁳睩瑣栠獴慴敭敮琬慹⁡渠數灲敳獩潮映瑨攠捨慲慣瑥爠摡瑡⁴祰攠景汬潷⁴桥⁳睩瑣栠步祷潲搿⁉渠愠獷楴捨⁳瑡瑥浥湴Ⱐ浡礠瑨攠數灲敳獩潮⁦潬汯睩湧⁡⁣慳攠步祷潲搠扥⁡⁶慲楡扬政⁗桩捨敹睯牤⁩渠愠獷楴捨⁳瑡瑥浥湴⁣潲牥獰潮摳⁴漠瑨攠敬獥敹睯牤⁩渠慮⁩映⽥汳攠楦 敬獥⁳瑡瑥浥湴㼩ਾ㸊敮摯扪ਊ㘹〠〠潢樊㰼ਯ䑥獴⁛㌳ㄠ〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㤱‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㠹‰⁒ਯ呩瑬攠⣾＀䌀栀愀瀀琀攀爀 㘀㨀 一攀猀琀攀搀 椀昀 匀琀愀琀攀洀攀渀琀猀 愀渀搀 䰀漀最椀挀愀氀 伀瀀攀爀愀琀漀爀猀 伀瘀攀爀瘀椀攀眀 䌀栀愀瀀琀攀爀 㔀 戀攀最愀渀 眀椀琀栀 琀栀攀 漀瀀攀渀椀渀最 眀漀爀搀猀 漀昀 琀栀攀 昀愀洀漀甀猀 瀀漀攀洀†ᰀ吀栀攀 刀漀愀搀 一漀琀 吀愀欀攀渠ᴀ 戀礀 刀漀戀攀爀琀 䘀爀漀猀琀㨀†ᰀ吀眀漀 爀漀愀搀猀 搀椀瘀攀爀最攀搀 椀渀 愀 礀攀氀氀漀眀 眀漀漀搀Ⰰ 愀渀搀 猀漀爀爀礀 䤀 挀漀甀氀搀 渀漀琀 琀爀愀瘀攀氀 戀漀琀栀⸠ᴀ 一漀琀 琀漀 戀攀 愀 瀀漀攀琀爀礀 挀爀椀琀椀挀Ⰰ 戀甀琀 漀昀琀攀渀 琀栀攀爀攀 愀爀攀 洀漀爀攀 琀栀愀渀 琀眀漀 爀漀愀搀猀⸀ 䤀渀 䌀栀愀瀀琀攀爀 㔀Ⰰ 眀攀 攀瘀愀氀甀愀琀攀搀 漀渀氀礀 漀渀攀 䈀漀漀氀攀愀渀 攀砀瀀爀攀猀猀椀漀渀 愀琀 愀 琀椀洀攀Ⰰ 愀渀搀 挀栀漀猀攀 眀栀椀挀栀 漀昀 琀栀攀 琀眀漀 爀漀愀搀猀 漀甀爀 挀漀搀攀 眀漀甀氀搀 琀爀愀瘀攀氀 搀漀眀渀 搀攀瀀攀渀搀椀渀最 漀渀 眀栀攀琀栀攀爀 琀栀攀 攀砀瀀爀攀猀猀椀漀渀 眀愀猀 琀爀甀攀 漀爀 昀愀氀猀攀⸀ 䠀漀眀攀瘀攀爀Ⰰ 猀漀洀攀琀椀洀攀猀 琀眀漀 (or more⤀ 䈀漀漀氀攀愀渀 攀砀瀀爀攀猀猀椀漀渀猀 渀攀攀搀 琀漀 戀攀 攀瘀愀氀甀愀琀攀搀 琀漀 搀攀琀攀爀洀椀渀攀 琀栀攀 瀀愀琀栀 琀栀攀 挀漀搀攀 眀椀氀氀 琀爀愀瘀攀氀⸀ 䘀漀爀 攀砀愀洀瀀氀攀Ⰰ 礀漀甀 愀爀攀 攀氀椀最椀戀氀攀 琀漀 瘀漀琀攀 漀渀氀礀 椀昀 礀漀甀 愀爀攀 愀 挀椀琀椀稀攀渀 愀渀搀 礀漀甀 愀爀攀 愀琀 氀攀愀猀琀 ㄀㠀 礀攀愀爀猀 漀氀搀⸀ 夀漀甀 挀愀渀渀漀琀 瘀漀琀攀 甀渀氀攀猀猀 戀漀琀栀 挀漀渀搀椀琀椀漀渀猀 愀爀攀 琀爀甀攀⸀ 伀琀栀攀爀 琀椀洀攀猀 眀椀琀栀 䈀漀漀氀攀愀渀 攀砀瀀爀攀猀猀椀漀渀猀Ⰰ 礀漀甀 愀爀攀 琀攀猀琀椀渀最 椀昀 攀椀琀栀攀爀 漀昀 琀眀漀 挀漀洀瀀愀爀椀猀漀渀猀 椀猀 琀爀甀攀⸀ 䘀漀爀 攀砀愀洀瀀氀攀Ⰰ 礀漀甀 洀愀礀 最攀琀 椀渀琀漀 愀 洀漀瘀椀攀 昀爀攀攀 椀昀 礀漀甀 愀爀攀 攀椀琀栀攀爀 愀 猀攀渀椀漀爀 挀椀琀椀稀攀渀 (65 years or older⤀ 漀爀 愀 挀栀椀氀搀 (12 or under⤀⸀ 吀栀甀猀Ⰰ 礀漀甀 最攀琀 椀渀 昀爀攀攀 椀昀 攀椀琀栀攀爀 挀漀渀搩ਾ㸊敮摯扪ਊ㘹ㄠ〠潢樊㰼ਯ䑥獴⁛㌳ㄠ〠删⽘奚‵‷㠸畬汝ਯ乥硴‶㤲‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㤰‰⁒ਯ呩瑬攠⡃桡灴敲‶㨠乥獴敤⁩映却慴敭敮瑳⁡湤⁌潧楣慬⁏灥牡瑯牳⤊㸾੥湤潢樊ਸ਼㤲‰扪਼㰊⽄敳琠嬳㐰‰⁒ 塙娠㔠㈳㔴畬汝ਯ乥硴‶㤳‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㤱‰⁒ਯ呩瑬攠⣾＀一攀猀琀攀搀 椀昀 匀琀愀琀攀洀攀渀琀猀䄀渀 椀昀 猀琀愀琀攀洀攀渀琀 洀愀礀 愀瀀瀀攀愀爀 椀渀猀椀搀攀 愀渀漀琀栀攀爀 椀昀 猀琀愀琀攀洀攀渀琀⸀ 圀栀攀渀 琀栀椀猀 椀猀 搀漀渀攀Ⰰ 琀栀攀 椀渀渀攀爀 椀昀 猀琀愀琀攀洀攀渀琀 椀猀 猀愀椀搀 琀漀 戀攀†ᰀ渀攀猀琀攀搠ᴀ 椀渀猀椀搀攀 琀栀攀 漀甀琀攀爀 椀昀 猀琀愀琀攀洀攀渀琀⸀ 夀漀甀 挀愀渀 渀攀猀琀 椀昀 猀琀愀琀攀洀攀渀琀猀 琀漀 搀攀琀攀爀洀椀渀攀 椀昀 戀漀琀栀 漀昀 琀眀漀 䈀漀漀氀攀愀渀 攀砀瀀爀攀猀猀椀漀渀猀 愀爀攀 琀爀甀攀Ⰰ 漀爀 椀昀 攀椀琀栀攀爀 漀昀 琀栀攀 攀砀瀀爀攀猀猀椀漀渀猀 椀猀 琀爀甀攀⸀ 吀攀猀琀椀渀最 椀昀 䈀漀琀栀 䈀漀漀氀攀愀渀 䔀砀瀀爀攀猀猀椀漀渀猀 䄀爀攀 吀爀甀攀 吀栀攀 昀漀氀氀漀眀椀渀最 瀀爀漀最爀愀洀 猀栀漀眀猀 琀栀攀 甀猀攀 漀昀 渀攀猀琀攀搀 椀昀 猀琀愀琀攀洀攀渀琀猀 椀渀 搀攀琀攀爀洀椀渀椀渀最 椀昀 戀漀琀栀 漀昀 琀眀漀 䈀漀漀氀攀愀渀 攀砀瀀爀攀猀猀椀漀渀猀 愀爀攀 琀爀甀攀⸀ 䤀昀 琀栀攀 甀猀攀爠ᤀ猀 椀渀瀀甀琀 椀猀 琀栀愀琀 琀栀攀礀 愀爀攀 愀琀 氀攀愀猀琀 ㄀㠀 礀攀愀爀猀 漀氀搀 愀渀搀 愀 挀椀琀椀稀攀渀Ⰰ 琀栀攀 瀀爀漀最爀愀洀 漀甀琀瀀甀琀猀 琀栀愀琀 琀栀攀礀 愀爀攀 攀氀椀最椀戀氀攀 琀漀 瘀漀琀攀⸀ 伀琀栀攀爀眀椀猀攀Ⰰ 琀栀攀 瀀爀漀最爀愀洀 漀甀琀瀀甀琀猀 琀栀愀琀 琀栀攀礀 愀爀攀 渀漀琀 攀氀椀最椀戀氀攀 琀漀 瘀漀琀攀⸀ ⌀椀渀挀氀甀搀攀 㰀椀漀猀琀爀攀愀洀㸀 甀猀椀渀最 渀愀洀攀猀瀀愀挀攀 猀琀搀㬀 椀渀琀 洀愀椀渀(void⤀ 笀 ꀀꀀ椀渀琀 愀最攀㬀 ꀀꀀ挀栀愀爀 挀栀漀椀挀攀㬀 ꀀꀀ戀漀漀氀 挀椀琀椀稀攀渀㬀 ꀀꀀ挀漀甀琀 㰀㰀 ∀䔀渀琀攀爀 礀漀甀爀 愀最攀㨀 ∀㬀 ꀀꀀ挀椀渀 㸀㸀 愀最攀㬀 ꀀꀀ挀漀甀琀 㰀㰀 ∀䄀爀攀 礀漀甀 愀 挀椀琀椀稀攀渀 (Y/N⤀㨀 ∀㬀 ꀀꀀ挀椀渀 㸀㸀 挀栀漀椀挀攀㬀 ꀀꀀ椀昀 (choice == 'Y'⤀ ꀀꀀꀀꀀꀀ挀椀琀椀稀攀渀 㴀 琀爀甀攀㬀 ꀀꀀ攀氀猀攀 ꀀꀀꀀꀀꀀ挀椀琀椀稀攀渀 㴀 昀愀氀猀攀㬀 ꀀꀀ椀昀 (age >= 18⤀ ꀀꀀꀀꀀꀀ椀昀(citizen == true⤀ ꀀꀀꀀꀀꀀꀀꀀꀀ挀漀甀琀 㰀㰀 ∀夀漀甀 愀爀攀 攀氀椀最椀戩ਾ㸊敮摯扪ਊ㘹㌠〠潢樊㰼ਯ䑥獴⁛㌵㌠〠删⽘奚‵″ㄳ㜠湵汬崊⽎數琠㘹㐠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘹㈠〠刊⽔楴汥
Logical OperatorsC++ has logical operators that enable you to combine comparisons in one if or else if statement. Table 6-1 lists the logical operators supported by C++ and describes what each does. Table 6-1: Logical Operators Operator Name What It Does && And Connects two relational expressions. Both expressions must be true for the overall expression to be true. || Or Connects two relational expressions. If either expression is true, the overall expression is true. ! Not Reverses the “truth” of an expression, making a true expression false, and a false expression true. The && Operator The && operator also is known as the logical And operator. It is a binary operator; it takes two Boolean expressions as operands. It returns true only if both expressions are true. If either expression is false, the overall expression is false. Of course, if both expressions are false, the overall expression is false. Table 6-2 illustrates this. Table 6-2: The Logical And Operator Expression #1 Express⤊㸾੥湤潢樊ਸ਼㤴‰扪਼㰊⽄敳琠嬳㘱‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘹㔠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘹㌠〠刊⽔楴汥
啳楮朠瑨攠獷楴捨⁓瑡瑥浥湴⁷楴梠䱯杩捡沠佰敲慴潲猠周攠獷楴捨⁳瑡瑥浥湴⁷慳⁤楳捵獳敤⁡琠獯浥敮杴栠楮⁃桡灴敲‵⸠䡯睥癥爬⁳漠晡爠楮⁴桩猠捨慰瑥爠楴⁨慳⁢敥渠捯湳灩捵潵猠批⁩瑳⁡扳敮捥⸠䥮⁃桡灴敲‵Ⱐ睥⁤楳捵獳敤⁨潷⁴桥⁳睩瑣栠獴慴敭敮琠睡猠捵浢敲獯浥⁷桥渠摥慬楮朠睩瑨⁡⁲慮来映湵浢敲献⁔桥⁲敡獯渠睡猠瑨慴⁴桥⁣慳攠步祷潲搠捡湮潴⁢攠景汬潷敤⁢礠愠牡湧攠潦畭扥牳⁢散慵獥⁩琠浵獴⁩湳瑥慤⁢攠景汬潷敤⁢礠愠獩湧汥⁩湴敧敲⁣潮獴慮琮⁈潷敶敲Ⱐ瑨攠獷楴捨⁳瑡瑥浥湴慹⁢攠畳敤⁷楴栠數灲敳獩潮猠瑨慴⁵獥⁴桥潧楣慬⁁湤爠佲灥牡瑯爮⁔桥⁲敡獯渠楳⁴桡琠瑨敳攠數灲敳獩潮猠桡癥湬礠潮攠潦⁴睯⁰潳獩扬攠癡汵敳Ⱐ瑲略爠晡汳攮⁔牵攠慮搠晡汳攠慲攠扯瑨⁣潮獴慮瑳㬠瑨攠癡汵攠潦⁴牵攠楳⁡汷慹猠瑲略⁡湤⁴桥⁶慬略映晡汳攠楳⁡汷慹猠晡汳攮⁗桩汥⁴牵攠慮搠晡汳攠慲攠䉯潬敡渠癡汵敳Ⱐ敡捨⁨慳⁡⁣潲牥獰潮摩湧⁩湴敧敲⁶慬略㨠ㄠ慮搠〮⁔桥牥景牥Ⱐ瑨攠捡獥敹睯牤慹⁢攠景汬潷敤⁢礠瑲略爠晡汳攬畳琠慳⁩渠䍨慰瑥爠㔠睨敲攠瑨攠捡獥敹睯牤⁣慮⁢攠景汬潷敤⁢礠愠捨慲慣瑥爠獩湣攠愠捨慲慣瑥爠桡猠愠捯牲敳灯湤楮朠楮⤊㸾੥湤潢樊ਸ਼㤵‰扪਼㰊⽄敳琠嬳㘵‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘹㘠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘹㐠〠刊⽔楴汥
卵浭慲礠䥮⁃桡灴敲‵Ⱐ睥⁥癡汵慴敤湬礠潮攠䉯潬敡渠數灲敳獩潮⁡琠愠瑩浥⁴漠摥瑥牭楮攠睨楣栠潦⁴睯⁡汴敲湡瑩癥⁢汯捫猠潦⁣潤攠獨潵汤⁥硥捵瑥⸠䡯睥癥爬晴敮⁴睯
潲潲攩⁂潯汥慮⁥硰牥獳楯湳敥搠瑯⁢攠敶慬畡瑥搠瑯⁤整敲浩湥⁷桩捨⁢汯捫映捯摥⁳桯畬搠數散畴攮⁉渠瑨攠數慭灬攠楮⁷桩捨⁹潵⁡牥⁥汩杩扬攠瑯⁶潴攠潮汹⁩映瑨攠畳敲⁩猠愠捩瑩穥渠慮搠慴敡獴‱㠠祥慲猠潬搬⁢潴栠䉯潬敡渠數灲敳獩潮猠浵獴⁢攠瑲略⁩渠潲摥爠景爠瑨攠灲潧牡洠瑯畴灵琠瑨慴⁴桥⁵獥爠楳⁥汩杩扬攠瑯⁶潴攮⁉渠慮潴桥爠數慭灬攬⁩渠睨楣栠祯甠来琠楮瑯⁡潶楥⁦牥攠楦⁴桥⁵獥爠楳⁥楴桥爠愠獥湩潲⁣楴楺敮
㘵⁹敡牳爠潬摥爩爠愠捨楬搠⠱㈠潲⁵湤敲⤬⁴桥⁰牯杲慭畴灵瑳⁴桡琠瑨攠畳敲⁧整猠楮瑯⁴桥潶楥⁦牥攠楦⁥楴桥爠䉯潬敡渠數灲敳獩潮⁩猠瑲略⸠周楳⁣桡灴敲⁣潶敲敤⁴睯⁤楦晥牥湴⁡灰牯慣桥猠潦⁥癡汵慴楮朠瑷漠䉯潬敡渠數灲敳獩潮猠瑯⁤整敲浩湥⁷桩捨⁣潤攠獨潵汤⁥硥捵瑥⸠周攠晩牳琠慰灲潡捨敳瑥搠潮攠楦⁳瑡瑥浥湴⁩湳楤攠慮潴桥爮⁔桥⁳散潮搠慰灲潡捨⁩湴牯摵捥搠瑨牥攠汯杩捡氠潰敲慴潲献⁔桥潧楣慬…☠⡁湤⤠潰敲慴潲⁩猠畳敤⁷桥渠扯瑨⁂⤊㸾੥湤潢樊ਸ਼㤶‰扪਼㰊⽄敳琠嬳㘷‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘹㜠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘹㔠〠刊⽔楴汥
Quiz Can you use nested if statements as an alternative to the logical And and Or operators? Can an if statement be nested in the else if or else part of an if / else if / else statement, or just the if part? For which of the logical operators do both Boolean expressions have to be true for the overall Boolean expression to be true? For which of the logical operators do both Boolean expressions have to be false for the overall Boolean expression to be false? Which of the logical operators reverses the “truth” of a Boolean expression, making a true expression false and a false expression true? Assuming resident is a Boolean variable, is if⠀爀攀猀椀搀攀渀琀) the same as if⠀爀攀猀椀搀攀渀琀 㴀㴀 琀爀甀攀)? Which of the logical operators is a unary rather than binary operator? Which of the logical operators has a higher precedence than the relational operators? Which logical operator has a higher precedence, And or Or? Can a Boolean value of either true or false be used following the case keyword in a switch st⤊㸾੥湤潢樊ਸ਼㤷‰扪਼㰊⽄敳琠嬳㘹‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘹㠠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘹㘠〠刊⽔楴汥
Chapter 7: The For Loop Overview Parents customarily remind their children not to repeat themselves. Indeed, parents often illustrate another saying ⠠ᰀ䐀漀 愀猀 䤀 猀愀礀Ⰰ 渀漀琀 愀猀 䤀 搀漠ᴀ) by continually repeating that reminder. This is my nifty way of introducing the idea that, in the world of computers, sometimes you want your code to repeat itself, too. For example, if the user enters invalid data, you may want to ask the user whether they want to retry or quit. If they retry and still enter invalid data, you again would ask the user whether they want to retry or quit. This process keeps repeating until the user either enters valid data or quits. You use a loop to repeat the execution of code statements. A loop in C++ is a structure that repeats the execution of code until a condition becomes false. In the preceding example, the condition is that the data is invalid and the user wants to retry, thus the repeating code is the prompt asking the user whether they want to retry or quit. This chapt⤊㸾੥湤潢樊ਸ਼㤸‰扪਼㰊⽄敳琠嬳㘹‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㘹㤠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘹㜠〠刊⽔楴汥
䍨慰瑥爠㜺⁔桥⁆潲⁌潯瀩ਾ㸊敮摯扪ਊ㘹㤠〠潢樊㰼ਯ䑥獴⁛㌷㜠〠删⽘奚‵′㌵㐠湵汬崊⽎數琠㜰〠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㘹㠠〠刊⽔楴汥
Increment and Decrement OperatorsIncrement means to increase a value by one. Conversely, decrement means to decrease a value by one. C++ has an increment operator that you can use to increase a value by one and a decrement operator that you can use to decrease a value by one. This section will show you how to use both, something that will be useful in the next section on the for loop, which uses increment and decrement operators. The Increment Operator In the following program, the statement num += 1 increases the value of the integer variable num, which was initialized to the value 2, by 1, so the output will be 3. #include <iostream> using namespace std; int main⠀瘀漀椀搀) {   int num = 2;   num += 1;   cout << num;   return 0; } Another way to accomplish the same result is by using the increment operator, ++. The increment operator is unary—that is, it operates on one operand. That operand generally is a whole number variable, such as an int. We can use the increment operator simply by⤊㸾੥湤潢樊਷〰‰扪਼㰊⽄敳琠嬳㤳‰⁒ 塙娠㔠㐷〳畬汝ਯ乥硴‷〱‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‶㤹‰⁒ਯ呩瑬攠⣾＀吀栀攀 䘀漀爀 䰀漀漀瀀䤀昀 礀漀甀 眀愀渀琀攀搀 琀漀 漀甀琀瀀甀琀 琀栀攀 渀甀洀戀攀爀猀 戀攀琀眀攀攀渀 ㄀ 愀渀搀 ㄀ Ⰰ 礀漀甀 挀漀甀氀搀 眀爀椀琀攀 愀 瀀爀漀最爀愀洀 猀甀挀栀 愀猀 琀栀攀 昀漀氀氀漀眀椀渀最㨀 ⌀椀渀挀氀甀搀攀 㰀椀漀猀琀爀攀愀洀㸀 甀猀椀渀最 渀愀洀攀猀瀀愀挀攀 猀琀搀㬀 椀渀琀 洀愀椀渀(void⤀ 笀 ꀀꀀ椀渀琀 渀甀洀 㴀 ㄀㬀 ꀀꀀ挀漀甀琀 㰀㰀 渀甀洀⬀⬀㬀 ꀀꀀ挀漀甀琀 㰀㰀 渀甀洀⬀⬀㬀 ꀀꀀ挀漀甀琀 㰀㰀 渀甀洀⬀⬀㬀 ꀀꀀ挀漀甀琀 㰀㰀 渀甀洀⬀⬀㬀 ꀀꀀ挀漀甀琀 㰀㰀 渀甀洀⬀⬀㬀 ꀀꀀ挀漀甀琀 㰀㰀 渀甀洀⬀⬀㬀 ꀀꀀ挀漀甀琀 㰀㰀 渀甀洀⬀⬀㬀 ꀀꀀ挀漀甀琀 㰀㰀 渀甀洀⬀⬀㬀 ꀀꀀ挀漀甀琀 㰀㰀 渀甀洀⬀⬀㬀 ꀀꀀ挀漀甀琀 㰀㰀 渀甀洀⬀⬀㬀 ꀀꀀ爀攀琀甀爀渀  㬀 紀 䠀漀眀攀瘀攀爀Ⰰ 礀漀甀 挀漀甀氀搀 眀爀椀琀攀 琀栀攀 猀愀洀攀 瀀爀漀最爀愀洀 眀椀琀栀 昀愀爀 氀攀猀猀 挀漀搀攀 戀礀 甀猀椀渀最 愀 昀漀爀 氀漀漀瀀㨀 ⌀椀渀挀氀甀搀攀 㰀椀漀猀琀爀攀愀洀㸀 甀猀椀渀最 渀愀洀攀猀瀀愀挀攀 猀琀搀㬀 椀渀琀 洀愀椀渀(void⤀ 笀 ꀀꀀ昀漀爀 (int num = 1; num <= 10; num++⤀ ꀀꀀꀀꀀꀀ挀漀甀琀 㰀㰀 渀甀洀 㰀㰀 ∀ ∀㬀 ꀀꀀ爀攀琀甀爀渀  㬀 紀 吀栀攀 搀椀昀昀攀爀攀渀挀攀 戀攀琀眀攀攀渀 琀栀攀 琀眀漀 瀀爀漀最爀愀洀猀 戀攀挀漀洀攀猀 洀漀爀攀 瀀爀漀渀漀甀渀挀攀搀 椀昀 礀漀甀 挀栀愀渀最攀 琀栀攀 猀瀀攀挀椀昀椀挀愀琀椀漀渀 昀爀漀洀 漀甀琀瀀甀琀琀椀渀最 琀栀攀 渀甀洀戀攀爀猀 戀攀琀眀攀攀渀 ㄀ 愀渀搀 ㄀  琀漀 漀甀琀瀀甀琀琀椀渀最 琀栀攀 渀甀洀戀攀爀猀 戀攀琀眀攀攀渀 ㄀ 愀渀搀 ㄀  ⸀ 䤀 眀漀渠ᤀ琀 爀攀眀爀椀琀攀 琀栀攀 昀椀爀猀琀 瀀爀漀最爀愀洀 戀攀挀愀甀猀攀 椀琀 眀漀甀氀搀 琀愀欀攀 甀瀀 琀漀漀 洀愀渀礀 瀀愀最攀猀㬀 猀甀昀昀椀挀攀 椀琀 琀漀 猀愀礀Ⰰ 礀漀甀 眀漀甀氀搀 栀愀瘀攀 琀漀 愀搀搀 㤀  洀漀爀攀 挀漀甀琀 猀琀愀琀攀洀攀渀琀猀⸀ 䠀漀眀攀瘀攀爀Ⰰ 琀栀攀 猀愀洀攀 瀀爀漀最爀愀洀 甀猀椀渀最 愀 昀漀爀 氀漀漀瀀 眀漀甀氀搀 戀攀㨀 ⌀椀渀挀氀甀搀攀 㰀椀漀猀琀爀攀愀洀㸀 甀猀椀渀最 渀愀洀攀猩ਾ㸊敮摯扪ਊ㜰ㄠ〠潢樊㰼ਯ䑥獴⁛㐰㔠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷〲‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷〰‰⁒ਯ呩瑬攠⡓畭浡特奯甠畳攠愠汯潰⁴漠牥灥慴⁴桥⁥硥捵瑩潮映捯摥⁳瑡瑥浥湴献⁁潯瀠楳⁡⁳瑲畣瑵牥⁴桡琠牥灥慴猠瑨攠數散畴楯渠潦⁣潤攠畮瑩氠愠捯湤楴楯渠扥捯浥猠晡汳攮⁙潵敡牮敤⁩渠瑨楳⁣桡灴敲⁨潷⁴漠畳攠潮攠瑹灥映汯潰㨠瑨攠景爠汯潰⸠䡯睥癥爬⁢敦潲攠摩獣畳獩湧⁴桥⁦潲潯瀬⁉⁳桯睥搠祯甠桯眠瑯⁵獥⁩湣牥浥湴⁡湤⁤散牥浥湴灥牡瑯牳Ⱐ睨楣栠慲攠畳敤⁩渠景爠慮搠潴桥爠瑹灥猠潦潯灳⸠䤠瑨敮⁥硰污楮敤⁴桥⁤楦晥牥湣攠扥瑷敥渠灲敦楸⁡湤⁰潳瑦楸⁷桥渠畳楮朠瑨攠楮捲敭敮琠慮搠摥捲敭敮琠潰敲慴潲献⁙潵⁡汳漠汥慲湥搠楮⁴桩猠捨慰瑥爠桯眠瑯⁵獥⁴桥⁢牥慫敹睯牤⁴漠灲敭慴畲敬礠瑥牭楮慴攠愠景爠汯潰⁡湤⁴桥⁣潮瑩湵攠步祷潲搠瑯⁰牥浡瑵牥汹⁴敲浩湡瑥⁴桥⁣畲牥湴⁩瑥牡瑩潮映瑨攠汯潰⸠奯甠瑨敮敡牮敤⁨潷⁴漠畳攠瑨攠汯杩捡氠潰敲慴潲猠慳⁡渠慬瑥牮慴楶攠瑯⁴桥⁢牥慫⁡湤⁣潮瑩湵攠步祷潲摳⸠奯甠慬獯敡牮敤⁡扯畴敳瑩湧湥⁦潲潯瀠楮獩摥⁡湯瑨敲⸠周攠景爠汯潰⁧敮敲慬汹⁩猠畳敤⁷桥渠瑨攠汯潰⁷楬氠數散畴攠愠晩硥搠湵浢敲映瑩浥献⁈潷敶敲Ⱐ獯浥瑩浥猠瑨攠湵浢敲映瑩浥猠愠汯潰⁷楬氠數散畴攠楳⁵湰牥摩捴慢汥Ⱐ摥灥湤楮朩ਾ㸊敮摯扪ਊ㜰㈠〠潢樊㰼ਯ䑥獴⁛㐰㜠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷〳‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷〱‰⁒ਯ呩瑬攠⣾＀儀甀椀稀 圀栀愀琀 搀漀攀猀 琀栀攀 椀渀挀爀攀洀攀渀琀 漀瀀攀爀愀琀漀爀 搀漀㼀 圀栀愀琀 搀漀攀猀 琀栀攀 搀攀挀爀攀洀攀渀琀 漀瀀攀爀愀琀漀爀 搀漀㼀 圀栀椀挀栀 漀挀挀甀爀猀 昀椀爀猀琀Ⰰ 搀攀挀爀攀洀攀渀琀椀渀最 漀爀 琀栀攀 漀甀琀瀀甀琀琀椀渀最 漀昀 琀栀攀 瘀愀氀甀攀 漀昀 渀甀洀Ⰰ 椀渀 琀栀攀 猀琀愀琀攀洀攀渀琀 挀漀甀琀 㰀㰀 ⴀⴀ渀甀洀㼀 圀栀愀琀 椀猀 愀渀 椀琀攀爀愀琀椀漀渀㼀 圀栀愀琀 椀猀 琀栀攀 甀猀甀愀氀 瀀甀爀瀀漀猀攀 漀昀 琀栀攀 昀椀爀猀琀 攀砀瀀爀攀猀猀椀漀渀 椀渀 琀栀攀 瀀愀爀攀渀琀栀攀猀攀猀 昀漀氀氀漀眀椀渀最 琀栀攀 昀漀爀 欀攀礀眀漀爀搀㼀 圀栀愀琀 椀猀 琀栀攀 瀀甀爀瀀漀猀攀 漀昀 琀栀攀 猀攀挀漀渀搀 攀砀瀀爀攀猀猀椀漀渀 椀渀 琀栀攀 瀀愀爀攀渀琀栀攀猀攀猀 昀漀氀氀漀眀椀渀最 琀栀攀 昀漀爀 欀攀礀眀漀爀搀㼀 圀栀愀琀 椀猀 琀栀攀 甀猀甀愀氀 瀀甀爀瀀漀猀攀 漀昀 琀栀攀 琀栀椀爀搀 攀砀瀀爀攀猀猀椀漀渀 椀渀 琀栀攀 瀀愀爀攀渀琀栀攀猀攀猀 昀漀氀氀漀眀椀渀最 琀栀攀 昀漀爀 欀攀礀眀漀爀搀㼀 䌀愀渀 漀渀攀 漀爀 洀漀爀攀 漀昀 琀栀攀 攀砀瀀爀攀猀猀椀漀渀猀 椀渀 琀栀攀 瀀愀爀攀渀琀栀攀猀攀猀 昀漀氀氀漀眀椀渀最 琀栀攀 昀漀爀 欀攀礀眀漀爀搀 戀攀 攀洀瀀琀礀㼀 圀栀愀琀 椀猀 琀栀攀 瀀甀爀瀀漀猀攀 漀昀 琀栀攀 戀爀攀愀欀 欀攀礀眀漀爀搀 椀渀 愀 昀漀爀 氀漀漀瀀㼀 圀栀愀琀 椀猀 琀栀攀 瀀甀爀瀀漀猀攀 漀昀 琀栀攀 挀漀渀琀椀渀甀攀 欀攀礀眀漀爀搀 椀渀 愀 昀漀爀 氀漀漀瀀㼀 䤀昀 礀漀甀 眀攀爀攀 最漀椀渀最 琀漀 甀猀攀 渀攀猀琀攀搀 昀漀爀 氀漀漀瀀猀 琀漀 瀀爀椀渀琀 爀漀眀猀 愀渀搀 挀漀氀甀洀渀猀Ⰰ 眀栀椀挀栀 昀漀爀 氀漀漀瀀 眀漀甀氀搀 瀀爀椀渀琀 琀栀攀 挀漀氀甀洀渀猠᐀椀渀渀攀爀 漀爀 漀甀琀攀爀㼩ਾ㸊敮摯扪ਊ㜰㌠〠潢樊㰼ਯ䑥獴⁛㐰㤠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷〴‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷〲‰⁒ਯ呩瑬攠⡃桡灴敲‸㨠坨楬攠慮搠䑯⁗桩汥⁌潯灳⁏癥牶楥眠周攠景爠汯潰⁧敮敲慬汹⁩猠畳敤⁷桥渠瑨攠汯潰⁷楬氠楴敲慴攠愠晩硥搠湵浢敲映瑩浥献⁈潷敶敲Ⱐ獯浥瑩浥猠瑨攠湵浢敲映瑩浥猠愠汯潰⁷楬氠楴敲慴攠楳⁵湰牥摩捴慢汥Ⱐ摥灥湤楮朠潮⁵獥爠楮灵琠摵物湧⁲畮瑩浥⸠䙯爠數慭灬攬⁩渠愠摡瑡⁥湴特⁡灰汩捡瑩潮Ⱐ祯甠浡礠睡湴⁡潯瀠瑨慴Ⱐ異潮⁥湴特映楮癡汩搠摡瑡Ⱐ慳歳⁴桥⁵獥爠睨整桥爠瑨敹⁷慮琠瑯⁲整特爠煵楴Ⱐ慮搠楦⁴桥礠睡湴⁴漠牥瑲礬⁧楶敳⁴桥⁵獥爠慮潴桥爠潰灯牴畮楴礠瑯⁥湴敲⁤慴愮⁔桥畭扥爠潦⁴業敳⁴桩猠汯潰慹⁩瑥牡瑥⁩猠畮灲敤楣瑡扬攬⁳楮捥⁩琠睩汬敥瀠牥灥慴楮朠畮瑩氠瑨攠畳敲⁥楴桥爠敮瑥牳⁶慬楤⁤慴愠潲ⁱ畩瑳⸠周楳⁣桡灴敲⁷楬氠獨潷⁹潵⁨潷⁴漠畳攠瑨攠睨楬攠汯潰Ⱐ睨楣栠楳⁡⁢整瑥爠捨潩捥⁴桡渠愠景爠汯潰⁷桥渠瑨攠湵浢敲映瑩浥猠愠汯潰⁷楬氠楴敲慴攠楳⁵湰牥摩捴慢汥⸠坨楬攠瑨攠瑯瑡氠湵浢敲映汯潰⁩瑥牡瑩潮猠浡礠扥⁵湰牥摩捴慢汥Ⱐ瑨敲攠潦瑥渠慲攠獩瑵慴楯湳⁩渠睨楣栠瑨攠汯潰⁷楬氠楴敲慴攠慴敡獴湣攮⁁渠數慭灬攠楳⁡潯瀠瑨慴⁤楳灬慹猠愠浥湵⁷楴栠癡物潵猠捨潩捥猬⁩湣汵摩湧⁥硩瑩湧⁴桥⁰牯杲慭⸠䥮⤊㸾੥湤潢樊਷〴‰扪਼㰊⽄敳琠嬴〹‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㜰㔠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜰㌠〠刊⽔楴汥
䍨慰瑥爠㠺⁗桩汥⁡湤⁄漠坨楬攠䱯潰猩ਾ㸊敮摯扪ਊ㜰㔠〠潢樊㰼ਯ䑥獴⁛㐲㌠〠删⽘奚‵‴㜰㌠湵汬崊⽎數琠㜰㘠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜰㐠〠刊⽔楴汥
周攠坨楬攠䱯潰⁔桥⁷桩汥潯瀠楳⁳業楬慲⁴漠愠景爠汯潰⁩渠瑨慴⁢潴栠桡癥⁴桥⁴祰楣慬⁣桡牡捴敲楳瑩捳映愠汯潰㨠瑨攠捯摥⁩湳楤攠敡捨⁣潮瑩湵敳⁴漠楴敲慴攠畮瑩氠愠捯湤楴楯渠扥捯浥猠晡汳攮⁔桥⁤楦晥牥湣攠扥瑷敥渠瑨敭⁩猠楮⁴桥⁰慲敮瑨敳敳⁦潬汯睩湧⁴桥⁦潲⁡湤⁷桩汥敹睯牤献⁔桥⁰慲敮瑨敳敳⁦潬汯睩湧⁴桥⁦潲敹睯牤⁣潮獩獴猠潦⁴桲敥⁥硰牥獳楯湳Ⱐ楮楴楡汩穡瑩潮Ⱐ捯湤楴楯測⁡湤⁵灤慴攮⁂礠捯湴牡獴Ⱐ瑨攠灡牥湴桥獥猠景汬潷楮朠瑨攠睨楬攠步祷潲搠捯湳楳瑳湬礠潦⁴桥⁣潮摩瑩潮㬠祯甠桡癥⁴漠瑡步⁣慲攠潦⁡湹⁩湩瑩慬楺慴楯渠慮搠異摡瑥⁥汳敷桥牥⁩渠瑨攠捯摥⸠周楳⁤楦晥牥湣攠楳⁩汬畳瑲慴敤⁢礠瑨攠景汬潷楮朠灲潧牡洠瑨慴畴灵瑳⁴桥畭扥牳⁢整睥敮‱⁡湤‱〮⁃桡灴敲‷⁩湣汵摥搠瑨攠景汬潷楮朠灲潧牡洠瑨慴畴灵瑳⁴桥畭扥牳⁢整睥敮‱⁡湤‱〠畳楮朠瑨攠景爠汯潰⸠⍩湣汵摥‼楯獴牥慭㸠畳楮朠湡浥獰慣攠獴搻⁩湴慩渨癯楤⤠笠ꂠ景爠⡩湴畭‽‱㬠湵洠㰽‱〻畭⬫⤠ꂠꂠꁣ潵琠㰼畭‼㰠∠∻₠ꁲ整畲渠〻⁽⁔桥⁳慭攠灲潧牡洠畳楮朠瑨攠睨楬攠汯潰⁣潵汤⁢攠⍩湣汵摥‼楯獴牥慭㸠畳楮朠湡浥獰慣攠獴搻⁩湴慩渨癯楤⤠笠ꂠ楮⤊㸾੥湤潢樊਷〶‰扪਼㰊⽄敳琠嬴㌷‰⁒ 塙娠㔠ㄵ㜱畬汝ਯ乥硴‷〷‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷〵‰⁒ਯ呩瑬攠⡔桥⁄漠坨楬攠䱯潰周攠摯⁷桩汥潯瀠楳⁳業楬慲⁴漠瑨攠睨楬攠汯潰⸠周攠灲業慲礠摩晦敲敮捥⁩猠瑨慴⁷楴栠愠摯⁷桩汥潯瀠瑨攠捯湤楴楯渠楳⁴敳瑥搠慴⁴桥⁢潴瑯洠潦⁴桥潯瀬⁵湬楫攠愠睨楬攠汯潰⁷桥牥⁴桥⁣潮摩瑩潮⁩猠瑥獴敤⁡琠瑨攠瑯瀮⁔桩猠浥慮猠瑨慴⁡⁤漠睨楬攠汯潰⁷楬氠慬睡祳⁥硥捵瑥⁡琠汥慳琠潮捥Ⱐ睨敲敡猠愠睨楬攠汯潰慹敶敲⁥硥捵瑥⁡琠慬氠楦⁩瑳⁣潮摩瑩潮⁩猠晡汳攠慴⁴桥畴獥琮⁓祮瑡砠周攠獹湴慸映愠摯⁷桩汥潯瀠楳⁤漠笠ꂠ獴慴敭敮琨猩㬠素睨楬攠⡣潮摩瑩潮⤻⁔桥⁤漠步祷潲搠獴慲瑳⁴桥潯瀮⁔桥⁳瑡瑥浥湴爠獴慴敭敮瑳⁢敬潮杩湧⁴漠瑨攠汯潰⁡牥⁥湣汯獥搠楮⁣畲汹⁢牡捥献⁁晴敲⁴桥⁣汯獥⁣畲汹⁢牡捥Ⱐ瑨攠睨楬攠步祷潲搠慰灥慲猬⁦潬汯睥搠批⁴桥⁣潮摩瑩潮⁩渠灡牥湴桥獥猬⁴敲浩湡瑥搠批⁡⁳敭楣潬潮⸠䄠䑯⁗桩汥⁌潯瀠䕸慭灬攠周攠景汬潷楮朠灲潧牡洠楳⁡潤楦楣慴楯渠潦⁴桥湥⁥慲汩敲⁩渠瑨楳⁣桡灴敲⁴桡琠畳敤⁡⁷桩汥潯瀠瑯⁣潮瑩湵攠瑯⁰牯浰琠瑨攠畳敲⁴漠敮瑥爠愠灯獩瑩癥畭扥爠畮瑩氠瑨攠畳敲⁥楴桥爠摩搠獯爠煵楴Ⱐ慮搠瑨敮⁥楴桥爠潵瑰畴瑥搠瑨攠灯獩瑩癥畭扥爠潲⁡敳獡来⁴桡琠瑨攠畳敲⁤楤漩ਾ㸊敮摯扪ਊ㜰㜠〠潢樊㰼ਯ䑥獴⁛㐴㌠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷〸‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷〶‰⁒ਯ呩瑬攠⡓畭浡特⁃桡灴敲‷⁩湴牯摵捥搠瑨攠晩牳琠潦⁳敶敲慬潯灳㨠瑨攠景爠汯潰⸠周攠景爠汯潰⁷潲歳⁷敬氠楮⁳楴畡瑩潮猠睨敲攠瑨攠汯潰⁷楬氠楴敲慴攠愠晩硥搠湵浢敲映瑩浥献⁏晴敮Ⱐ桯睥癥爬⁴桥畭扥爠潦⁴業敳⁡潯瀠睩汬⁩瑥牡瑥⁩猠畮灲敤楣瑡扬攠獩湣攠瑨攠湵浢敲映楴敲慴楯湳⁤数敮摳渠畳敲⁩湰畴⁤畲楮朠牵湴業攮⁏湥⁥硡浰汥⁤楳捵獳敤⁩渠瑨楳⁣桡灴敲⁩猠愠摡瑡⁥湴特⁡灰汩捡瑩潮⁩渠睨楣栠瑨攠汯潰Ⱐ異潮⁥湴特映楮癡汩搠摡瑡Ⱐ慳歳⁴桥⁵獥爠睨整桥爠瑨敹⁷慮琠瑯⁲整特爠煵楴Ⱐ慮搠楦⁴桥礠睡湴⁴漠牥瑲礬⁧楶敳⁴桥⁵獥爠慮潴桥爠潰灯牴畮楴礠瑯⁥湴敲⁤慴愮⁔桥畭扥爠潦⁴業敳⁴桩猠汯潰慹⁩瑥牡瑥⁩猠畮灲敤楣瑡扬攬⁳楮捥⁩琠睩汬敥瀠牥灥慴楮朠畮瑩氠瑨攠畳敲⁥楴桥爠敮瑥牳⁶慬楤⁤慴愠潲ⁱ畩瑳⸠周楳⁣桡灴敲⁳桯睥搠祯甠桯眠瑯⁵獥⁴桥⁷桩汥潯瀬⁷桩捨⁷潲歳⁢整瑥爠瑨慮⁡⁦潲潯瀠睨敮⁴桥畭扥爠潦⁴業敳⁡潯瀠睩汬⁥硥捵瑥⁩猠畮灲敤楣瑡扬攮⁗桩汥⁴桥⁰慲敮瑨敳敳⁦潬汯睩湧⁴桥⁦潲敹睯牤⁣潮獩獴猠潦⁴桲敥⁥硰牥獳楯湳Ⱐ楮楴楡汩穡瑩潮Ⱐ捯湤楴楯測⁡湤⁵灤慴攬⁴桥⁰慲敮瑨敳敳⁦潬汯睩湧⁴桥⁷桩汥敹睯牤⁣潮獩猩ਾ㸊敮摯扪ਊ㜰㠠〠潢樊㰼ਯ䑥獴⁛㐴㔠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷〹‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷〷‰⁒ਯ呩瑬攠⣾＀儀甀椀稀 圀栀椀挀栀 漀昀 琀栀攀 琀栀爀攀攀 氀漀漀瀀猠᐀昀漀爀Ⰰ 眀栀椀氀攀Ⰰ 漀爀 搀漀 眀栀椀氀攠᐀攀砀攀挀甀琀攀猀 愀琀 氀攀愀猀琀 漀渀挀攀㼀 圀栀椀挀栀 漀昀 琀栀攀 琀栀爀攀攀 氀漀漀瀀猠᐀昀漀爀Ⰰ 眀栀椀氀攀Ⰰ 漀爀 搀漀 眀栀椀氀攠᐀椀猀 琀栀攀 戀攀猀琀 挀栀漀椀挀攀 眀栀攀渀 琀栀攀 渀甀洀戀攀爀 漀昀 椀琀攀爀愀琀椀漀渀猀 椀猀 瀀爀攀搀椀挀琀愀戀氀攀㼀 䤀猀 琀栀攀 瀀愀爀攀渀琀栀攀琀椀挀愀氀 攀砀瀀爀攀猀猀椀漀渀 昀漀氀氀漀眀椀渀最 琀栀攀 眀栀椀氀攀 欀攀礀眀漀爀搀 昀漀爀 椀渀椀琀椀愀氀椀稀愀琀椀漀渀Ⰰ 挀漀渀搀椀琀椀漀渀 漀爀 甀瀀搀愀琀攀㼀 䴀愀礀 琀栀攀 瀀愀爀攀渀琀栀攀琀椀挀愀氀 攀砀瀀爀攀猀猀椀漀渀 昀漀氀氀漀眀椀渀最 琀栀攀 眀栀椀氀攀 欀攀礀眀漀爀搀 戀攀 琀爀甀攀Ⰰ 猀甀挀栀 愀猀 眀栀椀氀攀 (true⤀㼀 䌀愀渀 琀栀攀 瀀愀爀攀渀琀栀攀琀椀挀愀氀 攀砀瀀爀攀猀猀椀漀渀 昀漀氀氀漀眀椀渀最 琀栀攀 眀栀椀氀攀 欀攀礀眀漀爀搀 挀漀洀戀椀渀攀 琀眀漀 攀砀瀀爀攀猀猀椀漀渀猀㼀 圀栀愀琀 椀猀 琀栀攀 瀀甀爀瀀漀猀攀 漀昀 琀栀攀 戀爀攀愀欀 欀攀礀眀漀爀搀 椀渀 愀 眀栀椀氀攀 氀漀漀瀀㼀 圀栀愀琀 椀猀 琀栀攀 瀀甀爀瀀漀猀攀 漀昀 琀栀攀 挀漀渀琀椀渀甀攀 欀攀礀眀漀爀搀 椀渀 愀 眀栀椀氀攀 氀漀漀瀀㼀 圀栀愀琀 椀猀 愀 昀氀愀最㼀 䤀昀 礀漀甀 眀攀爀攀 最漀椀渀最 琀漀 甀猀攀 渀攀猀琀攀搀 眀栀椀氀攀 氀漀漀瀀猀 琀漀 瀀爀椀渀琀 爀漀眀猀 愀渀搀 挀漀氀甀洀渀猀Ⰰ 眀栀椀挀栀 昀漀爀 氀漀漀瀀 眀漀甀氀搀 瀀爀椀渀琀 琀栀攀 爀漀眀猀Ⰰ 椀渀渀攀爀 漀爀 漀甀琀攀爀㼀 䐀漀攀猀 愀 瘀愀爀椀愀戀氀攀 搀攀挀氀愀爀攀搀 椀渀猀椀搀攀 琀栀攀 戀漀搀礀 漀昀 愀 搀漀 眀栀椀氀攀 氀漀漀瀀 栀愀瘀攀 猀挀漀瀀攀 椀渀 琀栀攀 瀀愀爀攀渀琀栀攀琀椀挀愀氀 攀砀瀀爀攀猀猀椀漀渀 昀漀氀氀漀眀椀渀最 琀栀攀 眀栀椀氀攀 欀攀礀眀漀爀搀㼩ਾ㸊敮摯扪ਊ㜰㤠〠潢樊㰼ਯ䑥獴⁛㐴㜠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷㄰‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷〸‰⁒ਯ呩瑬攠⣾＀䌀栀愀瀀琀攀爀 㤀㨀 䘀甀渀挀琀椀漀渀猀 伀瘀攀爀瘀椀攀眀 䄀 昀甀渀挀琀椀漀渀 椀猀 愀 最爀漀甀瀀 漀昀 猀琀愀琀攀洀攀渀琀猀 琀栀愀琀 琀漀最攀琀栀攀爀 瀀攀爀昀漀爀洀 愀 琀愀猀欀⸀ 匀漀 昀愀爀Ⰰ 漀甀爀 瀀爀漀最爀愀洀猀 栀愀瘀攀 栀愀搀 漀渀攀 昀甀渀挀琀椀漀渀Ⰰ 洀愀椀渀⸀ 䄀搀搀椀琀椀漀渀愀氀氀礀Ⰰ 愀琀 琀椀洀攀猀 眀攀 栀愀瘀攀 甀猀攀搀 昀甀渀挀琀椀漀渀猀 搀攀昀椀渀攀搀 椀渀 愀 猀琀愀渀搀愀爀搀 氀椀戀爀愀爀礀Ⰰ 猀甀挀栀 愀猀 琀栀攀 瀀漀眀 昀甀渀挀琀椀漀渀 椀渀 琀栀攀 挀洀愀琀栀 氀椀戀爀愀爀礀Ⰰ 甀猀攀搀 琀漀 爀愀椀猀攀 愀 渀甀洀戀攀爀 琀漀 愀 挀攀爀琀愀椀渀 瀀漀眀攀爀⸀ 一漀 瀀爀漀最爀愀洀 渀攀攀搀猀 洀漀爀攀 琀栀愀渀 愀 洀愀椀渀 昀甀渀挀琀椀漀渀⸀ 䠀漀眀攀瘀攀爀Ⰰ 愀猀 礀漀甀 眀爀椀琀攀 洀漀爀攀 挀漀洀瀀氀攀砀 愀渀搀 猀漀瀀栀椀猀琀椀挀愀琀攀搀 瀀爀漀最爀愀洀猀Ⰰ 礀漀甀 洀愀礀 昀椀渀搀 礀漀甀爀 洀愀椀渀 昀甀渀挀琀椀漀渀 戀攀挀漀洀椀渀最 攀砀琀爀攀洀攀氀礀 氀漀渀最⸀ 一攀椀琀栀攀爀 琀栀攀 挀漀洀瀀椀氀攀爀 渀漀爀 琀栀攀 爀甀渀琀椀洀攀 挀愀爀攀猀 椀昀 礀漀甀爀 洀愀椀渀 昀甀渀挀琀椀漀渀 椀猀 猀栀漀爀琀 漀爀 氀漀渀最⸀ 䠀漀眀攀瘀攀爀Ⰰ 礀漀甀 猀栀漀甀氀搀 挀愀爀攀⸀ 䄀 洀愀椀渀 昀甀渀挀琀椀漀渀 琀栀愀琀 挀漀渀琀椀渀甀攀猀 昀漀爀 瀀愀最攀猀 椀猀 搀椀昀昀椀挀甀氀琀 琀漀 甀渀搀攀爀猀琀愀渀搀 漀爀 昀椀砀 椀昀 攀爀爀漀爀猀 愀爀椀猀攀⸀ 䈀礀 愀渀愀氀漀最礀Ⰰ 琀栀椀猀 戀漀漀欀 椀猀 猀攀瘀攀爀愀氀 栀甀渀搀爀攀搀 瀀愀最攀猀 氀漀渀最⸀ 䤀琀 眀漀甀氀搀 戀攀 栀愀爀搀攀爀 琀漀 甀渀搀攀爀猀琀愀渀搀 椀昀 攀愀挀栀 挀栀愀瀀琀攀爀 眀愀猀 渀漀琀 搀椀瘀椀搀攀搀 椀渀琀漀 猀攀挀琀椀漀渀猀⸀ 吀栀椀猀 戀漀漀欀 眀漀甀氀搀 戀攀 猀琀椀氀氀 栀愀爀搀攀爀 琀漀 甀渀搀攀爀猀琀愀渀搀 椀昀 椀琀 挀漀渀猀椀猀琀攀搀 漀昀 漀渀氀礀 漀渀攀Ⰰ 瘀攀爀礀 氀漀渀最 挀栀愀瀀琀攀爀⸀ 䈀礀 搀椀瘀椀搀椀渀最 琀栀椀猀 戀漀漀欠ᤀ猀 挀漀渀琀攀渀琀 椀渀琀漀 挀栀愀瀀琀攀爀猀Ⰰ 愀渀搀 攀愀挀栀 挀栀愀瀀琀攀爀 椀渀琀漀 猀攀挀琀椀漀渀猀Ⰰ 琀栀椀猀 戀漀漀欀 椀猀 攀愀猩ਾ㸊敮摯扪ਊ㜱〠〠潢樊㰼ਯ䑥獴⁛㐴㜠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷ㄱ‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷〹‰⁒ਯ呩瑬攠⡃桡灴敲‹㨠䙵湣瑩潮猩ਾ㸊敮摯扪ਊ㜱ㄠ〠潢樊㰼ਯ䑥獴⁛㐵㘠〠删⽘奚‵′㌵㐠湵汬崊⽎數琠㜱㈠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜱〠〠刊⽔楴汥
Defining and Calling a FunctionImplementing any function in addition to main involves two steps: Defining the function Calling the function The explanation of these steps uses terminology we have not discussed before, so that terminology is reviewed first. Terminology of a Function Let’s look at a simple program with one function, main: #include <iostream> using namespace std; int main ⠀) {   cout << "Hello world!";   return 0; } The first line, int main ⠀), is the function header. Unlike a statement, the function header is not followed by a semicolon. The function header consists of a return type, a function name, and an argument list. The data type int preceding main is the return type, main is the function name, and the parentheses, empty in this example but not always, contains the argument list. A function header always is followed by an open curly brace, which begins the function body. The function body ends with a close curly brace. There may be other open and curly braces betwe⤊㸾੥湤潢樊਷ㄲ‰扪਼㰊⽄敳琠嬴㘶‰⁒ 塙娠㔠㈳㔴畬汝ਯ乥硴‷ㄳ‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷ㄱ‰⁒ਯ呩瑬攠⣾＀嘀愀爀椀愀戀氀攀 匀挀漀瀀攀 愀渀搀 䰀椀昀攀琀椀洀攀 吀栀甀猀 昀愀爀Ⰰ 愀氀氀 瘀愀爀椀愀戀氀攀猀 栀愀瘀攀 戀攀攀渀 搀攀昀椀渀攀搀 愀琀 琀栀攀 琀漀瀀 漀昀 琀栀攀 洀愀椀渀 昀甀渀挀琀椀漀渀⸀ 䤀渀 瀀爀漀最爀愀洀猀 眀栀攀爀攀 琀栀攀 漀渀氀礀 昀甀渀挀琀椀漀渀 椀猀 洀愀椀渀Ⰰ 琀栀漀猀攀 瘀愀爀椀愀戀氀攀猀 挀愀渀 戀攀 愀挀挀攀猀猀攀搀 琀栀爀漀甀最栀漀甀琀 琀栀攀 攀渀琀椀爀攀 瀀爀漀最爀愀洀 猀椀渀挀攀 洀愀椀渀 椀猀 琀栀攀 攀渀琀椀爀攀 瀀爀漀最爀愀洀⸀ 䠀漀眀攀瘀攀爀Ⰰ 漀渀挀攀 眀攀 猀琀愀爀琀 搀椀瘀椀搀椀渀最 甀瀀 琀栀攀 挀漀搀攀 椀渀琀漀 猀攀瀀愀爀愀琀攀 昀甀渀挀琀椀漀渀猀Ⰰ 椀猀猀甀攀猀 愀爀椀猀攀 挀漀渀挀攀爀渀椀渀最 瘀愀爀椀愀戀氀攀 猀挀漀瀀攀 愀渀搀 氀椀昀攀琀椀洀攀⸀ 吀栀攀 椀猀猀甀攀 漀昀 瘀愀爀椀愀戀氀攀 猀挀漀瀀攀 眀愀猀 椀渀琀爀漀搀甀挀攀搀 椀渀 䌀栀愀瀀琀攀爀 㠀 椀渀 挀漀渀渀攀挀琀椀漀渀 眀椀琀栀 琀栀攀 搀漀 眀栀椀氀攀 氀漀漀瀀⸀ 吀栀攀 椀猀猀甀攀 漀昀 瘀愀爀椀愀戀氀攀 氀椀昀攀琀椀洀攀 椀猀 渀攀眀⸀ 䰀漀挀愀氀 嘀愀爀椀愀戀氀攀猀 夀漀甀 挀愀渀 挀愀氀氀 琀栀攀 猀愀洀攀 昀甀渀挀琀椀漀渀 洀甀氀琀椀瀀氀攀 琀椀洀攀猀⸀ 吀栀攀 昀漀氀氀漀眀椀渀最 瀀爀漀最爀愀洀 愀琀琀攀洀瀀琀猀 琀漀 挀愀氀氀 琀栀攀 瀀爀椀渀琀䴀攀猀猀愀最攀 昀甀渀挀琀椀漀渀 椀渀 愀 氀漀漀瀀 甀渀琀椀氀 琀栀攀 甀猀攀爀 搀攀挀椀搀攀猀 琀漀 猀琀漀瀀Ⰰ 愀渀搀 攀愀挀栀 琀椀洀攀 漀甀琀瀀甀琀猀 琀栀攀 渀甀洀戀攀爀 漀昀 琀椀洀攀猀 琀栀攀 瀀爀椀渀琀䴀攀猀猀愀最攀 昀甀渀挀琀椀漀渀 栀愀猀 戀攀攀渀 挀愀氀氀攀搀⸀ 吀栀攀 最漀愀氀 椀猀 琀栀愀琀 琀栀攀 昀椀爀猀琀 琀椀洀攀 琀栀攀 瀀爀椀渀琀䴀攀猀猀愀最攀 昀甀渀挀琀椀漀渀 椀猀 挀愀氀氀攀搀Ⰰ 琀栀攀 漀甀琀瀀甀琀 眀椀氀氀 戀攀†ᰀ吀栀椀猀 昀甀渀挀琀椀漀渀 挀愀氀氀攀搀 ㄀ 琀椀洀攀猠ᴀ (pardon the bad grammar⤀Ⰰ 琀栀攀 猀攀挀漀渀搀 琀椀洀攀 琀栀攀 瀀爀椀渀琀䴀攀猀猀愀最攀 昀甀渀挀琀椀漀渀 椀猀 挀愀氀氀攀搀Ⰰ 琀栀攀 漀甀琀瀀甀琀 眀椀氀氀 戀攀†ᰀ吀栀椀猀 昀甀渀挀琀椀漀渀 挀愀氀氀攀搀 ㈀ 琀椀洀攀猩ਾ㸊敮摯扪ਊ㜱㌠〠潢樊㰼ਯ䑥獴⁛㐸㌠〠删⽘奚‵″㤲〠湵汬崊⽎數琠㜱㐠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜱㈠〠刊⽔楴汥
Sending Information to a Function The printMessage function in the Hello World program outputs “Hello world!” It does not need any further information to do its job. Let’s make the printMessage function more useful so that it does not always output “Hello world” but instead outputs whatever message we ask it to. Of course, the printMessage function is not a mind reader; we need to tell it the message we want it to output. Let’s try to write a program in which the user enters in main the string to be outputted, that user input is stored in a string variable str, and then the printMessage function attempts to output the value of that str variable. One approach is to make the variable str global so it can be accessed in both the main and printMessage functions: #include <iostream> #include <string> using namespace std; void printMessage⠀);   string str; int main ⠀) {      cout << "Enter a string: ";   cin >> str;   printMessage⠀ );   return 0; } void printMessage ⠀ ) {   cout << "You inpu⤊㸾੥湤潢樊਷ㄴ‰扪਼㰊⽄敳琠嬴㤳‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㜱㔠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜱㌠〠刊⽔楴汥
剥瑵牮楮朠愠噡汵攠晲潭⁡⁆畮捴楯渠䅲杵浥湴猠慲攠畳敤⁴漠灡獳⁶慬略猠瑯⁡⁣慬汥搠晵湣瑩潮⸠䄠牥瑵牮⁶慬略⁣慮⁢攠畳敤⁴漠灡獳⁡⁶慬略⁦牯洠愠捡汬敤⁦畮捴楯渠扡捫⁴漠瑨攠晵湣瑩潮⁴桡琠捡汬敤⁩琮⁆潲⁥硡浰汥Ⱐ楮⁴桥⁰牥癩潵猠灲潧牡洠瑨攠晵湣瑩潮⁡摤乵浢敲猠桡搠瑨牥攠慲杵浥湴猬⁴桥⁦楲獴⁴睯⁢敩湧⁴桥畭扥牳⁴漠扥⁡摤敤Ⱐ瑨攠瑨楲搠扥楮朠瑨敩爠獵洮⁔桥⁦潬汯睩湧⁰牯杲慭潤楦楥猠瑨攠灲敶楯畳湥⁢礠敬業楮慴楮朠瑨攠瑨楲搠慲杵浥湴Ⱐ扵琠慤摩湧⁡⁲整畲渠癡汵攠瑯⁴桥⁦畮捴楯渺‣楮捬畤攠㱩潳瑲敡派⁵獩湧慭敳灡捥⁳瑤㬠楮琠慤摎畭扥牳⡩湴Ⱐ楮琩㬠ꀠ楮琠浡楮
⤠笠ꂠ楮琠晩牳瑎畭Ⱐ獥捯湤乵洬⁳畭‽‰㬠ꂠ捯畴‼㰠≅湴敲⁦楲獴畭扥爺•㬠ꂠ捩渠㸾⁦楲獴乵活₠ꁣ潵琠㰼•䕮瑥爠獥捯湤畭扥爺•㬠ꂠ捩渠㸾⁳散潮摎畭㬠ꂠ獵洠㴠慤摎畭扥牳
晩牳瑎畭Ⱐ獥捯湤乵洩㬠ꂠ捯畴‼㰠晩牳瑎畭‼㰠∠⬠∠㰼⁳散潮摎畭‼㰠∠㴠∠㰼⁳畭㬠ꂠ牥瑵牮‰㬠素楮琠慤摎畭扥牳
楮琠砬⁩湴⁹⤠笠ꂠ牥瑵牮⁸‫⁹㬠素周攠獡浰汥⁩湰畴⁡湤畴灵琠浡礠扥⁴桥⁳慭攠慳⁩渠瑨攠灲敶楯畳⁰牯杲慭㨠䕮瑥爠晩牳琠湵浢敲㨠㌠䕮瑥爠晩牳琠湵浢敲㨠㘠㌠⬠㘠㴠㤠周攠牥瑵牮⤊㸾੥湤潢樊਷ㄵ‰扪਼㰊⽄敳琠嬴㤸‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㜱㘠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜱㐠〠刊⽔楴汥
卵浭慲礠䄠晵湣瑩潮⁩猠愠杲潵瀠潦⁳瑡瑥浥湴猠瑨慴⁴潧整桥爠灥牦潲洠愠瑡獫⸠坨楬攠湯⁰牯杲慭敥摳潲攠瑨慮⁡慩渠晵湣瑩潮Ⱐ慳⁹潵⁷物瑥潲攠捯浰汥砠慮搠獯灨楳瑩捡瑥搠灲潧牡浳Ⱐ祯畲⁣潤攠睩汬⁢攠敡獩敲⁴漠睲楴攬⁵湤敲獴慮搬⁡湤⁦楸⁩映祯甠摩癩摥⁴桥⁣潤攠異⁡浯湧⁤楦晥牥湴⁦畮捴楯湳Ⱐ敡捨⁦畮捴楯渠灥牦潲浩湧⁡⁳灥捩晩挠瑡獫⸠奯甠業灬敭敮琠愠晵湣瑩潮⁩渠慤摩瑩潮⁴漠浡楮⁢礠晩牳琠摥晩湩湧⁩琠慮搠瑨敮⁣慬汩湧⁩琮⁁⁦畮捴楯渠摥晩湩瑩潮⁣潮獩獴猠潦⁡⁦畮捴楯渠桥慤敲⁡湤⁡⁦畮捴楯渠扯摹⸠周攠晵湣瑩潮⁨敡摥爠捯湳楳瑳映愠牥瑵牮⁴祰攬⁡⁦畮捴楯渠湡浥Ⱐ慮搠慮⁡牧畭敮琠汩獴⸠周攠晵湣瑩潮⁨敡摥爠慬睡祳⁩猠景汬潷敤⁢礠慮灥渠捵牬礠扲慣攬⁷桩捨⁢敧楮猠瑨攠晵湣瑩潮⁢潤礮⁔桥⁦畮捴楯渠扯摹⁥湤猠睩瑨⁡⁣汯獥⁣畲汹⁢牡捥⁡湤⁣潮瑡楮猠潮攠潲潲攠獴慴敭敮瑳Ⱐ来湥牡汬礠敮摩湧⁷楴栠愠牥瑵牮⁳瑡瑥浥湴⸠䅤摩瑩潮慬汹Ⱐ畮汥獳⁴桥⁦畮捴楯渠楳⁤敦楮敤⁡扯癥⁷桥牥⁩琠楳⁣慬汥搬⁩琠浵獴⁢攠灲潴潴祰敤⸠䥮⁰牯杲慭猠睨敲攠瑨攠潮汹⁦畮捴楯渠楳慩測⁡汬⁶慲楡扬敳⁤敦楮敤⁡琠瑨攠瑯瀠潦⁴桡琠晵湣瑩潮散敳獡物汹⁣慮⤊㸾੥湤潢樊਷ㄶ‰扪਼㰊⽄敳琠嬵〰‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㜱㜠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜱㔠〠刊⽔楴汥
兵楺⁗桡琠楳⁴桥⁤楦晥牥湣攠扥瑷敥渠癡物慢汥⁳捯灥⁡湤楦整業政⁍畳琠愠晵湣瑩潮瑨敲⁴桡渠浡楮⁢攠灲潴潴祰敤㼠䥳⁡⁦畮捴楯渠牥煵楲敤⁴漠桡癥⁡琠汥慳琠潮攠慲杵浥湴㼠䍡渠愠晵湣瑩潮⁨慶攠浯牥⁴桡渠潮攠慲杵浥湴㼠坨慴⁩猠瑨攠敦晥捴渠愠癡物慢汥⁩渠浡楮⁩映楴⁩猠灡獳敤⁢礠癡汵攠瑯⁡湯瑨敲⁦畮捴楯渠睨楣栠捨慮来猠瑨攠慲杵浥湴⁣潲牥獰潮摩湧⁴漠瑨慴⁶慲楡扬政⁗桡琠楳⁴桥⁥晦散琠潮⁡⁶慲楡扬攠楮慩渠楦⁩琠楳⁰慳獥搠批⁲敦敲敮捥⁴漠慮潴桥爠晵湣瑩潮⁷桩捨⁣桡湧敳⁴桥⁡牧畭敮琠捯牲敳灯湤楮朠瑯⁴桡琠癡物慢汥㼠䵵獴⁡⁦畮捴楯渠桡癥⁡⁲整畲渠癡汵政⁃慮⁡⁦畮捴楯渠桡癥潲攠瑨慮湥⁲整畲渠癡汵政⁍慹⁡⁦畮捴楯渠桡癥敩瑨敲⁡⁲整畲渠癡汵攠湯爠慮礠慲杵浥湴猿⁍慹⁡⁦畮捴楯渠桡癥⁢潴栠愠牥瑵牮⁶慬略⁡湤⁡牧畭敮瑳㼩ਾ㸊敮摯扪ਊ㜱㜠〠潢樊㰼ਯ䑥獴⁛㔰㈠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷ㄸ‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷ㄶ‰⁒ਯ呩瑬攠⣾＀䌀栀愀瀀琀攀爀 ㄀ 㨀 䄀爀爀愀礀猀 伀瘀攀爀瘀椀攀眀 吀栀攀 瘀愀爀椀愀戀氀攀猀 眀攀 栀愀瘀攀 眀漀爀欀攀搀 眀椀琀栀 猀漀 昀愀爀 挀愀渀 栀漀氀搀 漀渀氀礀 漀渀攀 瘀愀氀甀攀 愀琀 愀 琀椀洀攀⸀ 䘀漀爀 攀砀愀洀瀀氀攀Ⰰ 椀昀 礀漀甀 搀攀挀氀愀爀攀 愀渀 椀渀琀攀最攀爀 瘀愀爀椀愀戀氀攀 渀愀洀攀搀 琀攀猀琀匀挀漀爀攀 琀漀 爀攀瀀爀攀猀攀渀琀 愀 猀琀甀搀攀渀琠ᤀ猀 琀攀猀琀 猀挀漀爀攀Ⰰ 琀栀愀琀 瘀愀爀椀愀戀氀攀 挀愀渀 栀漀氀搀 漀渀氀礀 漀渀攀 琀攀猀琀 猀挀漀爀攀⸀ 吀栀攀 昀愀挀琀 琀栀愀琀 琀栀攀 瘀愀爀椀愀戀氀攀 琀攀猀琀匀挀漀爀攀 挀愀渀 栀漀氀搀 漀渀氀礀 漀渀攀 琀攀猀琀 猀挀漀爀攀 椀猀 渀漀琀 愀 瀀爀漀戀氀攀洀 猀漀 氀漀渀最 愀猀 琀栀愀琀 猀琀甀搀攀渀琀 漀渀氀礀 琀愀欀攀猀 漀渀攀 琀攀猀琀⸀ 䠀漀眀攀瘀攀爀Ⰰ 椀昀 琀栀攀 猀愀洀攀 猀琀甀搀攀渀琀 琀愀欀攀猀 愀渀漀琀栀攀爀 琀攀猀琀Ⰰ 漀爀 愀渀漀琀栀攀爀 猀琀甀搀攀渀琀 琀愀欀攀猀 琀栀攀 猀愀洀攀 琀攀猀琀Ⰰ 眀栀攀爀攀 搀漀 礀漀甀 猀琀漀爀攀 琀栀攀 猀攀挀漀渀搀 琀攀猀琀 猀挀漀爀攀㼀 䤀昀 礀漀甀 猀琀漀爀攀 琀栀攀 猀攀挀漀渀搀 猀挀漀爀攀 椀渀 琀攀猀琀匀挀漀爀攀Ⰰ 琀栀攀渀 礀漀甀 氀漀猀攀 琀栀攀 愀戀椀氀椀琀礀 琀漀 爀攀琀爀椀攀瘀攀 琀栀攀 昀椀爀猀琀 猀挀漀爀攀 昀爀漀洀 琀栀攀 瘀愀爀椀愀戀氀攀 琀攀猀琀匀挀漀爀攀Ⰰ 猀椀渀挀攀 琀栀愀琀 瘀愀爀椀愀戀氀攀 挀愀渀 栀漀氀搀 漀渀氀礀 漀渀攀 琀攀猀琀 猀挀漀爀攀 愀琀 愀 琀椀洀攀⸀ 吀栀攀爀攀昀漀爀攀Ⰰ 椀昀 礀漀甀 眀愀渀琀攀搀 琀漀 欀攀攀瀀 琀爀愀挀欀 漀昀Ⰰ 昀漀爀 攀砀愀洀瀀氀攀Ⰰ ㄀   琀攀猀琀 猀挀漀爀攀猀Ⰰ 礀漀甀爀 挀漀搀攀 洀椀最栀琀 氀漀漀欀 氀椀欀攀 琀栀椀猀㨀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀㄀㬀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀㈀㬀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀㌀㬀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀㐀㬀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀㔀㬀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀㘀㬀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀㜀㬀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀㠀㬀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀㤀㬀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀㄀ 㬀 ꀀꀀ⼀⼀ 搀攀挀氀愀爀攀 琀攀猀琀匀挩ਾ㸊敮摯扪ਊ㜱㠠〠潢樊㰼ਯ䑥獴⁛㔰㈠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷ㄹ‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷ㄷ‰⁒ਯ呩瑬攠⡃桡灴敲‱〺⁁牲慹猩ਾ㸊敮摯扪ਊ㜱㤠〠潢樊㰼ਯ䑥獴⁛㔱〠〠删⽘奚‵′㌵㐠湵汬崊⽎數琠㜲〠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜱㠠〠刊⽔楴汥
䑥捬慲楮朠慮⁁牲慹䅮⁡牲慹⁩猠愠癡物慢汥⸠周敲敦潲攬楫攠瑨攠潴桥爠癡物慢汥猠睥⁨慶攠捯癥牥搠獯⁦慲Ⱐ慮⁡牲慹畳琠扥⁤散污牥搠扥景牥⁩琠捡渠扥⁵獥搮⁔桥⁳祮瑡砠景爠摥捬慲楮朠慮⁡牲慹⁩猠慬浯獴⁩摥湴楣慬⁴漠瑨攠獹湴慸⁦潲⁤散污物湧⁩湴敧敲猬⁣桡牡捴敲猬爠潴桥爠癡物慢汥献⁆潲⁥硡浰汥Ⱐ祯甠睯畬搠摥捬慲攠慮⁩湴敧敲⁶慲楡扬攠瑥獴卣潲攠慳⁦潬汯睳㨠ꂠ楮琠瑥獴卣潲攻⁂礠捯湴牡獴Ⱐ祯甠睯畬搠摥捬慲攠慮⁡牲慹映瑨牥攠瑥獴⁳捯牥猠瑨楳⁷慹㨠ꂠ楮琠瑥獴卣潲敛㍝㬠周楳⁤散污牡瑩潮⁣潮瑡楮猠慮⁡牲慹映楮瑥来牳⸠奯甠楮獴敡搠捯畬搠摥捬慲攠慮⁡牲慹映晬潡瑳Ⱐ捨慲慣瑥牳Ⱐ潲⁳瑲楮杳⁩渠瑨攠景汬潷楮朠浡湮敲㨠ꂠ晬潡琠䝐䄠嬵崻₠ꁣ桡爠杲慤敳嬷崻₠ꁳ瑲楮朠湡浥獛㙝㬠坨楬攠慮⁡牲慹慹⁢攠潮攠潦⁳敶敲慬⁤慴愠瑹灥猬⁡汬⁴桥⁶慬略猠楮⁡⁰慲瑩捵污爠慲牡礠浵獴⁢攠潦⁴桥⁳慭攠摡瑡⁴祰攮⁙潵⁣慮湯琠桡癥⁡渠慲牡礠楮⁷桩捨⁳潭攠敬敭敮瑳⁡牥⁦汯慴猬瑨敲猠慲攠獴物湧猬⁳瑩汬瑨敲猠慲攠楮瑥来牳Ⱐ慮搠獯渮⁔桥⁤散污牡瑩潮映扯瑨⁡⁳楮杬攠癡物慢汥⁡湤⁡渠慲牡礠潦⁶慲楡扬敳⁢敧楮猠睩瑨⁴桥⁤慴愠瑹灥⁦潬汯睥搠批⁡⁶慲楡⤊㸾੥湤潢樊਷㈰‰扪਼㰊⽄敳琠嬵㈳‰⁒ 塙娠㔠㈳㔴畬汝ਯ乥硴‷㈱‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷ㄹ‰⁒ਯ呩瑬攠⣾＀䤀渀椀琀椀愀氀椀稀愀琀椀漀渀䄀猀 昀椀爀猀琀 搀椀猀挀甀猀猀攀搀 椀渀 䌀栀愀瀀琀攀爀 ㌀Ⰰ 椀渀椀琀椀愀氀椀稀愀琀椀漀渀 椀猀 眀栀攀渀 礀漀甀 愀猀猀椀最渀 愀 瘀愀氀甀攀 琀漀 愀 瘀愀爀椀愀戀氀攀 椀渀 琀栀攀 猀愀洀攀 猀琀愀琀攀洀攀渀琀 椀渀 眀栀椀挀栀 礀漀甀 搀攀挀氀愀爀攀 琀栀愀琀 瘀愀爀椀愀戀氀攀⸀ 䈀礀 挀漀渀琀爀愀猀琀Ⰰ 愀猀猀椀最渀洀攀渀琀 椀猀 眀栀攀渀 礀漀甀 愀猀猀椀最渀 愀 瘀愀氀甀攀 琀漀 愀 瘀愀爀椀愀戀氀攀 椀渀 愀 猀琀愀琀攀洀攀渀琀 愀昀琀攀爀 琀栀攀 漀渀攀 椀渀 眀栀椀挀栀 礀漀甀 搀攀挀氀愀爀攀 琀栀愀琀 瘀愀爀椀愀戀氀攀⸀ 圀攀 眀椀氀氀 搀椀猀挀甀猀猀 愀猀猀椀最渀椀渀最 瘀愀氀甀攀猀 琀漀 愀渀 愀爀爀愀礀 氀愀琀攀爀 椀渀 琀栀椀猀 挀栀愀瀀琀攀爀 椀渀 琀栀攀 猀攀挀琀椀漀渀†ᰀ䄀猀猀椀最渀椀渀最 愀渀搀 䐀椀猀瀀氀愀礀椀渀最 䄀爀爀愀礀 嘀愀氀甀攀猀⸠ᴀ 吀栀椀猀 猀攀挀琀椀漀渀 挀漀瘀攀爀猀 椀渀椀琀椀愀氀椀稀愀琀椀漀渀 漀昀 愀渀 愀爀爀愀礀⸀ 夀漀甀 栀愀瘀攀 琀眀漀 愀氀琀攀爀渀愀琀椀瘀攀 洀攀琀栀漀搀猀 漀昀 椀渀椀琀椀愀氀椀稀椀渀最 愀渀 愀爀爀愀礀⸀ 吀栀攀 昀椀爀猀琀 愀氀琀攀爀渀愀琀椀瘀攀 椀猀 攀砀瀀氀椀挀椀琀 愀爀爀愀礀 猀椀稀椀渀最Ⰰ 椀渀 眀栀椀挀栀 琀栀攀 猀焀甀愀爀攀 戀爀愀挀欀攀琀猀 挀漀渀琀愀椀渀 愀 渀甀洀攀爀椀挀愀氀 挀漀渀猀琀愀渀琀 琀栀愀琀 攀砀瀀氀椀挀椀琀氀礀 猀瀀攀挀椀昀椀攀猀 琀栀攀 猀椀稀攀 漀昀 琀栀攀 愀爀爀愀礀⸀ 吀栀攀 猀攀挀漀渀搀 愀氀琀攀爀渀愀琀椀瘀攀 椀猀 椀洀瀀氀椀挀椀琀 愀爀爀愀礀 猀椀稀椀渀最Ⰰ 椀渀 眀栀椀挀栀 琀栀攀 猀焀甀愀爀攀 戀爀愀挀欀攀琀猀 愀爀攀 攀洀瀀琀礀 愀渀搀 琀栀攀 猀椀稀攀 漀昀 琀栀攀 愀爀爀愀礀 椀猀 椀渀搀椀挀愀琀攀搀 椀洀瀀氀椀挀椀琀氀礀 戀礀 琀栀攀 渀甀洀戀攀爀 漀昀 攀氀攀洀攀渀琀猀 漀渀 琀栀攀 爀椀最栀琀 猀椀搀攀 漀昀 琀栀攀 愀猀猀椀最渀洀攀渀琀 漀瀀攀爀愀琀漀爀⸀ 䔀砀瀀氀椀挀椀琀 䄀爀爀愀礀 匀椀稀椀渀最 吀栀攀 昀漀氀氀漀眀椀渀最 愀爀攀 攀砀愀洀瀀氀攀猀 漀昀 攀砀瀀氀椀挀椀琀 愀爀爀愀礀 猀椀稀椀渀最㨀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀嬀㌀崀 㴀 笀 㜀㐀Ⰰ 㠀㜀Ⰰ 㤀㄀ 紀㬩ਾ㸊敮摯扪ਊ㜲ㄠ〠潢樊㰼ਯ䑥獴⁛㔳㜠〠删⽘奚‵″㤲〠湵汬崊⽎數琠㜲㈠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜲〠〠刊⽔楴汥
䅳獩杮楮朠慮搠䑩獰污祩湧⁁牲慹⁖慬略獔桥⁦潬汯睩湧⁰牯杲慭⁳桯睳⁨潷⁴漠慳獩杮⁶慬略猠瑯⁡渠慲牡礬湥⁥汥浥湴⁡琠愠瑩浥⸠周攠慳獩杮浥湴⁳瑡牴猠睩瑨⁴桥⁦楲獴⁩湤數Ⱐ〬⁡湤⁥湤猠睩瑨⁴桥慳琠楮摥砬′Ⱐ睨楣栠楳湥敳猠瑨慮⁴桥畭扥爠潦⁥汥浥湴猬″⸠周攠灲潧牡洠瑨敮畴灵瑳⁴桥⁡牲慹⁶慬略猬湥⁡琠愠瑩浥⸠⍩湣汵摥‼楯獴牥慭㸠畳楮朠湡浥獰慣攠獴搻⁩湴慩渠⠩⁻₠ꁩ湴⁴敳瑓捯牥嬳崻₠ꁣ潵琠㰼•䕮瑥爠瑥獴⁳捯牥‣ㄺ•㬠ꂠ捩渠㸾⁴敳瑓捯牥嬰崻₠ꁣ潵琠㰼•䕮瑥爠瑥獴⁳捯牥‣㈺•㬠ꂠ捩渠㸾⁴敳瑓捯牥嬱崻₠ꁣ潵琠㰼•䕮瑥爠瑥獴⁳捯牥‣㌺•㬠ꂠ捩渠㸾⁴敳瑓捯牥嬲崻₠ꁣ潵琠㰼•呥獴⁳捯牥‣ㄺ•‼㰠瑥獴卣潲敛そ‼㰠敮摬㬠ꂠ捯畴‼㰠≔敳琠獣潲攠⌲㨠∠㰼⁴敳瑓捯牥嬱崠㰼⁥湤氻₠ꁣ潵琠㰼•呥獴⁳捯牥‣㌺•‼㰠瑥獴卣潲敛㉝‼㰠敮摬㬠ꂠ牥瑵牮‰㬠素卯浥⁳慭灬攠楮灵琠慮搠潵瑰畴⁣潵汤⁢攺⁅湴敲⁴敳琠獣潲攠⌱㨠㜷⁅湴敲⁴敳琠獣潲攠⌲㨠㤱⁅湴敲⁴敳琠獣潲攠⌳㨠㠴⁔敳琠獣潲攠⌱㨠㜷⁔敳琠獣潲攠⌲㨠㤱⁔敳琠獣潲攠⌳㨠㠴⁈潷敶敲Ⱐ瑨楳湥ⵥ汥浥湴ⵡ琭愭瑩浥⁡灰牯慣栠桡猠湯⁡摶慮瑡来癥爠瑨攠景汬潷楮朠灲潧牡洬⁷桩捨⁤潥猠湯⤊㸾੥湤潢樊਷㈲‰扪਼㰊⽄敳琠嬵㔰‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㜲㌠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜲ㄠ〠刊⽔楴汥
偡獳楮朠䅲牡祳⁡猠䙵湣瑩潮⁁牧畭敮瑳偲敶楯畳汹⁩渠瑨楳⁣桡灴敲Ⱐ睥⁵獥搠瑨攠景汬潷楮朠灲潧牡洠瑯⁤敭潮獴牡瑥⁨潷潯灳⁡牥⁥晦散瑩癥⁩渠慳獩杮楮朠慮搠摩獰污祩湧⁡牲慹⁶慬略猺‣楮捬畤攠㱩潳瑲敡派⁵獩湧慭敳灡捥⁳瑤㬠捯湳琠楮琠䵁堠㴠㌻⁩湴慩渠⠩⁻₠ꁩ湴⁴敳瑓捯牥孍䅘崻₠ꁦ潲
楮琠椠㴠〻⁩‼⁍䅘㬠椫⬩₠ꁻ₠ꂠꂠ捯畴‼㰠≅湴敲⁴敳琠獣潲攠⌢‼㰠椠⬠ㄠ㰼•㨠∻₠ꂠꂠ捩渠㸾⁴敳瑓捯牥孩崻₠ꁽ₠ꁦ潲
椠㴠〻⁩‼⁍䅘㬠椫⬩₠ꁻ₠ꂠꂠ捯畴‼㰠≔敳琠獣潲攠⌢‼㰠椠⬠ㄠ㰼•㨠∠ꂠꂠꂠꂠ㰼⁴敳瑓捯牥孩崠㰼⁥湤氻₠ꁽ₠ꁲ整畲渠〻⁽⁎潷⁷攠慲攠杯楮朠瑯慫攠瑨楳⁰牯杲慭潲攠浯摵污爠批⁷物瑩湧湥⁦畮捴楯渠瑯⁡獳楧渠癡汵敳⁴漠瑨攠慲牡礬⁡湤⁡湯瑨敲⁦畮捴楯渠瑯⁤楳灬慹⁶慬略猠晲潭⁴桥⁡牲慹Ⱐ牡瑨敲⁴桡渠摯楮朠慬氠瑨慴⁷潲欠楮⁴桥慩渠晵湣瑩潮⸠⍩湣汵摥‼楯獴牥慭㸠畳楮朠湡浥獰慣攠獴搻⁶潩搠慳獩杮噡汵敳⡩湴孝Ⱐ楮琩㬠癯楤⁤楳灬慹噡汵敳⡩湴孝Ⱐ楮琩㬠捯湳琠楮琠䵁堠㴠㌻⁩湴慩渠⠩⁻₠ꁩ湴⁴敳瑓捯牥孍䅘崻₠ꁡ獳楧湖慬略猨瑥獴卣潲攬⁍䅘⤻₠ꁤ楳灬慹噡汵敳⡴敳瑓捯牥Ⱐ䵁堩㬠ꀠꂠ牥瑵牮‰㬠素癯楤⁡獳楧湖慬略猨楮琠瑥獴獛⤊㸾੥湤潢樊਷㈳‰扪਼㰊⽄敳琠嬵㔴‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㜲㐠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜲㈠〠刊⽔楴汥
SummaryThe variables we have worked with before this chapter could hold only one value at a time. In this chapter, you learned about an array, which permits you to use a single variable to store many values. The values are stored at consecutive indexes, starting with zero and then incrementing by one for each additional element of the array. The data type of an array may be integer, float, or character. However, a particular array cannot contain integers, floats, and characters. All the elements of an array must be of the same data type. You need to declare an array before you can use it. The syntax for declaring an array is almost identical to the syntax for declaring integer, character, or other variables. The only difference between declaring a single scalar variable and an array of scalar variables is that, when declaring an array, the variable name is followed by a number within square brackets. That number is the array’s size declarator. The size declarator must be a literal or a⤊㸾੥湤潢樊਷㈴‰扪਼㰊⽄敳琠嬵㔶‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㜲㔠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜲㌠〠刊⽔楴汥
兵楺⁃慮⁡⁰慲瑩捵污爠慲牡礠捯湴慩渠楮瑥来牳Ⱐ晬潡瑳Ⱐ慮搠捨慲慣瑥牳㼠坨慴⁩猠瑨攠湵浢敲映瑨攠獴慲瑩湧⁩湤數映慮⁡牲慹㼠坨慴⁩猠瑨攠湵浢敲映瑨攠敮摩湧⁩湤數映慮⁡牲慹㼠坨慴⁩猠瑨攠摩晦敲敮捥⁢整睥敮⁩湩瑩慬楺慴楯渠慮搠慳獩杮浥湴㼠坨慴⁡牥⁴桥⁴睯⁡汴敲湡瑩癥整桯摳映楮楴楡汩穩湧⁡渠慲牡礿⁗桡琠楳⁴桥⁰畲灯獥映瑨攠湵汬⁣桡牡捴敲㼠坨慴⁩猠瑨攠癡汵攠潦⁴桥慭攠潦⁡渠慲牡礿⁓桯畬搠瑨攠污獴⁥汥浥湴映愠捨慲慣瑥爠慲牡礠慬睡祳⁢攠愠湵汬⁣桡牡捴敲㼠坨慴⁩猠瑨攠摩晦敲敮捥⁢整睥敮⁴桥⁧整⁡湤⁧整汩湥⁦畮捴楯湳映瑨攠捩渠潢橥捴㼠坨敮⁹潵⁰慳猠慮⁡牲慹慭攠慳⁡⁦畮捴楯渠慲杵浥湴Ⱐ慲攠祯甠灡獳楮朠楴⁢礠癡汵攬⁲敦敲敮捥Ⱐ潲⁡摤牥獳㼩ਾ㸊敮摯扪ਊ㜲㔠〠潢樊㰼ਯ䑥獴⁛㔵㠠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷㈶‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷㈴‰⁒ਯ呩瑬攠⣾＀䌀栀愀瀀琀攀爀 ㄀㄀㨀 圀栀愀琠ᤀ猀 琀栀攀 䄀搀搀爀攀猀猀㼀 倀漀椀渀琀攀爀猀 伀瘀攀爀瘀椀攀眀 䴀礀 瀀愀爀攀渀琀猀 琀漀氀搀 洀攀 眀栀攀渀 䤀 眀愀猀 愀 挀栀椀氀搀 琀栀愀琀 椀琀 眀愀猀 渀漀琀 瀀漀氀椀琀攀 琀漀 瀀漀椀渀琀⸀ 䠀漀眀攀瘀攀爀Ⰰ 攀愀挀栀 猀攀洀攀猀琀攀爀 䤀 琀攀愀挀栀 洀礀 挀漀洀瀀甀琀攀爀 瀀爀漀最爀愀洀洀椀渀最 猀琀甀搀攀渀琀猀 栀漀眀 琀漀 瀀漀椀渀琀⸀ 一漀Ⰰ 䤀 愀洀 渀漀琀 琀爀礀椀渀最 琀漀 瀀爀漀洀漀琀攀 爀甀搀攀 戀攀栀愀瘀椀漀爀⸀ 刀愀琀栀攀爀Ⰰ 䤀 愀洀 琀攀愀挀栀椀渀最 洀礀 猀琀甀搀攀渀琀猀 愀戀漀甀琀 瀀漀椀渀琀攀爀猀Ⰰ 眀栀椀挀栀†ᰀ瀀漀椀渀琠ᴀ 琀漀 愀渀漀琀栀攀爀 瘀愀爀椀愀戀氀攀 漀爀 挀漀渀猀琀愀渀琀⸀ 夀漀甀 礀漀甀爀猀攀氀昀 洀愀礀 栀愀瘀攀 愀挀琀攀搀 愀猀 愀 瀀漀椀渀琀攀爀 椀渀 琀栀攀 瀀愀猀琀⸀ 䠀愀瘀攀 礀漀甀 攀瘀攀爀 戀攀攀渀 愀猀欀攀搀 眀栀攀爀攀 猀漀洀攀漀渀攀 氀椀瘀攀猀㼀 䤀昀 琀栀愀琀 栀漀甀猀攀 眀愀猀 渀攀愀爀戀礀Ⰰ 礀漀甀 洀愀礀 栀愀瘀攀 瀀漀椀渀琀攀搀 椀琀 漀甀琀⸀ 吀栀攀 瀀漀椀渀琀攀爀 瀀攀爀昀漀爀洀猀 愀 猀椀洀椀氀愀爀 昀甀渀挀琀椀漀渀⸀ 䄀 瀀漀椀渀琀攀爀 瀀漀椀渀琀猀 琀漀 愀渀漀琀栀攀爀 瘀愀爀椀愀戀氀攀 漀爀 挀漀渀猀琀愀渀琀⸀ 伀昀 挀漀甀爀猀攀Ⰰ 琀栀攀 瀀漀椀渀琀攀爀 搀漀攀猀 渀漀琀 瀀漀椀渀琀 眀椀琀栀 愀渀 愀爀洀 愀渀搀 昀椀渀最攀爀猀 愀猀 礀漀甀 眀漀甀氀搀⸀ 刀愀琀栀攀爀Ⰰ 琀栀攀 瀀漀椀渀琀攀爠ᤀ猀 瘀愀氀甀攀 椀猀 琀栀攀 愀搀搀爀攀猀猀 漀昀 琀栀攀 瘀愀爀椀愀戀氀攀 漀爀 挀漀渀猀琀愀渀琀 琀漀 眀栀椀挀栀 椀琀 瀀漀椀渀琀猀⸀ 䤀渀搀攀攀搀Ⰰ 礀漀甀 洀愀礀 栀愀瘀攀 搀漀渀攀 猀漀洀攀琀栀椀渀最 猀椀洀椀氀愀爀⸀ 䤀昀 礀漀甀 眀攀爀攀 愀猀欀攀搀 眀栀攀爀攀 猀漀洀攀漀渀攀 氀椀瘀攀猀 愀渀搀 琀栀愀琀 栀漀甀猀攀 眀愀猀 渀漀琀 挀氀漀猀攀 攀渀漀甀最栀 琀漀 瀀栀礀猀椀挀愀氀氀礀 瀀漀椀渀琀 漀甀琀Ⰰ 礀漀甀 椀渀猀琀攀愀搀 洀愀礀 栀愀瘀攀 瀀爀漀瘀椀搀攀搀 愀渀 愀搀搀爀攀猀猀 戀礀 眀栀椀挀栀 琀栀攀 栀漀甀猀攀 挀漀甀氀搀 戀攀 氀漀挀愀琀攀搀⸀ 倀漀椀渀琀攀爀猀 栀愀瘀攀 栀愀搀 愀 爀攀瀩ਾ㸊敮摯扪ਊ㜲㘠〠潢樊㰼ਯ䑥獴⁛㔵㠠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷㈷‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷㈵‰⁒ਯ呩瑬攠⣾＀䌀栀愀瀀琀攀爀 ㄀㄀㨀 圀栀愀琠ᤀ猀 琀栀攀 䄀搀搀爀攀猀猀㼀 倀漀椀渀琀攀爀猩ਾ㸊敮摯扪ਊ㜲㜠〠潢樊㰼ਯ䑥獴⁛㔶㐠〠删⽘奚‵‱㔷ㄠ湵汬崊⽎數琠㜲㠠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜲㘠〠刊⽔楴汥
Declaring a PointerLike any variable or constant, you must declare a pointer before you can work with it. The syntax of declaring a pointer is almost the same as declaring a variable which stores a value rather than an address. However, the meaning of the pointer’s data type is quite different than the meaning of the data type of a variable which stores a value rather than an address. Syntax of a Pointer Declaration The syntax of declaring a pointer is almost the same as the syntax of declaring the variables we have worked with in previous chapters. The following statement declares an integer pointer variable: int* iPtr; The asterisk you use to declare a pointer is the same asterisk that you use for multiplication. However, in this statement the asterisk is being used in a declaration, so in this context it is being used to designate a variable as a pointer. Later in this chapter, we will use the asterisk for a third purpose, as an indirection operator. Note  It is common in C++ for a⤊㸾੥湤潢樊਷㈸‰扪਼㰊⽄敳琠嬵㜰‰⁒ 塙娠㔠ㄵ㜱畬汝ਯ乥硴‷㈹‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷㈷‰⁒ਯ呩瑬攠⣾＀䄀猀猀椀最渀椀渀最 愀 嘀愀氀甀攀 琀漀 愀 倀漀椀渀琀攀爀吀栀椀猀 猀攀挀琀椀漀渀 眀椀氀氀 攀砀瀀氀愀椀渀 栀漀眀 礀漀甀 愀猀猀椀最渀 愀 瘀愀氀甀攀 琀漀 愀 瀀漀椀渀琀攀爀⸀ 吀栀漀甀最栀Ⰰ 戀攀昀漀爀攀 䤀 攀砀瀀氀愀椀渀 栀漀眀Ⰰ 瀀攀爀栀愀瀀猀 䤀 猀栀漀甀氀搀 攀砀瀀氀愀椀渀 眀栀礀⸀ 圀栀礀 夀漀甀 匀栀漀甀氀搀 一漀琀 吀爀礀 琀漀 唀猀攀 愀渀 唀渀愀猀猀椀最渀攀搀ꀀ倀漀椀渀琀攀爀 䈀愀挀欀 椀渀 攀氀攀洀攀渀琀愀爀礀 猀挀栀漀漀氀 眀攀 眀攀爀攀 琀愀甀最栀琀 愀 瘀攀爀猀攀㨀†ᰀ䤀 猀栀漀琀 愀渀 愀爀爀漀眀 椀渀琀漀 琀栀攀 愀椀爀Ⰰ 眀栀攀爀攀 椀琀 氀愀渀搀猀Ⰰ 䤀 搀漀渠ᤀ琀 挀愀爀攀⸠ᴀ 䰀漀漀欀椀渀最 戀愀挀欀Ⰰ 䤀 眀漀渀搀攀爀 眀栀礀 礀漀甀渀最 挀栀椀氀搀爀攀渀 眀攀爀攀 琀愀甀最栀琀 琀栀椀猀 瘀攀爀猀攀⸀ 䤀琀 洀愀礀 爀栀礀洀攀Ⰰ 戀甀琀 椀琀猀 洀攀猀猀愀最攀 椀猀 爀攀愀氀氀礀 渀漀琀 愀瀀瀀爀漀瀀爀椀愀琀攀 昀漀爀 氀椀琀琀氀攀 漀渀攀猀⸀ 䠀漀眀攀瘀攀爀Ⰰ 眀栀攀渀 礀漀甀 搀攀挀氀愀爀攀 愀 瀀漀椀渀琀攀爀 戀甀琀 琀栀攀渀 甀猀攀 椀琀 眀椀琀栀漀甀琀 昀椀爀猀琀 愀猀猀椀最渀椀渀最 椀琀 愀 瘀愀氀甀攀Ⰰ 礀漀甀 愀爀攀Ⰰ 愀氀愀猀Ⰰ 搀漀椀渀最 琀栀攀 瀀爀漀最爀愀洀洀椀渀最 攀焀甀椀瘀愀氀攀渀琀 漀昀 琀栀愀琀 瘀攀爀猀攀⸀ 吀栀攀 昀漀氀氀漀眀椀渀最 瀀爀漀最爀愀洀 搀攀挀氀愀爀攀猀 愀 瀀漀椀渀琀攀爀 愀渀搀 琀栀攀渀 愀琀琀攀洀瀀琀猀 琀漀 漀甀琀瀀甀琀 椀琀猀 瘀愀氀甀攀 眀椀琀栀漀甀琀 昀椀爀猀琀 愀猀猀椀最渀椀渀最 椀琀 愀 瘀愀氀甀攀㨀 ⌀椀渀挀氀甀搀攀 㰀椀漀猀琀爀攀愀洀㸀 甀猀椀渀最 渀愀洀攀猀瀀愀挀攀 猀琀搀㬀 椀渀琀 洀愀椀渀 (⤀ 笀 ꀀꀀ椀渀琀⨀ 椀倀琀爀㬀 ꀀꀀ挀漀甀琀 㰀㰀 ∀吀栀攀 瘀愀氀甀攀 漀昀 椀倀琀爀 椀猀 ∀ 㰀㰀 椀倀琀爀 㰀㰀 攀渀搀氀㬀 ꀀꀀ爀攀琀甀爀渀  㬀 紀 吀栀攀 爀攀猀甀氀琀Ⰰ 搀攀瀀攀渀搀椀渀最 漀渀 礀漀甀爀 挀漀洀瀀椀氀攀爀 愀渀搀 漀瀀攀爀愀琀椀渀最 猀礀猀琀攀洀Ⰰ 洀愀礀 戀攀 愀 挀漀洀瀀椀氀攀爀 攀爀爀漀爀Ⰰ 愀 爀甀渀琀椀洀攀 攀爀爀漀爀Ⰰ 漀爀 愀 挀漀洀瀀甀琀攀爀 琀栀愀琀 氀漀挀欀猀 甀瀀⸀ 刀攀最愀爀搀氀攀猀猀Ⰰ 愩ਾ㸊敮摯扪ਊ㜲㤠〠潢樊㰼ਯ䑥獴⁛㔷㔠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷㌰‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷㈸‰⁒ਯ呩瑬攠⡉湤楲散瑩潮⁏灥牡瑯爠慮搠䑥牥晥牥湣楮朠周攠灲業慲礠畳攠潦⁡⁰潩湴敲⁩猠瑯⁡捣敳猠慮搬⁩映慰灲潰物慴攬⁣桡湧攠瑨攠癡汵攠潦⁴桥⁶慲楡扬攠瑨慴⁴桥⁰潩湴敲⁩猠灯楮瑩湧⁴漮⁉渠瑨攠景汬潷楮朠灲潧牡洬⁴桥⁶慬略映瑨攠楮瑥来爠癡物慢汥畭⁩猠捨慮来搠瑷楣攮‣楮捬畤攠㱩潳瑲敡派⁵獩湧慭敳灡捥⁳瑤㬠楮琠浡楮
⤠笠ꂠ楮琠湵洠㴠㔻₠ꁩ湴⨠楐瑲‽…湵活₠ꁣ潵琠㰼•周攠癡汵攠潦畭⁩猠∠㰼畭‼㰠敮摬㬠ꂠ湵洠㴠㄰㬠ꂠ捯畴‼㰠≔桥⁶慬略映湵洠慦瑥爠湵洠㴠㄰⁩猠∠ꂠꂠꀼ㰠湵洠㰼⁥湤氻₠ꀪ楐瑲‽‱㔻₠ꁣ潵琠㰼•周攠癡汵攠潦畭⁡晴敲‪楐瑲‽‱㔠楳•₠ꂠꂠ㰼畭‼㰠敮摬㬠ꂠ牥瑵牮‰㬠素周攠牥獵汴楮朠潵瑰畴⁩猠周攠癡汵攠潦畭⁩猠㔠周攠癡汵攠潦畭⁡晴敲畭‽‱〠楳‱〠周攠癡汵攠潦畭⁡晴敲‪楐瑲‽‱㔠楳‱㔠周攠晩牳琠捨慮来⁳桯畬搠扥⁦慭楬楡爬⁢礠瑨攠摩牥捴⁡獳楧湭敮琠潦⁡⁶慬略⁴漠湵洬⁳畣栠慳畭‽‱〮⁈潷敶敲Ⱐ瑨攠獥捯湤⁣桡湧攠楳⁡捣潭灬楳桥搠愠湥眠睡礬⁵獩湧⁴桥⁩湤楲散瑩潮灥牡瑯爺‪楐瑲‽‱㔻⁔桥⁩湤楲散瑩潮灥牡瑯爠楳⁡渠慳瑥物獫Ⱐ瑨攠獡浥⁡獴敲楳欠瑨慴⁹潵⁵獥搠瑯⁤散污牥⁴桥⁰潩湴敲爠瑯⁰敲景牭⤊㸾੥湤潢樊਷㌰‰扪਼㰊⽄敳琠嬵㜹‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㜳ㄠ〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜲㤠〠刊⽔楴汥
The Pointer as a Variable or a ConstantA pointer may be a variable or a constant. Let’s examine both possibilities. Pointer as a Variable The preceding program had the pointer pointing to one integer variable. However, a pointer variable, being a variable, can point to different variables at different times in the program. In the following program, the value of the pointer is changed to point to two different integer variables. #include <iostream> using namespace std; int main ⠀) {   int num1 = 5, num2 = 14;   int* iPtr = &num1;   cout << "The value of num1 is " << num1 << endl;   *iPtr *= 2;   cout << "The value of num1 after *iPtr *= 2 is "      << *iPtr << endl;   iPtr = &num2;   cout << "The value of num2 is " << num2 << endl;   *iPtr /= 2;   cout << "The value of num after *iPtr /= 2 is "      << *iPtr << endl;   return 0; } The resulting output is therefore: The value of num1 is 5 The value of num1 after *iPtr *= 2 is 10 The value of num2 is 14 The value of num after *iPtr /= 2 i⤊㸾੥湤潢樊਷㌱‰扪਼㰊⽄敳琠嬵㠸‰⁒ 塙娠㔠㈳㔴畬汝ਯ乥硴‷㌲‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷㌰‰⁒ਯ呩瑬攠⣾＀倀漀椀渀琀攀爀 䄀爀椀琀栀洀攀琀椀挀吀栀攀 瘀愀氀甀攀 漀昀 愀 瀀漀椀渀琀攀爀Ⰰ 攀瘀攀渀 琀栀漀甀最栀 椀琀 椀猀 愀渀 愀搀搀爀攀猀猀Ⰰ 椀猀 愀 渀甀洀攀爀椀挀 瘀愀氀甀攀⸀ 吀栀攀爀攀昀漀爀攀Ⰰ 礀漀甀 挀愀渀 瀀攀爀昀漀爀洀 愀爀椀琀栀洀攀琀椀挀 漀瀀攀爀愀琀椀漀渀猀 漀渀 愀 瀀漀椀渀琀攀爀 樀甀猀琀 愀猀 礀漀甀 挀愀渀 愀 渀甀洀攀爀椀挀 瘀愀氀甀攀⸀ 唀猀椀渀最 愀 嘀愀爀椀愀戀氀攀 倀漀椀渀琀攀爀 琀漀 倀漀椀渀琀 琀漀 愀渀 䄀爀爀愀礀 倀漀椀渀琀攀爀 愀爀椀琀栀洀攀琀椀挀 椀猀 搀漀渀攀 漀昀琀攀渀 眀椀琀栀 愀爀爀愀礀猀⸀ 䠀漀眀攀瘀攀爀Ⰰ 猀椀渀挀攀 礀漀甀 挀愀渀渀漀琀 挀栀愀渀最攀 琀栀攀 瘀愀氀甀攀 漀昀 琀栀攀 渀愀洀攀 漀昀 愀渀 愀爀爀愀礀Ⰰ 椀琀 戀攀椀渀最 愀 挀漀渀猀琀愀渀琀 瀀漀椀渀琀攀爀Ⰰ 礀漀甀 昀椀爀猀琀 猀栀漀甀氀搀 搀攀挀氀愀爀攀 愀 瘀愀爀椀愀戀氀攀 瀀漀椀渀琀攀爀 愀渀搀 琀栀攀渀 愀猀猀椀最渀 椀琀 琀漀 琀栀攀 愀搀搀爀攀猀猀 漀昀 愀渀 愀爀爀愀礀⸀ 匀漀Ⰰ 眀攀 戀攀最椀渀 眀椀琀栀 愀渀 攀猀琀愀戀氀椀猀栀攀搀 瀀漀椀渀琀 漀昀 爀攀昀攀爀攀渀挀攀Ⰰ 氀攀琠ᤀ猀 猀琀愀爀琀 眀椀琀栀 琀栀攀 昀漀氀氀漀眀椀渀最 瀀爀漀最爀愀洀Ⰰ 眀栀椀挀栀 漀甀琀瀀甀琀猀 琀栀攀 愀搀搀爀攀猀猀 愀渀搀 瘀愀氀甀攀 愀琀 攀愀挀栀 攀氀攀洀攀渀琀 漀昀 愀渀 愀爀爀愀礀 甀猀椀渀最 琀栀攀 渀愀洀攀 漀昀 琀栀攀 愀爀爀愀礀㨀 ⌀椀渀挀氀甀搀攀 㰀椀漀猀琀爀攀愀洀㸀 甀猀椀渀最 渀愀洀攀猀瀀愀挀攀 猀琀搀㬀 挀漀渀猀琀 椀渀琀 䴀䄀堀 㴀 ㌀㬀 椀渀琀 洀愀椀渀 (⤀ 笀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀嬀䴀䄀堀崀 㴀 笀㐀Ⰰ 㜀Ⰰ ㄀紀㬀 ꀀꀀ昀漀爀 (int i = 0; i < MAX; i++⤀ ꀀꀀ笀 ꀀꀀꀀꀀꀀ挀漀甀琀 㰀㰀 ∀吀栀攀 愀搀搀爀攀猀猀 漀昀 椀渀搀攀砀 ∀ 㰀㰀 椀 ꀀꀀꀀꀀꀀꀀꀀꀀ㰀㰀 ∀ 漀昀 琀栀攀 愀爀爀愀礀 椀猀 ∀㰀㰀 ☀琀攀猀琀匀挀漀爀攀嬀椀崀 㰀㰀 攀渀搀氀㬀 ꀀꀀꀀꀀꀀ挀漀甀琀 㰀㰀 ∀吀栀攀 瘀愀氀甀攀 愀琀 椀渀搀攀砀 ∀ 㰀㰀 椀 ꀀꀀꀀꀀꀀꀀꀀꀀ㰀㰀 ∀ 漀昀 琀栀攀 愀爀爀愀礀 椀猀 ∀㰀㰀 琀攀猀琀匀挀漀爀攀嬀椀崀 㰀㰀 攀渀搀氀㬀 ꀀꀀ紀 ꀀꀀ爀攀琀甩ਾ㸊敮摯扪ਊ㜳㈠〠潢樊㰼ਯ䑥獴⁛㔹㤠〠删⽘奚‵′㌵㐠湵汬崊⽎數琠㜳㌠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜳ㄠ〠刊⽔楴汥
偯楮瑥牳⁡猠䙵湣瑩潮⁁牧畭敮瑳偯楮瑥牳慹⁢攠灡獳敤⁡猠晵湣瑩潮⁡牧畭敮瑳⸠偯楮瑥爠湯瑡瑩潮⁵獵慬汹⁩猠畳敤⁴漠湯瑥⁴桡琠慮⁡牧畭敮琠楳⁡⁰潩湴敲⸠䡯睥癥爬⁩映瑨攠灯楮瑥爠慲杵浥湴⁩猠瑨攠湡浥映慮⁡牲慹Ⱐ獵扳捲楰琠湯瑡瑩潮⁡汴敲湡瑩癥汹慹⁢攠畳敤⸠偡獳楮朠慮⁁牲慹⁕獩湧⁐潩湴敲⁎潴慴楯渠䥮⁃桡灴敲‱〬⁷攠敭灬潹敤⁴桥⁦潬汯睩湧⁰牯杲慭⁴桡琠畳敤湥⁦畮捴楯渠瑯⁡獳楧渠癡汵敳⁴漠瑨攠慲牡礠慮搠慮潴桥爠晵湣瑩潮⁴漠摩獰污礠癡汵敳⁦牯洠瑨攠慲牡礬⁲慴桥爠瑨慮⁤潩湧⁡汬⁴桡琠睯牫⁩渠瑨攠浡楮⁦畮捴楯渮‣楮捬畤攠㱩潳瑲敡派⁵獩湧慭敳灡捥⁳瑤㬠癯楤⁡獳楧湖慬略猨楮瑛崬⁩湴⤻⁶潩搠摩獰污祖慬略猨楮瑛崬⁩湴⤻⁣潮獴⁩湴⁍䅘‽″㬠楮琠浡楮
⤠笠ꂠ楮琠瑥獴卣潲敛䵁塝㬠ꂠ慳獩杮噡汵敳⡴敳瑓捯牥Ⱐ䵁堩㬠ꂠ摩獰污祖慬略猨瑥獴卣潲攬⁍䅘⤻₠₠ꁲ整畲渠〻⁽⁶潩搠慳獩杮噡汵敳⡩湴⁴敳瑳孝Ⱐ楮琠湵洩⁻₠ꁦ潲
楮琠椠㴠〻⁩‼畭㬠椫⬩₠ꁻ₠ꂠꂠ捯畴‼㰠≅湴敲⁴敳琠獣潲攠⌢‼㰠椠⬠ㄠ㰼•㨠∻₠ꂠꂠ捩渠㸾⁴敳瑳孩崻₠ꁽ⁽⁶潩搠摩獰污祖慬略猨楮琠獣潲敳孝Ⱐ楮琠敬敭猩⁻⁦潲
楮琠椠㴠〻⁩‼⁥汥浳㬠椫⬩₠ꁻ₠ꂠꂠ捯畴‼㰠≔敳⤊㸾੥湤潢樊਷㌳‰扪਼㰊⽄敳琠嬶〷‰⁒ 塙娠㔠ㄵ㜱畬汝ਯ乥硴‷㌴‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷㌲‰⁒ਯ呩瑬攠⣾＀䐀礀渀愀洀椀挀 䴀攀洀漀爀礀 䄀氀氀漀挀愀琀椀漀渀 䄀猀 搀椀猀挀甀猀猀攀搀 椀渀 䌀栀愀瀀琀攀爀 ㄀ Ⰰ 眀栀攀渀 搀攀挀氀愀爀椀渀最 愀渀 愀爀爀愀礀Ⰰ 琀栀攀 猀椀稀攀 搀攀挀氀愀爀愀琀漀爀 洀甀猀琀 戀攀 攀椀琀栀攀爀 愀 氀椀琀攀爀愀氀 漀爀 愀 挀漀渀猀琀愀渀琀Ⰰ 愀渀搀 洀愀礀 渀漀琀 戀攀 愀 瘀愀爀椀愀戀氀攀⸀ 吀栀攀 昀漀氀氀漀眀椀渀最 瀀爀漀最爀愀洀 昀爀漀洀 䌀栀愀瀀琀攀爀 ㄀  愀琀琀攀洀瀀琀猀Ⰰ 甀渀猀甀挀挀攀猀猀昀甀氀氀礀Ⰰ 琀漀 甀猀攀 愀 瘀愀爀椀愀戀氀攀 渀甀洀吀攀猀琀猀 椀渀 搀攀挀氀愀爀椀渀最 琀栀攀 猀椀稀攀 漀昀 愀渀 愀爀爀愀礀㨀 ⌀椀渀挀氀甀搀攀 㰀椀漀猀琀爀攀愀洀㸀 甀猀椀渀最 渀愀洀攀猀瀀愀挀攀 猀琀搀㬀 椀渀琀 洀愀椀渀 (⤀ 笀 ꀀꀀ椀渀琀 渀甀洀吀攀猀琀猀㬀 ꀀꀀ挀漀甀琀 㰀㰀 ∀䔀渀琀攀爀 琀栀攀 渀甀洀戀攀爀 漀昀 琀攀猀琀 猀挀漀爀攀猀㨀∀㬀 ꀀꀀ挀椀渀 㸀㸀 渀甀洀吀攀猀琀猀㬀 ꀀꀀ椀渀琀 琀攀猀琀匀挀漀爀攀嬀渀甀洀吀攀猀琀猀崀㬀 ꀀꀀ爀攀琀甀爀渀  㬀 紀 吀栀攀 爀攀猀甀氀琀 椀猀 愀 挀漀洀瀀椀氀攀爀 攀爀爀漀爀⸀ 吀栀攀 挀漀洀瀀椀氀攀爀 眀椀氀氀 昀氀愀最 琀栀攀 搀攀挀氀愀爀愀琀椀漀渀 漀昀 琀栀攀 愀爀爀愀礀 (int testScore[numTests]⤀ 愀渀搀 挀漀洀瀀氀愀椀渀 琀栀愀琀 愀 挀漀渀猀琀愀渀琀 攀砀瀀爀攀猀猀椀漀渀 眀愀猀 攀砀瀀攀挀琀攀搀⸀ 吀栀攀 爀攀愀猀漀渀 愀 挀漀渀猀琀愀渀琀 (or literal⤀ 攀砀瀀爀攀猀猀椀漀渀 椀猀 爀攀焀甀椀爀攀搀 椀猀 琀栀愀琀 椀渀 琀栀椀猀 瀀爀漀最爀愀洀 眀攀 愀爀攀 愀氀氀漀挀愀琀椀渀最 洀攀洀漀爀礀 昀漀爀 琀栀攀 愀爀爀愀礀 愀琀 挀漀洀瀀椀氀攀 琀椀洀攀⸀ 吀栀攀 挀漀洀瀀椀氀攀爀 渀攀攀搀猀 琀漀 欀渀漀眀 攀砀愀挀琀氀礀 栀漀眀 洀甀挀栀 洀攀洀漀爀礀 琀漀 愀氀氀漀挀愀琀攀⸀ 䠀漀眀攀瘀攀爀Ⰰ 椀昀 愀 瘀愀爀椀愀戀氀攀 椀猀 琀栀攀 猀椀稀攀 搀攀挀氀愀爀愀琀漀爀Ⰰ 琀栀攀 挀漀洀瀀椀氀攀爀 搀漀攀猀 渀漀琀 欀渀漀眀 栀漀眀 洀甀挀栀 洀攀洀漀爀礀 琀漀 愀氀氀漀挀愀琀攀 戀攀挀愀甀猀攀 愀 瘀愀爀椀愀戀氀攠ᤀ猀 瘀愀氀甀攀 洀愀礀 挀栀愀渀最攀⸀ 䤀渀搀攀攀搀Ⰰ 椀渀 琀栀攀 瀀爀攀挀攀搀椀渀朩ਾ㸊敮摯扪ਊ㜳㐠〠潢樊㰼ਯ䑥獴⁛㘱㘠〠删⽘奚‵‱㔷ㄠ湵汬崊⽎數琠㜳㔠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜳㌠〠刊⽔楴汥
剥瑵牮楮朠偯楮瑥牳⁦牯洠䙵湣瑩潮猠䥮⁃桡灴敲‱〬⁹潵敡牮敤⁳敶敲慬⁷慹猠瑯⁩湩瑩慬楺攠愠捨慲慣瑥爠慲牡礮⁔桥⁦潬汯睩湧⁰牯杲慭⁳桯睳⁹潵⁡渠慤摩瑩潮慬⁷慹㨠⍩湣汵摥‼楯獴牥慭㸠畳楮朠湡浥獰慣攠獴搻⁣桡爠⨠獥瑎慭攨⤻⁩湴慩渠⡶潩搩⁻₠ꁣ桡爪⁳瑲‽•䩥晦⁋敮琢㬠ꂠ捯畴‼㰠獴爻₠ꁲ整畲渠〻⁽⁗楴栠獯浥⁳慭灬攠楮灵琠慮搠潵瑰畴㨠䕮瑥爠祯畲慭攺⁊敦映䭥湴⁙潵爠湡浥⁩猠䩥晦⁋敮琠周攠步礠獴慴敭敮琠楳₠ꁣ桡爪⁳瑲‽•䩥晦⁋敮琢㬠周楳⁳瑡瑥浥湴⁩猠慬浯獴⁴桥⁳慭攠慳㨠ꂠ捨慲⁳瑲孝‽•䩥晦⁋敮琢㬠䥮⁢潴栠獴慴敭敮瑳Ⱐ獴爠楳⁡⁣桡牡捴敲⁰潩湴敲Ⱐ慮搠業灬楣楴⁡牲慹⁳楺楮朠楳⁵獥搮⁔桥⁤楦晥牥湣攠楳⁴桡琠獴爠楮⁴桥⁦楲獴⁳瑡瑥浥湴
捨慲⨠獴爩⁩猠愠癡物慢汥⁰潩湴敲⁷桥牥慳⁳瑲⁩渠瑨攠獥捯湤⁳瑡瑥浥湴
捨慲⁳瑲孝⤠楳⁡⁣潮獴慮琠灯楮瑥爮⁒整畲湩湧⁡⁐潩湴敲⁴漠愠䱯捡氠噡物慢汥
乯琠愠䝯潤⁉摥愩⁎潷Ⱐ景汬潷楮朠瑨攠慤癩捥⁩渠䍨慰瑥爠㤠瑯慫攠祯畲⁰牯杲慭潲攠浯摵污爬⁹潵⁴特⁴漠睲楴攠愠獥灡牡瑥⁦畮捴楯測⁳整乡浥Ⱐ瑯扴慩渠瑨攠畳敲⁩湰畴⸠周攠獥瑎慭攠晵湣瑩潮⁣牥慴敳⁡⁣桡牡捴敲⁡牲慹Ⱐ慳獩杮猠畳敲⁩湰畴⤊㸾੥湤潢樊਷㌵‰扪਼㰊⽄敳琠嬶㈲‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㜳㘠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜳㐠〠刊⽔楴汥
卵浭慲祁⁰潩湴敲⁩猠愠癡物慢汥爠捯湳瑡湴⁷桯獥⁶慬略⁩猠瑨攠慤摲敳猠潦⁡湯瑨敲⁶慲楡扬攠潲⁣潮獴慮琮⁓潭攠䌫⬠瑡獫猠慲攠灥牦潲浥搠浯牥⁥慳楬礠睩瑨⁰潩湴敲猬⁷桩汥瑨敲⁃⬫⁴慳歳Ⱐ獵捨⁡猠摹湡浩挠浥浯特⁡汬潣慴楯測⁣慮湯琠扥⁰敲景牭敤⁷楴桯畴⁰潩湴敲献⁌楫攠慮礠癡物慢汥爠捯湳瑡湴Ⱐ祯甠浵獴⁤散污牥⁡⁰潩湴敲⁢敦潲攠祯甠捡渠睯牫⁷楴栠楴⸠周攠潮汹⁤楦晥牥湣攠扥瑷敥渠摥捬慲楮朠愠灯楮瑥爠慮搠愠癡物慢汥爠捯湳瑡湴⁷桩捨⁳瑯牥猠愠癡汵攠楮獴敡搠潦⁡渠慤摲敳猠楳⁴桡琠瑨攠灯楮瑥爠摥捬慲慴楯渠楮捬畤敳⁡渠慳瑥物獫⁢整睥敮⁴桥⁤慴愠瑹灥⁡湤⁴桥⁰潩湴敲慭攮⁈潷敶敲Ⱐ瑨攠摡瑡⁴祰攠楮⁴桥⁤散污牡瑩潮映愠灯楮瑥爠楳潴⁴桥⁤慴愠瑹灥映楴猠癡汵攬⁡猠楳⁴桥⁣慳攠睩瑨⁡⁶慲楡扬攠潲⁣潮獴慮琠睨楣栠獴潲敳⁡⁶慬略⁩湳瑥慤映慮⁡摤牥獳⸠周攠慣瑵慬⁤慴愠瑹灥映瑨攠癡汵攠潦⁡汬⁰潩湴敲猬⁷桥瑨敲⁩湴敧敲Ⱐ晬潡琬⁣桡牡捴敲Ⱐ潲瑨敲睩獥Ⱐ楳⁴桥⁳慭攬⁡潮朠桥硡摥捩浡氠湵浢敲⁴桡琠牥灲敳敮瑳⁡敭潲礠慤摲敳献⁒慴桥爬⁴桥⁤慴愠瑹灥⁩渠瑨攠摥捬慲慴楯渠潦⁡⁰潩湴敲⁲敦敲猠瑯⁴桥⁤慴愠瑹灥映慮潴桥爠癡物⤊㸾੥湤潢樊਷㌶‰扪਼㰊⽄敳琠嬶㈶‰⁒ 塙娠㔠㜸㠠湵汬崊⽎數琠㜳㜠〠刊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜳㔠〠刊⽔楴汥
兵楺⁗桡琠楳⁡⁰潩湴敲㼠乡浥⁡⁃⬫⁴慳欠瑨慴⁲敱畩牥猠愠灯楮瑥爠瑯⁢攠灥牦潲浥搮⁗桡琠楳⁴桥⁤楦晥牥湣攠扥瑷敥渠摥捬慲楮朠慮⁩湴敧敲⁶慲楡扬攠慮搠摥捬慲楮朠慮⁩湴敧敲⁰潩湴敲⁶慲楡扬政⁗桡琠楳⁴桥敡湩湧映瑨攠摡瑡⁴祰攠楮⁴桥⁤散污牡瑩潮映愠灯楮瑥爿⁗桡琠楳⁴桥敡湩湧⁡湤⁰畲灯獥映乕䱌㼠坨慴灥牡瑯爠摯⁹潵⁵獥⁴漠慳獩杮⁡⁰潩湴敲⁴桥⁡摤牥獳映慮潴桥爠癡物慢汥爠捯湳瑡湴㼠坨慴⁩猠瑨攠灵牰潳攠潦⁴桥⁩湤楲散瑩潮灥牡瑯爿⁍慹⁡⁰潩湴敲⁰潩湴⁴漠摩晦敲敮琠浥浯特⁡摤牥獳敳⁡琠摩晦敲敮琠瑩浥猠楮⁴桥⁰牯杲慭㼠䵡礠浯牥⁴桡渠潮攠灯楮瑥爠灯楮琠瑯⁴桥⁳慭攠浥浯特⁡摤牥獳㼠坨慴⁩猠瑨攠敦晥捴映楮捲敭敮瑩湧⁡⁰潩湴敲⁶慲楡扬政⁗桡琠慲攠瑨攠灵牰潳敳映瑨攠湥眠慮搠摥汥瑥灥牡瑯牳㼩ਾ㸊敮摯扪ਊ㜳㜠〠潢樊㰼ਯ䑥獴⁛㘲㠠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷㌸‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷㌶‰⁒ਯ呩瑬攠⣾＀䌀栀愀瀀琀攀爀 ㄀㈀㨀 䌀栀愀爀愀挀琀攀爀Ⰰ 䌀ⴀ匀琀爀椀渀最Ⰰ 愀渀搀 䌀⬀⬀ 匀琀爀椀渀最 䌀氀愀猀猀 䘀甀渀挀琀椀漀渀猀 伀瘀攀爀瘀椀攀眀 吀栀攀 眀漀爀搀†ᰀ挀栀愀爀愀挀琀攀爠ᴀ 栀愀猀 洀愀渀礀 洀攀愀渀椀渀最猀⸀ 伀渀攀 挀漀洀瀀氀椀洀攀渀琀愀爀礀 搀攀昀椀渀椀琀椀漀渀 ꀀ椀猀 甀猀攀搀 琀漀 搀攀渀漀琀攀 愀 瀀攀爀猀漀渀 眀椀琀栀 最漀漀搀 挀栀愀爀愀挀琀攀爀⸀ 䄀 氀攀猀猀 挀漀洀瀀氀椀洀攀渀琀愀爀礀 洀攀愀渀椀渀最Ⰰ 眀栀椀挀栀 䤀 栀攀愀爀搀 洀漀爀攀 琀栀愀渀 漀渀挀攀 昀爀漀洀 洀礀 瀀愀爀攀渀琀猀 愀渀搀 琀攀愀挀栀攀爀猀Ⰰ 眀愀猀 琀栀愀琀 䤀 眀愀猀 愀 挀栀愀爀愀挀琀攀爀⸀ 吀栀攀 眀漀爀搀 挀栀愀爀愀挀琀攀爀 栀愀猀 愀 琀栀椀爀搀 洀攀愀渀椀渀最 椀渀 瀀爀漀最爀愀洀洀椀渀最Ⰰ 琀栀漀甀最栠᐀愀猀 愀 搀愀琀愀 琀礀瀀攀⸀ 䄀猀 礀漀甀 氀攀愀爀渀攀搀 椀渀 䌀栀愀瀀琀攀爀 ㈀Ⰰ 攀愀挀栀 瀀爀椀渀琀愀戀氀攀 (letter, digit, punctuation⤀ 漀爀 眀栀椀琀攀猀瀀愀挀攀 (ENTER, TAB, SPACEBAR⤀ 欀攀礀 漀渀 琀栀攀 欀攀礀戀漀愀爀搀 栀愀猀 愀 挀漀爀爀攀猀瀀漀渀搀椀渀最 䄀一匀䤀Ⰰ 䄀匀䌀䤀䤀Ⰰ 漀爀 唀渀椀挀漀搀攀 瘀愀氀甀攀⸀ 吀栀甀猀Ⰰ 礀漀甀 挀愀渀 愀猀猀椀最渀 愀渀礀 猀椀渀最氀攀 甀猀攀爀 椀渀瀀甀琀 琀漀 愀 挀栀愀爀愀挀琀攀爀 瘀愀爀椀愀戀氀攀 眀椀琀栀漀甀琀 昀攀愀爀 漀昀 愀 搀愀琀愀 琀礀瀀攀 洀椀猀洀愀琀挀栀⸀ 伀昀 挀漀甀爀猀攀Ⰰ 漀昀琀攀渀 愀 甀猀攀爠ᤀ猀 椀渀瀀甀琀 洀愀礀 挀漀渀猀椀猀琀 漀昀 洀漀爀攀 琀栀愀渀 漀渀攀 挀栀愀爀愀挀琀攀爀⸀ 䄀猀 礀漀甀 氀攀愀爀渀攀搀 椀渀 䌀栀愀瀀琀攀爀 ㄀ Ⰰ 椀渀搀椀瘀椀搀甀愀氀 挀栀愀爀愀挀琀攀爀猀 洀愀礀 戀攀 漀爀最愀渀椀稀攀搀 琀漀最攀琀栀攀爀 愀猀 愀 挀栀愀爀愀挀琀攀爀 愀爀爀愀礀⸀ 唀猀甀愀氀氀礀Ⰰ 愀 挀栀愀爀愀挀琀攀爀 愀爀爀愀礀 椀猀 攀渀搀攀搀 戀礀 愀 渀甀氀氀 挀栀愀爀愀挀琀攀爀Ⰰ 猀漀 椀琀猀 瘀愀氀甀攀 挀愀渀 戀攀 漀甀琀瀀甀琀琀攀搀 戀礀 琀栀攀 挀漀甀琀 漀戀樀攀挀琀 愀渀搀 琀栀攀 猀琀爀攀愀洀 椀渀猀攀爀琀椀漀渀 漀瀀攀爀愀琀漀爀 (<<⤀⸀ 匀甀挀栀 愀 渀甀氩ਾ㸊敮摯扪ਊ㜳㠠〠潢樊㰼ਯ䑥獴⁛㘲㠠〠删⽘奚‵‷㠸畬汝ਯ乥硴‷㌹‰⁒ਯ偡牥湴‶㐳‰⁒ਯ偲敶‷㌷‰⁒ਯ呩瑬攠⡃桡灴敲‱㈺⁃桡牡捴敲Ⱐ䌭却物湧Ⱐ慮搠䌫⬠却物湧⁃污獳⁆畮捴楯湳⤊㸾੥湤潢樊਷㌹‰扪਼㰊⽄敳琠嬶㌲‰⁒ 塙娠〠㜹㈠湵汬崊⽐慲敮琠㘴㌠〠刊⽐牥瘠㜳㠠〠刊⽔楴汥
Reading a CharacterYou may legitimately be wondering why I am devoting an entire section of this chapter to reading a character. After all, reading a character usually is relatively simple. You just use the cin object and the stream insertion operator ⠀㸀㸀) as in the following code fragment: char grade; cout << "Enter a grade: "; cin >> grade; However, in programming, as in life, matters rarely are as simple as they first appear to be, and this is no exception. The seemingly minor detail of the ENTER key being pressed to end input gives rise to several interesting, and fortunately quite solvable, problems. The “Press Any Key to Continue” Problem The preceding code fragment had the user enter a character which was then assigned to a character variable. However, the purpose of a user inputting a character is not always to assign that input to a variable. For example, programs often prompt the user to press any key to continue. Indeed, a standard technical support joke concerns a user who⤊㸾੥湤潢樊਷㐰‰扪਼㰠⽌敮杴栱‷㘵〰 䙩汴敲⁛⽆污瑥䑥捯摥崠⽌敮杴栠㐳㘲㌠⽄䰠㜶㔰〠㸾ੳ瑲敡洊碜貼ॠ哕绎맫昲ﭶ枟撒찤㌓預졥ঋ耄㐒ᘁԥ覈聒ꊢ膠䊭묭戕䖥㗬Ƭꉕ宫빢毝멈ﮢ戵⵭텪ℳ얾﷿맜珏㵷饳黥䘈㝢偺咭瑽헟ꃥ秘术땒﹧뜠蓋ဒ幛鼿⟄쓗ⱙ빦燏⽞뤎↓蓐緇非㕿퇛韻湃台糝剨냼췲㕂㬒瀞奺햻쟘缅ၲ➖꽘㢟절붸ዎ⮯鶿뫃肥鼢皸忮塱﷊ⷿ﴾葆᜻몮ㇼჶ삹즊ん썛ᆇჷ゗腞䙋䝦㭚䰬頣葧㢖⌌诐溤ﺉ뒘뫢먕䡁狱ⱷ条㳎ࡁ籄䆸堬튫끏䄁첽좃僱侰龀ﵤ硦ᤊឮ

  • Using an IDE to Create and Run the “Hello World!” ProjectYou can use any plain-text editor such as Notepad to write the source code. You also can download a free compiler, which usually includes a preprocessor and linker. You then can compile and run your code from the command line. The command line may be, for example, a DOS prompt at which you type a command that specifies the action you want, such as compiling, followed by the name of the file you want to compile. While there is nothing wrong with using a plain-text editor and command line tools, many programmers, including me, prefer to create, compile, and run their programs in a C++ Integrated Development Environment, known by the acronym IDE. The term “integrated” in IDE means that the text editor, preprocessor, compiler, and linker are all together under one ⠀猀漀昀琀眀愀爀攀) roof. Thus, the IDE enables you to create, compile, and run your code using one program rather than separate programs. Additionally, most IDEs have a graphical u

  • Summary Computers can store huge amounts of information, recall that information quickly and accurately, and perform calculations with lightning speed and perfect accuracy. However, computers cannot think by themselves, and need step-by-step instructions from us telling them what to do. These instructions are called a computer program, written by a human computer programmer in a programming language such as C++. A compiler, together with a preprocessor and a linker, translates the computer program into machine language that a computer understands. We then analyzed a C++ program, which outputs “Hello World!” to the screen. The program looks simple, but much is going on behind the scenes. We analyzed that code, line by line. You then created and ran your own “Hello World!” C++ application.

  • Quiz What is a computer program? Name several advantages a computer has over humans in processing information? What is a programming language? Why is C++ a good programming language to learn? What is a function? How many main functions should a C++ program have? What is a standard library file? What is the purpose of an include directive? What does a preprocessor do? What does a compiler do? What does a linker do?

  • Chapter 2: Memory and Data Types Overview After I wrote my first book, I expectantly waited every day for my mail, hoping to receive requests for my autograph. The result was proof of the adage “be careful what you ask for.” My mailbox was stuffed with numerous requests for my autograph. Alas, these requests came from those who wanted to share my money, not my fame. My autograph was requested on checks to pay my mortgage, credit cards, insurance, phone service, electricity; well, you get the picture. These companies who love sending me bills could not possibly keep track of their thousands of customers by using pencil and paper. Instead, they use computer programs, which harness the computer’s ability to store very large amounts of information and to retrieve that stored information very quickly. We use our memory to store and recall information. So do computers. However, a computer’s memory is very different from ours. This chapter will explain how a computer’s memory works. Informati

  • Chapter 2: Memory and Data Types

  • MemoryComputer programs consist of instructions and data. As discussed in Chapter 1, instructions, written in a programming language such as C++ and then translated by the compiler and linker into machine language, give the computer step-by-step directions on what to do. The data is the information that is the subject of the program. For example, if the user of your computer program wants a list of all students with a GPA of 4.0, the data could be a list of all students and their GPAs. The program then would follow instructions to determine and output the list of all students with a GPA of 4.0. The computer program’s instructions and data have to be in the computer’s memory for the program to work. This section will explain the different types of computer memory, as well as how and where instructions and data are stored in computer memory. Types of Memory There are three principal memory locations on your computer. The central processing unit ⠀䌀倀唀) Random access memory ⠀刀䄀䴀) Persistent

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan