A Guide To Programming In C++
User Manual:
Open the PDF directly: View PDF .
Page Count: 846
Download | |
Open PDF In Browser | View PDF |
A Complete Guide to Programming in C++ Ulla Kirch-Prinz Peter Prinz JONES AND BARTLETT PUBLISHERS A Complete Guide to Programming in C++ Ulla Kirch-Prinz Peter Prinz World Headquarters Jones and Bartlett Publishers 40 Tall Pine Drive Sudbury, MA 01776 978-443-5000 info@jbpub.com www.jbpub.com Jones and Bartlett Publishers Canada 2406 Nikanna Road Mississauga, ON L5C 2W6 CANADA Jones and Bartlett Publishers International Barb House, Barb Mews London W6 7PA UK Copyright © 2002 by Jones and Bartlett Publishers, Inc. All rights reserved. No part of the material protected by this copyright notice may be reproduced or utilized in any form, electronic or mechanical, including photocopying, recording, or any information storage or retrieval system, without written permission from the copyright owner. Cover Image: Stones on shore-line and yellow leaf, Bjorkliden, Sweden, by Peter Lilja Library of Congress Cataloging-in-Publication Data Prinz, Peter. [C++ Lernen und professionell anwenden. English] A complete guide to programming in C++ / Peter Prinz, Ulla Kirch-Prinz; translated by Ian Travis. p. cm. ISBN: 0-7637-1817-3 1. C++ (Computer program language) I. Kirch-Prinz, Ulla. II. Title. QA76.73.C153 P73713 2001 005.13'3—dc21 2090 2001029617 Chief Executive Officer: Clayton Jones Chief Operating Officer: Don W. Jones, Jr. V.P., Managing Editor: Judith H. Hauck V.P., Design and Production: Anne Spencer V.P., Manufacturing and Inventory Control: Therese Bräuer Editor-in-Chief: Michael Stranz Development and Product Manager: Amy Rose Marketing Manager: Nathan Schultz Production Assistant: Tara McCormick Cover Design: Night & Day Design Composition: Northeast Compositors Text Design: Mary McKeon Printing and Binding: Courier Westford Cover printing: John Pow Company, Inc. This book was typeset in QuarkXpress 4.11 on a Macintosh G4. The font families used were Goudy, Gill Sans, Courier, Rubino Serif, and Seven Sans. The first printing was printed on 50 lb. Finch Opaque. Printed in the United States of America 05 04 03 02 01 10 9 8 7 6 5 4 3 2 1 Dedicated to our children, Vivi and Jeany This page intentionally left blank preface This book was written for readers interested in learning the C++ programming language from scratch, and for both novice and advanced C++ programmers wishing to enhance their knowledge of C++. It was our goal from the beginning to design this text with the capabilities of serving dual markets, as a textbook for students and as a holistic reference manual for professionals. The C++ language definition is based on the American National Standards Institute ANSI Standard X3J16. This standard also complies with ISO norm 14882, which was ratified by the International Standardization Organization in 1998. The C++ programming language is thus platform-independent in the main with a majority of C++ compilers providing ANSI support. New elements of the C++ language, such as exception handling and templates, are supported by most of the major compilers. Visit the Jones and Bartlett web site at www.jbpub.com for a listing of compilers available for this text. The chapters in this book are organized to guide the reader from elementary language concepts to professional software development, with in-depth coverage of all the C++ language elements en route. The order in which these elements are discussed reflects our goal of helping the reader to create useful programs at every step of the way. v vi ■ PREFACE Each double-page spread in the book is organized to provide a description of the language elements on the right-hand page while illustrating them by means of graphics and sample programs on the left-hand page. This type of visual representation offered by each spread will provide students and professionals with an unmatched guide throughout the text. The sample programs were chosen to illustrate a typical application for each language element. In addition, filter programs and case studies introduce the reader to a wide range of application scenarios. To gain command over a programming language, students need a lot of experience in developing programs. Thus, each chapter includes exercises followed by sample solutions, allowing the reader to test and enhance his or her performance and understanding of C++. The appendix provides further useful information, such as binary number representation, pre-processor directives, and operator precedence tables, making this book a wellstructured and intelligible reference guide for C++ programmers. In order to test and expand your acquired knowledge, you can download sample programs and solutions to the exercises at: http://completecpp.jbpub.com Content Organization Chapter 1 gives a thorough description of the fundamental characteristics of the objectoriented C++ programming language. In addition, students are introduced to the steps necessary for creating a fully functional C++ program. Many examples are provided to help enforce these steps and to demonstrate the basic structure of a C++ program. Chapter 2 provides a complete introduction to the basic types and objects used by C++ programs. Integral types and constants, fundamental types, and Boolean constants are just a few of the topics discussed. Chapter 3 describes how to declare and call standard functions. This chapter also teaches students to use standard classes, including standard header files. In addition, students work with string variables for the first time in this chapter. Chapter 4 explains the use of streams for input and output, with a focus on formatting techniques. Formatting flags and manipulators are discussed, as are field width, fill characters, and alignment. Chapter 5 introduces operators needed for calculations and selections. Binary, unary, relational, and logical operators are all examined in detail. Chapter 6 describes the statements needed to control the flow of a program. These include loops with while, do-while, and for; selections with if-else, switch, and the conditional operator; and jumps with goto, continue, and break. Chapter 7 provides a thorough introduction to the definition of symbolic constants and macros, illustrating their significance and use. Furthermore, a comprehensive examination of standard macros for character handling is included. Chapter 8 introduces implicit type conversions, which are performed in C++ whenever different arithmetic types occur in expressions. Additionally, the chapter explores an operator for explicit type conversion. PREFACE ■ vii Chapter 9 takes an in-depth look at the standard class string, which is used to represent strings. In addition to defining strings, the chapter looks at the various methods of string manipulation. These include inserting and erasing, searching and replacing, comparing, and concatenating strings. Chapter 10 describes how to write functions of your own. The basic rules are covered, as are passing arguments, the definition of inline functions, overloading functions and default arguments, and the principle of recursion. Chapter 11 gives a thorough explanation of storage classes for objects and functions. Object lifetime and scope are discussed, along with global, static, and auto objects. Namespaces and external and static functions are also included in the discussion. Chapter 12 explains how to define references and pointers and how to use them as parameters and/or return values of functions. In this context, passing by reference and read-only access to arguments are introduced. Chapter 13 provides a complete description of how classes are defined and how instances of classes, or objects, are used. In addition, structs and unions are introduced as examples of special classes. Chapter 14 describes how constructors and destructors are defined to create and destroy objects. Also discussed are how inline methods, access methods, and read-only methods can be used. Furthermore, the chapter explains the pointer this, which is available for all methods, and what you need to pay attention to when passing objects as arguments or returning objects. Chapter 15 gives a complete explanation of member objects and how they are initialized, and of data members that are created only once for all the objects in a class. In addition, this chapter describes constant members and enumerated types. Chapter 16 takes an in-depth look at how to define and use arrays. Of particular interest are one-dimensional and multidimensional arrays, C strings, and class arrays. Chapter 17 describes the relationship between pointers and arrays. This includes pointer arithmetic, pointer versions of functions, pointers as return values and read-only pointers, and pointer arrays. Students learn that operations that use C strings illustrate how to use pointers for efficient programming, and that string access via the command line of an application program is used to illustrate pointer arrays. Chapter 18 explains sequential file access using file streams. Students will develop an understanding of how file streams provide simple and portable file handling techniques. Chapter 19 provides a complete description of the various uses of overloaded operators. Arithmetic operators, comparisons, the subscript operator, and the shift operators for input and output are overloaded to illustrate the appropriate techniques. In addition, the concept of friend functions, which is introduced in this context, is particularly important for overloading operators. Students learn how overloading operators allows them to apply existing operators to objects of class type. Chapter 20 discusses how implicit type conversion occurs in C++ when an expression cannot be compiled directly but can be compiled after applying a conversion rule. The programmer can stipulate how the compiler will perform implicit type conversion for classes by defining conversion constructors and functions. Finally, the chapter discusses ambiguity that occurs due to type conversion and how to avoid it. viii ■ PREFACE Chapter 21 describes how a program can allocate and release memory dynamically in line with current memory requirements. Dynamic memory allocation is an important factor in many C++ programs, and the following chapters contain several case studies to help students review the subject. Chapter 22 explains how to implement classes containing pointers to dynamically allocated memory. These include your own copy constructor definition and overloading the assignment operator. A class designed to represent arrays of any given length is used as a sample application. Chapter 23 provides a thorough description of how derived classes can be constructed from existing classes by inheritance. In addition to defining derived classes, this chapter discusses how members are redefined, how objects are constructed and destroyed, and how access control to base classes can be realized. Chapter 24 discusses implicit type conversion within class hierarchies, which occurs in the context of assignments and function calls. Explicit type casting in class hierarchies is also described, paying particular attention to upcasting and downcasting. Chapter 25 gives a complete explanation of how to develop and manage polymorphic classes. In addition to defining virtual functions, dynamic downcasting in polymorphic class hierarchies is introduced. Chapter 26 describes how defining pure virtual methods can create abstract classes and how you can use abstract classes at a polymorphic interface for derived classes. To illustrate this, an inhomogeneous list, that is, a linked list whose elements can be of various class types, is implemented. Chapter 27 describes how new classes are created by multiple inheritance and explains their uses. Besides introducing students to the creation and destruction of objects in multiply-derived classes, virtual base classes are depicted to avoid ambiguity in multiple inheritance. Chapter 28 explains how a C++ program uses error-handling techniques to resolve error conditions. In addition to throwing and catching exceptions, the chapter also examines how exception specifications are declared and exception classes are defined. In addition, the use of standard exception classes is discussed. Chapter 29 examines random access to files based on file streams, and options for querying file state. Exception handling for files is discussed as well. The chapter illustrates how to make objects in polymorphic classes persistent, that is, how to save them in files. The applications introduced in this chapter include simple index files and hash tables. Chapter 30 provides a thorough explanation of the advanced uses of pointers. These include pointers to pointers, functions with a variable number of arguments, and pointers to functions. In addition, an application that defines a class used to represent dynamic matrices is introduced. Chapter 31 describes bitwise operators and how to use bit masks. The applications included demonstrate calculations with parity bits, conversion of lowercase and capital letters, and converting binary numbers. Finally, the definition of bit-fields is introduced. Chapter 32 discusses how to define and use function and class templates. In addition, special options, such as default arguments, specialization, and explicit instantiation, are PREFACE ■ ix discussed. Students learn that templates allow the construction of functions and classes based on types that have not yet been stated. Thus, templates are a powerful tool for automating program code generation. Chapter 33 explains standard class templates used to represent containers for more efficient management of object collections. These include sequences, such as lists and double ended queues; container adapters, such as stacks, queues, and priority queues; associative containers, such as sets and maps; and bitsets. In addition to discussing how to manage containers, the chapter also looks at sample applications, such as bitmaps for raster images, and routing techniques. Additional Features Chapter Goals A concise chapter introduction, which contains a description of the chapter’s contents, is presented at the beginning of each chapter. These summaries also provide students with an idea of the key points to look for throughout the chapter. Chapter Exercises Each chapter contains exercises, including programming problems, designed to test students’ knowledge and understanding of the main ideas. The exercises also provide reinforcement for key chapter concepts. Solutions are included to allow students to check their work immediately and correct any possible mistakes. Case Studies Every chapter contains a number of case studies that were designed to introduce the reader to a wide range of application scenarios. Notes This feature provides students with helpful tips and information useful to learning C++. Important concepts and rules are highlighted for additional emphasis and easy access. Hints These are informative suggestions for easier programming. Also included are common mistakes and how to avoid making them. Acknowledgements Our thanks go out to everyone who helped produce this book, particularly to Ian Travis, for his valuable contributions to the development of this book. Alexa Doehring, who reviewed all samples and program listings, and gave many valuable hints from the American perspective. Michael Stranz and Amy Rose at Jones and Bartlett Publishers, who managed the publishing agreement and the production process so smoothly. Our children, Vivi and Jeany, who left us in peace long enough to get things finished! And now all that remains is to wish you, Dear Reader, lots of fun with C++! Ulla Kirch-Prinz Peter Prinz This page intentionally left blank contents Chapter 1 Fundamentals 1 Development and Properties of C++ 2 Object-Oriented Programming 4 Developing a C++ Program 6 A Beginner’s C++ Program 8 Structure of Simple C++ Programs 10 Exercises 12 Solutions 14 Chapter 2 Fundamental Types, Constants, and Variables Fundamental Types 16 Constants 22 Escape Sequences 26 Names 28 Variables 30 The Keywords const and volatile Exercises 34 Solutions 36 15 32 xi xii ■ CONTENTS Chapter 3 Using Functions and Classes 39 Declaring Functions 40 Function Calls 42 Type void for Functions 44 Header Files 46 Standard Header Files 48 Using Standard Classes 50 Exercises 52 Solutions 54 Chapter 4 Input and Output with Streams 57 Streams 58 Formatting and Manipulators 60 Formatted Output of Integers 62 Formatted Output of Floating-Point Numbers 64 Output in Fields 66 Output of Characters, Strings, and Boolean Values Formatted Input 70 Formatted Input of Numbers 72 Unformatted Input/Output 74 Exercises 76 Solutions 78 Chapter 5 Operators for Fundamental Types 81 Binary Arithmetic Operators 82 Unary Arithmetic Operators 84 Assignments 86 Relational Operators 88 Logical Operators 90 Exercises 92 Solutions 94 Chapter 6 Control Flow 95 The while Statement 96 The for Statement 98 The do-while Statement 102 Selections with if-else 104 Else-if Chains 106 Conditional Expressions 108 Selecting with switch 110 Jumps with break, continue, and goto Exercises 114 Solutions 116 112 68 CONTENTS Chapter 7 Symbolic Constants and Macros 119 Macros 120 Macros with Parameters 122 Working with the #define Directive 124 Conditional Inclusion 126 Standard Macros for Character Manipulation 128 Redirecting Standard Input and Output 130 Exercises 132 Solutions 134 Chapter 8 Converting Arithmetic Types 139 Implicit Type Conversions 140 Performing Usual Arithmetic Type Conversions 142 Implicit Type Conversions in Assignments 144 More Type Conversions 146 Exercises 148 Solutions 150 Chapter 9 The Standard Class string 153 Defining and Assigning Strings 154 Concatenating Strings 156 Comparing Strings 158 Inserting and Erasing in Strings 160 Searching and Replacing in Strings 162 Accessing Characters in Strings 164 Exercises 166 Solutions 168 Chapter 10 Functions 171 Significance of Functions in C++ Defining Functions 174 Return Value of Functions 176 Passing Arguments 178 Inline Functions 180 Default Arguments 182 Overloading Functions 184 Recursive Functions 186 Exercises 188 Solutions 191 Chapter 11 172 Storage Classes and Namespaces Storage Classes of Objects 198 The Storage Class extern 200 197 ■ xiii xiv ■ CONTENTS The Storage Class static 202 The Specifiers auto and register 204 The Storage Classes of Functions 206 Namespaces 208 The Keyword using 210 Exercises 212 Solutions 216 Chapter 12 References and Pointers 221 Defining References 222 References as Parameters 224 References as Return Value 226 Expressions with Reference Type 228 Defining Pointers 230 The Indirection Operator 232 Pointers as Parameters 234 Exercises 236 Solutions 238 Chapter 13 Defining Classes 243 The Class Concept 244 Defining Classes 246 Defining Methods 248 Defining Objects 250 Using Objects 252 Pointers to Objects 254 Structs 256 Unions 258 Exercise 260 Solution 262 Chapter 14 Methods 265 Constructors 266 Constructor Calls 268 Destructors 270 Inline Methods 272 Access Methods 274 const Objects and Methods 276 Standard Methods 278 this Pointer 280 Passing Objects as Arguments 282 Returning Objects 284 Exercises 286 Solutions 290 CONTENTS Chapter 15 Member Objects and Static Members 297 Member Objects 298 Member Initializers 300 Constant Member Objects 302 Static Data Members 304 Accessing Static Data Members 306 Enumeration 308 Exercises 310 Solutions 314 Chapter 16 Arrays 321 Defining Arrays 322 Initializing Arrays 324 Arrays 326 Class Arrays 328 Multidimensional Arrays Member Arrays 332 Exercises 334 Solutions 338 Chapter 17 Arrays and Pointers 330 349 Arrays and Pointers (1) 350 Arrays and Pointers (2) 352 Pointer Arithmetic 354 Arrays as Arguments 356 Pointer Versions of Functions 358 Read-Only Pointers 360 Returning Pointers 362 Arrays of Pointers 364 Command Line Arguments 366 Exercises 368 Solutions 372 Chapter 18 Fundamentals of File Input and Output Files 380 File Streams 382 Creating File Streams 384 Open Modes 386 Closing Files 388 Reading and Writing Blocks Object Persistence 392 Exercises 394 Solutions 398 390 379 ■ xv xvi ■ CONTENTS Chapter 19 Overloading Operators 411 Generals 412 Operator Functions (1) 414 Operator Functions (2) 416 Using Overloaded Operators 418 Global Operator Functions 420 Friend Functions 422 Friend Classes 424 Overloading Subscript Operators 426 Overloading Shift-Operators for I/O 428 Exercises 430 Solutions 432 Chapter 20 Type Conversion for Classes Conversion Constructors 442 Conversion Functions 444 Ambiguities of Type Conversions Exercise 448 Solution 450 Chapter 21 Dynamic Memory Allocation 441 446 453 The Operator new 454 The Operator delete 456 Dynamic Storage Allocation for Classes 458 Dynamic Storage Allocation for Arrays 460 Application: Linked Lists 462 Representing a Linked List 464 Exercises 466 Solutions 468 Chapter 22 Dynamic Members 477 Members of Varying Length 478 Classes with a Dynamic Member 480 Creating and Destroying Objects 482 Implementing Methods 484 Copy Constructor 486 Assignment 488 Exercises 490 Solutions 492 Chapter 23 Inheritance 499 Concept of Inheritance Derived Classes 502 500 CONTENTS Members of Derived Classes 504 Member Access 506 Redefining Members 508 Constructing and Destroying Derived Classes Objects of Derived Classes 512 Protected Members 514 Exercises 516 Solutions 520 Chapter 24 Type Conversion in Class Hierarchies 510 529 Converting to Base Classes 530 Type Conversions and Assignments 532 Converting References and Pointers 534 Explicit Type Conversions 536 Exercises 538 Solutions 540 Chapter 25 Polymorphism 543 Concept of Polymorphism 544 Virtual Methods 546 Destroying Dynamically Allocated Objects Virtual Method Table 550 Dynamic Casts 552 Exercises 554 Solutions 558 Chapter 26 Abstract Classes 548 565 Pure Virtual Methods 566 Abstract and Concrete Classes 568 Pointers and References to Abstract Classes 570 Virtual Assignment 572 Application: Inhomogeneous Lists 574 Implementing an Inhomogeneous List 576 Exercises 578 Solutions 580 Chapter 27 Multiple Inheritance 587 Multiply-Derived Classes 588 Multiple Indirect Base Classes 590 Virtual Base Classes 592 Constructor Calls 594 Initializing Virtual Base Classes 596 Exercises 598 Solutions 602 ■ xvii xviii ■ CONTENTS Chapter 28 Exception Handling 607 Traditional Error Handling 608 Exception Handling 610 Exception Handlers 612 Throwing and Catching Exceptions 614 Nesting Exception Handling 616 Defining Your Own Error Classes 618 Standard Exception Classes 620 Exercises 622 Solutions 626 Chapter 29 More About Files 637 Opening a File for Random Access 638 Positioning for Random Access 640 File State 644 Exception Handling for Files 646 Persistence of Polymorphic Objects 648 Application: Index Files 652 Implementing an Index File 654 Exercises 656 Solutions 660 Chapter 30 More About Pointers 681 Pointer to Pointers 682 Variable Number of Arguments Pointers to Functions 688 Complex Declarations 690 Defining Typenames 692 Application: Dynamic Matrices Exercises 696 Solutions 698 Chapter 31 Manipulating Bits 684 694 705 Bitwise Operators 706 Bitwise Shift Operators 708 Bit Masks 710 Using Bit Masks 712 Bit-Fields 714 Exercises 716 Solutions 718 Chapter 32 Templates 721 Function and Class Templates Defining Templates 724 722 CONTENTS Template Instantiation 726 Template Parameters 728 Template Arguments 730 Specialization 732 Default Arguments of Templates Explicit Instantiation 736 Exercises 738 Solutions 742 Chapter 33 Containers 734 749 Container Types 750 Sequences 752 Iterators 754 Declaring Sequences 756 Inserting in Sequences 758 Accessing Objects 760 Length and Capacity 762 Deleting in Sequences 764 List Operations 766 Associative Containers 768 Sets and Multisets 770 Maps and Multimaps 772 Bitsets 774 Exercise 778 Solution 780 Appendix 783 Binary Numbers 784 Preprocessor Directives 787 Pre-Defined Standard Macros 792 Binding C Functions 793 Operators Overview 795 Operator Precedence Table 797 ASCII Code Table 798 Screen Control Sequences 800 Literature 801 Index 803 ■ xix This page intentionally left blank chapter 1 Fundamentals This chapter describes the fundamental characteristics of the objectoriented C++ programming language. In addition, you will be introduced to the steps necessary for creating a fully functional C++ program.The examples provided will help you retrace these steps and also demonstrate the basic structure of a C++ program. 1 2 ■ CHAPTER 1 FUNDAMENTALS ■ DEVELOPMENT AND PROPERTIES OF C++ Characteristics C++ C -universal -efficient -close to the machine -portable OOP -data abstraction -data hiding -inheritance -polymorphism Extensions -exception handling -templates DEVELOPMENT AND PROPERTIES OF C++ ■ 3 䊐 Historical Perspective The C++ programming language was created by Bjarne Stroustrup and his team at Bell Laboratories (AT&T, USA) to help implement simulation projects in an object-oriented and efficient way. The earliest versions, which were originally referred to as “C with classes,” date back to 1980. As the name C++ implies, C++ was derived from the C programming language: ++ is the increment operator in C. As early as 1989 an ANSI Committee (American National Standards Institute) was founded to standardize the C++ programming language. The aim was to have as many compiler vendors and software developers as possible agree on a unified description of the language in order to avoid the confusion caused by a variety of dialects. In 1998 the ISO (International Organization for Standardization) approved a standard for C++ (ISO/IEC 14882). 䊐 Characteristics of C++ C++ is not a purely object-oriented language but a hybrid that contains the functionality of the C programming language. This means that you have all the features that are available in C: ■ ■ ■ universally usable modular programs efficient, close to the machine programming portable programs for various platforms. The large quantities of existing C source code can also be used in C++ programs. C++ supports the concepts of object-oriented programming (or OOP for short), which are: ■ ■ ■ ■ data abstraction, that is, the creation of classes to describe objects data encapsulation for controlled access to object data inheritance by creating derived classes (including multiple derived classes) polymorphism (Greek for multiform), that is, the implementation of instructions that can have varying effects during program execution. Various language elements were added to C++, such as references, templates, and exception handling. Even though these elements of the language are not strictly object-oriented programming features, they are important for efficient program implementation. 4 ■ CHAPTER 1 FUNDAMENTALS ■ OBJECT-ORIENTED PROGRAMMING Traditional concept function1 data1 function2 data2 function3 Object-oriented concept object1 object2 Properties Properties Capacities Capacities OBJECT-ORIENTED PROGRAMMING ■ 5 䊐 Traditional Procedural Programming In traditional, procedural programming, data and functions (subroutines, procedures) are kept separate from the data they process. This has a significant effect on the way a program handles data: ■ ■ the programmer must ensure that data are initialized with suitable values before use and that suitable data are passed to a function when it is called if the data representation is changed, e.g. if a record is extended, the corresponding functions must also be modified. Both of these points can lead to errors and neither support low program maintenance requirements. 䊐 Objects Object-oriented programming shifts the focus of attention to the objects, that is, to the aspects on which the problem is centered. A program designed to maintain bank accounts would work with data such as balances, credit limits, transfers, interest calculations, and so on. An object representing an account in a program will have properties and capacities that are important for account management. OOP objects combine data (properties) and functions (capacities). A class defines a certain object type by defining both the properties and the capacities of the objects of that type. Objects communicate by sending each other “messages,” which in turn activate another object’s capacities. 䊐 Advantages of OOP Object-oriented programming offers several major advantages to software development: ■ ■ ■ reduced susceptibility to errors: an object controls access to its own data. More specifically, an object can reject erroneous access attempts easy re-use: objects maintain themselves and can therefore be used as building blocks for other programs low maintenance requirement: an object type can modify its own internal data representation without requiring changes to the application. 6 ■ CHAPTER 1 FUNDAMENTALS ■ DEVELOPING A C++ PROGRAM Translating a C++ program Editor Source file Header file Compiler Object file Standard library Linker Other libraries, object files Executable file DEVELOPING A C++ PROGRAM ■ 7 The following three steps are required to create and translate a C++ program: 1. First, a text editor is used to save the C++ program in a text file. In other words, the source code is saved to a source file. In larger projects the programmer will normally use modular programming. This means that the source code will be stored in several source files that are edited and translated separately. 2. The source file is put through a compiler for translation. If everything works as planned, an object file made up of machine code is created. The object file is also referred to as a module. 3. Finally, the linker combines the object file with other modules to form an executable file. These further modules contain functions from standard libraries or parts of the program that have been compiled previously. It is important to use the correct file extension for the source file’s name. Although the file extension depends on the compiler you use, the most commonly found file extensions are .cpp and .cc. Prior to compilation, header files, which are also referred to as include files, can be copied to the source file. Header files are text files containing information needed by various source files, for example, type definitions or declarations of variables and functions. Header files can have the file extension .h, but they may not have any file extension. The C++ standard library contains predefined and standardized functions that are available for any compiler. Modern compilers normally offer an integrated software development environment, which combines the steps mentioned previously into a single task. A graphical user interface is available for editing, compiling, linking, and running the application. Moreover, additional tools, such as a debugger, can be launched. ✓ NOTE If the source file contains just one syntax error, the compiler will report an error. Additional error messages may be shown if the compiler attempts to continue despite having found an error. So when you are troubleshooting a program, be sure to start with the first error shown. In addition to error messages, the compiler will also issue warnings. A warning does not indicate a syntax error but merely draws your attention to a possible error in the program’s logic, such as the use of a non-initialized variable. 8 ■ CHAPTER 1 FUNDAMENTALS ■ A BEGINNER’S C++ PROGRAM Sample program #includeusing namespace std; int main() { cout << "Enjoy yourself with C++!" return 0; } << endl; Screen output Enjoy yourself with C++! Structure of function main() Function name Type of function Beginning of function int main() { . . What What the the program program does does (satements) (statements) . . End of function } Function block A BEGINNER’S C++ PROGRAM ■ 9 A C++ program is made up of objects with their accompanying member functions and global functions, which do not belong to any single particular class. Each function fulfills its own particular task and can also call other functions. You can create functions yourself or use ready-made functions from the standard library. You will always need to write the global function main() yourself since it has a special role to play; in fact it is the main program. The short programming example on the opposite page demonstrates two of the most important elements of a C++ program. The program contains only the function main() and displays a message. The first line begins with the number symbol, #, which indicates that the line is intended for the preprocessor. The preprocessor is just one step in the first translation phase and no object code is created at this time. You can type #include to have the preprocessor copy the quoted file to this position in the source code. This allows the program access to all the information contained in the header file. The header file iostream comprises conventions for input and output streams. The word stream indicates that the information involved will be treated as a flow of data. Predefined names in C++ are to be found in the std (standard) namespace. The using directive allows direct access to the names of the std namespace. Program execution begins with the first instruction in function main(), and this is why each C++ program must have a main function. The structure of the function is shown on the opposite page. Apart from the fact that the name cannot be changed, this function’s structure is not different from that of any other C++ function. In our example the function main() contains two statements. The first statement cout << "Enjoy yourself with C++!" << endl; outputs the text string Enjoy yourself with C++! on the screen. The name cout (console output) designates an object responsible for output. The two less-than symbols, <<, indicate that characters are being “pushed” to the output stream. Finally endl (end of line) causes a line feed. The statement return 0; terminates the function main() and also the program, returning a value of 0 as an exit code to the calling program. It is standard practice to use the exit code 0 to indicate that a program has terminated correctly. Note that statements are followed by a semicolon. By the way, the shortest statement comprises only a semicolon and does nothing. 10 ■ CHAPTER 1 FUNDAMENTALS ■ STRUCTURE OF SIMPLE C++ PROGRAMS A C++ program with several functions /****************************************************** A program with some functions and comments ******************************************************/ #include using namespace std; void line(), message(); // Prototypes int main() { cout << "Hello! The program starts in main()." << endl; line(); message(); line(); cout << "At the end of main()." << endl; return 0; } void line() // To draw a line. { cout << "--------------------------------" << endl; } void message() // To display a message. { cout << "In function message()." << endl; } Screen output Hello! The program starts in main(). ----------------------------------In function message(). ----------------------------------At the end of main(). STRUCTURE OF SIMPLE C++ PROGRAMS ■ 11 The example on the opposite page shows the structure of a C++ program containing multiple functions. In C++, functions do not need to be defined in any fixed order. For example, you could define the function message() first, followed by the function line(), and finally the main() function. However, it is more common to start with the main() function as this function controls the program flow. In other words, main() calls functions that have yet to be defined. This is made possible by supplying the compiler with a function prototype that includes all the information the compiler needs. This example also introduces comments. Strings enclosed in /* . . . */ or starting with // are interpreted as comments. EXAMPLES: /* I can cover several lines */ // I can cover just one line In single-line comments the compiler ignores any characters following the // signs up to the end of the line. Comments that cover several lines are useful when troubleshooting, as you can use them to mask complete sections of your program. Both comment types can be used to comment out the other type. As to the layout of source files, the compiler parses each source file sequentially, breaking the contents down into tokens, such as function names and operators. Tokens can be separated by any number of whitespace characters, that is, by spaces, tabs, or new line characters. The order of the source code is important but it is not important to adhere to a specific layout, such as organizing your code in rows and columns. For example void message ( ){ cout << "In function message()." endl;} << might be difficult to read, but it is a correct definition of the function message(). Preprocessor directives are one exception to the layout rule since they always occupy a single line. The number sign, #, at the beginning of a line can be preceded only by a space or a tab character. To improve the legibility of your C++ programs you should adopt a consistent style, using indentation and blank lines to reflect the structure of your program. In addition, make generous use of comments. ■ CHAPTER 1 FUNDAMENTALS ■ exercise s 12 EXERCISES Program listing of exercise 3 #include using namespace std; void pause(); // Prototype int main() { cout << endl << "Dear reader, " << endl << "have a "; pause(); cout << "!" << endl; return 0; } void pause() { cout << "BREAK"; } EXERCISES Exercise 1 Write a C++ program that outputs the following text on screen: Oh what a happy day! Oh yes, what a happy day! Use the manipulator endl where appropriate. Exercise 2 The following program contains several errors: */ Now you should not forget your glasses // #include int main { cout << "If this text", cout >> " appears on your display, "; cout << " endl;" cout << 'you can pat yourself on ' << " the back!" << endl. return 0; ) Resolve the errors and run the program to test your changes. Exercise 3 What does the C++ program on the opposite page output on screen? ■ 13 ■ CHAPTER 1 ■ solutions 14 FUNDAMENTALS SOLUTIONS Exercise 1 // Let's go ! #include using namespace std; int main() { cout << cout << cout << cout << " " " " Oh what " << a happy day! Oh yes, " << what a happy endl; " << endl; endl; day! " << endl; return 0; } Exercise 2 The corrected places are underlined. /* Now you should not forget your glasses */ #include using namespace std; int main() { cout << " If this text "; cout << " appears on your display, "; cout << endl; cout << " you can pat yourself on " << " the back!" << endl; return 0; } Exercise 3 The screen output begins on a new line: Dear reader, have a BREAK! chapter 2 Fundamental Types, Constants, and Variables This chapter introduces you to the basic types and objects used by C++ programs. 15 16 ■ CHAPTER 2 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES ■ FUNDAMENTAL TYPES Overview* For boolean values bool char For characters wchar_t short For integers int long float For floating-point values double long double * without type void, which will be introduced later. FUNDAMENTAL TYPES ■ 17 A program can use several data to solve a given problem, for example, characters, integers, or floating-point numbers. Since a computer uses different methods for processing and saving data, the data type must be known. The type defines 1. the internal representation of the data, and 2. the amount of memory to allocate. A number such as -1000 can be stored in either 2 or 4 bytes. When accessing the part of memory in which the number is stored, it is important to read the correct number of bytes. Moreover, the memory content, that is the bit sequence being read, must be interpreted correctly as a signed integer. The C++ compiler recognizes the fundamental types, also referred to as built-in types, shown on the opposite page, on which all other types (vectors, pointers, classes, ...) are based. 䊐 The Type bool The result of a comparison or a logical association using AND or OR is a boolean value, which can be true or false. C++ uses the bool type to represent boolean values. An expression of the type bool can either be true or false, where the internal value for true will be represented as the numerical value 1 and false by a zero. 䊐 The char and wchar_t Types These types are used for saving character codes. A character code is an integer associated with each character. The letter A is represented by code 65, for example. The character set defines which code represents a certain character. When displaying characters on screen, the applicable character codes are transmitted and the “receiver,” that is the screen, is responsible for correctly interpreting the codes. The C++ language does not stipulate any particular characters set, although in general a character set that contains the ASCII code (American Standard Code for Information Interchange) is used. This 7-bit code contains definitions for 32 control characters (codes 0 – 31) and 96 printable characters (codes 32 – 127). The char (character) type is used to store character codes in one byte (8 bits). This amount of storage is sufficient for extended character sets, for example, the ANSI character set that contains the ASCII codes and additional characters such as German umlauts. The wchar_t (wide character type) type comprises at least 2 bytes (16 bits) and is thus capable of storing modern Unicode characters. Unicode is a 16-bit code also used in Windows NT and containing codes for approximately 35,000 characters in 24 languages. 18 ■ CHAPTER 2 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES ■ FUNDAMENTAL TYPES (CONTINUED) Integral types Type Size char 1 byte unsigned char 1 byte signed char 1 byte int 2 byte resp. 4 byte unsigned int Range of Values (decimal) — 128 to +127 or 0 to 255 0 to 255 — 128 to +127 — 32768 to +32767 resp. — 2147483648 to +2147483647 2 byte resp. 0 to 65535 resp. 4 byte 0 to 4294967295 short 2 byte unsigned short 2 byte long 4 byte unsigned long 4 byte — 32768 to +32767 0 to 65535 — 2147483648 to +2147483647 0 to 4294967295 Sample program #include #include using namespace std; // Definition of INT_MIN, ... int main() { cout << "Range of types int and unsigned int" << endl << endl; cout << "Type Minimum Maximum" << endl << "--------------------------------------------" << endl; cout << "int " << << INT_MIN << " INT_MAX << endl; " cout << "unsigned int " << " 0 << UINT_MAX << endl; " return 0; } FUNDAMENTAL TYPES (CONTINUED) ■ 19 䊐 Integral Types The types short, int, and long are available for operations with integers. These types are distinguished by their ranges of values. The table on the opposite page shows the integer types, which are also referred to as integral types, with their typical storage requirements and ranges of values. The int (integer) type is tailor-made for computers and adapts to the length of a register on the computer. For 16-bit computers, int is thus equivalent to short, whereas for 32-bit computers int will be equivalent to long. C++ treats character codes just like normal integers. This means you can perform calculations with variables belonging to the char or wchar_t types in exactly the same way as with int type variables. char is an integral type with a size of one byte. The range of values is thus –128 to +127 or from 0 to 255, depending on whether the compiler interprets the char type as signed or unsigned. This can vary in C++. The wchar_t type is a further integral type and is normally defined as unsigned short. 䊐 The signed and unsigned Modifiers ✓ The short, int, and long types are normally interpreted as signed with the highest bit representing the sign. However, integral types can be preceded by the keyword unsigned. The amount of memory required remains unaltered but the range of values changes due to the highest bit no longer being required as a sign. The keyword unsigned can be used as an abbreviation for unsigned int. The char type is also normally interpreted as signed. Since this is merely a convention and not mandatory, the signed keyword is available. Thus three types are available: char, signed char, and unsigned char. NOTE In ANSI C++ the size of integer types is not preset. However, the following order applies: char <= short <= int <= long Moreover, the short type comprises at least 2 bytes and the long type at least 4 bytes. The current value ranges are available in the climits header file. This file defines constants such as CHAR_MIN, CHAR_MAX, INT_MIN, and INT_MAX, which represent the smallest and greatest possible values. The program on the opposite page outputs the value of these constants for the int and unsigned int types. 20 ■ CHAPTER 2 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES ■ FUNDAMENTAL TYPES (CONTINUED) Floating-point types Type Size Range of Values Lowest Positive Value Accuracy (decimal) float 4 bytes –3.4E+38 1.2E—38 6 digits double 8 bytes –1.7E+308 2.3E—308 15 digits 10 bytes –1.1E+4932 3.4E—4932 19 digits long double ✓ NOTE IEEE format (IEEE = Institute of Electrical and Electronic Engineers) is normally used to represent floating-point types. The table above makes use of this representation. Arithmetic types Integral types bool char, signed char, unsigned char, wchar_t short, unsigned short int, unsigned int long, unsigned long Floating-point types float double long double ✓ NOTE Arithmetic operators are defined for arithmetic types, i.e. you can perform calculations with variables of this type. FUNDAMENTAL TYPES (CONTINUED) ■ 21 䊐 Floating-Point Types Numbers with a fraction part are indicated by a decimal point in C++ and are referred to as floating-point numbers. In contrast to integers, floating-point numbers must be stored to a preset accuracy. The following three types are available for calculations involving floating-point numbers: float double long double for simple accuracy for double accuracy for high accuracy The value range and accuracy of a type are derived from the amount of memory allocated and the internal representation of the type. Accuracy is expressed in decimal places. This means that “six decimal places” allows a programmer to store two floating-point numbers that differ within the first six decimal places as separate numbers. In reverse, there is no guarantee that the figures 12.3456 and 12.34561 will be distinguished when working to a accuracy of six decimal places. And remember, it is not a question of the position of the decimal point, but merely of the numerical sequence. If it is important for your program to display floating-point numbers with an accuracy supported by a particular machine, you should refer to the values defined in the cfloat header file. Readers interested in additional material on this subject should refer to the Appendix, which contains a section on the representation of binary numbers on computers for both integers and floating-point numbers. 䊐 The sizeof Operator The amount of memory needed to store an object of a certain type can be ascertained using the sizeof operator: sizeof(name) yields the size of an object in bytes, and the parameter name indicates the object type or the object itself. For example, sizeof(int)represents a value of 2 or 4 depending on the machine. In contrast, sizeof(float) will always equal 4. 䊐 Classification The fundamental types in C++ are integer types, floating-point types, and the void type. The types used for integers and floating-point numbers are collectively referred to as arithmetic types, as arithmetic operators are defined for them. The void type is used for expressions that do not represent a value. A function call can thus take a void type. 22 ■ CHAPTER 2 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES ■ CONSTANTS Examples for integral constants ✓ Decimal Octal Hexadecimal Type 16 020 0x10 int 255 0377 OXff int 32767 077777 0x7FFF int 32768U 0100000U 0x8000U unsigned int 100000 0303240 0x186A0 int (32 bit-) long (16 bitCPU) 10L 012L 0xAL long 27UL 033UL 0x1bUL unsigned long 2147483648 020000000000 0x80000000 unsigned long NOTE In each line of the above table, the same value is presented in a different way. Sample program // To display hexadecimal integer literals and // decimal integer literals. // #include using namespace std; int main() { // cout outputs integers as decimal integers: cout << "Value of 0xFF = " << 0xFF << " decimal" << endl; // Output: 255 decimal // The manipulator hex changes output to hexadecimal // format (dec changes to decimal format): cout << "Value of 27 = " << hex << 27 <<" hexadecimal" << endl; // Output: 1b hexadecimal return 0; } CONSTANTS ■ 23 The boolean keywords true and false, a number, a character, or a character sequence (string) are all constants, which are also referred to as a literals. Constants can thus be subdivided into ■ ■ ■ ■ boolean constants numerical constants character constants string constants. Every constant represents a value and thus a type—as does every expression in C++. The type is defined by the way the constant is written. 䊐 Boolean Constants A boolean expression can have two values that are identified by the keywords true and false. Both constants are of the bool type. They can be used, for example, to set flags representing just two states. 䊐 Integral Constants Integral numerical constants can be represented as simple decimal numbers, octals, or hexadecimals: ■ ■ ■ a decimal constant (base 10) begins with a decimal number other than zero, such as 109 or 987650 an octal constant (base 8) begins with a leading 0, for example 077 or 01234567 a hexadecimal constant (base 16) begins with the character pair 0x or 0X, for example 0x2A0 or 0X4b1C. Hexadecimal numbers can be capitalized or noncapitalized. Integral constants are normally of type int. If the value of the constant is too large for the int type, a type capable of representing larger values will be applied. The ranking for decimal constants is as follows: int, long, unsigned long You can designate the type of a constant by adding the letter L or l (for long), or U or u (for unsigned). For example, 12L 12U 12UL and and and 12l 12u 12ul correspond to the type long correspond to the type unsigned int correspond to the type unsigned long 24 ■ CHAPTER 2 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES ■ CONSTANTS (CONTINUED) Examples for floating-point constants 5.19 12. 0.519E1 12.0 0.0519e2 519.OE-2 0.75 0.00004 0.4e-4 .75 .12E+2 .4E-4 7.5e-1 12e0 4E-5 75E-2 Examples for character constants Constant Character Constant Value (ASCII code decimal) 'A' Capital A 65 'a' Lowercase a 97 ' ' Blank 32 '.' Dot 46 '0' Digit 0 48 Terminating null character 0 '\0' Internal representation of a string literal String literal: "Hello!" Stored byte sequence: 'H' 'e' '1' '1' 'o' '!' '\0' CONSTANTS (CONTINUED) ■ 25 䊐 Floating-Point Constants Floating-point numbers are always represented as decimals, a decimal point being used to distinguish the fraction part from the integer part. However, exponential notation is also permissible. EXAMPLES: 27.1 1.8E–2 // Type: double Here, 1.8E–2 represents a value of 1.8*10–2. E can also be written with a small letter e. A decimal point or E (e) must always be used to distinguish floating-point constants from integer constants. Floating-point constants are of type double by default. However, you can add F or f to designate the float type, or add L or l for the long double type. 䊐 Character Constants A character constant is a character enclosed in single quotes. Character constants take the type char. EXAMPLE: 'A' // Type: char The numerical value is the character code representing the character. The constant 'A' thus has a value of 65 in ASCII code. 䊐 String Constants You already know string constants, which were introduced for text output using the cout stream. A string constant consists of a sequence of characters enclosed in double quotes. EXAMPLE: "Today is a beautiful day!" A string constant is stored internally without the quotes but terminated with a null character, \0, represented by a byte with a numerical value of 0 — that is, all the bits in this byte are set to 0. Thus, a string occupies one byte more in memory than the number of characters it contains. An empty string, "", therefore occupies a single byte. The terminating null character \0 is not the same as the number zero and has a different character code than zero. Thus, the string EXAMPLE: "0" comprises two bytes, the first byte containing the code for the character zero 0 (ASCII code 48) and the second byte the value 0. The terminating null character \0 is an example of an escape sequence. Escape sequences are described in the following section. 26 ■ CHAPTER 2 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES ■ ESCAPE SEQUENCES Overview Single character Meaning ASCII code (decimal) \a alert (BEL) 7 \b backspace (BS) 8 \t horizontal tab (HT) 9 \n line feed (LF) 10 \v vertical tab (VT) 11 \f form feed (FF) 12 \r carriage return (CR) 13 \" " (double quote) 34 \' ' (single quote) 39 \? ? (question mark) 63 \\ \ (backslash) 92 \0 string terminating character 0 \ooo numerical value of a character ooo (octal!) numerical value of a character hh (hexadecimal!) (up to 3 octal digits) \xhh (hexadecimal digits) Sample program #include using namespace std; int main() { cout << "\nThis is\t a string\n\t\t" " with \"many\" escape sequences!\n"; return 0; } Program output: This is a string with "many" escape sequences! ESCAPE SEQUENCES ■ 27 䊐 Using Control and Special Characters Nongraphic characters can be expressed by means of escape sequences, for example \t, which represents a tab. The effect of an escape sequence will depend on the device concerned. The sequence \t, for example, depends on the setting for the tab width, which defaults to eight blanks but can be any value. An escape sequence always begins with a \ (backslash) and represents a single character. The table on the opposite page shows the standard escape sequences, their decimal values, and effects. You can use octal and hexadecimal escape sequences to create any character code. Thus, the letter A (decimal 65) in ASCII code can also be expressed as \101 (three octals) or \x41 (two hexadecimals). Traditionally, escape sequences are used only to represent non-printable characters and special characters. The control sequences for screen and printer drivers are, for example, initiated by the ESC character (decimal 27), which can be represented as \33 or \x1b. Escape sequences are used in character and string constants. EXAMPLES: '\t' "\tHello\n\tMike!" The characters ', ", and \ have no special significance when preceded by a backslash, i.e. they can be represented as \', \", and \\ respectively. When using octal numbers for escape sequences in strings, be sure to use three digits, for example, \033 and not \33. This helps to avoid any subsequent numbers being evaluated as part of the escape sequence. There is no maximum number of digits in a hexadecimal escape sequence. The sequence of hex numbers automatically terminates with the first character that is not a valid hex number. The sample program on the opposite page demonstrates the use of escape sequences in strings. The fact that a string can occupy two lines is another new feature. String constants separated only by white spaces will be concatenated to form a single string. To continue a string in the next line you can also use a backslash \ as the last character in a line, and then press the Enter key to begin a new line, where you can continue typing the string. EXAMPLE: "I am a very, very \ long string" Please note, however, that the leading spaces in the second line will be evaluated as part of the string. It is thus generally preferable to use the first method, that is, to terminate the string with " and reopen it with ". 28 ■ CHAPTER 2 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES ■ NAMES Keywords in C++ asm do inline short typeid auto double int signed typename bool dynamic_cast long sizeof union break else mutable static unsigned case enum namespace static_cast using catch explicit new struct virtual char extern operator switch void class false private template volatile const float protected this wchar_t const_cast for public throw while continue friend register true default goto reinterpret_cast try delete if return typedef Examples for names valid: a US us _var SetTextColor B12 top_of_window VOID a_very_long_name123467890 invalid: goto US$ 586_cpu true object-oriented écu NAMES ■ 29 䊐 Valid Names Within a program names are used to designate variables and functions. The following rules apply when creating names, which are also known as identifiers: ■ ■ ■ ■ a name contains a series of letters, numbers, or underscore characters ( _ ). German umlauts and accented letters are invalid. C++ is case sensitive; that is, upper- and lowercase letters are different. the first character must be a letter or underscore there are no restrictions on the length of a name and all the characters in the name are significant C++ keywords are reserved and cannot be used as names. The opposite page shows C++ keywords and some examples of valid and invalid names. The C++ compiler uses internal names that begin with one or two underscores followed by a capital letter. To avoid confusion with these names, avoid use of the underscore at the beginning of a name. Under normal circumstances the linker only evaluates a set number of characters, for example, the first 8 characters of a name. For this reason names of global objects, such as functions, should be chosen so that the first eight characters are significant. 䊐 Conventions In C++ it is standard practice to use small letters for the names of variables and functions. The names of some variables tend to be associated with a specific use. EXAMPLES: c, ch i, j, k, l, m, n x, y, z for characters for integers, in particular indices for floating-point numbers To improve the readability of your programs you should choose longer and more selfexplanatory names, such as start_index or startIndex for the first index in a range of index values. In the case of software projects, naming conventions will normally apply. For example, prefixes that indicate the type of the variable may be assigned when naming variables. 30 ■ CHAPTER 2 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES ■ VARIABLES Sample program // Definition and use of variables #include using namespace std; int gVar1; int gVar2 = 2; // Global variables, // explicit initialization int main() { char ch('A'); // Local variable being initialized // or: char ch = 'A'; cout << "Value of gVar1: cout << "Value of gVar2: cout << "Character in ch: " << gVar1 " << gVar2 " << ch << endl; << endl; << endl; int sum, number = 3; // Local variables with // and without initialization sum = number + 5; cout << "Value of sum: " << sum << endl; return 0; } ✓ HINT Both strings and all other values of fundamental types can be output with cout. Integers are printed in decimal format by default. Screen output Value of gVar1: Value of gVar2: Character in ch: Value of sum: 0 2 A 8 VARIABLES ■ 31 Data such as numbers, characters, or even complete records are stored in variables to enable their processing by a program. Variables are also referred to as objects, particularly if they belong to a class. 䊐 Defining Variables A variable must be defined before you can use it in a program. When you define a variable the type is specified and an appropriate amount of memory reserved. This memory space is addressed by reference to the name of the variable. A simple definition has the following syntax: SYNTAX: typ name1 [name2 ... ]; This defines the names of the variables in the list name1 [, name2 ...] as variables of the type type. The parentheses [ ... ] in the syntax description indicate that this part is optional and can be omitted. Thus, one or more variables can be stated within a single definition. EXAMPLES: char c; int i, counter; double x, y, size; In a program, variables can be defined either within the program’s functions or outside of them. This has the following effect: ■ ■ a variable defined outside of each function is global, i.e. it can be used by all functions a variable defined within a function is local, i.e. it can be used only in that function. Local variables are normally defined immediately after the first brace—for example at the beginning of a function. However, they can be defined wherever a statement is permitted. This means that variables can be defined immediately before they are used by the program. 䊐 Initialization A variable can be initialized, i.e. a value can be assigned to the variable, during its definition. Initialization is achieved by placing the following immediately after the name of the variable: ■ ■ an equals sign ( = ) and an initial value for the variable or round brackets containing the value of the variable. EXAMPLES: char c = 'a'; float x(1.875); Any global variables not explicitly initialized default to zero. In contrast, the initial value for any local variables that you fail to initialize will have an undefined initial value. 32 ■ CHAPTER 2 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES ■ THE KEYWORDS const AND volatile Sample program // Circumference and area of a circle with radius 2.5 #include using namespace std; const double pi = 3.141593; int main() { double area, circuit, radius = 1.5; area = pi * radius * radius; circuit = 2 * pi * radius; cout << "\nTo Evaluate a Circle\n" << endl; cout << "Radius: " << radius << "Circumference: " << circuit << "Area: " << area << endl << endl << endl; return 0; } ✓ NOTE By default cout outputs a floating-point number with a maximum of 6 decimal places without trailing zeros. Screen output To Evaluate a Circle Radius: Circumference: Area: 1.5 9.42478 7.06858 THE KEYWORDS CONST AND VOLATILE ■ 33 A type can be modified using the const and volatile keywords. 䊐 Constant Objects The const keyword is used to create a “read only” object. As an object of this type is constant, it cannot be modified at a later stage and must be initialized during its definition. EXAMPLE: const double pi = 3.1415947; Thus the value of pi cannot be modified by the program. Even a statement such as the following will merely result in an error message: pi = pi + 2.0; // invalid 䊐 Volatile Objects The keyword volatile, which is rarely used, creates variables that can be modified not only by the program but also by other programs and external events. Events can be initiated by interrupts or by a hardware clock, for example. EXAMPLE: volatile unsigned long clock_ticks; Even if the program itself does not modify the variable, the compiler must assume that the value of the variable has changed since it was last accessed. The compiler therefore creates machine code to read the value of the variable whenever it is accessed instead of repeatedly using a value that has been read at a prior stage. It is also possible to combine the keywords const and volatile when declaring a variable. EXAMPLE: volatile const unsigned time_to_live; Based on this declaration, the variable time_to_live cannot be modified by the program but by external events. ■ CHAPTER 2 ■ exercise s 34 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES EXERCISES Screen output for exercise 2 I "RUSH" \TO\ AND /FRO/ For exercise 3 Defining and initializing variables: int a(2.5); int b = '?'; char z(500); int big = 40000; double he's(1.2E+5); const long large; char c('\''); unsigned char ch = '\201'; unsigned size(40000); float val = 12345.12345; EXERCISES ■ 35 Exercise 1 The sizeof operator can be used to determine the number of bytes occupied in memory by a variable of a certain type. For example, sizeof(short) is equivalent to 2. Write a C++ program that displays the memory space required by each fundamental type on screen. Exercise 2 Write a C++ program to generate the screen output shown on the opposite page. Exercise 3 Which of the variable definitions shown on the opposite page is invalid or does not make sense? Exercise 4 Write a C++ program that two defines variables for floating-point numbers and initializes them with the values 123.456 and 76.543 Then display the sum and the difference of these two numbers on screen. ■ CHAPTER 2 ■ solutions 36 FUNDAMENTAL TYPES, CONSTANTS, AND VARIABLES SOLUTIONS Exercise 1 #include using namespace std; int main() { cout << << << cout << cout << cout << cout << cout << cout << cout << << "\nSize of Fundamental Types\n" " Type Number of Bytes\n" "----------------------------------" << endl; " char: " << sizeof(char) << endl; " short: " << sizeof(short)<< endl; " int: " << sizeof(int) << endl; " long: " << sizeof(long) << endl; " float: " << sizeof(float)<< endl; " double: " << sizeof(double)< using namespace std; int main() { cout << "\n\n\t I" "\n\n\t\t \"RUSH\"" "\n\n\t\t\t \\TO\\" "\n\n\t\t AND" "\n\n\t /FRO/" << endl; return 0; } // // // // // Instead of tabs you can send the suited number of blanks to the output. SOLUTIONS Exercise 3 Incorrect: int a(2.5); const long large; char z(500); int big = 40000; double he's(1.2E+5); float val = 12345.12345; // // // // // // // // // // 2.5 is not an integer value Without initialization The value 500 is too large to fit in a byte Attention! On 16-bit systems int values are <= 32767 The character ' is not allowed in names The accuracy of float is only 6 digits Exercise 4 // Defining and initializing variables #include using namespace std; int main() { float x = 123.456F, y = 76.543F, sum; // or double sum = x + y; cout << "Total: " << x << " + " << y << " = " << sum << endl; cout << "Difference: " << x << " — " << y << " = " << (x — y) << endl; return 0; } ■ 37 This page intentionally left blank chapter 3 Using Functions and Classes This chapter describes how to ■ declare and call standard functions and ■ use standard classes. This includes using standard header files. In addition, we will be working with string variables, i.e. objects belonging to the standard class string for the first time. Functions and classes that you define on your own will not be introduced until later in the book. 39 40 ■ CHAPTER 3 USING FUNCTIONS AND CLASSES ■ DECLARING FUNCTIONS Example of a function prototype Function name long func (int, double); Types of arguments Function type = type of return value The prototype above yields the following information to the compiler: ■ func is the function name ■ the function is called with two arguments: the first argument is of type int, the second of type double the return value of the function is of type long. ■ Mathematical standard functions double sin (double); // Sine double cos (double); // Cosine double tan (double); // Tangent double atan (double); // Arc tangent double cosh (double); // Hyperbolic Cosine double sqrt (double); // Square Root double pow (double, double); // Power double exp (double); // Exponential Function double log (double); // Natural Logarithm double log10 (double); // Base-ten Logarithm DECLARING FUNCTIONS ■ 41 䊐 Declarations Each name (identifier) occurring in a program must be known to the compiler or it will cause an error message. That means any names apart from keywords must be declared, i.e. introduced to the compiler, before they are used. Each time a variable or a function is defined it is also declared. But conversely, not every declaration needs to be a definition. If you need to use a function that has already been introduced in a library, you must declare the function but you do not need to redefine it. 䊐 Declaring Functions A function has a name and a type, much like a variable. The function’s type is defined by its return value, that is, the value the function passes back to the program. In addition, the type of arguments required by a function is important. When a function is declared, the compiler must therefore be provided with information on ■ ■ the name and type of the function and the type of each argument. This is also referred to as the function prototype. Examples: int toupper(int); double pow(double, double); This informs the compiler that the function toupper() is of type int, i.e. its return value is of type int, and it expects an argument of type int. The second function pow() is of type double and two arguments of type double must be passed to the function when it is called. The types of the arguments may be followed by names, however, the names are viewed as a comment only. Examples: int toupper(int c); double pow(double base, double exponent); From the compiler’s point of view, these prototypes are equivalent to the prototypes in the previous example. Both junctions are standard junctions. Standard function prototypes do not need to be declared, nor should they be, as they have already been declared in standard header files. If the header file is included in the program’s source code by means of the #include directive, the function can be used immediately. Example: #include Following this directive, the mathematical standard functions, such as sin(), cos(), and pow(), are available. Additional details on header files can be found later in this chapter. 42 ■ CHAPTER 3 USING FUNCTIONS AND CLASSES ■ FUNCTION CALLS Sample program // // Calculating powers with the standard function pow() #include #include // Declaration of cout // Prototype of pow(), thus: // double pow( double, double); using namespace std; int main() { double x = 2.5, y; // By means of a prototype, the compiler generates // the correct call or an error message! // Computes x raised y = pow("x", 3.0); y = pow(x + 3.0); y = pow(x, 3.0); y = pow(x, 3); to // // // // // the power 3: Error! String is not a number Error! Just one argument ok! ok! The compiler converts the int value 3 to double. cout << "2.5 raised to 3 yields: << y << endl; " // Calculating with pow() is possible: cout << "2 + (5 raised to the power 2.5) yields: " << 2.0 + pow(5.0, x) << endl; return 0; } Screen output 2.5 raised to the power 3 yields: 2 + (5 raised to the power 2.5) yields: 15.625 57.9017 FUNCTION CALLS ■ 43 䊐 Function Calls A function call is an expression of the same type as the function and whose value corresponds to the return value. The return value is commonly passed to a suitable variable. Example: y = pow( x, 3.0); In this example the function pow()is first called using the arguments x and 3.0, and the result, the power x3, is assigned to y. As the function call represents a value, other operations are also possible. Thus, the function pow() can be used to perform calculations for double values. Example: cout << 2.0 + pow( 5.0, x); This expression first adds the number 2.0 to the return value of pow(5.0,x), then outputs the result using cout. Any expression can be passed to a function as an argument, such as a constant or an arithmetical expression. However, it is important that the types of the arguments correspond to those expected by the function. The compiler refers to the prototype to check that the function has been called correctly. If the argument type does not match exactly to the type defined in the prototype, the compiler performs type conversion, if possible. Example: y = pow( x, 3); // also ok! The value 3 of type int is passed to the function as a second argument. But since the function expects a double value, the compiler will perform type conversion from int to double. If a function is called with the wrong number of arguments, or if type conversion proves impossible, the compiler generates an error message. This allows you to recognize and correct errors caused by calling functions at the development stage instead of causing runtime errors. Example: float x = pow(3.0 + 4.7); // Error! The compiler recognizes that the number of arguments is incorrect. In addition, the compiler will issue a warning, since a double, i.e. the return value of pow(), is assigned to a float type variable. 44 ■ CHAPTER 3 USING FUNCTIONS AND CLASSES ■ TYPE void FOR FUNCTIONS Sample program // Outputs three random numbers #include #include // // // // Declaration of cin and cout Prototypes of srand(), rand(): void srand( unsigned int seed ); int rand( void ); using namespace std; int main() { unsigned int seed; int z1, z2, z3; cout << " --- Random Numbers --- \n" << endl; cout << "To initialize the random number generator, " << "\n please enter an integer value: "; cin >> seed; // Input an integer srand( seed); // and use it as argument for a // new sequence of random numbers. z1 = rand(); z2 = rand(); z3 = rand(); // Compute three random numbers. cout << "\nThree random numbers: " << z1 << " " << z2 << " " << z3 << endl; return 0; } ✓ NOTE The statement cin >> seed; reads an integer from the keyboard, because seed is of the unsigned int type. Sample screen output --- Random Numbers --- To initialize the random number generator, please enter an integer value: 7777 Three random numbers: 25435 6908 14579 TYPE VOID FOR FUNCTIONS ■ 45 䊐 Functions without Return Value You can also write functions that perform a certain action but do not return a value to the function that called them. The type void is available for functions of this type, which are also referred to as procedures in other programming languages. Example: void srand( unsigned int seed ); The standard function srand() initializes an algorithm that generates random numbers. Since the function does not return a value, it is of type void. An unsigned value is passed to the function as an argument to seed the random number generator. The value is used to create a series of random numbers. 䊐 Functions without Arguments If a function does not expect an argument, the function prototype must be declared as void or the braces following the function name must be left empty. Example: int rand( void ); // or int rand(); The standard function rand() is called without any arguments and returns a random number between 0 and 32767. A series of random numbers can be generated by repeating the function call. 䊐 Usage of srand() and rand() The function prototypes for srand() and rand() can be found in both the cstdlib and stdlib.h header files. Calling the function rand() without previously having called srand() creates the same sequence of numbers as if the following statement would have been proceeded: srand(1); If you want to avoid generating the same sequence of random numbers whenever the program is executed, you must call srand() with a different value for the argument whenever the program is run. It is common to use the current time to initialize a random number generator. See Chapter 6 for an example of this technique. 46 ■ CHAPTER 3 USING FUNCTIONS AND CLASSES ■ HEADER FILES Using header files Header file Header file iostream myheader.h // // // // // Declaration // of cin, cout, // . . . Declaration of self-defined functions and classes long myfunc(int); Source file application.cpp Copy #include #include "myheader.h" int main() { int a; . . . cin >> a; cout << myfunc (a); . . . return 0; } Copy HEADER FILES ■ 47 䊐 Using Header Files Header files are text files containing declarations and macros. By using an #include directive these declarations and macros can be made available to any other source file, even in other header files. Pay attention to the following points when using header files: ■ ■ ■ header files should generally be included at the start of a program before any other declarations you can only name one header file per #include directive the file name must be enclosed in angled brackets < ... > or double quotes " ... ". 䊐 Searching for Header Files The header files that accompany your compiler will tend to be stored in a folder of their own—normally called include. If the name of the header file is enclosed by angled brackets < ... >, it is common to search for header files in the include folder only. The current directory is not searched to increase the speed when searching for header files. C++ programmers commonly write their own header files and store them in the current project folder. To enable the compiler to find these header files, the #include directive must state the name of the header files in double quotes. Example: #include "project.h" The compiler will then also search the current folder. The file suffix .h is normally used for user-defined header files. 䊐 Standard Class Definitions In addition to standard function prototypes, the header files also contain standard class definitions. When a header file is included, the classes defined and any objects declared in the file are available to the program. Example: #include using namespace std; Following these directives, the classes istream and ostream can be used with the cin and cout streams. cin is an object of the istream class and cout an object of the ostream class. 48 ■ CHAPTER 3 USING FUNCTIONS AND CLASSES ■ STANDARD HEADER FILES Header files of the C++ standard library ✓ algorithm ios map stack bitset iosfwd memory stdexcept complex iostream new streambuf dequeue istream numeric string exception iterator ostream typeinfo fstream limits queue utility functional list set valarray iomanip locale sstream vector NOTE Some IDE’s put the old-fashioned iostream.h and iomanip.h header files at your disposal. Within these header files the identifiers of iostream and iomanip are not contained in the std namespace but are declared globally. Header files of the C standard library assert.h limits.h stdarg.h time.h ctype.h locale.h stddef.h wchar.h errno.h math.h stdio.h wctype.h float.h setjmp.h stdlib.h iso646.h signal.h string.h STANDARD HEADER FILES ■ 49 The C++ standard library header files are shown opposite. They are not indicated by the file extension .h and contain all the declarations in their own namespace, std. Namespaces will be introduced in a later chapter. For now, it is sufficient to know that identifiers from other namespaces cannot be referred to directly. If you merely stipulate the directive Example: #include the compiler would not be aware of the cin and cout streams. In order to use the identifiers of the std namespace globally, you must add a using directive. Example: #include #include using namespace std; You can then use cin and cout without any additional syntax. The header file string has also been included. This makes the string class available and allows userfriendly string manipulations in C++. The following pages contain further details on this topic. 䊐 Header Files in the C Programming Language The header files standardized for the C programming language were adopted for the C++ standard and, thus, the complete functionality of the standard C libraries is available to C++ programs. Example: #include Mathematical functions are made available by this statement. The identifiers declared in C header files are globally visible. This can cause name conflicts in large programs. For this reason each C header file, for example name.h, is accompanied in C++ by a second header file, cname, which declares the same identifiers in the std namespace. Including the file math.h is thus equivalent to Example: #include using namespace std; The string.h or cstring files must be included in programs that use standard functions to manipulate C strings. These header files grant access to the functionality of the C string library and are to be distinguished from the string header file that defines the string class. Each compiler offers additional header files for platform dependent functionalities. These may be graphics libraries or database interfaces. 50 ■ CHAPTER 3 USING FUNCTIONS AND CLASSES ■ USING STANDARD CLASSES Sample program using class string // To use strings. #include #include using namespace std; // Declaration of cin, cout // Declaration of class string int main() { // Defines four strings: string prompt("What is your name: "), name, // An empty line( 40, '-'), // string with 40 '-' total = "Hello "; // is possible! cout << prompt; getline( cin, name); total = total + name; // Request for input. // Inputs a name in one line // Concatenates and // assigns strings. cout << line << endl // Outputs line and name << total << endl; cout << " Your name is " // Outputs length << name.length() << " characters long!" << endl; cout << line << endl; return 0; } ✓ NOTE Both the operators + and += for concatenation and the relational operators <, <=, >, >=, ==, and != are defined for objects of class string. Strings can be printed with cout and the operator <<. The class string will be introduced in detail later on. Sample screen output What is your name: Rose Summer --------------------------------------Hello Rose Summer Your name is 11 characters long! --------------------------------------- USING STANDARD CLASSES ■ 51 Several classes are defined in the C++ standard library. These include stream classes for input and output, but also classes for representing strings or handling error conditions. Each class is a type with certain properties and capacities. As previously mentioned, the properties of a class are defined by its data members and the class’s capacities are defined by its methods. Methods are functions that belong to a class and cooperate with the members to perform certain operations. Methods are also referred to as member functions. 䊐 Creating Objects An object is a variable of a class type, also referred to as an instance of the class. When an object is created, memory is allocated to the data members and initialized with suitable values. Example: string s("I am a string"); In this example the object s, an instance of the standard class string (or simply a string), is defined and initialized with the string constant that follows. Objects of the string class manage the memory space required for the string themselves. In general, there are several ways of initializing an object of a class. A string can thus be initialized with a certain number of identical characters, as the example on the opposite page illustrates. 䊐 Calling Methods All the methods defined as public within the corresponding class can be called for an object. In contrast to calling a global function, a method is always called for one particular object. The name of the object precedes the method and is separated from the method by a period. Example: s.length(); // object.method(); The method length() supplies the length of a string, i.e. the number of characters in a string. This results in a value of 13 for the string s defined above. 䊐 Classes and Global Functions Globally defined functions exist for some standard classes. These functions perform certain operations for objects passed as arguments. The global function getline(), for example, stores a line of keyboard input in a string. Example: getline(cin, s); The keyboard input is terminated by pressing the return key to create a new-line character, '\n', which is not stored in the string. ■ CHAPTER 3 USING FUNCTIONS AND CLASSES ■ exercise s 52 EXERCISES Screen output for exercise 1 Number Square Root 4 12.25 0.0121 2 3.5 0.11 Listing for exercise 2 // A program containing errors! # include , # include # void srand( seed); int main() { string message "\nLearn from your mistakes!"; cout << message << endl; int len = length( message); cout << "Length of the string: " << len << endl; // And a random number in addition: int a, b; a = srand(12.5); b = rand( a ); cout << "\nRandom number: " << b << endl; return 0; } EXERCISES ■ 53 Exercise 1 Create a program to calculate the square roots of the numbers 4 12.25 0.0121 and output them as shown opposite.Then read a number from the keyboard and output the square root of this number. To calculate the square root, use the function sqrt(), which is defined by the following prototype in the math.h (or cmath ) header file: double sqrt( double x); The return value of the sqrt() function is the square root of x. Exercise 2 The program on the opposite page contains several errors! Correct the errors and ensure that the program can be executed. Exercise 3 Create a C++ program that defines a string containing the following character sequence: I have learned something new again! and displays the length of the string on screen. Read two lines of text from the keyboard. Concatenate the strings using " * " to separate the two parts of the string. Output the new string on screen. ■ CHAPTER 3 ■ solutions 54 USING FUNCTIONS AND CLASSES SOLUTIONS Exercise 1 // Compute square roots #include #include using namespace std; int main() { double x1 = 4.0, x2 = 12.25, x3 = 0.0121; cout << "\n cout << "\n << "\n << "\n Number \t Square Root" " << x1 << " \t " << " << x2 << " \t " << " << x3 << " \t " << << endl; sqrt(x1) sqrt(x2) sqrt(x3) << endl; cout << "\nType a number whose square root is to be" " computed. "; cin >> x1; cout << "\n cout << "\n Number \t Square Root" << endl; " << x1 << " \t " << sqrt(x1) << endl; return 0; } Exercise 2 // The corrected program: #include #include // Just one header file in a line #include // Prototypes of functions // void srand( unsigned int seed); // int rand(void); // or: // #include using namespace std; // Introduces all names of namespace // std into the global scope. int main() { string message = "\nLearn from your mistakes!";...// = cout << message << endl; SOLUTIONS ■ int len = message.length(); // instead of: length(message); cout << "Length of the string: " << len << endl; // And another random number: int b; // Variable a is not needed. srand(12); // instead of: a = srand(12.5); b = rand(); // instead of: b = rand(a); cout << "\nRandom number: " << b << endl; return 0; } Exercise 3 #include #include using namespace std; // Declaration of cin, cout // Declaration of class string int main() { string message("I have learned something new again!\n"), prompt("Please input two lines of text:"), str1, str2, sum; cout << message << endl; // Outputs the message cout << prompt << endl; // Request for input getline( cin, str1); getline( cin, str2); sum = str1 + " * " + str2; cout << sum << endl; // Reads the first // and the second line of text return 0; } // Concatenates, assigns // and outputs strings. 55 This page intentionally left blank chapter 4 Input and Output with Streams This chapter describes the use of streams for input and output, focusing on formatting techniques. 57 58 ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS ■ STREAMS Stream classes for input and output ios istream ostream iostream The four standard streams ■ ■ ■ ■ cin cout cerr clog Object of class istream to control standard input Object of class ostream to control standard output Object of class ostream to control unbuffered error output Object of class ostream to control buffered error output STREAMS ■ 59 䊐 I/O Stream Classes During the development of C++ a new class-based input/output system was implemented. This gave rise to the I/O stream classes, which are now available in a library of their own, the so-called iostream library. The diagram on the opposite page shows how a so-called class hierarchy develops due to inheritance. The class ios is the base class of all other stream classes. It contains the attributes and abilities common to all streams. Effectively, the ios class ■ ■ manages the connection to the physical data stream that writes your program’s data to a file or outputs the data on screen contains the basic functions needed for formatting data. A number of flags that determine how character input is interpreted have been defined for this purpose. The istream and ostream classes derived from ios form a user-friendly interface for stream manipulation. The istream class is used for reading streams and the ostream class is used for writing to streams. The operator >> is defined in istream and << is defined in ostream, for example. The iostream class is derived by multiple inheritance from istream and ostream and thus offers the functionality of both classes. Further stream classes, a file management class, for example, are derived from the classes mentioned above. This allows the developer to use the techniques described for file manipulation. These classes, which also contain methods for opening and closing files, will be discussed in a later chapter. 䊐 Standard Streams The streams cin and cout, which were mentioned earlier, are instances of the istream or ostream classes. When a program is launched these objects are automatically created to read standard input or write to standard output. Standard input is normally the keyboard and standard output the screen. However, standard input and output can be redirected to files. In this case, data is not read from the keyboard but from a file, or data is not displayed on screen but written to a file. The other two standard streams cerr and clog are used to display messages when errors occur. Error messages are displayed on screen even if standard output has been redirected to a file. 60 ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS ■ FORMATTING AND MANIPULATORS Example: Calling a manipulator Here the manipulator showpos is called. ↓ cout << showpos << 123; // Output: +123 The above statement is equivalent to cout.setf( ios::showpos); cout << 123; The other positive numbers are printed with their sign as well: cout << 22; // Output: +22 The output of a positive sign can be canceled by the manipulator noshowpos: cout << noshowpos << 123; // Output: 123 The last statement is equivalent to cout.unsetf( ios::showpos); cout << 123; ✓ HINTS ■ The operators >> and << format the input and/or output according to how the flags in the base class ios are set ■ The manipulator showpos is a function that calls the method cout.setf(ios::showpos);, ios::showpos being the flag showpos belonging to the ios class ■ Using manipulators is easier than directly accessing flags. For this reason, manipulators are described in the following section, whereas the methods setf() and unsetf() are used only under exceptional circumstances. ■ Old compilers only supply some of the manipulators. In this case, you have to use the methods setf() and unsetf(). FORMATTING AND MANIPULATORS ■ 61 䊐 Formatting When reading keyboard input, a valid input format must be used to determine how input is to be interpreted. Similarly, screen output adheres to set of rules governing how, for example, floating-point numbers are displayed. The stream classes istream and ostream offer various options for performing these tasks. For example, you can display a table of numeric values in a simple way. In previous chapters we have looked at the cin and cout streams in statements such as: cout << "Please enter a number: "; cin >> x; The following sections systematically describe the abilities of the stream classes. This includes: ■ ■ ■ the >> and << operators for formatted input and output. These operators are defined for expressions with fundamental types—that is, for characters, boolean values, numbers and strings. manipulators, which can be inserted into the input or output stream. Manipulators can be used to generate formats for subsequent input/output. One manipulator that you are already familiar with is endl, which generates a line feed at the end of a line. other methods for determining or modifying the state of a stream and unformatted input and output. 䊐 Flags and Manipulators Formatting flags defined in the parent class ios determine how characters are input or output. In general, flags are represented by individual bits within a special integral variable. For example, depending on whether a bit is set or not, a positive number can be output with or without a plus sign. Each flag has a default setting. For example, integral numbers are output as decimals by default, and positive numbers are output without a plus sign. It is possible to modify individual formatting flags. The methods setf() and unsetf() can be used for this purpose. However, the same effect can be achieved simply by using so-called manipulators, which are defined for all important flags. Manipulators are functions that can be inserted into the input or output stream and thus be called. 62 ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS ■ FORMATTED OUTPUT OF INTEGERS Manipulators formatting integers Manipulator Effects oct Octal base hex Hexadecimal base dec Decimal base (by default) showpos Generates a + sign in non-negative numeric output. noshowpos Generates non-negative numeric output without a + sign (by default). uppercase Generates capital letters in hexadecimal output. nouppercase Generates lowercase letters in hexadecimal output (by default). Sample program // Reads integral decimal values and // generates octal, decimal, and hexadecimal output. #include using namespace std; // Declarations of cin, cout and // manipulators oct, hex, ... int main() { int number; cout << "Please enter an integer: "; cin >> number; cout << uppercase // for hex-digits << " octal \t decimal \t hexadecimal\n " << oct << number << " \t " << dec << number << " \t " << hex << number << endl; return 0; } FORMATTED OUTPUT OF INTEGERS ■ 63 䊐 Formatting Options The << operator can output values of type short, int, long or a corresponding unsigned type. The following formatting options are available: ■ ■ ■ define the numeric system in which to display the number: decimal, octal, or hexadecimal use capitals instead of small letters for hexadecimals display a sign for positive numbers. In addition, the field width can be defined for the above types. The field width can also be defined for characters, strings, and floating-point numbers, and will be discussed in the following sections. 䊐 Numeric System Integral numbers are displayed as decimals by default. The manipulators oct, hex, and dec can be used for switching from and to decimal display mode. Example: cout << hex << 11; // Output: b Hexadecimals are displayed in small letters by default, that is, using a, b, ..., f. The manipulator uppercase allows you to use capitals. Example: cout << hex << uppercase << 11; //Output: B The manipulator nouppercase returns the output format to small letters. 䊐 Negative Numbers When negative numbers are output as decimals, the output will always include a sign. You can use the showpos manipulator to output signed positive numbers. Example: cout << dec << showpos << 11; //Output: +11 You can use noshowpos to revert to the original display mode. When octal or hexadecimal numbers are output, the bits of the number to be output are always interpreted as unsigned! In other words, the output shows the bit pattern of a number in octal or hexadecimal format. Example: cout << dec << -1 << " " << hex << -1; This statement causes the following output on a 32-bit system: -1 ffffffff 64 ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS ■ FORMATTED OUTPUT OF FLOATING-POINT NUMBERS Manipulators formatting floating-point numbers Manipulator showpoint Effects Generates a decimal point character shown in floating-point output. The number of digits after the decimal point corresponds to the used precision. noshowpoint Trailing zeroes after the decimal point are not printed. If there are no digits after the decimal point, the decimal point is not printed (by default). fixed Output in fixed point notation scientific Output in scientific notation setprecision (int n) Sets the precision to n. Methods for precision Manipulator ✓ Effects int precision (int n); Sets the precision to n. int precision() const; Returns the used precision. NOTE The key word const within the prototype of precision() signifies that the method performs only read operations. Sample program #include using namespace std; int main() { double x = 12.0; cout.precision(2); // Precision 2 cout << " By default: " << x << endl; cout << " showpoint: " << showpoint << x << endl; cout << " fixed: " << fixed << x << endl; cout << " scientific: " << scientific << x << endl; return 0; } FORMATTED OUTPUT OF FLOATING-POINT NUMBERS ■ 65 䊐 Standard Settings Floating-points are displayed to six digits by default. Decimals are separated from the integral part of the number by a decimal point. Trailing zeroes behind the decimal point are not printed. If there are no digits after the decimal point, the decimal point is not printed (by default). Examples: cout << 1.0; cout << 1.234; cout << 1.234567; // Output: 1 // Output: 1.234 // Output: 1.23457 The last statement shows that the seventh digit is not simply truncated but rounded. Very large and very small numbers are displayed in exponential notation. Example: cout << 1234567.8; // Output: 1.23457e+06 䊐 Formatting The standard settings can be modified in several ways. You can ■ ■ ■ change the precision, i.e. the number of digits to be output force output of the decimal point and trailing zeroes stipulate the display mode (fixed point or exponential). Both the manipulator setprecision()and the method precision() can be used to redefine precision to be used. Example: cout << setprecision(3); // Precision: 3 // or: cout.precision(3); cout << 12.34; // Output: 12.3 Note that the header file iomanip must be included when using the manipulator setprecision(). This also applies to all standard manipulators called with at least one argument. The manipulator showpoint outputs the decimal point and trailing zeroes. The number of digits being output (e.g. 6) equals the current precision. Example: cout << showpoint << 1.0; // Output: 1.00000 However, fixed point output with a predetermined number of decimal places is often more useful. In this case, you can use the fixed manipulator with the precision defining the number of decimal places. The default value of 6 is assumed in the following example. Example: cout << fixed << 66.0; // Output: 66.000000 In contrast, you can use the scientific manipulator to specify that floating-point numbers are output as exponential expressions. 66 ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS ■ OUTPUT IN FIELDS Element functions for output in fields Method Effects int width() const; Returns the minimum field width used int width(int n); Sets the minimum field width to n int fill() const; Returns the fill character used int fill(int ch); Sets the fill character to ch Manipulators for output in fields Manipulator Effects setw(int n) Sets the minimum field width to n setfill(int ch) Sets the fill character to ch left Left-aligns output in fields right Right-aligns output in fields internal Left-aligns output of the sign and right-aligns output of the numeric value ✓ NOTE The manipulators setw() and setfill() are declared in the header file iomanip . Examples #include #include using namespace std; // Obligatory // declarations 1st Example: cout << '|' << setw(6) << 'X' << '|'; Output: | X| // Field width 6 2nd Example: cout << fixed << setprecision(2) Output: << setw(10) << 123.4 << endl << "1234567890" << endl; 123.40 // Field width 10 1234567890 OUTPUT IN FIELDS ■ 67 The << operator can be used to generate formatted output in fields. You can ■ ■ ■ specify the field width set the alignment of the output to right- or left-justified specify a fill-character with which to fill the field. 䊐 Field Width The field width is the number of characters that can be written to a field. If the output string is larger than the field width, the output is not truncated but the field is extended. The output will always contain at least the number of digits specified as the field width. You can either use the width() method or the setw() manipulator to define field width. Example: cout.width(6); // or: cout << setw(6); One special attribute of the field width is the fact that this value is non-permanent: the field width specified applies to the next output only, as is illustrated by the examples on the opposite page. The first example outputs the character 'X' to a field with width of 6, but does not output the '|' character. The default field width is 0. You can also use the width() method to get the current field width. To do so, call width() without any other arguments. Example: int fieldwidth = cout.width(); 䊐 Fill Characters and Alignment If a field is larger than the string you need to output, blanks are used by default to fill the field. You can either use the fill() method or the setfill() manipulator to specify another fill character. Example: cout << setfill('*') << setw(5) << 12; // Output: ***12 The fill character applies until another character is defined. As the previous example shows, output to fields is normally right-aligned. The other options available are left-aligned and internal, which can be set by using the manipulators left and internal. The manipulator internal left-justifies the sign and rightjustifies the number within a field. Example: cout.width(6); cout.fill('0'); cout << internal << -123; // Output: -00123 68 ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS ■ OUTPUT OF CHARACTERS, STRINGS, AND BOOLEAN VALUES Sample program // Enters a character and outputs its // octal, decimal, and hexadecimal code. #include #include // Declaration of cin, cout // For manipulators being called // with arguments. #include using namespace std; int main() { int number = ' '; cout << "The white space code is as follows: " << number << endl; char ch; string prompt = "\nPlease enter a character followed by " " : "; cout << prompt; cin >> ch; number = ch; // Read a character cout << "The character " << ch << " has code" << number << endl; cout << << << << << uppercase // For hex-digits " octal decimal hexadecimal\n " oct << setw(8) << number dec << setw(8) << number hex << setw(8) << number << endl; return 0; } OUTPUT OF CHARACTERS, STRINGS, AND BOOLEAN VALUES ■ 69 䊐 Outputting Characters and Character Codes The >> operator interprets a number of type char as the character code and outputs the corresponding character: Example: char ch = '0'; cout << ch << ' ' << 'A'; // Outputs three characters: 0 A It is also possible to output the character code for a character. In this case the character code is stored in an int variable and the variable is then output. Example: int code = '0'; cout << code; // Output: 48 The '0' character is represented by ASCII Code 48. The program on the opposite page contains further examples. 䊐 Outputting Strings You can use the >> operator both to output string literals, such as "Hello", and string variables, as was illustrated in previous examples. As in the case of other types, strings can be positioned within output fields. Example: string s("spring flowers "); cout << left // Left-aligned << setfill('?') // Fill character ? << setw(20) << s ; // Field width 20 This example outputs the string "spring flowers??????". The manipulator right can be used to right-justify the output within the field. 䊐 Outputting Boolean Values By default the << operator outputs boolean values as integers, with the value 0 representing false and 1 true. If you need to output the strings true or false instead, the flag ios::boolalpha must be set. To do so, use either the setf() method or the manipulator boolalpha. Example: bool ok = true; cout << ok << endl << boolalpha << ok << endl; // 1 // true You can revert this setting using the noboolalpha manipulator. 70 ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS ■ FORMATTED INPUT Sample program // Inputs an article label and a price #include #include #include using namespace std; // Declarations of cin, cout,... // Manipulator setw() int main() { string label; double price; cout << "\nPlease enter an article label: "; // Input the label (15 characters maximum): cin >> setw(16); // or: cin.width(16); cin >> label; cin.sync(); cin.clear(); // Clears the buffer and resets // any error flags that may be set cout << "\nEnter the price of the article: "; cin >> price; // Input the price // Controlling output: cout << fixed << setprecision(2) << "\nArticle:" << "\n Label: " << label << "\n Price: " << price << endl; // ... The program to be continued return 0; } ✓ NOTE The input buffer is cleared and error flags are reset by calling the sync() and clear() methods. This ensures that the program will wait for new input for the price, even if more than 15 characters have been entered for the label. FORMATTED INPUT ■ 71 The >> operator, which belongs to the istream class, takes the current number base and field width flags into account when reading input: ■ ■ the number base specifies whether an integer will be read as a decimal, octal, or hexadecimal the field width specifies the maximum number of characters to be read for a string. When reading from standard input, cin is buffered by lines. Keyboard input is thus not read until confirmed by pressing the key. This allows the user to press the backspace key and correct any input errors, provided the return key has not been pressed. Input is displayed on screen by default. 䊐 Input Fields The >> operator will normally read the next input field, convert the input by reference to the type of the supplied variable, and write the result to the variable. Any white space characters (such as blanks, tabs, and new lines) are ignored by default. Example: char ch; cin >> ch; // Enter a character When the following keys are pressed the character 'X' is stored in the variable ch. An input field is terminated by the first white space character or by the first character that cannot be processed. Example: int i; cin >> i; Typing 123FF stores the decimal value 123 in the variable i. However, the characters that follow, FF and the newline character, remain in the input buffer and will be read first during the next read operation. When reading strings, only one word is read since the first white space character will begin a new input field. Example: string city; cin >> city; // To read just one word! If Lao Kai is input, only Lao will be written to the city string. The number of characters to be read can also be limited by specifying the field width. For a given field width of n, a maximum of n–1 characters will be read, as one byte is required for the null character. Any initial white space will be ignored. The program on the opposite page illustrates this point and also shows how to clear the input buffer. 72 ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS ■ FORMATTED INPUT OF NUMBERS Sample program // Enter hexadecimal digits and a floating-point number // #include #include using namespace std; int main() { int number = 0; cout << "\nEnter a hexadecimal number: " << endl; cin >> hex >> number; // Input hex-number cout << "Your decimal input: " << number << endl; // If an invalid input occurred: cin.sync(); // Clears the buffer cin.clear(); // Reset error flags double x1 = 0.0, x2 = 0.0; cout << "\nNow enter two floating-point values: " << endl; cout cin cout cin << >> << >> "1. number: "; x1; "2. number: "; x2; // Read first number // Read second number cout << fixed << setprecision(2) << "\nThe sum of both numbers: << setw(10) << x1 + x2 << endl; " cout << "\nThe product of both numbers: " << setw(10) << x1 * x2 << endl; return 0; } FORMATTED INPUT OF NUMBERS ■ 73 䊐 Inputting Integers You can use the hex, oct, and dec manipulators to stipulate that any character sequence input is to processed as a hexadecimal, octal, or decimal number. Example: int n; cin >> oct >> n; An input value of 10 will be interpreted as an octal, which corresponds to a decimal value of 8. Example: cin >> hex >> n; Here, any input will be interpreted as a hexadecimal, enabling input such as f0a or -F7. 䊐 Inputting Floating-Point Numbers The >> operator interprets any input as a decimal floating-point number if the variable is a floating-point type, i.e. float, double, or long double. The floating-point number can be entered in fixed point or exponential notation. Example: double x; cin >> x; The character input is converted to a double value in this case. Input, such as 123, -22.0, or 3e10 is valid. 䊐 Input Errors But what happens if the input does not match the type of variable defined? Example: int i, j; cin >> i >> j; Given input of 1A5 the digit 1 will be stored in the variable i. The next input field begins with A. But since a decimal input type is required, the input sequence will not be processed beyond the letter A. If, as in our example, no type conversion is performed, the variable is not written to and an internal error flag is raised. It normally makes more sense to read numerical values individually, and clear the input buffer and any error flags that may have been set after each entry. Chapter 6, “Control Flow,” and Chapter 28, “Exception Handling,” show how a program can react to input errors. 74 ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS ■ UNFORMATTED INPUT/OUTPUT Sample program // // Reads a text with the operator >> and the function getline(). #include #include using namespace std; string header = " --- Demonstrates Unformatted Input ---"; int main() { string word, rest; cout << header << "\n\nPress to go on" << endl; cin.get(); // Read the new line // without saving. cout << "\nPlease enter a sentence with several words!" << "\nEnd with and ." << endl; cin >> word; getline( cin, rest, '!'); // Read the first word // and the remaining text // up to the character ! cout << "\nThe first word: " << word << "\nRemaining text: " << rest << endl; return 0; } ✓ NOTE 1. A text of more than one line can be entered. 2. The sample program requires that at least one word and a following white space are entered. UNFORMATTED INPUT/OUTPUT ■ 75 Unformatted input and output does not use fields, and any formatting flags that have been set are ignored. The bytes read from a stream are passed to the program “as is.” More specifically, you should be aware that any white space characters preceding the input will be processed. 䊐 Reading and Writing Characters You can use the methods get() and put() to read or write single characters. The get() method reads the next character from a stream and stores it in the given char variable. Example: char ch; cin.get(ch); If the character is a white space character, such as a newline, it will still be stored in the ch variable. To prevent this from happening you can use cin >> ch; to read the first non-white space character. The get() method can also be called without any arguments. In this case, get() returns the character code of type int. Example: int c = cin.get(); The put() method can be used for unformatted output of a character. The character to be output is passed to put() as an argument. Example: cout.put('A'); This statement is equivalent to cout << 'A'; , where the field width is undefined or has been set to 1. 䊐 Reading a Line The >> operator can only be used to read one word into a string. If you need to read a whole line of text, you can use the global function getline(), which was introduced earlier in this chapter. Example: getline(cin, text); This statement reads characters from cin and stores them in the string variable text until a new line character occurs. However, you can specify a different delimiting character by passing the character to the getline() function as a third argument. Example: getline(cin, s, '.'); The delimiting character is read, but not stored in the string. Any characters subsequent to the first period will remain in the input buffer of the stream. ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS ■ exercise s 76 EXERCISES Screen output for exercise 3 Article Number ....... Number of Pieces Price per piece ...... ...... Dollar Program listing for exercise 5 // A program with resistant mistakes #include using namespace std; int main() { char ch; string word; cin >> "Let's go! Press the return key: " >> ch; cout << "Enter a word containing three characters at most: "; cin >> setprecision(3) >> word; cout >> "Your input: " >> ch >> endl; return 0; } EXERCISES ■ 77 Exercise 1 What output is generated by the program on the page entitled “Formatted output of floating-point numbers” in this chapter? Exercise 2 Formulate statements to perform the following: a. Left-justify the number 0.123456 in an output field with a width of 15. b. Output the number 23.987 as a fixed point number rounded to two decimal places, right-justifying the output in a field with a width of 12. c. Output the number –123.456 as an exponential and with four decimal spaces. How useful is a field width of 10? Exercise 3 Write a C++ program that reads an article number, a quantity, and a unit price from the keyboard and outputs the data on screen as displayed on the opposite page. ✓ Exercise 4 Write a C++ program that reads any given character code (a positive integer) from the keyboard and displays the corresponding character and the character code as a decimal, an octal, and a hexadecimal on screen. TIP The variable type defines whether a character or a number is to be read or output. Why do you think the character P is output when the number 336 is entered? Exercise 5 Correct the mistakes in the program on the opposite page. ■ CHAPTER 4 ■ solutions 78 INPUT AND OUTPUT WITH STREAMS SOLUTIONS Exercise 1 Output of a sample program formatting floating-point numbers: By default: showpoint: fixed: scientific: 12 12. 12.00 1.20e+001 Exercise 2 #include #include using namespace std; // For setw() and setprecision() int main() { double x1 = 0.123456, x2 = 23.987, x3 = -123.456; // a) cout << left << setw(15) << x1 << endl; // b) cout << fixed << setprecision(2) << right << setw(12) << x2 << endl; // c) cout << scientific << setprecision(4) << x3 << endl; // Output: -1.2346e+002 // A field width of 12 or more would be convenient! return 0; } Exercise 3 // Input and formatted output of article characteristics. #include #include using namespace std; int main() { long number = 0; int count = 0; double price = 0.0; // Input: cout << "\nPlease enter article characteristics.\n"; cout << "Article number: "; cin >> number; SOLUTIONS cout << "Number of pieces: cin >> count; cout << "Price per piece: cin >> price; "; "; // Output: cout << "\n\tArticle Number cout << << << << << "\n\t" setw(8) setw(16) fixed setw(16) << << << << Quantity Price per piece "; number count setprecision(2) price << " Dollar" << endl; return 0; } Exercise 4 #include #include using namespace std; // Manipulator setw() int main() { unsigned char c = 0; unsigned int code = 0; cout << "\nPlease enter a decimal character code: "; cin >> code; c = code; // Save for output cout << "\nThe corresponding character: " << c << endl; code = c; cout << << << << << // Character code. Is only // necessary, if input is > 255. "\nCharacter codes" "\n decimal: " << setw(3) << dec << code "\n octal: " << setw(3) << oct << code "\n hexadecimal: " << setw(3) << hex << code endl; return 0; } ■ 79 80 ■ CHAPTER 4 INPUT AND OUTPUT WITH STREAMS When entering 336, the value 80 is stored in the low byte of variable code (336 = 256 + 80).Thus after the assignment, the variable c contains the value 80, representing the character P. Exercise 5 The corrected program: // Corrections are commented. // #include #include // Manipulator setw() #include // Class string using namespace std; int main() { string word; // To read a word. // char ch; is not needed. // cout << ...instead of cout << "Let's go! Press the return key: "; cin.get(); cin >> . // Input newline character cout << " Enter a word " // " "containing three characters at the most: ";// " cin >> setw(3) >> word; cout << "Your input: " << word << endl; return 0; } // setw(3) instead of // setprecision(3) // << // instead of >> ch chapter 5 Operators for Fundamental Types In this chapter, operators needed for calculations and selections are introduced. Overloading and other operators, such as those needed for bit manipulations, are introduced in later chapters. 81 82 ■ CHAPTER 5 OPERATORS FOR FUNDAMENTAL TYPES ■ BINARY ARITHMETIC OPERATORS Binary operator and operands Operator a + b Left operand Right operand The binary arithmetic operators Operator Significance + - Addition * Multiplication / % Division Subraction Remainder Sample program #include using namespace std; int main() { double x, y; cout << "\nEnter two floating-point values: "; cin >> x >> y; cout << "The average of the two numbers is: " << (x + y)/2.0 << endl; return 0; } Sample output for the program Enter two floating-point values: 4.75 12.3456 The average of the two numbers is: 8.5478 BINARY ARITHMETIC OPERATORS ■ 83 If a program is to be able to process the data input it receives, you must define the operations to be performed for that data. The operations being executed will depend on the type of data — you could add, multiply, or compare numbers, for example. However, it would make no sense at all to multiply strings. The following sections introduce you to the most important operators that can be used for arithmetic types. A distinction is made between unary and binary operators. A unary operator has only one operand, whereas a binary operator has two. 䊐 Binary Arithmetic Operators Arithmetic operators are used to perform calculations. The opposite page shows an overview. You should be aware of the following: ■ ■ Divisions performed with integral operands will produce integral results; for example, 7/2 computes to 3. If at least one of the operands is a floating-point number, the result will also be a floating-point number; e.g., the division 7.0/2 produces an exact result of 3.5. Remainder division is only applicable to integral operands and returns the remainder of an integral division. For example, 7%2 computes to 1. 䊐 Expressions In its simplest form an expression consists of only one constant, one variable, or one function call. Expressions can be used as the operands of operators to form more complex expressions. An expression will generally tend to be a combination of operators and operands. Each expression that is not a void type returns a value. In the case of arithmetic expressions, the operands define the type of the expression. Examples: int a(4); double a * 512 // 1.0 + sin(x) // x – 3 // // x(7.9); Type int Type double Type double, since one operand is of type double An expression can be used as an operand in another expression. Example: 2 + 7 * 3 // Adds 2 and 21 Normal mathematical rules (multiplication before addition) apply when evaluating an expression, i.e. the *, /, % operators have higher precedence than + and -. In our example, 7*3 is first calculated before adding 2. However, you can use parentheses to apply a different precedence order. Example: (2 + 7) * 3 // Multiplies 9 by 3. 84 ■ CHAPTER 5 OPERATORS FOR FUNDAMENTAL TYPES ■ UNARY ARITHMETIC OPERATORS The unary arithmetic operators Operator Significance - + Unary sign operators ++ Increment operator -- Decrement operator Precedence of arithmetic operators Precedence Grouping Operator High -- ++ + -- (prefix) - (sign) * / + - Low (postfix) ++ left to right right to left left to right % left to right (addition) (subtraction) Effects of prefix and postfix notation #include using namespace std; int main() { int i(2), j(8); cout cout cout cout << << << << i++ i j---j return 0; } << << << << endl; endl; endl; endl; // // // // Output: Output: Output: Output: 2 3 8 6 UNARY ARITHMETIC OPERATORS ■ 85 There are four unary arithmetic operators: the sign operators + and -, the increment operator ++, and the decrement operator --. 䊐 Sign Operators The sign operator – returns the value of the operand but inverts the sign. Example: int n = –5; cout << -n; // Output: 5 The sign operator + performs no useful operation, simply returning the value of its operand. 䊐 Increment / Decrement Operators The increment operator ++ modifies the operand by adding 1 to its value and cannot be used with constants for this reason. Given that i is a variable, both i++ (postfix notation) and ++i (prefix notation) raise the value of i by 1. In both cases the operation i = i + 1 is performed. However, prefix ++ and postfix ++ are two different operators. The difference becomes apparent when you look at the value of the expression; ++i means that the value of i has already been incremented by 1, whereas the expression i++ retains the original value of i. This is an important difference if ++i or i++ forms part of a more complex expression: ++i i++ i is incremented first and the new value of i is then applied, the original value of i is applied before i is incremented. The decrement operator -- modifies the operand by reducing the value of the operand by 1. As the sample program opposite shows, prefix or postfix notation can be used with --. 䊐 Precedence How is an expression with multiple operators evaluated? Example: float val(5.0); cout << val++ – 7.0/2.0; Operator precedence determines the order of evaluation, i.e. how operators and operands are grouped. As you can see from the table opposite, ++ has the highest precedence and / has a higher precedence than -. The example is evaluated as follows: (val++) – (7.0/2.0). The result is 1.5, as val is incremented later. If two operators have equal precedence, the expression will be evaluated as shown in column three of the table. Example: 3 * 5 % 2 is equivalent to (3 * 5) % 2 86 ■ CHAPTER 5 OPERATORS FOR FUNDAMENTAL TYPES ■ ASSIGNMENTS Sample program // Demonstration of compound assignments #include #include using namespace std; int main() { float x, y; cout << "\n Please enter a starting value: cin >> x; "; cout << "\n Please enter the increment value: "; cin >> y; x += y; cout << "\n And now multiplication! "; cout << "\n Please enter a factor: "; cin >> y; x *= y; cout << "\n Finally division."; cout << "\n Please supply a divisor: "; cin >> y; x /= y; cout << "\n And this is " << "your current lucky number: " // without digits after // the decimal point: << fixed << setprecision(0) << x << endl; return 0; } ASSIGNMENTS ■ 87 䊐 Simple Assignments A simple assignment uses the assignment operator = to assign the value of a variable to an expression. In expressions of this type the variable must be placed on the left and the assigned value on the right of the assignment operator. Examples: z = 7.5; y = z; x = 2.0 + 4.2 * z; The assignment operator has low precedence. In the case of the last example, the right side of the expression is first evaluated and the result is assigned to the variable on the left. Each assignment is an expression in its own right, and its value is the value assigned. Example: sin(x = 2.5); In this assignment the number 2.5 is assigned to x and then passed to the function as an argument. Multiple assignments, which are always evaluated from right to left, are also possible. Example: i = j = 9; In this case the value 9 is first assigned to j and then to i. 䊐 Compound Assignments In addition to simple assignment operators there are also compound assignment operators that simultaneously perform an arithmetic operation and an assignment, for example. Examples. i += 3; i *= j + 2; is equivalent to i = i + 3; is equivalent to i = i * (j+2); The second example shows that compound assignments are implicitly placed in parentheses, as is demonstrated by the fact that the precedence of the compound assignment is just as low as that of the simple assignment. Compound assignment operators can be composed from any binary arithmetic operator (and, as we will see later, with bit operators). The following compound operators are thus available: +=, -=, *=, /=, and %=. You can modify a variable when evaluating a complex expression by means of an assignment or the ++, -- operators. This technique is referred to as a side effect. Avoid use of side effects if possible, as they often lead to errors and can impair the readability of your programs. 88 ■ CHAPTER 5 OPERATORS FOR FUNDAMENTAL TYPES ■ RELATIONAL OPERATORS The relational operators Operator Significance less than < less than or equal to <= greater than > >= geater than or equal to == equal != unequal Precedence of relational operators Precedence Operator High arithmetic operators < Low <= > == != >= assignment operators Examples for comparisons: Comparison Result 5 >= 6 false 1.7 < 1.8 true 4 + 2 == 5 false 2 * 4 != 7 true RELATIONAL OPERATORS ■ 89 䊐 The Result of Comparisons Each comparison in C++ is a bool type expression with a value of true or false, where true means that the comparison is correct and false means that the comparison is incorrect. Example: length == circuit // false or true If the variables length and circuit contain the same number, the comparison is true and the value of the relational expression is true. But if the expressions contain different values, the value of the expression will be false. When individual characters are compared, the character codes are compared. The result therefore depends on the character set you are using. The following expression results in the value true when ASCII code is used. Example: 'A' < 'a' // true, since 65 < 97 䊐 Precedence of Relational Operators Relational operators have lower precedence than arithmetic operators but higher precedence than assignment operators. Example: bool flag = index < max – 1; In our example, max – 1 is evaluated first, then the result is compared to index, and the value of the relational expression (false or true) is assigned to the flag variable. Similarly, in the following Example: int result; result = length + 1 == limit; length + 1 is evaluated first, then the result is compared to limit, and the value of the relational expression is assigned to the result variable. Since result is an int type, a numerical value is assigned instead of false or true, i.e. 0 for false and 1 for true. It is quite common to assign a value before performing a comparison, and parentheses must be used in this case. Example: (result = length + 1) == limit Our example stores the result of length + 1 in the variable result and then compares this expression with limit. ✓ NOTE You cannot use the assignment operator = to compare two expressions. The compiler will not generate an error message if the value on the left is a variable. This mistake has caused headaches for lots of beginners when troubleshooting their programs. 90 ■ CHAPTER 5 OPERATORS FOR FUNDAMENTAL TYPES ■ LOGICAL OPERATORS “Truth” table for logical operators A B A && B A || B true true true true true false false true false true false true false false false false A !A true false false true Examples for logical expressions ✓ x y Logical Expression Result 1 -1 x <= y || y >=0 false 0 0 x > -2 && y == 0 true -1 0 x && !y true 0 1 !(x+1) || y - 1 > 0 false NOTE A numeric value, such as x or x+1, is interpreted as “false” if its value is 0. Any value other than 0 is interpreted as “true.” LOGICAL OPERATORS ■ 91 The logical operators comprise the boolean operators && (AND), || (OR), and ! (NOT). They can be used to create compound conditions and perform conditional execution of a program depending on multiple conditions. A logical expression results in a value false or true, depending on whether the logical expression is correct or incorrect, just like a relational expression. 䊐 Operands and Order of Evaluation The operands for boolean type operators are of the bool type. However, operands of any type that can be converted to bool can also be used, including any arithmetic types. In this case the operand is interpreted as false, or converted to false, if it has a value of 0. Any other value than 0 is interpreted as true. The OR operator || will return true only if at least one operand is true, so the value of the expression Example: (length < 0.2) || (length > 9.8) is true if length is less than 0.2 or greater than 9.8. The AND operator && will return true only if both operands are true, so the logical expression Example: (index < max) && (cin >> number) is true, provided index is less than max and a number is successfully input. If the condition index < max is not met, the program will not attempt to read a number! One important feature of the logical operators && and || is the fact that there is a fixed order of evaluation. The left operand is evaluated first and if a result has already been ascertained, the right operand will not be evaluated! The NOT operator ! will return true only if its operand is false. If the variable flag contains the value false (or the value 0), !flag returns the boolean value true. 䊐 Precedence of Boolean Operators The && operator has higher precedence than ||. The precedence of both these operators is higher than the precedence of an assignment operator, but lower than the precedence of all previously used operators. This is why it was permissible to omit the parentheses in the examples earlier on in this chapter. The ! operator is a unary operator and thus has higher precedence. Refer to the table of precedence in the Appendix for further details. ■ CHAPTER 5 OPERATORS FOR FUNDAMENTAL TYPES ■ exercise s 92 EXERCISES Program listing for exercise 4 // Evaluating operands in logical expressions. #include using namespace std; int main() { cout << boolalpha; // Outputs boolean values // as true or false bool res = false; int y = res = 7 cout << << cout << int 5; || (y = 0); "Result of (7 || (y = 0)): " << res endl; "Value of y: " << y << endl; a, b, c; a = b = c = 0; res = ++a || ++b cout << '\n' << " res = << ", a = << ", b = << ", c = a = b = c = 0; res = ++a && ++b cout << " res = << ", a = << ", b = << ", c = return 0; } && ++c; " " " " << << << << res a b c << endl; || ++c; " << res " << a " << b " << c << endl; EXERCISES ■ Exercise 1 What values do the following arithmetic expressions have? a. 3/10 d. 3 + 4 % 5 b. 11%4 e. 3 * 7 % 4 c. 15/2.0 f. 7 % 4 * 3 Exercise 2 a. How are operands and operators in the following expression associated? x = –4 * i++ – 6 % 4; Insert parentheses to form equivalent expressions. b. What value will be assigned in part a to the variable x if the variable i has a value of –2? Exercise 3 The int variable x contains the number 7. Calculate the value of the following logical expressions: a. x < 10 && x >= –1 b. !x && x >= 3 c. x++ == 8 || x == 7 Exercise 4 What screen output does the program on the opposite page generate? 93 ■ CHAPTER 5 ■ solutions 94 OPERATORS FOR FUNDAMENTAL TYPES SOLUTIONS Exercise 1 a. 0 d. 7 b. 3 e. 1 c. 7.5 f. 9 Exercise 2 a. x = ( ((–4) * (i++)) – (6 % 4) ) b. The value 6 will be assigned to the variable x. Exercise 3 a. true b. false c. false Exercise 4 Result of (7 || (y = 0)): true Value of y: 5 res = true, res = true, a = 1, a = 1, b = 0, b = 1, c = 0 c = 0 chapter 6 Control Flow This chapter introduces the statements needed to control the flow of a program.These are ■ loops with while, do-while, and for ■ selections with if-else, switch, and the conditional operator ■ jumps with goto, continue, and break. 95 96 ■ CHAPTER 6 CONTROL FLOW ■ THE while STATEMENT Structogram for while As long as the expression is true statement Sample program // average.cpp // Computing the average of numbers #include using namespace std; int main() { int x, count = 0; float sum = 0.0; cout << "Please enter some integers:\n" "(Break with any letter)" << endl; while( cin >> x ) { sum += x; ++count; } cout << "The average of the numbers: " << sum / count << endl; return 0; } Sample output from the above program Please enter some integers: (Break with any letter) 9 10 12q The average of the numbers: 10.3333 THE WHILE STATEMENT ■ 97 Loops are used to perform a set of instructions repeatedly. The set of instructions to be iterated is called the loop body. C++ offers three language elements to formulate iteration statements: while, do-while, and for. The number of times a loop is repeated is defined by a controlling expression. In the case of while and for statements this expression is verified before the loop body is executed, whereas a do-while loop is performed once before testing. The while statement takes the following format: Syntax: while( expression ) statement // loop body When entering the loop, the controlling expression is verified, i.e. the expression is evaluated. If this value is true, the loop body is then executed before the controlling expression is evaluated once more. If the controlling expression is false, i.e. expression evaluates to false, the program goes on to execute the statement following the while loop. It is common practice to place the loop body in a new line of the source code and to indent the statement to improve the readability of the program. Example: int count = 0; while( count < 10) cout << ++count << endl; As this example illustrates, the controlling expression is normally a boolean expression. However, the controlling expression might be any expression that can be converted to the bool type including any arithmetic expressions. As we already learned from the section on boolean operators, the value 0 converts to false and all other values convert to true. 䊐 Building Blocks If you need to repeat more than one statement in a program loop, you must place the statements in a block marked by parentheses { }. A block is syntactically equivalent to a statement, so you can use a block wherever the syntax requires a statement. The program on the opposite page calculates the average of a sequence of integers input via the keyboard. Since the loops contains two statements, the statements must be placed in a block. The controlling expression cin >> x is true provided the user inputs an integer. The result of converting the expression cin >> x to a bool type will be true for any valid input and false in any other case. Invalid input, if the user types a letter instead of an integer, for example, terminates the loop and executes the next statement. 98 ■ CHAPTER 6 CONTROL FLOW ■ THE for STATEMENT Structogram for for expression1 As long as expression2 is true statement expression3 Sample program // Euro1.cpp #include #include using namespace std; int main() { double rate = 1.15; // Exchange rate: // one Euro to one Dollar cout << fixed << setprecision(2); cout << "\tEuro \tDollar\n"; for( int euro = 1; euro <= 5; ++euro) cout << "\t " << euro << "\t " << euro*rate << endl; return 0; } Screen output Euro 1 2 3 4 5 Dollar 0.95 1.90 2.85 3.80 4.75 THE FOR STATEMENT ■ 99 䊐 Initializing and Reinitializing A typical loop uses a counter that is initialized, tested by the controlling expression and reinitialized at the end of the loop. Example: int count = 1; // Initialization while( count <= 10) // Controlling { // expression cout << count << ". loop" << endl; ++count; // Reinitialization } In the case of a for statement the elements that control the loop can be found in the loop header. The above example can also be expressed as a for loop: Example: int count; for( count = 1; count <= 10; ++count) cout << count << ". loop" << endl; Any expression can be used to initialize and reinitialize the loop. Thus, a for loop has the following form: Syntax: for( expression1; expression2; expression3 ) statement expression1 is executed first and only once to initialize the loop. expression2 is the controlling expression, which is always evaluated prior to executing the loop body: ■ ■ if expression2 is false, the loop is terminated if expression2 is true, the loop body is executed. Subsequently, the loop is reinitialized by executing expression3 and expression2 is re-tested. You can also define the loop counter in expression1. Doing so means that the counter can be used within the loop, but not after leaving the loop. Example: for( int i = 0; i < 10; cout << i++ ) ; As this example illustrates, the loop body can be an empty statement. This is always the case if the loop header contains all necessary statements. However, to improve readability, even the empty statement should occupy a line of its own. 100 ■ CHAPTER 6 CONTROL FLOW ■ THE for STATEMENT (CONTINUED) Sample program // EuroDoll.cpp // Outputs a table of exchange: Euro and US-$ #include #include using namespace std; int main() { long euro, maxEuro; double rate; // Amount in Euros // Exchange rate Euro <-> $ cout << "\n* * * TABLE OF EXCHANGE " << " Euro – US-$ * * *\n\n"; cout << "\nPlease give the rate of exchange: " " one Euro in US-$: "; cin >> rate; cout << "\nPlease enter the maximum euro: "; cin >> maxEuro; // --- Outputs the table --// Titles of columns: cout << '\n' << setw(12) << "Euro" << setw(20) << "US-$" << "\t\tRate: " << rate << endl; // Formatting US-$: cout << fixed << setprecision(2) << endl; long lower, upper, step; // Lower and upper limit // Step width // The outer loop determines the actual // lower limit and the step width: for( lower=1, step=1; lower <= maxEuro; step*= 10, lower = 2*step) // The inner loop outputs a "block": for( euro = lower, upper = step*10; euro <= upper && euro <= maxEuro; euro+=step) cout << setw(12) << euro << setw(20) << euro*rate << endl; return 0; } THE FOR STATEMENT (CONTINUED) ■ 101 Any of the three expressions in a for statement can be omitted, however, you must type at least two semicolons. The shortest loop header is therefore: Example: for(;;) This statement causes an infinite loop, since the controlling expression is assumed to be true if expression2 is missing. In the following Example: for( ; expression; ) the loop header is equivalent to while(expression). The loop body is executed as long as the test expression is true. 䊐 The Comma Operator You can use the comma operator to include several expressions where a single expression is syntactically correct. For example, several variables can be initialized in the loop header of a for statement. The following syntax applies for the comma operator Syntax: expression1, expression2 [, expression3 ...] The expressions separated by commas are evaluated from left to right. Example: int x, i, limit; for( i=0, limit=8; i < limit; i += 2) x = i * i, cout << setw(10) << x; The comma operator separates the assignments for the variables i and limit and is then used to calculate and output the value of x in a single statement. The comma operator has the lowest precedence of all operators — even lower than the assignment operators. This means you can leave out the parentheses in the above example. Like any other C++ expression, an expression containing the comma operator has a value and belongs to a certain type. The type and value are defined by the last expression in a statement separated by commas. Example: x = (a = 3, b = 5, a * b); In this example the statements in brackets are executed before the value of the product of a * b is assigned to x. 102 ■ CHAPTER 6 CONTROL FLOW ■ THE do-while STATEMENT Structogram for do-while statement As long as the expression is true Sample program // tone.cpp #include using namespace std; const long delay = 10000000L; int main() { int tic; cout << "\nHow often should the tone be output? "; cin >> tic; do { for( long i = 0; i < delay; ++i ) ; cout << "Now the tone!\a" << endl; } while( --tic > 0 ); cout << "End of the acoustic interlude!\n"; return 0; } THE DO-WHILE STATEMENT ■ 103 In contrast to while and for loops, which are controlled by their headers, the dowhile loop is controlled by its footer, i.e. the controlling expression is evaluated after executing the first loop. This results in the loop body being performed at least once. Syntax: do statement while( expression); When a do-while loop is executed, the loop body is processed first. Only then is the controlling expression evaluated. The loop body is iterated again if the result is true, otherwise the loop is terminated. ✓ NOTE The do-while loop must be followed by a semicolon. 䊐 Nesting Loops Loops can be nested, that is, the loop body can also contain a loop. The ANSI standard stipulates a maximum depth of 256 nested loops. The program on the opposite page outputs a number of tones with the number being defined by user input. The program contains two loops — one of which is nested in the other. Each time the outer do-while loop is repeated a short break occurs. The break is caused by the inner for loop where the variable i is incremented from 0 to the value of delay. Text and a tone are subsequently output. The tone is generated by outputting the control character BELL (ASCII code 7), which is represented by the escape sequence \a. Since a do-while statement is used, the program outputs a tone even if the user types 0 or a negative number. 104 ■ CHAPTER 6 CONTROL FLOW ■ SELECTIONS WITH if-else Structogram for the if-else statement if (expression) true statement1 false statement2 Sample program // if_else.cpp // Demonstrates the use of if-else statements #include using namespace std; int main() { float x, y, min; cout << "Enter two different numbers:\n"; if( cin >> x && cin >> y) // If both inputs are { // valid, compute if( x < y ) // the lesser. min = x; else min = y; cout << "\nThe smaller number is: " << min << endl; } else cout << "\nInvalid Input!" << endl; return 0; } Sample output for this program Enter two different numbers: 7.5 5.7 The smaller number is: 5.7 SELECTIONS WITH IF-ELSE ■ 105 The if-else statement can be used to choose between two conditional statements. Syntax: if( expression ) statement1 [ else statement2 ] When the program is run, expression is first evaluated and the program control branches accordingly. If the result is true, statement1 is executed and statement2 is executed in all other cases, provided an else branch exists. If there is no else and expression is false, the control jumps to the statement following the if statement. 䊐 Nested if-else Statements As the program opposite illustrates, multiple if-else statements can be nested. But not every if statement has an else branch. To solve the resulting problem, an else branch is always associated with the nearest preceding if statement that does not have an else branch. Example: if( n > 0 ) if( n%2 == 1 ) cout << " Positive odd number "; else cout << "Positive even number"; In this example, the else branch belongs to the second if, as is indicated by the fact that the statement has been indented. However, you can use a code block to redefine the association of an else branch. Example: if( n > 0 ) { if( n%2 == 1 ) cout << " Positive odd number \n"; } else cout << " Negative number or zero\n"; 䊐 Defining Variables in if Statements You can define and initialize a variable within an if statement. The expression is true if converting the variable’s value to a bool type yields true. In this case the variable is available within the if statement. Example: if( int x = func() ) { . . . } // Here to work with x. The return value of the function, func(), is used to initialize the variable x. If this value is not 0, the statements in the next block are executed. The variable x no longer exists after leaving the if statement. 106 ■ CHAPTER 6 CONTROL FLOW ■ Else-if CHAINS Structogram for an else-if chain if(expression) true false statement1 if(expression) true false statement2 if(expression) . . . true false statement(n) statement(n+1) Sample program // speed.cpp // Output the fine for driving too fast. #include using namespace std; int main() { float limit, speed, toofast; cout << "\nSpeed limit: "; cin >> limit; cout << "\nSpeed: "; cin >> speed; if( (toofast = speed – limit ) < 10) cout << "You were lucky!" << endl; else if( toofast < 20) cout << "Fine payable: 40,-. Dollars" << endl; else if( toofast < 30) cout << "Fine payable: 80,-. Dollars" << endl; else cout << "Hand over your driver's license!" << endl; return 0; } ELSE-IF CHAINS ■ 107 䊐 Layout and Program Flow You can use an else-if chain to selectively execute one of several options. An elseif chain implies a series of embedded if-else statements whose layout is normally as follows: if ( expression1 ) statement1 else if( expression2 ) statement2 . . . else if( expression(n) ) statement(n) [ else statement(n+1)] When the else-if chain is executed, expression1, expression2, ... are evaluated in the order in which they occur. If one of the expressions proves to be true, the corresponding statement is executed and this terminates the else-if chain. If none of the expressions are true, the else branch of the last if statement is executed. If this else branch is omitted, the program executes the statement following the else-if chain. 䊐 The Sample Program The program opposite uses an else-if chain to evaluate the penalty for driving too fast and outputs the fine on screen. The speed limit and the actual speed are read from the keyboard. If the user types 60 as the speed limit and 97.5 as the actual speed, the first three expressions are not true, and the last else branch is executed. This outputs the message "Hand over your driver's license!" on a new line. 108 ■ CHAPTER 6 CONTROL FLOW ■ CONDITIONAL EXPRESSIONS Structogram for a conditional expression expression true expression1 false expression2 Sample program // greater.cpp #include using namespace std; int main() { float x, y; cout << "Type two different numbers:\n"; if( !(cin >> x && cin >> y) ) // If the input was { // invalid. cout << "\nInvalid input!" << endl; } else { cout << "\nThe greater value is: " << (x > y ? x : y) << endl; } return 0; } Sample output for this program Type two different numbers: 173.2 216.7 The greater value is: 216.7 CONDITIONAL EXPRESSIONS ■ 109 䊐 Conditional Operator The conditional operator ?: is used to form an expression that produces either of two values, depending on the value of some condition. Because the value produced by such an expression depends on the value of a condition, it is called conditional expression. In contrast to the if-else statement the selection mechanism is based on expressions: one of two possible expressions is selected. Thus, a conditional expression is often a concise alternative to an if-else statement. Syntax: expression ? expression1 : expression2 expression is evaluated first. If the result is true, expression1 is evaluated; if not expression2 is executed. The value of the conditional expression is therefore either the value of expression1 or expression2. Example: z = (a >= 0) ? a : -a; This statement assigns the absolute value of a to the variable z. If a has a positive value of 12, the number 12 is assigned to z. But if a has a negative value, for example –8, the number 8 is assigned to z. Since this sample program stores the value of the conditional expression in the variable z, the statement is equivalent to if( a > 0 ) z = a; else z = -a; 䊐 Precedence The conditional operator is the only C++ operator with three operands. Its precedence is higher than that of the comma and assignment operators but lower than all other operators. In other words, you could omit the brackets in the first example. You can use the result of a conditional evaluation without assigning it, as the sample program on the opposite page shows. In this example, x is printed on screen if x is greater than y, and y is printed otherwise. However, you should assign the result of complex expressions to a variable explicitly to improve the readability of your program. 110 ■ CHAPTER 6 CONTROL FLOW ■ SELECTING WITH switch Structogram for the switch statement switch(expression) case Const1: case Const2: . . . statements break statements break default: statements break Example // Evaluates given input. int command = menu(); // The function menu() reads // a command. // Evaluate command. switch( command ) { case 'a': case 'A': action1(); // Carry out 1st action. break; case 'b': case 'B': action2(); // Carry out 2nd action. break; default: cout << '\a' << flush; // Beep on } // invalid input SELECTING WITH SWITCH ■ 111 䊐 The switch Statement Just like the else-if chain, the switch statement allows you to choose between multiple alternatives. The switch statement compares the value of one expression with multiple constants. switch( expression ) { case const1: [ statement ] [ break; ] case const2: [ statement ] [ break; ] . . . [default: statement ] } First, the expression in the switch statement is evaluated. It must be an integral type. The result is then compared to the constants, const1, const2, ..., in the case labels. The constants must be different and can only be integral types (boolean values and character constants are also integral types). If the value of an expression matches one of the case constants, the program branches to the appropriate case label. The program then continues and the case labels lose their significance. You can use break to leave the switch statement unconditionally. The statement is necessary to avoid executing the statements contained in any case labels that follow. If the value of the expression does not match any of the case constants, the program branches to the default label, if available. If you do not define a default label, nothing happens. The default does not need to be the last label; it can be followed by additional case labels. 䊐 Differences between switch and else-if Chains The else-if chain is more versatile than the switch statement. Every selection can be programmed using an else-if chain. But you will frequently need to compare the value of an integral expression with a series of possible values. In this case (and only this case), you can use a switch statement. As the example opposite shows, a switch statement is more easily read than an equivalent else-if chain, so use the switch statement whenever possible. 112 ■ CHAPTER 6 CONTROL FLOW ■ JUMPS WITH break, continue, AND goto Structogram for break within a while statement As long as expression is true break; statement, which follows the loop. Sample program containing a break statement // ascii.cpp : To output an ASCII Code Table #include #include using namespace std; int main() { int ac = 32; // To begin with ASCII Code 32 // without control characters. while(true) { cout << "\nCharacter Decimal Hexadecimal\n\n"; int upper; for( upper =ac + 20; ac < upper && ac < 256; ++ac) cout << " " << (char)ac // as character << setw(10) << dec << ac << setw(10) << hex << ac << endl; if( upper >= 256) break; cout <<"\nGo on -> ,Stop -> +"; char answer; cin.get(answer); if( answer == 'q' || answer == 'Q' ) break; cin.sync(); // Clear input buffer } return 0; } ✓ NOTE The expression (char)ac yields the value ac of type char. JUMPS WITH BREAK, CONTINUE, AND GOTO ■ 113 䊐 break The break statement exits from a switch or loop immediately. You can use the break keyword to jump to the first statement that follows the switch or loop. The program on the opposite page, which outputs a group of 20 ASCII characters and their corresponding codes, uses the break keyword in two places. The first break exits from an infinite while(true) { ... } loop when a maximum value of 256 has been reached. But the user can also opt to continue or terminate the program. The second break statement is used to terminate the while loop and hence the program. 䊐 continue The continue statement can be used in loops and has the opposite effect to break, that is, the next loop is begun immediately. In the case of a while or do-while loop the program jumps to the test expression, whereas a for loop is reinitialized. Example: for( int i = 0; i < 100; i++ ) { . . . // Processes all integers. if( i % 2 == 1) continue; . . . // Process even // numbers only. } 䊐 goto and Labels C++ also offers a goto statement and labels. This allows you to jump to any given point marked by a label within a function. For example, you can exit from a deeply embedded loop construction immediately. Example: for( . . . ) for( . . . ) if (error) goto errorcheck; . . . errorcheck: . . . // Error handling A label is a name followed by a colon. Labels can precede any statement. Any program can do without goto statements. If you need to use a goto statement, do so to exit from a code block, but avoid entering code blocks by this method. ■ CHAPTER 6 ■ exercise s 114 CONTROL FLOW EXERCISES Screen output for exercise 2 ****** MULTIPLICATION TABLE ****** 1 2 3 4 5 6 7 8 9 10 1 1 2 3 . . . . . . 10 2 2 4 6 20 3 . . . . 4 . . . . 5 . . . . 10 20 30 6 7 8 9 10 . . . . . . 100 Note on exercise 4 Use the function time() to initialize the random number generator: #include #include // Prototype of time() // Prototypes of srand() // and rand() long sec; time( &sec ); // Take the number of seconds and srand( (unsigned)sec ); // use it to initialize. EXERCISES ■ 115 Exercise 1 Rewrite the EuroDoll.cpp program in this chapter to replace both the for loops with while loops. Exercise 2 Write a C++ program that outputs a complete multiplication table (as shown opposite) on screen. Exercise 3 Write a C++ program that reads an integer between 0 and 65535 from the keyboard and uses it to seed a random number generator.Then output 20 random numbers between 1 and 100 on screen. Exercise 4 Write a program for the following numerical game: The computer stores a random number between 1 and 15 and the player (user) attempts to guess it.The player has a total of three attempts.After each wrong guess, the computer tells the user if the number was too high or too low. If the third attempt is also wrong, the number is output on screen. The player wins if he or she can guess the number within three attempts. The player is allowed to repeat the game as often as he or she wants. ✓ NOTE Use the system time to seed the random number generator as shown opposite. The time() function returns the number of seconds since 1/1/1970, 0:0. The long value of the sec variable is converted to unsigned by unsigned(sec) and then passed to the srand() function. ■ CHAPTER 6 ■ solutions 116 CONTROL FLOW SOLUTIONS Exercise 1 The for loops of program EuroDoll.cpp are equivalent to the following while loops: // The outer loop sets the lower // limit and the step width used: lower=1, step=1; while( lower <= maxEuro) { // The inner loop outputs a block: euro = lower; upper = step*10; while( euro <= upper && euro <= maxEuro) { cout << setw(12) << euro << setw(20) << euro*rate << endl; euro += step; } step *= 10, lower = 2*step; } Exercise 2 // // MultTable.cpp Outputs a multiplication table. #include #include using namespace std; int main() { int factor1, factor2; cout << "\n\n << " ****** << endl; " MULTIPLICATION TABLE ******" // Outputs the first and second line: cout << "\n\n\n "; // 1. line for( factor2 = 1 ; factor2 <= 10 ; ++factor2 ) cout << setw(5) << factor2; cout << "\n " // 2. line << "-------------------------------------------" << endl; SOLUTIONS // Outputs the remaining lines of the table: for( factor1 = 1 ; factor1 <= 10 ; ++factor1 ) { cout << setw(6) << factor1 << " |"; for( factor2 = 1 ; factor2 <= 10 ; ++factor2 ) cout << setw(5) << factor1 * factor2; cout << endl; } cout << "\n\n\n"; // To shift up the table return 0; } Exercise 3 // random.cpp // Outputs 20 random numbers from 1 to 100. #include #include #include using namespace std; int main() { unsigned int // Prototypes of srand() and rand() i, seed; cout << "\nPlease type an integer between " "0 and 65535: "; cin >> seed; srand( seed); cout << "\n\n "****** // Reads an integer. // Seeds the random // number generator. " RANDOM NUMBERS ******\n\n"; for( i = 1 ; i <= 20 ; ++i) cout << setw(20) << i << ". random number = " << setw(3) << (rand() % 100 + 1) << endl; return 0; } ■ 117 118 ■ CHAPTER 6 CONTROL FLOW Exercise 4 // NumGame.cpp : A numerical game against the computer #include // Prototypes of srand() and rand() #include // Prototype of time() #include using namespace std; int main() { int number, attempt; char wb = 'r'; // Repeat or finish. long sec; time( &sec); // Get the time in seconds. srand((unsigned)sec); // Seeds the random // number generator cout << "\n\n " << " ******* A NUMERICAL GAME *******" << endl; cout << "\n\nRules of the game:" << endl; while( wb == 'r') { cout << "I have a number between 1 and 15 in mind \n" << "You have three chances to guess correctly!\n" << endl; number = (rand() % 15) + 1; bool found = false; int count = 0; while( !found && count < 3 ) { cin.sync(); // Clear input buffer cin.clear(); cout << ++count << ". attempt: "; cin >> attempt; if(attempt < number) cout << "too small!"<< endl; else if(attempt > number) cout <<"too big!"<< endl; else found = true; } if( !found) cout << "\nI won!" << " The number in question was: " << number << endl; else cout << "\nCongratulations! You won!" << endl; cout << "Repeat —> Finish —> \n"; do cin.get(wb); while( wb != 'r' && wb != 'f'); } return 0; } chapter 7 Symbolic Constants and Macros This chapter introduces you to the definition of symbolic constants and macros illustrating their significance and use. In addition, standard macros for character handling are introduced. 119 120 ■ CHAPTER 7 SYMBOLIC CONSTANTS AND MACROS ■ MACROS Sample program // sintab.cpp // Creates a sine function table #include #include #include using namespace std; #define PI 3.1415926536 #define START 0.0 // Lower limit #define END (2.0 * PI) // Upper limit #define STEP (PI / 8.0) // Step width #define HEADER (cout << \ " ***** Sine Function Table *****\n\n") int main() { HEADER; // Title // Table Head: cout << setw(16) << "x" << setw(20) << "sin(x)\n" << " -----------------------------------------" << fixed << endl; double x; for( x = START; x < END + STEP/2; x += STEP) cout << setw(20) << x << setw(16) << sin(x) << endl; cout << endl << endl; return 0; } Screen output ****** Table for the Sine Function ****** x sin(x) -------------------------------------------0.000000 0.000000 0.392699 0.382683 0.785398 0.707107 . . . . . . MACROS ■ 121 C++ has a simple mechanism for naming constants or sequences of commands, that is for defining macros. You simply use the preprocessor’s #define directive. Syntax: #define name substitutetext This defines a macro called name. The preprocessor replaces name with substitutetext throughout the subsequent program. For example, in the program on the opposite page, the name PI is replaced by the number 3.1415926536 throughout the program in the first phase of compilation. There is one exception to this general rule: substitution does not take place within strings. For example, the statement cout << "PI"; outputs only PI and not the numerical value of PI. 䊐 Symbolic Constants Macros that are replaced by constants, such as the PI macro, are also known as symbolic constants. You should note that neither an equals sign nor a semicolon is used, as these would become part of the substitute text. You can use any macros you have previously defined in subsequent #define directives. The program opposite uses the symbolic constant PI to define other constants. 䊐 More about Working with Macros Any preprocessor directive, and this includes the #define directive, must be placed in a line of its own. If the substitute text is longer than one line, you can terminate the line with a backslash \ and continue the substitute text in the next line, as is illustrated by the macro HEADER on the opposite page. The rules that apply to naming variables also apply to naming macros. However, it is standard practice to capitalize symbolic constants to distinguish them from the names of variables in a program. Using macros makes a C++ program more transparent and flexible. There are two main advantages: 1. good readability: You can name a macro to indicate the use of the macro 2. easy to modify: If you need to change the value of a constant throughout a program, you simply change the value of the symbolic constant in the #define directive. 122 ■ CHAPTER 7 SYMBOLIC CONSTANTS AND MACROS ■ MACROS WITH PARAMETERS Sample program // ball1.cpp // Simulates a bouncing ball // --------------------------------------------------#include #include using namespace std; #define DELAY 10000000L // #define CLS (cout << "\033[2J") // #define LOCATE(z,s) (cout <<"\033["<< // Position the cursor in row Output delay Clear screen z <<';'<< s <<'H') z and column s void main() { int x = 2, y = 3, dx = 1, speed = 0; string floor(79, '-'), header = "**** JUMPING BALL ****"; CLS; LOCATE(1,25); LOCATE(25,1); cout << header; cout << floor; while(true) // Let the ball "always" bounce { // Terminate by interrupt key (^C) LOCATE(y,x); cout << 'o' << endl; // Show the ball for( long wait = 0; wait < DELAY; ++wait) ; if(x == 1 || x == 79) dx = -dx; // Bounce off // a wall? if( y == 24 ) // On the floor? { speed = - speed; if( speed == 0 ) speed = -7; // Restart } speed += 1; // Acceleration = 1 LOCATE(y,x); cout << ' '; // Clear output y += speed; x += dx; // New Position } } MACROS WITH PARAMETERS ■ 123 It is possible to call macros with arguments. To do so, you must supply the appropriate parameters when defining the macro. The parameters are replaced by valid arguments at run time. Example: #define SQUARE(a) ((a) * (a)) This defines a macro called SQUARE() with a parameter a. The name of the macro must be followed immediately by a left bracket. When the macro is called, for example Example: z = SQUARE(x+1); the preprocessor inserts the substitute text with the current arguments, which will be expanded as follows, in this case z = ((x+1) * (x+1)); This example also shows that you must be careful when using brackets to indicate parameters for macros. Omitting the brackets in the previous example, SQUARE, would cause the expression to be expanded as follows z = x + 1 * x + 1. The outer brackets in the definition ensure that even when the macro is used in a complex expression, the square is calculated before the result can be used for any further calculations. 䊐 Macros for Screen Control The program opposite uses macros to change the appearance of the screen. Peripheral devices, such as the screen or printers, can be controlled by special character sequences that normally begin with the ESC character (decimal 27, octal 033) and are thus known as escape sequences. A number of ANSI standard escape sequences exists for screen control.1 See the appendix on Escape Sequences for Screen Control for an overview of the most important sequences. CLS is a macro without any parameters that uses the escape sequence \033[2J to clear the screen. LOCATE is just one example of a macro with two parameters. LOCATE uses the escape sequence \033[z;sH to place the cursor at the position of the next screen output. The values z for the line and s for the column require decimal input with z = 1, s = 1 representing the top left corner of the screen or window. The ball is “thrown in” at the coordinates x = 2, y = 3 and bounces off the “floor” and the “walls.” In direction x (horizontally) the ball has a constant speed of dx = 1 or -1. In direction y (vertically) the ball is subject to a constant acceleration of 1, expressed as speed += 1. 1These escape sequences are valid for all standard UNIX terminals. The driver ansi.sys must be loaded for DOS or a DOS box in Win95 or Win98. For Win NT and Win 2000, corresponding functions based on system calls are offered for download. 124 ■ CHAPTER 7 SYMBOLIC CONSTANTS AND MACROS ■ WORKING WITH THE #define DIRECTIVE Using macros in different source files Header file proj.h Macros Classes and other type definitions Prototypes of global functions Source file 1 Source file 2 Source file n #include "proj.h" #include "proj.h" . . . . . . . . . ... #include "proj.h" WORKING WITH THE #DEFINE DIRECTIVE ■ 125 You can place the #define directive in any line of a program as long as it is placed prior to using the macro. However, it is recommended to place all definitions at the beginning of the source file for ease of location and modification. If you need to use the same macros in different source files, it makes sense to create a header file. You can then include the header file in your source files. This method also lends itself to large-scale software projects. The programmers working on the project then have access to the same set of macro definitions and other declarations. This concept is illustrated opposite using the header file proj.h as an example. Macros with parameters can be called just like functions. You should note the following important differences, however: ■ ■ Macros: A macro definition must be visible to the compiler. The substitute text is inserted and re-compiled each time the macro is called. For this reason, a macro should contain only a few statements to avoid inflating the object file each time the macro is called. The speed of program execution will, however, improve since the program does not need to branch to sub-routines in contrast to normal function calls. This can become apparent when macros are used within loops, for example. Side effects of macros are possible if the substitute text contains multiple instances of a parameter. The statement SQUARE( ++x ) expands to ((++x) * (++x)), for example. The variable x is incremented twice and the product does not represent the square of the incremented number. Functions: Functions are compiled independently. The linker then links them into the executable file. The program branches to the function whenever it is called, and this can reduce the speed of execution in comparison to a macro. However, the executable file will be shorter as it contains only one instance of the function code. The compiler checks the argument types, and the side effects seen with macros cannot occur. Inline functions, which are introduced in the chapter on functions, are an alternative to macros. 126 ■ CHAPTER 7 ■ SYMBOLIC CONSTANTS AND MACROS CONDITIONAL INCLUSION Multiple inclusions of header files Header file basis.h #ifndef_BASIS_H_ #define_BASIS_H_ //content of basis, //ex. #define BSIZE 1000 . . . #endif Header file Header file statist.h graph.h #include #include "basis.h" #include #include "basis.h" . . . . . . Source file application.cpp #include "statist.h" #include "graph.h" int main() { . . . return 0; } CONDITIONAL INCLUSION ■ 127 䊐 Redefining Macros A macro cannot simply be redefined. A macro definition is valid until it is removed by using an #undef directive. However, you do not need to supply the parameter list of a macro with parameters. Example: #define MIN(a,b) . . . #undef MIN ((a)<(b)? (a) : (b)) // Here MIN can be called The macro MIN cannot be used after this point, but it can be defined again, possibly with a different meaning, using the #define directive. 䊐 Conditional Inclusion You can use the preprocessor directives #ifdef and #ifndef to allow the compiler to check whether a macro has been defined. Syntax: #ifdef name . . . // Block, which will be compiled // if name is defined. #endif In the case of the #ifndef directive, the code block is compiled up to the next #endif only if the macro name has not been previously defined. On conditional inclusion else branching and nesting is also permissible. See Preprocessor Directives in the appendix for further information. A macro definition does not need to include a substitute text to be valid. Example: #define MYHEADER A symbol without a substitute text is often used to identify header files and avoid multiple inclusion. If you have a header file named "article.h", you can identify the header by defining a symbol, such as _ARTICLE_, within that file. Example: #ifndef _ARTICLE_ #define _ARTICLE_ . . . // Content of the header file #endif If you have already included the header, _ARTICLE_ will already be defined, and the contents of the header file need not be compiled. This technique is also employed by standard header files. 128 ■ CHAPTER 7 SYMBOLIC CONSTANTS AND MACROS ■ STANDARD MACROS FOR CHARACTER MANIPULATION Sample program // toupper.cpp: A filter that converts to capitals. // --------------------------------------------------#include #include using namespace std; int main() { char c; long nChar = 0, // Counts all characters nConv = 0; // and converted characters while ( cin.get(c) ) // As long as a character { ++nChar; // can be read, to increment. if( islower(c)) // Lowercase letter? { c = toupper(c); // Converts the character ++nConv; // and counts it. } cout.put(c); // Outputs the character. } clog << "\nTotal of characters: " << nChar << "\nTotal of converted characters: " << nConv << endl; return 0; } ✓ NOTE The program reads characters from a file until end-of-file. When reading keyboard input, end-of-file is simulated by Ctrl+Z (DOS) or Ctrl+D (UNIX). Macros for character classification Macro Return value true means: isalpha(c) c is a letter islower(c) c is a small letter isupper(c) c is a capital letter isdigit(c) c is a decimal digit isalnum(c) c is a letter or a digit isspace(c) c is a space letter isprint(c) c is a printable letter STANDARD MACROS FOR CHARACTER MANIPULATION ■ 129 The following section introduces macros that classify or convert single characters. The macros are defined in the header files ctype.h and cctype. 䊐 Case Conversion You can use the macro toupper to convert lowercase letters to uppercase. If c1 and c2 are variables of type char or int where c1 contains the code for a lowercase letter, you can use the following statement Example: c2 = toupper(c1); to assign the corresponding uppercase letter to the variable c2. However if c1 is not a lowercase letter, toupper(c1) returns the character “as is.” The sample program on the opposite page reads standard input, converts any letters from lower- to uppercase, and displays the letters. As toupper only converts the letters of the English alphabet by default, any national characters, such as accentuated characters in other languages, must be dealt with individually. A program of this type is known as a filter and can be applied to files. Refer to the next section for details. The macro tolower is available for converting uppercase letters to lowercase. 䊐 Testing Characters A number of macros, all of which begin with is..., are available for classifying characters. For example, the macro islower(c) checks whether c contains a lowercase letter returning the value true, in this case, and false in all other cases. Example: char c; cin >> c; // Reads and // classifies if( !isdigit(c) ) // a character. cout << "The character is no digit \n"; The following usage of islower() shows a possible definition of the toupper() macro: Example: #define toupper(c) \ (islower(c) ? ((c)-'a'+'A') : (c)) This example makes use of the fact that the codes of lower- and uppercase letters differ by a constant, as is the case for all commonly used character sets such as ASCII and EBCDIC. The opposite page contains an overview of macros commonly used to classify characters. 130 ■ CHAPTER 7 SYMBOLIC CONSTANTS AND MACROS ■ REDIRECTING STANDARD INPUT AND OUTPUT Sample program // lines.cpp // A filter that numbers lines. #include #include #include using namespace std; int main() { string line; int number = 0; while( getline( cin, line)) // As long as a line { // can be read. cout << setw(5) << ++number << ": " << line << endl; } return 0; } How to call the program 1. Redirecting the standard input: lines < text.dat | more This outputs the text file text.dat with line numbers. In addition, the data stream is sent to the standard filter more, which pauses screen output if the page is full. 2. Redirecting the standard output: lines > new.dat Here the program reads from the keyboard and adds the output to the new file new.dat. Please note, if the file already exists, it will be overwritten! You can use lines >> text.dat to append program output to the file text.dat. If the file text.dat does not already exist, it will be created. Type Ctrl+Z (DOS) or Ctrl+D (UNIX) to terminate keyboard input. REDIRECTING STANDARD INPUT AND OUTPUT ■ 131 䊐 Filter Programs The previous program, toupper.cpp, reads characters from standard input, processes them, and sends them to standard output. Programs of this type are known as filters. In the program toupper.cpp, the loop while( cin.get(c)) { ... } is repeated while the test expression cin.get(c) yields the value true, that is, as long as a valid character can be read for the variable c. The loop is terminated by end-of-file or if an error occurs since the test expression cin.get(c) will then be false. The program on the opposite page, lines.cpp, is also a filter that reads a text and outputs the same text with line numbers. But in this case standard input is read line by line. while( getline(cin,line)) { ... } The test expression getline(cin,line) is true while a line can be read. 䊐 Using Filter Programs Filter programs are extremely useful since various operating systems, such as DOS, Win**, WinNT, and UNIX are capable of redirecting standard input and output. This allows easy data manipulation. For example, if you need to output text.dat with line numbers on screen, you can execute the program lines by typing the following command: Example: lines < text.dat This syntax causes the program to read data from a file instead of from the keyboard. In other words, the standard input is redirected. The opposite page contains additional examples. You can redirect input and output simultaneously: Example: lines < text.dat > new.dat ✓ In this example the contents of text.dat and the line numbers are stored in new.dat. The program does not generate any screen output. NOTE These examples assume that the compiled program lines.exe is either in the current directory or in a directory defined in your system’s PATH variable. ■ CHAPTER 7 ■ exercise s 132 SYMBOLIC CONSTANTS AND MACROS EXERCISES Hints for Exercise 2 You can use the function kbhit() to test whether the user has pressed a key. If so, the function getch() can be used to read the character.This avoids interrupting the program when reading from the keyboard. These functions have not been standardized by ANSI but are available on almost every system. Both functions use operating system routines and are declared in the header file conio.h. The function kbhit() Prototype: Returns: int kbhit(); 0, if no key was pressed, otherwise != 0. When a key has been pressed, the corresponding character can be read by getch(). The function getch() Prototype: Returns: int getch(); The character code.There is no special return value on reaching end-of-file or if an error occurs. In contrast to cin.get(), getch() does not use an input buffer when reading characters, that is, when a character is entered, it is passed directly to the program and not printed on screen. Additionally, control characters, such as return ( = 13), Ctrl+Z ( = 26), and Esc ( = 27), are passed to the program “as is.” Example: int c; if( kbhit() != 0) { c = getch(); if( c == 27 ) // . . . } ✓ // Key was pressed? // Yes -> Get character // character == Esc? NOTE When a function key, such as F1, F2, ..., Ins, Del, etc. was pressed, the function getch() initially returns 0. A second call yields the key number. EXERCISES ■ 133 Exercise 1 Please write a. the macro ABS, which returns the absolute value of a number, b. the macro MAX, which determines the greater of two numbers. In both cases use the conditional operator ?: . Add these macros and other macros from this chapter to the header file myMacros.h and then test the macros. If your system supports screen control macros, also add some screen control macros to the header. For example, you could write a macro named COLOR(f,b) to define the foreground and background colors for the following output. Exercise 2 Modify the program ball1.cpp to a. display a white ball on a blue background, b. terminate the program when the Esc key is pressed, c. increase the speed of the ball with the + key and decrease the speed with the – key. You will need the functions kbhit() and getch() (shown opposite) to solve parts b and c of this problem. Exercise 3 Write a filter program to display the text contained in any given file.The program should filter any control characters out of the input with the exception of the characters \n (end-of-line) and \t (tabulator), which are to be treated as normal characters for the purpose of this exercise. Control characters are defined by codes 0 to 31. A sequence of control characters is to be represented by a single space character. A single character, that is, a character appearing between two control characters, is not to be output! ✓ NOTE Since the program must not immediately output a single character following a control character, you will need to store the predecessor of this character. You may want to use two counters to count the number of characters and control characters in the current string. ■ CHAPTER 7 ■ solutions 134 SYMBOLIC CONSTANTS AND MACROS SOLUTIONS Exercise 1 // -----------------------------------------------------// myMacros.h // Header file contains the Macros // ABS, MIN, MAX, CLS, LOCATE, COLOR, NORMAL, INVERS // and symbolic constants for colors. // -----------------------------------------------------#ifndef _MYMACROS_ #define _MYMACROS_ #include using namespace std; // -----------------------------------------------------// Macro ABS // Call: ABS( val) // Returns the absolute value of val #define ABS(a) ( (a) >= 0 ? (a) : -(a)) // -----------------------------------------------------// Macro MIN // Call: MIN(x,y) // Returns the minimum of x and y #define MIN(a,b) ( (a) <= (b) ? (a) : (b)) // -----------------------------------------------------// Macro MAX // Call: MAX(x,y) // Returns the maximum of x and y #define MAX(a,b) ( (a) >= (b) ? (a) : (b)) // -----------------------------------------------------// Macros for controlling the screen // -----------------------------------------------------// Macro CLS // Call: CLS; // Clears the screen #define CLS (cout << "\033[2J") // -----------------------------------------------------// Macro LOCATE // Call: LOCATE(row, column); // Positions the cursor to (row,column). // (1,1) is the upper left corner. #define LOCATE(r,c) (cout <<"\033["<< (r) <<';'<<(c)<<'H') SOLUTIONS // -----------------------------------------------------// Macro COLOR // Call: COLOR(foreground, background); // Sets the foreground and background color // for the following output. #define COLOR( f, b) (cout << "\033[1;3"<< (f) \ <<";4"<< (b) <<'m' << flush) // 1: light foreground // 3x: foreground x // 4x: background x // Color values for the macro COLOR // To call ex.: COLOR( WHITE,BLUE); #define BLACK 0 #define RED 1 #define GREEN 2 #define YELLOW 3 #define BLUE 4 #define MAGENTA 5 #define CYAN 6 #define WHITE 7 // -----------------------------------------------------// Macro INVERS // Call: INVERS; // The following output is inverted. #define INVERS (cout << "\033[7m") // -----------------------------------------------------// Macro NORMAL // Call: NORMAL; // Sets the screen attributes on default values. #define NORMAL (cout << "\033[0m") #endif // _MYMACROS_ Exercise 2 // --------------------------------------------------// ball2.cpp // Simulates a bouncing ball // --------------------------------------------------#include #include using namespace std; #include #include "myMacros.h" // For kbhit() and getch() ■ 135 136 ■ CHAPTER 7 SYMBOLIC CONSTANTS AND MACROS #define ESC 27 // ESC terminates the program unsigned long delay = 5000000; // Delay for output int main() { int x = 2, y = 2, dx = 1, speed = 0; bool end = false; string floor(80, '-'), header = "**** BOUNCING BALL ****", commands = "[Esc] = Terminate " "[+] = Speed up [-] = Slow down"; COLOR(WHITE,BLUE); CLS; LOCATE(1,25); cout << header; LOCATE(24,1); cout << floor; LOCATE(25,10); cout << commands; while( !end) // As long as the flag is not set { LOCATE(y,x); cout << 'o'; // Show the ball for( long wait = 0; wait < delay; ++wait) ; if(x == 1 || x == 79) dx = -dx; // Bounce off a wall? if( y == 23 ) // On the floor? { speed = - speed; if( speed == 0 ) speed = -7; // Kick } speed += 1; // Speed up = 1 LOCATE(y,x); cout << ' '; y += speed; x += dx; // Clear screen // New position if( kbhit() != 0 ) { switch(getch()) { case '+': delay -= delay/5; break; case '-': delay += delay/5; break; case ESC: end = true; } } // Key pressed? } NORMAL; CLS; return 0; } // Yes // Speed up // Slow down // Terminate SOLUTIONS ■ Exercise 3 // --------------------------------------------------// NoCtrl.cpp // Filter to ignore control characters // To call e.g.: NoCtrl < file // --------------------------------------------------#include using namespace std; #define isCtrl(c) ( c >= 0 int main() { char c, prec = 0; long nCtrl = 0, nChar = 0; && c <= 31 \ && c != '\n' && c != '\t') // // // // Character and predecessor Number of the following control characters or other characters while( cin.get(c)) { if( isCtrl(c)) // Control characters { ++nCtrl; nChar = 0; } else // Normal character { if( nCtrl > 0) { cout.put(' '); nCtrl = 0; } switch( ++nChar) { case 1: break; case 2: cout.put(prec); // Predecessor and default: cout.put(c); // current character } prec = c; } } return 0; } 137 This page intentionally left blank chapter 8 Converting Arithmetic Types This chapter introduces implicit type conversions, which are performed in C++ whenever different arithmetic types occur in expressions. Additionally, an operator for explicit type conversion is introduced. 139 140 ■ CHAPTER 8 ■ CONVERTING ARITHMETIC TYPES IMPLICIT TYPE CONVERSIONS Integer promotions bool int char, signed char, unsigned char short int if int equals long unsigned int if int equals short unsigned short Type hierarchy long double double float unsigned long long unsigned int not-existent, if int equals long int Example short size(512); double res, x = 1.5; res = size / 10 * x; // short -> int -> double int IMPLICIT TYPE CONVERSIONS ■ 141 C++ allows you to mix arithmetic types in a single expression — in other words, the operands of an operator can belong to different types. The compiler automatically performs implicit type conversion, where a common type, which allows the operation in question to be performed, is assigned for the values of both operands. You can generally assume that the “smaller” type will be converted to the “larger” type. The assignment operator is an exception to this rule and will be discussed separately. The result of an arithmetic operation belongs to the common type used to perform the calculation. However, comparison expressions will be bool types no matter what type of operands are involved. 䊐 Integer Promotion Integer promotion is first performed for any expression: ■ ■ bool, char, signed char, unsigned char, and short are converted to int unsigned short is also converted to int if the int type is greater than short, and to unsigned int in all other cases. This type conversion is performed so as to preserve the original values. The boolean value false is converted to 0 and true is converted to 1. Thus, C++ will always use int type values or greater when performing calculations. Given a char variable c, the values of c and 'a' in the expression Example: c < 'a' will be converted to int before being compared. 䊐 Usual Arithmetic Type Conversions If operands of different arithmetic types still occur after integer promotion, further implicit type conversions along the lines of the hierarchy on the opposite page will be necessary. In this case, the type of the operand with the highest rank in the hierarchy is applied. These type conversions and integer promotions are collectively known as usual arithmetic type conversions. In our example, size/10 * x, the value of size is first promoted to int before an integer division size/10 is performed. The interim result 50 is then converted to double and multiplied by x. Usual arithmetic type conversions are performed for all binary operators and the conditional operator ?: provided the operands belong to an arithmetic type, the only exceptions being the assignment operator and the logical operators && and ||. 142 ■ CHAPTER 8 ■ CONVERTING ARITHMETIC TYPES PERFORMING USUAL ARITHMETIC TYPE CONVERSIONS Converting signed integers a) Converting a positive number Sign bit(= 0 ↔ not negative) Binary representaion of the integer 10 as value of type signed char (8 bits): 0 26 25 24 23 22 21 20 0 0 0 1 0 1 0 Extension to int (here 16 bit) The value 10 is preserved. 0 0 0 214 213 0 0 0 0 0 0 0 0 0 1 0 1 0 • • •• • 28 27 • • • • • 21 20 Sign bit(= 0 ↔ not negative) b) Converting a negative number The bit pattern of –10 is computed by starting with the bit pattern of 10 and generating the binary complement (see Binary Representation of Numbers in the appendix). Sign bit(= 1 ↔ negative) Binary representaion of the integer –10 as value of type signed char (8 bits): 1 26 25 24 23 22 21 20 1 1 1 0 1 1 0 Extension to int (here 16 bit) The value –10 is preserved. 1 1 1 214 213 1 • 1 • 1 •• 1 1 1 • 28 27 1 • 1 • 1 • 0 • 1 1 0 • 21 20 Sign bit(= 1 ↔ negative) ✓ NOTE The value of a negative number changes if the bit pattern is interpreted as unsigned. The bit pattern 1111 0110 of –10, for example, corresponds to the unsigned char value 246 == 0*20+ 1*21 + 1*22 + 0*23 + 1*24 + 1*25 + 1*26 + 1*27 PERFORMING USUAL ARITHMETIC TYPE CONVERSIONS ■ 143 Usual arithmetic type conversions retain the value of a number provided it can be represented by the new type. The procedure for type conversion depends on the types involved: 1. Conversion of an unsigned type to a larger integral type Examples: unsigned char to int or unsigned int Zero extension is performed first. During this process, the bit pattern of the number to be converted is expanded to match the length of the new type by adding zeros from the left. 2. Conversion of a signed type to a larger integral type ■ The new type is also signed Examples: char to int, short to long ■ Signed integers are represented by generating the binary complement. The value is retained by performing sign extension. As shown in the example on the opposite page, the original bit pattern is expanded to match the length of the new type by padding the sign bit from the left. The new type is unsigned Examples: char to unsigned int, long to unsigned long In this case the value of negative numbers is not retained. If the new type is of the same length, the bit pattern is retained. However, the bit pattern will be interpreted differently. The sign bit loses its significance (see the note opposite). If the new type is longer, sign extension is performed first and the new bit pattern is then interpreted as unsigned. 3. Conversion of an integral type to a floating-point type Examples: int to double, unsigned long to float The number is converted to an exponential floating-point type and the value retained. When converting from long or unsigned long to float, some rounding may occur. 4. Conversion of a floating-point type to a larger floating-point type Examples: float to double, double to long double The value is retained during this type conversion. 144 ■ CHAPTER 8 CONVERTING ARITHMETIC TYPES ■ IMPLICIT TYPE CONVERSIONS IN ASSIGNMENTS Example 1: int i = 100; long lg = i + 50; // Result of type int is // converted to long. Example 2: long lg = 0x654321; short st; st = lg; //0x4321 is assigned to st. Example 3: int i = –2; unsigned int ui = 2; i = i * ui; // First the value contained in i is converted to // unsigned int (preserving the bit pattern) and // multiplied by 2 (overflow!). // While assigning the bit pattern the result // is interpreted as an int value again, // i.e. –4 is stored in i. Example 4: double db = –4.567; int i; unsigned int ui; i = db; // Assigning –4. i = db – 0.5; // Assigning –5. ui = db; // –4 is incompatible with ui. Example 5: double d = 1.23456789012345; float f; f = d; // 1.234568 is assigned to f. IMPLICIT TYPE CONVERSIONS IN ASSIGNMENTS ■ 145 Arithmetic types can also be mixed in assignments. The compiler adjusts the type of the value on the right of the assignment operator to match the type of the variable on the left. In the case of compound assignments, calculations using normal arithmetic type conversions are performed first before type conversion is performed following the rule for simple assignments. Two different cases can occur during type conversion in assignments: 1. If the type of the variable is larger than the type of the value to be assigned, the type of the value must be promoted. The rules for usual arithmetic type conversions are applied in this case (see Example 1). 2. If the type of the value to be assigned is larger, this type must be “demoted.” The following procedures are followed depending on individual circumstances: a. Conversion of an integral type to a smaller type: ■ the type is converted to a smaller type by removing the most significant byte(s). The bit pattern that remains will be interpreted as unsigned, if the new type is also unsigned, and as signed in all other cases. The value can only be retained if it can be represented by the new type (see Example 2). ■ when converting an unsigned type to a signed type of the same scale, the bit pattern is retained and will be interpreted as signed (see Example 3). b. Conversion of a floating-point type to an integral type The decimal part of the floating-point number is removed. For example, 1.9 converts to the integer 1. Rounding can be achieved by adding 0.5 to a positive floating-point number or subtracting 0.5 from a negative floating-point number. This would allow for converting (1.9 + 0.5) to 2. If the resulting integer is too large or too small for the new type, the result is unpredictable. This particularly applies to converting negative floatingpoint numbers to unsigned integers (see Example 4). c. Conversion of a floating-point type to a smaller type If the floating-point number falls within the range of the new type, the value will be retained, although the accuracy may be compromised. If the value is too large to be represented by the new type, the result is unpredictable (see Example 5). 146 ■ CHAPTER 8 CONVERTING ARITHMETIC TYPES ■ MORE TYPE CONVERSIONS Sample program // Ellipse.cpp // The program draws an ellipse. // The points (x,y) on an ellipse with center (0,0) // and axes A and B satisfy: // x = A*cos(t), y = B*sint(t) for 0 <= t <= 2*PI . //-----------------------------------------------------#include #include using namespace std; // Prototypes of sin() and cos() #define CLS (cout << "\033[2J") #define LOCATE(z,s) (cout <<"\033["<<(z)<<';'<<(s)<<'H') #define DOT(x,y) (LOCATE(y,x) << '*') #define #define #define #define #define PI Mx My A B int main() { int x, y; 3.1416 40 12 25 10 // // // // The point center of Length of Length of (Mx, My) is the the ellipse. main axis subsidiary axis // Screen coordinates. CLS; // 0 <= t <= PI/2 is a 1/4-circle: for( double t = 0.0 ; t <= PI/2 ; t += 0.03) { x = (int) (A * cos(t) + 0.5); y = (int) (B * sin(t) + 0.5); DOT( x+Mx, y+My); DOT( x+Mx,-y+My); DOT(-x+Mx, y+My); DOT(-x+Mx,-y+My); } LOCATE(24,0); return 0; } MORE TYPE CONVERSIONS ■ 147 䊐 Implicit Type Conversions in Function Calls In the case of function calls, arguments with arithmetic types are converted to the types of the corresponding parameters, similarly to conversions in assignments. Example: void func( short, double); int size = 1000; // . . . func( size, 77); // Prototype // Call The function func() has two parameters belonging to the short and double types. However, the function is called using two int arguments. This leads to implicit conversion of the value of size to short and the integer 77 to double. When an int is converted to short the compiler issues a warning, since some data loss may occur. You can use explicit type conversion to avoid warnings during type conversion. 䊐 Explicit Type Conversion It is possible to convert the type of an expression explicitly using the cast operator (type). Syntax: (type) expression This converts the value of an expression to the given type. Explicit type conversion is also known as casting. The cast operator (type) is a unary operator and thus has a higher precedence than the arithmetic operators. Example: int a = 1, b = 4; double x; x = (double)a/b; In this example the value of a is explicitly converted to a double. Following the conventions of usual implicit type conversion, b is also converted to double and a floatingpoint division is performed. The exact result, 0.25, is assigned to the variable x. Without casting, an integer division with a result of 0 would have occurred. C++ has additional operators for explicit type conversion—the cast operator dynamic_cast<>, for example. These operators, which are described in later chapters, are required for special circumstances, for example, to perform type checking at runtime when converting classes. ■ CHAPTER 8 ■ exercise s 148 CONVERTING ARITHMETIC TYPES EXERCISES Program listing for exercise 3 // Convert.cpp —> Demonstrates type conversions. #include #include using namespace std; int main() { char v_char = 'A'; cout << "v_char: " << setw(10) << v_char << setw(10) << (int)v_char << endl; short v_short = –2; cout << "v_short: " << dec << setw(10) << v_short << hex << setw(10) << v_short << endl; unsigned short v_ushort = v_short; cout << "v_ushort: " << dec << setw(10) << v_ushort << hex << setw(10) << v_ushort << endl; unsigned long v_ulong = v_short; cout << "v_ulong: " << hex << setw(20) << v_ulong << endl; float v_float = –1.99F; cout << "v_float: " << setw(10) << v_float << endl; cout << "(int)v_float: " << setw(10) << dec << (int)v_float << endl; return 0; } Graphic for exercise 4 ------- The Sine Function ------- Ÿ sin(x) Ő Ő 1 ******* Ő *** *** Ő ** ** Ő * * Ő ** ** Ő * * Ő ** ** Ő* * 2PI x ņŐņņņņņņņ*ņņņņņņņŐņņņņņņņŐņņņņņņņŐņņņņņņņ*ņņņņņņņŐņņņņņņņŐņņņņņņņŐņņņņņņņ*ņņņņŹ Ő * * Ő ** ** Ő * * Ő ** ** Ő * * Ő ** ** Ő *** *** Ő -1 ******* Ő EXERCISES ■ 149 Exercise 1 A function has the following prototype void func( unsigned int n); What happens when the function is called with –1 as an argument? Exercise 2 How often is the following loop executed? unsigned int limit = 1000; for (int i = –1; i < limit; i++) // . . . Exercise 3 What is output when the program opposite is executed? Exercise 4 Write a C++ program to output the sine curve on screen as in the graphic shown on the opposite page. ✓ NOTE 1. Plot one point of the curve in columns 10, 10+1, ..., 10+64 respectively. This leads to a step value of 2*PI/64 for x. 2. Use the following extended ASCII code characters to draw the axes: Character Decimal Octal – 196 304 + 197 305 16 020 30 036 Example: cout << '\020'; // up arrowhead ■ CHAPTER 8 ■ solutions 150 CONVERTING ARITHMETIC TYPES SOLUTIONS Exercise 1 When called, the value –1 is converted to parameter n, i.e. to unsigned int. The pattern of –1 is interpreted as unsigned, which yields the greatest unsigned value. On a 32-bit system, –1 has the bit pattern 0xFFFFFFFF, which, when interpreted as unsigned, corresponds to the decimal value 4 294 967 295. Exercise 2 The statement within the loop is not executed at all! In the expression i < limit the value of variable i, –1, is implicitly converted to unsigned int and thus it represents the greatest unsigned value (see Exercise 1). Exercise 3 The screen output of the program v_char: v_short: v_ushort: v_ulong: v_float: (int)v_float: A -2 65534 65 fffe fffe fffffffe -1.99 -1 Exercise 4 // ----------------------------------------------------// sinCurve.cpp // Outputs a sine curve // ----------------------------------------------------#include #include using namespace std; #define #define #define #define #define // Prototypes of sin() CLS (cout << "\033[2J") LOCATE(z,s) (cout <<"\033["<<(z)<<';'<<(s)<<'H') PI 3.1415926536 START 0.0 // Lower limit END (2.0 * PI) // Upper limit SOLUTIONS #define #define #define #define PNT STEP xA yA 64 // Number of points on the curve ((END-START)/PNT) 14 // Row of x-axis 10 // Column of y-axis int main() { int row, column; CLS; LOCATE(2,25); cout << "------// --- The Sine Function -------"; Draws the coordinate system: --- LOCATE(xA,1); // x-axis for( column = 1 ; column < 78 ; ++column) { cout << ((column - yA) % 8 ? '\304' : '\305'); } cout << '\020'; // top LOCATE(xA-1, yA+64); cout << "2PI x"; for( row = 5 ; row < 24 ; ++row) { LOCATE(row, yA); cout << '\305'; } LOCATE( 4, yA); cout << "\036 sin(x)"; LOCATE( xA-8, yA+1); LOCATE( xA+8, yA+1); // // y-axis // top cout << " 1"; cout << " -1"; --- Displays the sine function: --- int begpt = yA, endpt = begpt + PNT; for( column = begpt ; column <= endpt { double x = (column-yA) * STEP; row = (int)(xA - 8 * sin(x) + 0.5); LOCATE( row, column); cout << '*'; } LOCATE(25,1); return 0; } ■ ; ++column) // Cursor to the last row 151 This page intentionally left blank chapter 9 The Standard Class string This chapter introduces the standard class string, which is used to represent strings. Besides defining strings we will also look at the various methods of string manipulation.These include inserting and erasing, searching and replacing, comparing, and concatenating strings. 153 154 ■ CHAPTER 9 THE STANDARD CLASS STRING ■ DEFINING AND ASSIGNING STRINGS Initializing string message = "Good Morning!"; String message in memory: ✓ String literal: 'G' Length: 13 'o' 'o' 'd' '' 'M' 'o' 'r' 'n' 'i' 'n' g' '!' NOTE Objects of class string do not necessarily contain the string terminating character '\0', as is the case with C strings. Sample program // string1.cpp: Using strings #include #include using namespace std; string prompt("Enter a line of text: "), // Global line( 50, '*'); // strings int main() { string text; // Empty string cout << line << endl << prompt << endl; getline( cin, text); // Reads a line of text cout << line << endl << "Your text is " << text.size() << " characters long!" << endl; // Two new strings: string copy(text), // a copy and the start(text,0,10); // first 10 characters // starting with // position 0. cout << "Your text:\n" << copy << endl; text = "1234567890"; // Assignment cout << line << endl << "The first 10 characters:\n" << start << endl << text << endl; return 0; } DEFINING AND ASSIGNING STRINGS ■ 155 C++ uses the standard class string to represent and manipulate strings allowing for comfortable and safe string handling. During string operations the required memory space is automatically reserved or modified. The programmer does not need to concern himself or herself with internal memory allocation. The string class is defined in the string header file and was mentioned in Chapter 3 as an example for the use of classes. Several operators are overloaded for strings, that is, they were also defined for the string class. This allows for easy copying, concatenation, and comparison. Additionally, various methods for string manipulation such as insertion, erasing, searching, and replacing are available. 䊐 Initializing Strings A string, that is, an object belonging to the string class, can be initialized when you define it using ■ ■ ■ a predefined string constant a certain number of characters a predefined string or part of a string. If a string is not initialized explicitly, an empty string with a length of 0 is created. The length of a string, that is, the current number of characters in the string, is stored internally and can be accessed using the length() method or its equivalent size(). Example: string message("Good morning!"); cout << message.length(); // Output: 13 䊐 String Assignments When you assign a value to a string, the current contents are replaced by a new character sequence. You can assign the following to a string object: ■ ■ ■ another string a string constant or a single character. The memory space required is adjusted automatically. The program on the opposite page uses the function getline(), which was introduced in an earlier chapter, to store a line of text from the keyboard in a string. In contrast, the >> operator reads only one word, ignoring any leading white space. In both cases the original content of the string is lost. 156 ■ CHAPTER 9 THE STANDARD CLASS STRING ■ CONCATENATING STRINGS Sample program // string2.cpp: Reads several lines of text and // outputs in reverse order. #include #include using namespace std; string prompt("Please enter some text!\n"), line( 50, '-'); int main() { prompt+="Terminate the input with an empty line.\n "; cout << line << '\n' << prompt << line << endl; string text, line; // Empty strings while( true) { getline( cin, line); // Reads a line of text if( line.length() == 0) // Empty line? break; // Yes ->end of the loop text = line + '\n' + text; // Inserts a new // line at the beginning. } // Output: cout << line << '\n' << "Your lines of text in reverse order:" << '\n' << line << endl; cout << text << endl; return 0; } Sample output for this program --------------------------------------Please enter some text! Terminate the input with an empty line. --------------------------------------Babara, Bobby, and Susan will go to the movies today --------------------------------------Your lines of text in reverse order: --------------------------------------will go to the movies today Babara, Bobby, and Susan CONCATENATING STRINGS ■ 157 Within the string class the operators + and += are defined for concatenating, and the operators ==, !=, <, <=, >, and >= are defined for comparing strings. Although these operators are being applied to strings, the well-known rules apply: the + has precedence over the comparative operators, and these in turn have higher precedence than the assignment operators = and +=. 䊐 Using + to Concatenate Strings You can use the + operator to concatenate strings, that is, to join those strings together. Example: string sum, s1("sun"), s2("flower"); sum = s2 + s3; This example concatenates the strings s1 and s2. The result, "sunflower" is then assigned to sum. Two strings concatenated using the + operator will form an expression of the string type. This expression can in turn be used as an operand in a more complex expression. Example: string s1("sun"),s2("flower"),s3("seed"); cout << s1 + s2 + s3; Since the + operator has precedence over the << operator, the strings are concatenated before the “sum” is output. Concatenation takes place from left to right. String constants and single characters are also valid as operands in expressions containing strings: Example: string s("Good morning "); cout << s + "mister X" + '!'; ✓ NOTE At least one operand must be a string class object. The expression "Good morning " + "mister X" would be invalid! 䊐 Using += to Concatenate Strings Strings can also be concatenated by first performing concatenation and then assigning the result. Example: string s1("Good "),s2("luck!"); s1 = s1 + s2; // To concatenate s2 and s1 This example creates a temporary object as a result of s1 + s2 and then assigns the result to s1. However, you can obtain the same result using the assignment operator += , which is far more efficient. Example: s1 += s2; s1 += "luck!"; // To concatenate s2 and s1. // Also possible This adds the content of the second string directly to s1. Thus, the += operator is preferable to a combination of the + and = operators. 158 ■ CHAPTER 9 THE STANDARD CLASS STRING ■ COMPARING STRINGS Sample program // string3.cpp: Inputs and compares lines of text. #include #include using namespace std; string prompt = "Please enter two lines of text!\n", line( 30, '-'); int main() { string line1, line2, key = "y"; while( key == "y" || key == "Y") { cout << line << '\n' << prompt << line << endl; getline( cin, line1); // Read the first getline( cin, line2); // and second line. if( line1 == line2) cout << " Both lines are the same!" << endl; else { cout << "The smaller line is:\n\t"; cout << (line1 < line2 ? line1 : line2) << endl; int len1 = line1.length(), len2 = line2.length(); if( len1 == len2) cout << "Both lines have the same length! \n"; else { cout << "The shorter line is:\n\t"; cout << (len1 < len2 ? line1 : line2) << endl; } } cout << "\nRepeat? (y/n) "; do getline( cin, key); while( key != "y" && key != "Y" && key != "n" && key != "N"); } return 0; } ✓ NOTE The relational operators yield the desired result for strings only if at least one operand is an object of class string. See Chapter 17, Pointers and Arrays, for more information. COMPARING STRINGS ■ 159 The comparative operators == != < <= > >= were overloaded in the string class to allow easy comparison of strings. This also allows you to use strings to formulate the conditions for branches and loops. Example: // str1 and str2 are objects of type string if( str1 < str2) . . . // str1 is less than str2? 䊐 Results of Comparisons Strings are compared lexicographically, that is character by character, beginning at the first character. To decide whether a single character is smaller, greater, or identical to another character, the character codes of the character set are compared. Thus, if you are using the ASCII character set, the letter 'A' (ASCII code 65) is smaller than the letter 'a' (ASCII code 97). A comparison results in a bool type value. Given two strings s1 and s2: s1 == s2 s1 < s2 is true only if both strings are identical; this requires that both strings are exactly the same length. is true only if the first character in s1 that differs from the corresponding character in s2 is smaller than the corresponding character in s2, or if s2 is simply an extension of s1. All other comparative operations can be deduced from the above rules. For example, the expression s1 > s2 is true only if s2 < s1 is also true. In an expression comparing strings, one operand can again be a string constant or a single character. Example: while( key == 'y' ) { . . . } This example compares the string key with the single character 'y'. This is an alternative method of expressing the comparison key == "y". String comparisons can also be combined to form more complex expressions. Example: while( key == "y" || key == "Y") { . . . } The controlling expression is valid if the string key contains only the letter 'Y' or 'y'. Due to the higher precedence of the comparative operator versus the || operator, no parentheses are required in this example. 160 ■ CHAPTER 9 ■ THE STANDARD CLASS STRING INSERTING AND ERASING IN STRINGS 䊐 Inserting a string string s1("Miss Summer"); s1.insert(5, "Ashley "); // Insert at position: 5 Effect of the statement: Position: 0 1 2 3 4 5 6 7 8 9 10 String s1 'M' 'i' 's' 's' ' ' 'S' 'u' 'm' 'm' 'e' 'r' 'A' 's' 'h' 'l' 'e' 'y' ' ' Erasing a substring string s("The summer-time"); s.erase(4,7); // Start position: 4, Quantity: 7 Effect of the statement: Position: 0 1 2 3 4 String s before 'T' 'h' 'e' ' ' String s afterwards 'T' 'e' ' ' 'h' 5 6 7 8 9 10 11 12 13 14 's' 'u' 'm' 'm' 'e' 'r' '-' 't' 'i' 'm' 'e' 't' 'i' 'm' 'e' INSERTING AND ERASING IN STRINGS ■ 161 The string class contains numerous methods for performing string manipulations. A method exists for each operation, such as inserting, erasing, searching, and replacing. These methods generally allow passing a string constant instead of a second string. A single character can also be used wherever appropriate. 䊐 Insertion The method insert() inserts a string at a certain position of another string. The position is passed as the first argument and defines the character before which to insert the string. The first character in a string occupies position 0, the second character position 1, and so on. Example: string s1("Miss Summer"); s1.insert(5, "Ashley "); The string "Ashley " is inserted into the string s1 at position 5, that is in front of the 'S' character in "Summer". Following this, the string "Miss Ashley Summer" is assigned to s1. If you need to insert only part of a string into another string, you can pass two additional arguments to the insert() method, the starting position and the length of the string. Example: string s1("Ashley is a devil"), s2(" sweetheart"); s1.insert(12, s2, 0, 12); This example inserts the first 12 characters from the string s2 at position 13 in string s1. String s1 then contains the string “Ashley is a sweetheart". 䊐 Erasing You can use the erase() method to delete a given number of characters from a string. The starting position is supplied as the first argument and the number of characters to be erased is the second argument. Example: string s("The summer-time"); s.erase(4,6); // Result: "The time" This statement deletes 7 characters from string s starting at position 4. The erase() method can also be called without specifying a length and will then delete all the characters in the string up to the end of the string. Example: string s("winter-story"); s.erase(6); // s now contains "winter" You can also call erase() without any arguments to delete all the characters in a string. 162 ■ CHAPTER 9 ■ THE STANDARD CLASS STRING SEARCHING AND REPLACING IN STRINGS 䊐 Replacing substrings a. Example “Bob and Bill” string s1("There they go again!"), s2("Bob and Bill"); s1.replace(6, 4, s2); Effect of the statement: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 s1 'T' 'h' 'e' 'r' 'e' ' ' 't' 'h' 'e' 'y' ' ' 'g' 'o' '' 'a' 'g' 'a' 'i' 'n' '!' s2 'B' 'o' 'b' ' ' 'a' 'n' 'd' ' ' 'B' 'i' 'l' 'l' b. Example “my love” string s1("Here comes Mike!"), s2("my love?"); s1.replace(11, 4, s2, 0, 7); Effect of the statement: 0 1 2 3 4 s1 'H' 'e' 'r' 'e' ' ' s2 'm' 'y' '' 5 6 7 8 9 10 11 12 13 14 15 'c' 'o' 'm' 'e' 's' ' ' 'M' 'i' 'l' 'o' 'v' 'e' '?' 'k' 'e' '!' SEARCHING AND REPLACING IN STRINGS ■ 163 䊐 Searching You can search strings to find the first or last instance of a substring. If the string contains the required substring, the position of the substring found by the search is returned. If not, a pseudo-position npos, or –1, is returned. Since the npos constant is defined in the string class, you can reference it as string::npos. The find() method returns the position at which a substring was first found in the string. The method requires the substring to be located as an argument. Example: string youth("Bill is so young, so young"); int first = youth.find("young"); The variable first has a value of 11 in this example. You can use the “right find” method rfind() to locate the last occurrence of a substring in a string. This initializes the variable last with a value of 21 in our example. Example: int last = youth.rfind("young"); 䊐 Replacing When replacing in strings, a string overwrites a substring. The string lengths need not be identical. You can use the replace() method to perform this operation. The first two arguments supply the starting position and the length of the substring to be replaced. The third argument contains the replacement string. Example: string s1("There they go again!"), s2("Bob and Bill"); int pos = s1.find("they"); if( pos != string::npos ) s1.replace(pos, 2, s2); // pos == 6 This example uses the string s2 to replace 4 characters, "they", starting at position 6 in s1. After this operation s1 contains the string "There Bob and Bill go again!". If you only need to insert part of a string, you can use the fourth argument to define the starting position and the fifth to define the length of the substring. Example: string s1("Here comes Mike!"), s2("my love?"); s1.replace(11, 4, s2, 0, 7); The string s1 is changed to "Here comes my love!". 164 ■ CHAPTER 9 THE STANDARD CLASS STRING ■ ACCESSING CHARACTERS IN STRINGS Sample program // string4.cpp // The program counts words and white space characters. // (A word is the maximum sequence of characters // containing no white space characters.) //--------------------------------------------------------------#include #include #include // Macro isspace() using namespace std; int main() { string header(" **** Counts words ****\n"), prompt("Enter a text and terminate" " with a period and return:"), line( 60, '-'), text; // Empty string cout << header << endl << prompt << endl << line << endl; getline( cin, text, '.'); // Reads a text up to // the first '.' // Counts words and white space characters int i, // Index nSpace = 0, // Number of white spaces nWord = 0; // Number of words bool fSpace = true; // Flag for white space for( i = 0; i < text.length(); ++i) { if( isspace( text[i]) ) // white space? { ++nSpace; fSpace = true; } else if( fSpace) // At the beginning of a word? { ++nWord; fSpace = false; } } cout << line // Outputs the result. << "\nYour text contains (without periods)" << "\n characters: " << text.length() << "\n words: " << nWord << "\n white spaces: " << nSpace << endl; return 0; } ACCESSING CHARACTERS IN STRINGS ■ 165 When manipulating strings it is often important to access the individual characters that form the string. C++ has the operator [] and the method at() for this purpose. An individual character is always identified by its index, also referred to as subscript, that is, its position in the string. The first character will always have an index value of 0, the second an index of 1, and so on. 䊐 Subscript Operator The easiest way to access a single character in the string is to use the subscript operator []. If you define a string as follows, Example: string s = "Let"; the individual characters in the string are: s[0] == 'L', s[1] == 'e', s[2] == 't' The last character in a string always has an index of s.length() – 1. You can use the subscript operator to read any character in a string and also to overwrite a character, provided the string was not defined as a constant. Example: char c = s[0]; This statement copies the first character from s to the variable c. In contrast Example: s[s.length() –1] = 'g'; overwrites the last character in the string s. Following this, s will contain the string "Leg". 䊐 Invalid Indices Any integral expression can be used as an index. However, no error message occurs if the boundaries of a valid index are overstepped. Example: cout << s[5]; // Error Your program’s reaction to an invalid index is undefined; this requires careful attention by the programmer! You can call the at() method if you need to perform range checks. 䊐 The at() method You can also use the at()method to access a single character. Example: s.at(i) = 'X'; is equivalent to s[i] = 'X'; In contrast to the subscript operator, the at() method performs range checking. If an invalid index is found an exception occurs and the program will normally be terminated at this point. However, you can specify how a program should react to an exception. ■ CHAPTER 9 THE STANDARD CLASS STRING ■ exercise s 166 EXERCISES For exercise 3 // timeStr.cpp // Demonstrates operations on a string containing // the present time. #include #include #include using namespace std; // For time(), ctime(), ... int main() { long sec; time( &sec); // Reads the present time // (in seconds) into sec. string tm = ctime( &sec); // Converts the // seconds to a string. cout << "Date and time: " << tm << endl; string hr(tm, 11, 2); // Substring of tm starting at // position 11, 2 characters long. string greeting("Have a wonderful "); if( hr < "10") // Compares strings greeting += "Morning!"; else if( hr < "17") greeting += "Day!"; else greeting += "Evening!"; cout << greeting << endl; return 0; } EXERCISES ■ 167 Exercise 1 Write a C++ program to ■ ■ ■ ■ initialize a string s1 with the string "As time by ..." and a second string s2 with the string "goes", insert string s2 in front of "by" in string s1, erase the remainder of string s1 after the substring "by", replace the substring "time" in s1 with "Bill". In each case, your program should determine the position of the substring. Output string s1 on screen at the beginning of the program and after every modification. Exercise 2 Write a C++ program that reads a word from the keyboard, stores it in a string, and checks whether the word is a palindrome.A palindrome reads the same from left to right as from right to left.The following are examples of palindromes:“OTTO, ” “deed, ” and “level.” Use the subscript operator []. Modify the program to continually read and check words. Exercise 3 Write down the screen output for the program on the opposite page. ✓ NOTE The function time() returns the current time as the number of seconds since 1/1/1970, 0:0. The number of seconds is stored in the variable sec, whose address was supplied as &sec when the function was called. The function ctime() converts the number of seconds to a string with a date and time and returns this string. The string comprises exactly 26 characters including the null character \0 and has the following format: Weekday Month Day Hr:Min:Sec Year\n\0 Example: Wed Jan 05 02:03:55 2000\n\0 ■ CHAPTER 9 ■ solutions 168 THE STANDARD CLASS STRING SOLUTIONS Exercise 1 // -----------------------------------------------------// strDemo.cpp: Insert, search, and replace in strings. // -----------------------------------------------------#include #include using namespace std; string header = "Demonstrating the use of strings\n", s1 = "As time by ...", s2 = "goes "; int main() { int pos = 0; cout << header << endl; cout << "s1 : " << s1 << endl; // To insert: cout << "\nInserting in string \"" << s2 <<"\""<< endl; pos = s1.find("by"); if( pos != string::npos ) s1.insert(pos,s2); cout << "s1 : " << s1 << endl; // Result // To erase: cout << "\nTo erase remaining characters behind \"by\":" << endl; pos = s1.find("by"); if( pos != string::npos ) s1.erase(pos + 3); cout << "s1 : " << s1 << endl; // Result // To replace: cout << "\nTo replace \"time\" by \"Bill\":" << endl; pos = s1.find("time"); if( pos != string::npos ) s1.replace(pos, 4, "Bill"); cout << "s1 : " << s1 << endl; return 0; } // Result SOLUTIONS ■ Exercise 2 // ----------------------------------------------------// palindrome.cpp: Reads and compares lines of text. // ----------------------------------------------------#include #include using namespace std; string header = " * * * Testing palindromes * * * ", prompt = "Enter a word: ", line( 50, '-'); int main() { string word; char key = 'y'; // Empty string cout << "\n\t" << header << endl; while( key == 'y' || key == 'Y') { cout << '\n' << line << '\n' << prompt; cin >> word; // Compares the first and last character, // the second and the second to last etc. int i = 0, j = word.length() - 1; for( ; i <= j ; ++i, --j) if( word[i] != word[j] ) break; if( i > j) // All characters equal? cout << "\nThe word " << word << " is a P A L I N D R O M E !" << endl; else cout << "\nThe word " << word << " is not a palindrome" << endl; cout << "\nRepeat? (y/n) "; do cin.get(key); while( key != 'y' && key != 'Y' && key != 'n' && key != 'N'); cin.sync(); } return 0; } 169 170 ■ CHAPTER 9 THE STANDARD CLASS STRING Exercise 3 The program outputs the date and time first.Then a greeting is printed according the time of day. For example: Date and time: Thu Nov 28 09:01:37 2001 Have a wonderful morning! chapter 10 Functions This chapter describes how to write functions of your own. Besides the basic rules, the following topics are discussed: ■ passing arguments ■ definition of inline functions ■ overloading functions and default arguments ■ the principle of recursion. 171 172 ■ CHAPTER 10 ■ FUNCTIONS SIGNIFICANCE OF FUNCTIONS IN C++ Elements of a C++ program C++ program Core elements of C++ (built-in types, operators, control structures) Functions and classes of the standard library Self-defined functions and classes and other libraries SIGNIFICANCE OF FUNCTIONS IN C++ ■ 173 C++ supports efficient software development on the lines of the top-down principle. If you are looking to provide a solution for a more complex problem, it will help to divide the problem into smaller units. After identifying objects you will need to define classes that describe these objects. You can use available classes and functions to do so. In addition, you can make use of inheritance to create specialized classes without needing to change any existing classes. When implementing a class you must define the capacities of those objects, that is, the member functions, in your program. However, not every function is a member function. Functions can be defined globally, such as the function main() for example. Functions of this type do not belong to any particular class but normally represent algorithms of a more general nature, such as the search or sort functions of the standard library. 䊐 Libraries You will not need to program each “building block” yourself. Many useful global functions and classes are available from the C++ standard library. In addition, you can use other libraries for special purposes. Often a compiler package will offer commercial class libraries or graphical user interfaces. Thus, a C++ program will be made up of ■ ■ ■ language elements of the C++ core global functions and classes from the C++ standard library functions and classes you have programmed yourself and other libraries. Classes and functions that belong together are normally compounded to form separate source files, which can be compiled and tested independently. Using software components that you have already tested makes programming a complex solution much easier and improves the reliability of your programs. You can enhance the reusability of your source code by compiling your own libraries, but be sure to include comments for ease of readability. Compiled source files, also known as modules, are compounded by the linker to an executable file by reference to the libraries you include. If you modify a source file, you may also need to recompile other files. In large scale projects it is recommended to use the MAKE utility for module management. An integrated developer environment will offer the functionality of this utility when you create a new project. This includes your own source files, the libraries used, and the compiler/linker settings for program compilation. 174 ■ CHAPTER 10 ■ FUNCTIONS DEFINING FUNCTIONS Example of a function definition // func1.cpp #include using namespace std; void test( int, double ); // Prototype int main() { cout << "\nNow function test() will be called.\n"; test( 10, -7.5); // Call cout << "\nAnd back again in main()." << endl; return 0; } void test(int arg1, double arg2 ) // Definition { cout << "\nIn function test()." << "\n 1. argument: " << arg1 << "\n 2. argument: " << arg2 << endl; } General form of a function [type] name([declaration_list]) { . . What will be done . . } // Function header // Beginning // Function block // End DEFINING FUNCTIONS ■ 175 The following section describes how to program global functions. Chapter 13, Defining Classes, describes the steps for defining member functions. 䊐 Definition Functions can be defined in any order, however, the first function is normally main. This makes the program easier to understand, since you start reading at the point where the program starts to execute. The function test() is shown opposite as an example and followed by the general form of a function. The example can be read as follows: type name declaration_list is the function type, that is, the type of the return value. is the function name, which is formed like a variable name and should indicate the purpose of the function. contains the names of the parameters and declares their types. The list can be empty, as for the function main(), for example. A list of declarations that contains only the word void is equivalent to an empty list. The parameters declared in a list are no more than local variables. They are created when the function is called and initialized by the values of the arguments. Example: When test( 10, -7.5); is called, the parameter arg1 is initialized with a value of 10 and arg2 with -7.5. The left curved bracket indicates the start of a function block, which contains the statements defining what the function does. 䊐 Prototype and Definition In a function definition the function header is similar in form to the prototype of a function. The only difference when a function is defined is that the name and declaration list are not followed by a semicolon but by a function code block. The prototype is the declaration of the function and thus describes only the formal interface of that function. This means you can omit parameter names from the prototype, whereas compiling a function definition will produce machine code. 176 ■ CHAPTER 10 ■ FUNCTIONS RETURN VALUE OF FUNCTIONS Defining and calling the function area() // area.cpp // Example for a simple function returning a value. //----------------------------------------------------#include #include using namespace std; double area(double, double); int main() { double x = 3.5, y = 7.2, res = area( x, y+1); // Prototype res; // Call // To output to two decimal places: cout << fixed << setprecision(2); cout << "\n The area of a rectangle " << "\n with width " << setw(5) << x << "\n and length " << setw(5) << y+1 << "\n is " << setw(5) << res << endl; return 0; } // Defining the function area(): // Computes the area of a rectangle. double area( double width, double len) { return (width * len); // Returns the result. } Screen output: The area of a rectangle with width 3.50 and length 8.20 is 28.70 RETURN VALUE OF FUNCTIONS ■ 177 The program opposite shows how the function area() is defined and called. As previously mentioned, you must declare a function before calling it. The prototype provides the compiler with all the information it needs to perform the following actions when a function is called: ■ ■ check the number and type of the arguments correctly process the return value of the function. A function declaration can be omitted only if the function is defined within the same source file immediately before it is called. Even though simple examples often define and call a function within a single source file, this tends to be an exception. Normally the compiler will not see a function definition as it is stored in a different source file. When a function is called, an argument of the same type as the parameter must be passed to the function for each parameter. The arguments can be any kind of expressions, as the example opposite with the argument y+1 shows. The value of the expression is always copied to the corresponding parameter. 䊐 Return Statement When the program flow reaches a return statement or the end of a function code block, it branches back to the function that called it. If the function is any type other than void, the return statement will also cause the function to return a value to the function that called it. Syntax: return [expression] If expression is supplied, the value of the expression will be the return value. If the type of this value does not correspond to the function type, the function type is converted, where possible. However, functions should always be written with the return value matching the function type. The function area() makes use of the fact that the return statement can contain any expression. The return expression is normally placed in parentheses if it contains operators. If the expression in the return statement, or the return statement itself, is missing, the return value of the function is undefined and the function type must be void. Functions of the void type, such as the standard function srand(), will perform an action but not return any value. 178 ■ CHAPTER 10 ■ FUNCTIONS PASSING ARGUMENTS Calling function and called function long func2(int, double); // . . . void func1() { int x = 1.1; double y; . . . long a = func2(x,y); . . . } // Prototype // Call of func2(). // Pass by value long func2(int a, double b) // Definition { double x = 2.2; long result; . // Here the result . // is computed. . return result; } Stack content after calling a function On call “push” On return “pop” • • • further local objects return address first parameter • • • last parameter Stack PASSING ARGUMENTS ■ 179 䊐 Passing by Value Passing values to a function when the function is called is referred to as passing by value. Of course the called function cannot change the values of the arguments in the calling function, as it uses copies of the arguments. However, function arguments can also be passed by reference. In this case, the function is passed a reference to an object as an argument and can therefore access the object directly and modify it. An example of passing by reference was provided in the example containing the function time(). When time(&sek); is called, the address of the variable sek is passed as an argument, allowing the function to store the result in the variable. We will see how to create functions of this type later. Passing by value does, however, offer some important advantages: ■ ■ ■ function arguments can be any kind of expression, even constants, for example the called function cannot cause accidental modifications of the arguments in the calling function the parameters are available as suitable variables within the functions. Additional indirect memory access is unnecessary. However, the fact that copying larger objects is difficult can be a major disadvantage, and for this reason vectors are passed by reference to their starting address. 䊐 Local Objects The scope of function parameters and the objects defined within a function applies only to the function block. That is, they are valid within the function only and not related to any objects or parameters of the same name in any other functions. For example, the program structure opposite contains a variable a in the function func1() and in the function func2(). The variables do not collide because they reference different memory addresses. This also applies to the variables x in func1() and func2(). A function’s local objects are placed on the stack—the parameters of the function are placed first and in reverse order. The stack is an area of memory that is managed according to the LIFO (last in first out) principle. A stack of plates is a good analogy. The last plate you put on the stack has to be taken off first. The LIFO principle ensures that the last local object to be created is destroyed first. 180 ■ CHAPTER 10 ■ FUNCTIONS INLINE FUNCTIONS Call to a function not defined as inline Program Function Branching void func() { // 1st Call func(); // 2nd Call } func(); ✓ HINT The executable file only contains one instance of the function’s machine code. Call to an inline function Program // 1st Call func(); Inline function Copy inline void func() { } // 2nd Call func(); ✓ HINT The machine code of the function is stored in the executable file wherever the function is called. INLINE FUNCTIONS ■ 181 䊐 Jumping to Sub-Routines When a function is called, the program jumps to a sub-routine, which is executed as follows: ■ ■ ■ the function parameters are placed on the stack and initialized with appropriate arguments the so-called return address, that is, the place where the function was called, is stored on the stack and the program flow branches to the function after executing the function the program uses the return address it stored previously to return to the calling function. The part of the stack occupied by the function is then released. All this jumping back and forth can affect the run time of your program, especially if the function contains only a few instructions and is called quite often. The time taken to branch to a small function can be greater than the time needed to execute the function itself. However, you can define inline functions to avoid this problem. 䊐 Inline Definition The compiler inserts the code of an inline function at the address where the function is called and thus avoids jumping to a sub-routine. The definition of an inline function is introduced by the inline keyword in the function header. Example: inline int max( int x, int y) { return (x >= y ? x : y ); } The program code will expand each time an inline function is called. This is why inline functions should contain no more than one or two instructions. If an inline function contains too many instructions, the compiler may ignore the inline keyword and issue a warning. An inline function must be defined in the source file in which it is called. You cannot simply supply a prototype of the function. The code containing the instructions must also be available to the compiler. It therefore makes sense to define inline functions in header files, in contrast to “normal” functions. This means the function will be available in several source files. 䊐 Inline Functions and Macros Inline functions are an alternative to macros with parameters. When a macro is called, the preprocessor simply replaces a block of text. In contrast, an inline function behaves like a normal function, although the program flow is not interrupted by the function branching. The compiler performs a type check, for example. 182 ■ CHAPTER 10 ■ FUNCTIONS DEFAULT ARGUMENTS Defining the function capital() // Computes the final capital with interest and // compound interest. // Formula: capital = k0 * (1.0 + p/100)n // where k0 = start capital, p = rate, n = run time // ---------------------------------------------------#include double capital( double k0, double p, double n) { return (k0 * pow(1.0+p/100, n)); } Possible calls // Function capital() with two default arguments // Prototype: double capital( double k0, double p=3.5, double n=1.0); double endcap; endcap = capital( 100.0, 3.5, 2.5); endcap = capital( 2222.20, 4.8); endcap = capital( 3030.00); // ok // ok // ok endcap = capital( ); // not ok // The first argument has no default value. ✓ endcap = capital( 100.0, , 3.0); // No gap! // not ok endcap = capital( , 5.0); // No gap either. // not ok NOTE A function defined with default arguments is always called with the full number of arguments. For reasons of efficiency it may be useful to define several versions of the same function. DEFAULT ARGUMENTS ■ 183 So-called default arguments can be defined for functions. This allows you to omit some arguments when calling the function. The compiler simply uses the default values for any missing arguments. 䊐 Defining Default Arguments The default values of a function’s arguments must be known when the function is called. In other words, you need to supply them when you declare the function. Example: void moveTo( int x = 0, int y = 0); Parameter names can be omitted, as usual. Example: void moveTo( int = 0, int = 0); The function moveTo() can then be called with or without one or two arguments. Example: moveTo (); moveTo (24); moveTo(24, 50); The first two calls are equivalent to moveTo(0,0); or moveTo(24,0); . It is also possible to define default arguments for only some of the parameters. The following general rules apply: ■ ■ ■ the default arguments are defined in the function prototype. They can also be supplied when the function is defined, if the definition occurs in the same source file and before the function is called if you define a default argument for a parameter, all following parameters must have default arguments default arguments must not be redefined within the prototype scope (the next chapter gives more details on this topic). 䊐 Possible Calls When calling a function with default arguments you should pay attention to the following points: ■ ■ ■ you must first supply any arguments that do not have default values you can supply arguments to replace the defaults if you omit an argument, you must also omit any following arguments. You can use default arguments to call a function with a different number of arguments without having to write a new version of the function. 184 ■ CHAPTER 10 ■ FUNCTIONS OVERLOADING FUNCTIONS Sample program // random.cpp // To generate and output random numbers. //----------------------------------------------------#include #include #include // For rand(), srand() #include // For time() using namespace std; bool setrand = false; inline void init_random() { // Initializes the random // number generator with the // present time. if( !setrand ) { srand((unsigned int)time(NULL)); setrand = true; } } inline double myRandom() // Returns random number x { // with 0.0 <= x <= 1.0 init_random(); return (double)rand() / (double)RAND_MAX; } inline int myRandom(int start, int end) // Returns the { // random number n with init_random(); // start <= n <= end return (rand() % (end+1 - start) + start); } // Testing myRandom() and myRandom(int,int): int main() { int i; cout << "5 random numbers between 0.0 and 1.0 :" << endl; for( i = 0; i < 5; ++i) cout << setw(10) << myRandom(); cout << endl; cout << "\nAnd now 5 integer random numbers " "between -100 and +100 :" << endl; for( i = 0; i < 5; ++i) cout << setw(10) << myRandom(-100, +100); cout << endl; return 0; } OVERLOADING FUNCTIONS ■ 185 Functions in traditional programming languages, such as C, which perform the same task but have different arguments, must have different names. To define a function that calculated the maximum value of two integers and two floating-point numbers, you would need to program two functions with different names. Example: int int_max( int x, int y); double dbl_max( double x, double y); Of course this is detrimental to efficient naming and the readability of your program— but luckily, this restriction does not apply to C++. 䊐 Overloading C++ allows you to overload functions, that is, different functions can have the same name. Example: int max( int x, int y); double max( double x, double y); In our example two different function share the same name, max. The function max() was overloaded for int and double types. The compiler uses a function’s signature to differentiate between overloaded functions. 䊐 Function Signatures A function signature comprises the number and type of parameters. When a function is called, the compiler compares the arguments to the signature of the overloaded functions and simply calls the appropriate function. Example: double maxvalue, value = 7.9; maxvalue = max( 1.0, value); In this case the double version of the function max() is called. When overloaded functions are called, implicit type conversion takes place. However, this can lead to ambiguities, which in turn cause a compiler error to be issued. Example: maxvalue = max( 1, value); // Error! The signature does not contain the function type, since you cannot deduce the type by calling a function. It is therefore impossible to differentiate between overloaded functions by type. Example: int search(string key); string search(string name); Both functions have the same signature and cannot be overloaded. 186 ■ CHAPTER 10 ■ FUNCTIONS RECURSIVE FUNCTIONS Using a recursive function // recursive.cpp // Demonstrates the principle of recursion by a // function, which reads a line from the keyboard // and outputs it in reverse order. // ---------------------------------------------------#include using namespace std; void getput(void); int main() { cout << "Please enter a line of text:\n"; getput(); cout << "\nBye bye!" << endl; return 0; } void getput() { char c; if( cin.get(c) getput(); cout.put(c); } && c != '\n') Program flow after typing ok main() { ... getput(); } 1st Execution 2nd Execution 3rd Execution getput() { ... // c = 'o' getput(); getput() { ... // c = 'k' getput(); cout.put(c); } } getput() { ... // c = '\n' // No call of // getput() cout.put(c); } cout.put(c); RECURSIVE FUNCTIONS ■ 187 䊐 Recursion A function that calls itself is said to be recursive. This process can also be performed indirectly if the function first calls another function or multiple functions before it is called once more. But a break criterion is always necessary to avoid having the function call itself infinitely. The concept of local objects makes it possible to define recursive functions in C++. Recursion requires local objects to be created each time the function is called, and these objects must not have access to any other local objects from other function calls. What effectively happens is that the local objects are placed on the stack, and thus the object created last is destroyed first. 䊐 A Sample Program Let’s look at the principle of recursion by referring to the sample program opposite. The program contains the recursive function getput() that reads a line of text from the keyboard and outputs it in reverse order. The function getput() is first called by main() and reads a character from the keyboard, storing it in the local variable c. If the character is not '\n', the function getput() calls itself again and thus reads a further character from the keyboard before storing it in the local variable c. The chain of recursive function calls is terminated by the user pressing the Return key. The last character to be read, '\n' (line feed), is output and the program flow branches to the previous getput() instance. This outputs the second to last character, and so on. When the first character to have been read has finally been output, the program flow is handed back to main(). 䊐 Practical Usage The logic of various solutions to common problems results in a recursive structure, for example, browsing directory trees, using binary trees for data management, or some sorting algorithms, such as the quick sort algorithm. Recursive functions allow you to formulate this kind of logic in an efficient and elegant manner. However, always make sure that sufficient memory is available for the stack. ■ CHAPTER 10 ■ exercise s 188 FUNCTIONS EXERCISES Hint for exercise 1 Working with several source files: Within an integrated development environment a project, containing all source files of the program, first has to be created.This ensures that all the source files will be compiled and linked automatically. However, when calling the compiler/linker from the command line, it is sufficient to declare the source files, for example: cc sum_t.cpp sum.cpp Screen output for exercise 3 n Factorial of n 0 1 1 1 2 2 3 6 4 24 5 120 6 720 7 5040 . . . . . . . . . . . . . . . . . 19 121645100408832000 20 2432902008176640000 EXERCISES ■ 189 Exercise 1 a. Write the function sum() with four parameters that calculates the arguments provided and returns their sum. Parameters: Four variables of type long. Returns: The sum of type long. Use the default argument 0 to declare the last two parameter of the function sum().Test the function sum() by calling it by all three possible methods. Use random integers as arguments. b. Now restructure your program to store the functions main() and sum() in individual source files, for example, sum_t.cpp and sum.cpp . Exercise 2 a. Write an inline function, Max(double x, double y), which returns the maximum value of x and y. (Use Max instead of max to avoid a collision with other definitions of max.) Test the function by reading values from the keyboard. Can the function Max() also be called using arguments of the types char, int, or long? b. Now overload Max() by adding a further inline function Max(char x, char y) for arguments of type char . Can the function Max() still be called with two arguments of type int? Exercise 3 The factorial n! of a positive integer n is defined as n! = 1*2*3 . . . * (n-1) * n 0! = 1 Where Write a function to calculate the factorial of a number. Argument: A number n of type unsigned int. Returns: The factorial n! of type long double. Formulate two versions of the function, where the factorial is a. calculated using a loop b. calculated recursively Test both functions by outputting the factorials of the numbers 0 to 20 as shown opposite on screen. 190 ■ CHAPTER 10 FUNCTIONS Exercise 4 Write a function pow(double base, int exp) to calculate integral powers of floating-point numbers. Arguments: The base of type double and the exponent of type int. Returns: The power baseexp of type double. For example, calling pow(2.5, 3) returns the value 2.53 = 2.5 * 2.5 * 2.5 = 15.625 This definition of the function pow()means overloading the standard function pow(), which is called with two double values. Test your function by reading one value each for the base and the exponent from the keyboard. Compare the result of your function with the result of the standard function. ✓ NOTE 1. The power x0 is defined as 1.0 for a given number x. 2. The power xn is defined as (1/x)-n for a negative exponent n. 3. The power 0n where n > 0 will always yield 0.0 . The power 0n is not defined for n < 0. In this case, your function should return the value HUGE_VAL. This constant is defined in math.h and represents a large double value. Mathematical functions return HUGE_VAL when the result is too large for a double. SOLUTIONS solutions ■ ■ SOLUTIONS Exercise 1 // // // // ----------------------------------------------------sum_t.cpp Calls function sum() with default arguments. ----------------------------------------------------- #include #include #include #include using namespace std; long sum( long a1, long a2, long a3=0, long a4=0); int main() // Several calls to function sum() { cout << " **** Computing sums ****\n" << endl; srand((unsigned int)time(NULL)); // Initializes the // random number generator. long res, a = rand()/10, b = rand()/10, c = rand()/10, d = rand()/10; res = sum(a,b); cout << a << " + " << b << " = " << res << endl; res = sum(a,b,c); cout << a << " + " << b << " + " << c << " = " << res << endl; res = sum(a,b,c,d); cout << a << " + " << b << " + " << c << " + " << d << " = " << res << endl; return 0; } // // // // ----------------------------------------------------sum.cpp Defines the function sum() ----------------------------------------------------- long sum( long a1, long a2, long a3, long a4) { return (a1 + a2 + a3 + a4); } 191 192 ■ CHAPTER 10 FUNCTIONS Exercise 2 // // // // ----------------------------------------------------max.cpp Defines and calls the overloaded functions Max(). ------------------------------------------------------ // // // // As long as just one function Max() is defined, it can be called with any arguments that can be converted to double, i.e. with values of type char, int or long. After overloading no clear conversion will be possible. #include #include using namespace std; inline double Max(double x, double y) { return (x < y ? y : x); } inline char Max(char x, char y) { return (x < y ? y : x); } string header( "To use the overloaded function Max().\n"), line(50,'-'); int main() // Several different calls to function Max() { double x1 = 0.0, x2 = 0.0; line += '\n'; cout << line << header << line << endl; cout << "Enter two floating-point numbers:" << endl; if( cin >> x1 && cin >> x2) { cout << "The greater number is " << Max(x1,x2) << endl; } else cout << "Invalid input!" << endl; cin.sync(); cin.clear(); // Invalid input // was entered. SOLUTIONS cout << line << "And once more with characters!" << endl; cout << "Enter two characters:" << endl; char c1, c2; if( cin >> c1 && cin >> c2) { cout << "The greater character is " << Max(c1,c2) << endl; } else cout << "Invalid input!" << endl; cout << "Testing with int arguments." << endl; int a = 30, b = 50; cout << Max(a,b) << endl; // Error! Which // function Max()? return 0; } Exercise 3 // ----------------------------------------------------// factorial.cpp // Computes the factorial of an integer iteratively, // i.e. using a loop, and recursively. // ----------------------------------------------------#include #include using namespace std; #define N_MAX 20 long double fact1(unsigned int n); long double fact2(unsigned int n); // Iterative solution // Recursive solution int main() { unsigned int n; // Outputs floating-point values without // decimal places: cout << fixed << setprecision(0); ■ 193 194 ■ CHAPTER 10 FUNCTIONS // --- cout << << << << Iterative computation of factorial --- setw(10) << "n" << setw(30) << "Factorial of n" " (Iterative solution)\n" " -----------------------------------------" endl; for( n = 0; n <= N_MAX; ++n) cout << setw(10) << n << setw(30) << fact1(n) << endl; cout << "\nGo on with "; cin.get(); // --- cout << << << << Recursive computation of factorial ---- setw(10) << "n" << setw(30) << "Factorial of n" " (Recursive solution)\n" " -----------------------------------------" endl; for( n = 0; n <= N_MAX; ++n) cout << setw(10) << n << setw(30) << fact2(n) << endl; cout << endl; return 0; } long double fact1(unsigned int n) // Iterative { // solution. long double result = 1.0; for( unsigned int i = 2; i <= n; ++i) result *= i; return result; } long double fact2(unsigned int n) { if( n <= 1) return 1.0; else return fact2(n-1) * n; } // Recursive // solution. SOLUTIONS Exercise 4 // ----------------------------------------------------// power.cpp // Defines and calls the function pow() to // compute integer powers of a floating-point number. // Overloads the standard function pow(). // ----------------------------------------------------#include #include using namespace std; double pow(double base, int exp); int main() // Tests the self-defined function pow() { double base = 0.0; int exponent = 0; cout << " **** Computing Integer Powers ****\n" << endl; cout << "Enter test values.\n" << "Base (floating-point): "; cin >> base; cout << "Exponent (integer): "; cin >> exponent; cout << "Result of " << base << " to the power of " << exponent << " = " << pow( base, exponent) << endl; cout << "Computing with the standard function: " << pow( base, (double)exponent) << endl; return 0; } double pow(double base, int exp) { if( exp == 0) return 1.0; if( base == 0.0) if( exp > 0) return 0.0; else return HUGE_VAL; if( exp < 0) { base = 1.0 / base; exp = -exp; } double power = 1.0; for( int n = 1; n <= exp; ++n) power *= base; return power; } ■ 195 This page intentionally left blank chapter 11 Storage Classes and Namespaces This chapter begins by describing storage classes for objects and functions.The storage class is responsible for defining those parts of a program where an object or function can be used. Namespaces can be used to avoid conflicts when naming global identifiers. 197 198 ■ CHAPTER 11 ■ ST0RAGE CLASSES AND NAMESPACES STORAGE CLASSES OF OBJECTS 䊐 Availability of Objects C++ program program scope Module 1 file scope Function block scope Function block scope Module 2 file scope Function block scope 䊐 Storage Class Specifiers The storage class of an object is determined by ■ ■ the position of its declaration in the source file the storage class specifier, which can be supplied optionally. The following storage class specifiers can be used extern static auto register STORAGE CLASSES OF OBJECTS ■ 199 When an object is declared, not only are the object’s type and name defined but also its storage class. The storage class specifies the lifetime of the object, that is, the period of time from the construction of the object until its destruction. In addition, the storage class delimits the part of the program in which the object can be accessed directly by its name, the so-called object scope. Essentially, an object is only available after you have declared it within a translation unit. A translation unit, also referred to as module, comprises the source file you are compiling and any header files you have included. As a programmer, you can define an object with: ■ block scope ■ file scope ■ program scope The object is only available in the code block in which it was defined. The object is no longer visible once you have left the code block. The object can be used within a single module. Only the functions within this module can reference the object. Other modules cannot access the object directly. The object is available throughout the program, providing a common space in memory that can be referenced by any program function. For this reason, these objects are often referred to as global. Access to an object as defined by the object’s storage class is independent of any access controls for the elements of a class. Namespaces that subdivide program scope and classes will be introduced at a later stage. 䊐 Lifetime Objects with block scope are normally created automatically within the code block that defines them. Such objects can only be accessed by statements within that block and are called local to that block. The memory used for these objects is freed after leaving the code block. In this case, the lifetime of the objects is said to be automatic. However, it is possible to define objects with block scope that are available throughout the runtime of a program. The lifetime of these objects is said to be static. When the program flow re-enters a code block, any pre-existing conditions will apply. Objects with program and file scope are always static. These objects are created when a program is launched and are available until the program is terminated. Four storage classes are available for creating objects with the scope and lifetime you need. These storage classes will be discussed individually in the following sections. 200 ■ CHAPTER 11 ■ ST0RAGE CLASSES AND NAMESPACES THE STORAGE CLASS extern Source file 1 // Cutline1.cpp // A filter to remove white-space characters // at the ends of lines. // -------------------------------------------------#include #include using namespace std; void cutline( void ); string line; int main() { while( getline(cin, line)) { cutline(); cout << line << endl; } return 0; } // Prototype // Global string // // // // As long as a line can be read. Shorten the line. Output the line. Source file 2 // // // // // // Cutline2.cpp Containing the function cutline(), which removes tabulator characters at the end of the string line. The string line has to be globally defined in another source file. -------------------------------------------------- #include using namespace std; extern string line; void cutline() { int i = line.size(); } // extern declaration // Position after the // last character. while( i-- >= 0 ) if( line[i] != ' ' && line[i] != '\t' ) break; // If no blank and // no tab -> // stop the loop. line.resize(++i); // Fix new length. THE STORAGE CLASS extern ■ 201 䊐 Defining Global Objects If an object is not defined within a function, it belongs to the extern storage class. Objects in this storage class have program scope and can be read and, provided they have not been defined as const, modified at any place in the program. External objects thus allow you to exchange information between any functions without passing any arguments. To demonstrate this point, the program on the opposite page has been divided into two separate source files. The string line, which has a global definition, is used to exchange data. Global objects that are not explicitly initialized during definition receive an initial value of 0 (that is, all bits = 0) by default. This also applies to objects belonging to class types, if not otherwise stipulated by the class. 䊐 Using Global Objects An object belonging to the extern storage class is initially only available in the source file where it was defined. If you need to use an object before defining it or in another module, you must first declare the object. If you do not declare the object, the compiler issues a message stating that the object is unknown. The declaration makes the name and type of the object known to the compiler. In contrast to a definition, the storage class identifier extern precedes the object name in a declaration. Example: extern long position; // Declaration This statement declares position as an external object of type long. The extern declaration thus allows you to “import” an object from another source file. A global object must be defined once, and once only, in a program. However, it can be declared as often as needed and at any position in the program. You will normally declare the object before the first function in a source file or in a header file that you can include when needed. This makes the object available to any functions in the file. Remember, if you declare the object within a code block, the object can only be used within the same block. An extern declaration only refers to an object and should therefore not be used to initialize the object. If you do initialize the object, you are defining that object! ✓ NOTE Global objects affect the whole program and should be used sparingly. Large programs in particular should contain no more than a few central objects defined as extern. 202 ■ CHAPTER 11 ■ ST0RAGE CLASSES AND NAMESPACES THE STORAGE CLASS static // Passw1.cpp // The functions getPassword() and timediff() // to read and examine a password. // ----------------------------------------------------#include #include #include #include using namespace std; long timediff(void); static string secret = "ISUS"; static long maxcount = 3, maxtime = 60; // Prototype // Password // Limits bool getPassword() // Enters and checks a password. { // Return value: true, if password is ok. bool ok_flag = false; // For return value string word; // For input int count = 0, time = 0; timediff(); // To start the stop watch while( ok_flag != true && ++count <= maxcount) // Number of attempts { cout << "\n\nInput the password: "; cin.sync(); // Clear input buffer cin >> setw(20) >> word; time += timediff(); if( time >= maxtime ) // Within time limit? break; // No! if( word != secret) cout << "Invalid password!" << endl; else ok_flag = true; // Give permission } return ok_flag; // Result } long timediff() // Returns the number of { // seconds after the last call. static long sec = 0; // Time of last call. long oldsec = sec; // Saves previous time. time( &sec); // Reads new time. return (sec - oldsec); // Returns the difference. } THE STORAGE CLASS static ■ 203 䊐 Static Objects If an object definition is preceded by the static keyword, the object belongs to the static storage class. Example: static int count; The most important characteristic of static objects is their static (or permanent) lifetime. Static objects are not placed on the stack, but are stored in the data area of a program just like external objects. However, in contrast to external objects, access to static objects is restricted. Two conditions apply, depending on where the object is defined: 1. Definition external to all program functions In this case, the object is external static, that is, the object can be designated using its name within the module only, but will not collide with any objects using the same name in other modules. ✓ NOTE In contrast to objects with an extern definition, the name of an external static object is unknown to the linker and thus retains its private nature within a module. 2. Definition within a code block This means that the object is internal static, that is, the object is only visible within a single block. However, the object is created only once and is not destroyed on leaving the block. On re-entering the block, you can continue to work with the original object. The same rules apply to initializing static objects as they do to external objects. If the object is not initialized explicitly, a default value of 0 applies. 䊐 Notes on the Sample Programs Opposite The function getPassword() checks a password that is entered. Permission is refused following three unsuccessful attempts or when 60 seconds have elapsed. You could use the following instructions to call the function in another source file: Example: if( !getPassword() ) cout << "No authorization!\n"; exit(1); The string secret and the thresholds maxcount and maxtime are external static, whereas the variable sec in the function timediff() is internal static. Its value is zero only when the function is first called. It makes sense to add a further function to these source files providing for password changes. 204 ■ CHAPTER 11 ■ ST0RAGE CLASSES AND NAMESPACES THE SPECIFIERS auto AND register Sample function with a register variable // // // // // // // // // // // // StrToL.cpp The function strToLong() converts a string containing a leading integer into an integer of type long. Argument: A string. Return value: An integer of type long. -------------------------------------------------The digits are interpreted with base 10. White spaces and a sign can precede the sequence of digits. The conversion terminates when the end of the string is reached or when a character that cannot be converted is reached. -------------------------------------------------- #include #include using namespace std; long strToLong( string str) { register int i = 0; long vz = 1, num = 0; // Type string // isspace() and isdigit() // Index // Sign and number // Ignore leading white spaces. for(i=0; i < str.size() && isspace(str[i]); ++i) ; // Is there a sign? if( i < str.size()) { if( str[i] == '+' ) { vz = 1; ++i; } if( str[i] == '-' ) { vz = ---1; ++i; } } // Sequence of digits -> convert to integer for( ; i < str.size() && isdigit(str[i]); ++i) num = num * 10 + (str[i] - '0'); return vz * num; } THE SPECIFIERS auto AND register ■ 205 䊐 auto Objects The storage class auto (automatic) includes all those objects defined within a function but without the static keyword. The parameters of a function are also auto objects. You can use the auto keyword during a definition. Example: auto float radius; // Equivalent to: // float radius; When the program flow reaches the definition, the object is created on the stack, but in contrast to a static type object, the object is destroyed on leaving the block. auto objects have no specific initial value if they are not initialized explicitly. However, objects belonging to a class type are normally initialized with default values, which can be specified in the class definition. 䊐 Using CPU Registers To increase the speed of a program, commonly used auto variables can be stored in CPU registers instead of on the stack. In this case, the register keyword is used to declare the object. A register is normally the size of an int variable. In other words, it only makes sense to define register variables if the variable is not too large, as in the case of types such as char, short, int or pointers. If you omit the type when defining a register variable, an int is assumed. However, the compiler can ignore the register keyword. The number of registers available for register variables depends on your hardware, although two registers are normally available. If a program defines too many register variables in a code block, the superfluous variables are placed in the auto storage class. 䊐 Sample Function The function strToLong() illustrates an algorithm that converts a sequence of digits to a binary number. This is useful if you need to perform calculations with a number contained in a string. The algorithm using the string "37" and the long variable num: Step 0: Step 1: Step 2: num = 0; 1st character '3' → number 3 = ('3'-'0') num = num * 10 + 3; // = 3 2nd character '7' → number 7 = ('7'-'0') num = num * 10 + 7; // = 37 This pattern is followed for every number in a longer string. 206 ■ CHAPTER 11 ■ ST0RAGE CLASSES AND NAMESPACES THE STORAGE CLASSES OF FUNCTIONS 䊐 Example of a Program Structure Source file 1 extern bool getPassword(void); // Prototype int main() { // The function permission(), // but not the function timediff() // can be called here. . . . } Source file 2 static long timediff(void); // Prototype bool getPassword(void) // Definition { // timediff() can be called here. . . . } static long timediff(void) { . . . } // Definition THE STORAGE CLASSES OF FUNCTIONS ■ 207 Only two storage classes are available for functions: extern and static. Functions with block scope are invalid: you cannot define a function within another function. The storage class of a function defines access to the function, as it does for an object. External functions have program scope, whereas static functions have file scope. 䊐 External Functions If the keyword static is not used when defining a function, the function must belong to the extern storage class. In a similar manner to external objects, external functions can be used at any position in a program. If you need to call a function before defining it, or in another source file, you will need to declare that function. Example: extern bool getPassword(void); // Prototype As previously seen, you can omit the extern keyword, since functions belong to the extern storage class by default. 䊐 Static Functions To define a static function, simply place the keyword static before the function header. Example: static long timediff() { . . . } Functions in the static storage class have “private” character: they have file scope, just like external static objects. They can only be called in the source file that defines them. The name of a static function will not collide with objects and functions of the same name in other modules. If you need to call a static function before defining it, you must first declare the function in the source file. Example: static long timediff( void ); The program structure opposite takes up the example with the functions getPassword() and timediff() once more. The function timediff() is an auxiliary function and not designed to be called externally. The function is declared as static for this reason. 208 ■ CHAPTER 11 ■ ST0RAGE CLASSES AND NAMESPACES NAMESPACES Defining namespaces // namesp1.cpp // Defines and tests namespaces. // ---------------------------------------------------#include // Class string defined within // namespace std namespace MySpace { std::string mess = "Within namespace MySpace"; int count = 0; // Definition: MySpace::count double f( double); // Prototype: MySpace::f() } namespace YourSpace { std::string mess = "Within namespace YourSpace"; void f( ) // Definition of { // YourSpace::f() mess += '!'; } } namespace MySpace // Back in MySpace. { int g(void); // Prototype of MySpace::g() double f( double y) // Definition of { // MySpace::f() return y / 10.0; } } int MySpace::g( ) // Separate definition { // of MySpace::g() return ++count; } #include // cout, ... within namespace std int main() { std::cout << "Testing namespaces!\n\n" << MySpace::mess << std::endl; MySpace::g(); std::cout << "\nReturn value g(): " << MySpace::g() << "\nReturn value f(): " << MySpace::f(1.2) << "\n---------------------" << std::endl; YourSpace::f(); std::cout << YourSpace::mess << std::endl; return 0; } NAMESPACES ■ 209 Using global names in large-scale software projects can lead to conflicts, especially when multiple class libraries are in operation. C++ provides for the use of namespaces in order to avoid naming conflicts with global identifiers. Within a namespace, you can use identifiers without needing to check whether they have been defined previously in an area outside of the namespace. Thus, the global scope is subdivided into isolated parts. A normal namespace is identified by a name preceded by the namespace keyword. The elements that belong to the namespace are then declared within braces. Example: namespace myLib { int count; double calculate(double, int); // . . . } This example defines the namespace myLib that contains the variable count and the function calculate(). Elements belonging to a namespace can be referenced directly by name within the namespace. If you need to reference an element from outside of the namespace, you must additionally supply the namespace. To do so, place the scope resolution operator, ::, before the element name. Example: myLib::count = 7; // Outside of myLib This allows you to distinguish between identical names in different namespaces. You can also use the scope resolution operator :: to reference global names, that is, names declared outside of any namespaces. To do so, simply omit the name of the namespace. This technique is useful when you need to access a global name that is hidden by an identical name defined in the current namespace. Example: ::demo(); // Not belonging to any namespace Be aware of the following when using namespaces: ■ ■ namespaces do not need to be defined contiguously. You can reopen and expand a namespace you defined previously at any point in the program namespaces can be nested, that is, you can define a namespace within another namespace. Global identifiers belonging to the C++ standard library automatically belong to the standard namespace std. 210 ■ CHAPTER 11 ■ ST0RAGE CLASSES AND NAMESPACES THE KEYWORD using Sample program // namesp2.cpp // Demonstrates the use of using-declarations and // using-directives. // ---------------------------------------------------#include // Namespace std void message() // Global function ::message() { std::cout << "Within function ::message()\n"; } namespace A { using namespace std; // Names of std are visible here void message() // Function A::message() { cout << "Within function A::message()\n"; } } namespace B { using std::cout; // void message(void); // } void B::message(void) // { cout << "Within function } int main() { using namespace std; using B::message; Declaring cout of std. Function B::message() Defining B::message() B::message()\n"; // Names of namespace std // Function name without // braces! cout << "Testing namespaces!\n"; cout << "\nCall of A::message()" << endl; A::message(); cout << "\nCall of B::message()" << endl; message(); // ::message() is hidden because // of the using-declaration. cout << "\nCall of::message()" << endl; ::message(); // Global function return 0; } THE KEYWORD using ■ 211 You can simplify access to the elements of a namespace by means of a using declaration or using directive. In this case, you do not need to repeatedly quote the namespace. Just like normal declarations, using declarations and using directives can occur at any part of the program. 䊐 using Declarations A using declaration makes an identifier from a namespace visible in the current scope. Example: using myLib::calculate; // Declaration You can then call the function calculate() from the myLib namespace. double erg = calculate( 3.7, 5); This assumes that you have not previously used the name calculate in the same scope. 䊐 using Directive The using directive allows you to import all the identifiers in a namespace. Example: using namespace myLib; This statement allows you to reference the identifiers in the myLib namespace directly. If myLib contains an additional namespace and a using directive, this namespace is also imported. If identical identifiers occur in the current namespace and an imported namespace, the using directive does not automatically result in a conflict. However, referencing an identifier can lead to ambiguities. In this case, you should use the scope resolution operator to resolve the situation. C++ header files without file extensions are used to declare the global identifiers in the standard namespace std. The using directive was used in previous examples to import any required identifiers to the global scope: Example: #include using namespace std; When developing large-scale programs or libraries, it is useful to declare the elements of any proprietary namespaces in header files. Normal source files are used to define these elements. ■ CHAPTER 11 ■ exe rc i se s 212 ST0RAGE CLASSES AND NAMESPACES EXERCISES Program listing for exercise 1 // scope.cpp // Accessing objects with equal names // --------------------------------------------------#include #include using namespace std; int var = 0; namespace Special { int var = 100; } int main() { int var = 10; cout << setw(10) << var; { int var = 20; cout << setw(10) << var << endl; { ++var; cout << setw(10) << var; cout << setw(10) << ++ ::var; cout << setw(10) << Special::var * 2 << endl; } cout << setw(10) << var - ::var; } cout << setw(10) << var << endl; return 0; } // 1. // 2. // 3. // 4. // 5. // 6. // 7. EXERCISES ■ 213 Exercise 1 In general, you should use different names for different objects. However, if you define a name for an object within a code block and the name is also valid for another object, you will reference only the new object within the code block. The new declaration hides any object using the same name outside of the block. When you leave the code block, the original object once more becomes visible. The program on the opposite page uses identical variable names in different blocks.What does the program output on screen? Exercise 2 You are developing a large-scale program and intend to use two commercial libraries, tool1 and tool2.The names of types, functions, macros, and so on are declared in the header files tool1.h and tool2.h for users of these libraries. Unfortunately, the libraries use the same global names in part. In order to use both libraries, you will need to define namespaces. Write the following program to simulate this situation: ■ Define an inline function called calculate() that returns the sum of two numbers for the header file tool1.h.The function interface is as follows: double calculate(double num1, double num2); ■ ■ Define an inline function called calculate() that returns the product of two numbers for a second header file tool2.h.This function has the same interface as the function in tool1.h. Then write a source file containing a main function that calls both functions with test values and outputs the results. To resolve potential naming conflicts, define the namespaces TOOL1 and TOOL2 that include the relevant header files. 214 ■ CHAPTER 11 ST0RAGE CLASSES AND NAMESPACES Program listing for exercise 3 // static.cpp // Tests an internal static variable // --------------------------------------------------#include #include using namespace std; double x = 0.5, fun(void); int main() { while( x < 10.0 ) { x += fun(); cout << " Within main(): " << setw(5) << x << endl; } return 0; } double fun() { static double x = 0; cout << " Within fun():" << setw(5) << x++; return x; } EXERCISES ■ 215 Exercise 3 Test your knowledge of external and static variables by reference to the program on the opposite page.What screen output does the program generate? Exercise 4 a. The function getPassword(), which checks password input, was introduced previously as an example of the use of static variables. Modify the source file Passw1.cpp, which contains the function getPassword(), by adding the function changePassword().This function allows the user to change his or her password. Save the modified source file as Passw2.cpp. b. A large-scale program with several users is used to perform bookings. Only authorized users, that is, users that have access to the password, are allowed to perform bookings. In the initial stages of program development, you need to test the functionality of the source file, Passw2.cpp.To do so, create a new source file with a main function that contains only the following menu items in its main loop: B = Booking E = End of program When B is typed, the password is first checked. If the user enters the correct password, he or she can change the password.The program does not need to perform any real bookings. ✓ NOTE The modified password is only available during runtime as it is not stored permanently. ■ CHAPTER 11 ■ solutions 216 ST0RAGE CLASSES AND NAMESPACES SOLUTIONS Exercise 1 Screen output of the program 10 21 20 20 1 10 200 Exercise 2 // // // // ---------------------------------------------------tool1.h Defining first function calculate() inline. ---------------------------------------------------- #ifndef _TOOL1_H_ #define _TOOL1_H_ inline double calculate( double num1, double num2) { return num1 + num2; } #endif // // // // // End of _TOOL1_H_ ---------------------------------------------------tool2.h Defining second function calculate() inline. ---------------------------------------------------- #ifndef _TOOL2_H_ #define _TOOL2_H_ inline double calculate( double num1, double num2) { return num1 * num2; } #endif // End of _TOOL2_H_ SOLUTIONS // // // // ■ -------------------------------------------------------tool_1_2.cpp Uses two "libraries" and tests name lookup conflicts. -------------------------------------------------------- #include namespace TOOL1 { #include "tool1.h" } namespace TOOL2 { #include "tool2.h" } #include int main() { using namespace std; double x = 0.5, y = 10.5, res = 0.0; cout << "Calling function of Tool1!" << endl; res = TOOL1::calculate( x, y); cout << "Result: " << res << "\n---------------------------------" << endl; cout << "Calling function of Tool2!" << endl; res = TOOL2::calculate( x, y); cout << "Result: " << res << endl; return 0; } Exercise 3 Screen output of the program In In In In fun(): fun(): fun(): fun(): 0 1 2 3 In In In In main(): main(): main(): main(): 1.5 3.5 6.5 10.5 217 218 ■ CHAPTER 11 ST0RAGE CLASSES AND NAMESPACES Exercise 4 // ----------------------------------------------------// Passw2.cpp // Defines the functions getPassword(), timediff() and // changePassword() to examine and change a password. // ----------------------------------------------------#include #include #include #include using namespace std; static long timediff(void); // Prototype static string secret = "guest"; // Password static long maxcount = 3, maxtime = 60; // Limits bool getPassword() { // As before. // . . . } // Read and verify a password. // Auxiliary function timediff() --> defining static static long timediff() // Returns the number of seconds { // since the last call. // As before. // . . . } bool changePassword() { string word1,word2; // Changes password. // Returns: true, if the // password has been changed // For input // To read a new password cout <<"\nEnter a new password (2 - 20 characters): "; cin.sync(); // Discards former input cin >> setw(20) >> word1; SOLUTIONS ■ if( word1.size() > 1) { cout << "\nEnter the password once more: "; cin >> setw(20) >> word2; if( word1 == word2) // Password confirmed? { // Yes! secret = word1; return true; } } return false; // No new password } // ----------------------------------------------------// Password.cpp // Testing the functions getPassword() and // changePassword(). // // After entering the password correctly (max. three // attempts within 60 seconds), the user can change it. // ----------------------------------------------------#include #include #include #include using namespace std; bool getPassword(void); // Read a password. bool changePassword(void); // Change a password. // Inline functions: inline void cls() { cout << "\033[2J"; } inline void go_on() { cout << "\n\nGo on with return! "; cin.sync(); cin.clear(); // Only new input while( cin.get() != '\n') ; } inline char getYesOrNo() // Read character Y or N. { char c = 0; cin.sync(); cin.clear(); // Just new input do { cin.get(c); c = toupper(c); // Permitting lower case letters also. } while( c != 'Y' && c != 'N'); return c; } 219 220 ■ CHAPTER 11 ST0RAGE CLASSES AND NAMESPACES static string header = "\n\n **** Test password handling ****\n\n"; static string menu = "\n\n B = Booking " "\n\n E = End of program" "\n\n Your choice: "; int main() { char choice = 0; while( choice != 'E') { cls(); cout << header << menu; // Header and Menu cin.get(choice); choice = toupper(choice); cls(); cout << header << endl; // Header switch( choice) { case 'B': // Booking if( !getPassword() ) { cout << "Access denied!" << endl; go_on(); } else { cout << "Welcome!\n\n" << "Do you want to change the password? (y/n)"; if( getYesOrNo() == 'Y') { if( changePassword() ) cout << "Password changed!" << endl; else cout << "Password unchanged!" << endl; go_on(); } // Place statements for booking here. } break; case 'E': cls(); cout << "\n Bye Bye!" << endl; break; } } // End of while return 0; } chapter 12 References and Pointers This chapter describes how to define references and pointers and how to use them as parameters and/or return values of functions. In this context, passing by reference and read-only access to arguments are introduced. 221 222 ■ CHAPTER 12 ■ REFERENCES AND POINTERS DEFINING REFERENCES Example float x = 10.7, &rx = x; Object names: The object in memory ... x, rx 10.7 ... Sample program // Ref1.cpp // Demonstrates the definition and use of references. // --------------------------------------------------#include #include using namespace std; float x = 10.7F; int main() { float &rx = x; // double &ref = x; // Global // Local reference to x // Error: different type! rx *= 2; cout << " x = " << x << endl // x = 21.4 << " rx = " << rx << endl; // rx = 21.4 const float& cref = x; // Read-only reference cout << "cref = " << cref << endl; // ok! // ++cref; // Error: read-only! const string str = "I am a constant string!"; // str = "That doesn't work!"; // Error: str constant! // string& text = str; // Error: str constant! const string& text = str; // ok! cout << text << endl; // ok! Just reading. return 0; } DEFINING REFERENCES ■ 223 A reference is another name, or alias, for an object that already exists. Defining a reference does not occupy additional memory. Any operations defined for the reference are performed with the object to which it refers. References are particularly useful as parameters and return values of functions. 䊐 Definition The ampersand character, &, is used to define a reference. Given that T is a type, T& denotes a reference to T. Example: float x = 10.7; float& rx = x; // or: float &rx = x; rx is thus a different way of expressing the variable x and belongs to the type “reference to float”. Operations with rx, such as Example: --rx; // equivalent to --x; will automatically affect the variable x. The & character, which indicates a reference, only occurs in declarations and is not related to the address operator &! The address operator returns the address of an object. If you apply this operator to a reference, it returns the address of the referenced object. Example: &rx // Address of x, thus is equal to &x A reference must be initialized when it is declared, and cannot be modified subsequently. In other words, you cannot use the reference to address a different variable at a later stage. 䊐 Read-Only References A reference that addresses a constant object must be a constant itself, that is, it must be defined using the const keyword to avoid modifying the object by reference. However, it is conversely possible to use a reference to a constant to address a non-constant object. Example: int a; const int& cref = a; // ok! The reference cref can be used for read-only access to the variable a, and is said to be a read-only identifier. A read-only identifier can be initialized by a constant, in contrast to a normal reference: Example: const double& pi = 3.1415927; Since the constant does not take up any memory space, the compiler creates a temporary object which is then referenced. 224 ■ CHAPTER 12 ■ REFERENCES AND POINTERS REFERENCES AS PARAMETERS Sample program // // // // Ref2.cpp Demonstrating functions with parameters of reference type. -------------------------------------------------- #include #include using namespace std; // Prototypes: bool getClient( string& name, long& nr); void putClient( const string& name, const long& nr); int main() { string clientName; long clientNr; cout << "\nTo input and output client data \n" << endl; if( getClient( clientName, clientNr)) // Calls putClient( clientName, clientNr); else cout << "Invalid input!" << endl; return 0; } bool getClient( string& name, long& nr) // Definition { cout << "\nTo input client data!\n" << " Name: "; if( !getline( cin, name)) return false; cout << " Number: "; if( !( cin >> nr)) return false; return true; } // Definition void putClient( const string& name, const long& nr) { // name and nr can only be read! cout << "\n-------- Client Data ---------\n" << "\n Name: "; cout << name << "\n Number: "; cout << nr << endl; } REFERENCES AS PARAMETERS ■ 225 䊐 Passing by Reference A pass by reference can be programmed using references or pointers as function parameters. It is syntactically simpler to use references, although not always permissible. A parameter of a reference type is an alias for an argument. When a function is called, a reference parameter is initialized with the object supplied as an argument. The function can thus directly manipulate the argument passed to it. Example: void test( int& a) { ++a; } Based on this definition, the statement test( var); // For an int variable var increments the variable var. Within the function, any access to the reference a automatically accesses the supplied variable, var. If an object is passed as an argument when passing by reference, the object is not copied. Instead, the address of the object is passed to the function internally, allowing the function to access the object with which it was called. 䊐 Comparison to Passing by Value In contrast to a normal pass by value an expression, such as a+b, cannot be used as an argument. The argument must have an address in memory and be of the correct type. Using references as parameters offers the following benefits: ■ ■ arguments are not copied. In contrast to passing by value, the run time of a program should improve, especially if the arguments occupy large amounts of memory a function can use the reference parameter to return multiple values to the calling function. Passing by value allows only one result as a return value, unless you resort to using global variables. If you need to read arguments, but not copy them, you can define a read-only reference as a parameter. Example: void display( const string& str); The function display() contains a string as an argument. However, it does not generate a new string to which the argument string is copied. Instead, str is simply a reference to the argument. The caller can rest assured that the argument is not modified within the function, as str is declared as a const. 226 ■ CHAPTER 12 ■ REFERENCES AND POINTERS REFERENCES AS RETURN VALUE Sample program // // // // Ref3.cpp Demonstrates the use of return values with reference type. -------------------------------------------------- #include #include using namespace std; // Returns a // reference to // the minimum. double& refMin( double&, double&); int main() { double x1 = 1.1, x2 = x1 + 0.5, y; y = refMin( x1, x2); // Assigns the minimum to y. cout << "x1 = " << x1 << " " << "x2 = " << x2 << endl; cout << "Minimum: " << y << endl; ++refMin( x1, cout << "x1 = << "x2 = ++refMin( x1, cout << "x1 << "x2 refMin( x1, cout << "x1 << "x2 refMin( x1, cout << "x1 << "x2 return 0; x2); // ++x1, as x1 is minimal " << x1 << " " // x1 = 2.1 " << x2 << endl; // x2 = 1.6 x2); // ++x2, because x2 is // the minimum. = " << x1 << " " // x1 = 2.1 = " << x2 << endl; // x2 = 2.6 x2) = 10.1; // x1 = 10.1, because // x1 is the minimum. = " << x1 << " " // x1 = 10.1 = " << x2 << endl; // x2 = 2.6 x2) += 5.0; // x2 += 5.0, because // x2 is the minimum. = " << x1 << " " // x1 = 10.1 = " << x2 << endl; // x2 = 7.6 } double& refMin( double& a, double& b) { return a <= b ? a : b; } ✓ // Returns a // reference to // the minimum. NOTE The expression refMin(x1,x2) represents either the object x1 or the object x2, that is, the object containing the smaller value. REFERENCES AS RETURN VALUE ■ 227 䊐 Returning References The return type of a function can also be a reference type. The function call then represents an object, and can be used just like an object. Example: string& message() // Reference! { static string str = "Today only cold cuts!"; return str; } This function returns a reference to a static string, str. Pay attention to the following point when returning references and pointers: The object referenced by the return value must exist after leaving the function. It would be a critical error to declare the string str as a normal auto variable in the function message(). This would destroy the string on leaving the function and the reference would point to an object that no longer existed. 䊐 Calling a Reference Type Function The function message() (mentioned earlier in this section) is of type “reference to string.” Thus, calling message() represents a string type object, and the following statements are valid: message() = "Let's go to the beer garden!"; message() += " Cheers!"; cout << "Length: " << message().length(); In these examples, a new value is first assigned to the object referenced by the function call. Then a new string is appended before the length of the referenced string is output in the third statement. If you want to avoid modifying the referenced object, you can define the function type as a read-only reference. Example: const string& message(); // Read-only! References are commonly used as return types when overloading operators. The operations that an operator has to perform for a user-defined type are always implemented by an appropriate function. Refer to the chapters on overloading operators later in this book for more details. However, examples with operators from standard classes can be provided at this point. 228 ■ CHAPTER 12 ■ REFERENCES AND POINTERS EXPRESSIONS WITH REFERENCE TYPE Example: Operator << of class ostream cout << "Good morning" << '!'; cout << "Good morning" Reference to cout <<'!'; Sample assignments of class string // Ref4.cpp // Expressions with reference type exemplified by // string assignments. // -------------------------------------------------#include #include #include // For toupper() using namespace std; void strToUpper( string& ); // Prototype int main() { string text("Test with assignments \n"); strToUpper(text); cout << text << endl; strToUpper( text = "Flowers"); cout << text << endl; strToUpper( text += " cheer you up!\n"); cout << text << endl; return 0; } void strToUpper( string& str) // Converts the content { // of str to uppercase. int len = str.length(); for( int i=0; i < len; ++i) str[i] = toupper( str[i]); } EXPRESSIONS WITH REFERENCE TYPE ■ 229 Every C++ expression belongs to a certain type and also has a value, if the type is not void. Reference types are also valid for expressions. 䊐 The Stream Class Shift Operators The << and >> operators used for stream input and output are examples of expressions that return a reference to an object. Example: cout << " Good morning " This expression is not a void type but a reference to the object cout, that is, it represents the object cout. This allows you to repeatedly use the << on the expression: cout << "Good morning" << '!' The expression is then equivalent to (cout << " Good morning ") << '!' Expressions using the << operator are composed from left to right, as you can see from the table of precedence contained in the appendix. Similarly, the expression cin >> variable represents the stream cin. This allows repeated use of the >> operator. Example: int a; double x; cin >> a >> x; // (cin >> a) >> x; 䊐 Other Reference Type Operators Other commonly used reference type operators include the simple assignment operator = and compound assignments, such as += and *=. These operators return a reference to the operand on the left. In an expression such as a = b or a += b a must therefore be an object. In turn, the expression itself represents the object a. This also applies when the operators refer to objects belonging to class types. However, the class definition stipulates the available operators. For example, the assignment operators = and += are available in the standard class string. Example: string name("Jonny "); name += "Depp"; //Reference to name Since an expression of this type represents an object, the expression can be passed as an argument to a function that is called by reference. This point is illustrated by the example on the opposite page. 230 ■ CHAPTER 12 ■ REFERENCES AND POINTERS DEFINING POINTERS Sample program // pointer1.cpp // Prints the values and addresses of variables. // -------------------------------------------------#include using namespace std; int var, *ptr; // Definition of variables var and ptr int main() { var = 100; ptr = &var; // Outputs the values and addresses // of the variables var and ptr. cout << " Value of var: " << var << " Address of var: " << &var << endl; cout << " Value of ptr: " << ptr << " Address of ptr: " << &ptr << endl; return 0; } Sample screen output Value of var: 100 Value of ptr: 00456FD4 Address of var: 00456FD4 Address of ptr: 00456FD0 The variables var and ptr in memory Variable Value of the Variable Address (hexadecimal) ... var 100 456FD4 ptr 456FD4 456FD0 ... DEFINING POINTERS ■ 231 Efficient program logic often requires access to the memory addresses used by a program’s data, rather than manipulation of the data itself. Linked lists or trees whose elements are generated dynamically at runtime are typical examples. 䊐 Pointers A pointer is an expression that represents both the address and type of another object. Using the address operator, &, for a given object creates a pointer to that object. Given that var is an int variable, Example: &var // Address of the object var is the address of the int object in memory and thus a pointer to var. A pointer points to a memory address and simultaneously indicates by its type how the memory address can be read or written to. Thus, depending on the type, we refer to pointers to char, pointers to int, and so on, or use an abbreviation, such as char pointer, int pointer, and so on. 䊐 Pointer Variables An expression such as &var is a constant pointer; however, C++ allows you to define pointer variables, that is, variables that can store the address of another object. Example: int *ptr; // or: int* ptr; This statement defines the variable ptr, which is an int* type (in other words, a pointer to int). ptr can thus store the address of an int variable. In a declaration, the star character * always means “pointer to.” Pointer types are derived types. The general form is T*, where T can be any given type. In the above example T is an int type. Objects of the same base type T can be declared together. Example: int a, *p, &r = a; // Definition of a, p, r After declaring a pointer variable, you must point the pointer at a memory address. The program on the opposite page does this using the statement ptr = &var;. 䊐 References and Pointers References are similar to pointers: both refer to an object in memory. However, a pointer is not merely an alias but an individual object that has an identity separate from the object it references. A pointer has its own memory address and can be manipulated by pointing it at a new memory address and thus referencing a different object. 232 ■ CHAPTER 12 REFERENCES AND POINTERS ■ THE INDIRECTION OPERATOR Using the indirection operator double x, y, *px; px *px *px y = &x; = 12.3; += 4.5; = sin(*px); // // // // Let px point to x. Assign the value 12.3 to x Increment x by 4.5. To assign sine of x to y. Address and value of the variables x and px px x Address of px Address of x = value of px Value of x &px &x px x *px Notes on addresses in a program ■ ■ ■ Each pointer variable occupies the same amount of space, independent of the type of object it references. That is, it occupies as much space as is necessary to store an address. On a 32-bit computer, such as a PC, this is four bytes. The addresses visible in a program are normally logic addresses that are allocated and mapped to physical addresses by the system. This allows for efficient storage management and the swapping of currently unused memory blocks to the hard disk. C++ guarantees that any valid address will not be equal to 0. Thus, the special value 0 is used to indicate an error. For pointers, the symbolic constant NULL is defined as 0 in standard header files. A pointer containing the value NULL is also called NULL pointer. THE INDIRECTION OPERATOR ■ 233 䊐 Using Pointers to Access Objects The indirection operator * is used to access an object referenced by a pointer: Given a pointer, ptr, *ptr is the object referenced by ptr. As a programmer, you must always distinguish between the pointer ptr and the addressed object *ptr. Example: long a = 10, b, *ptr; ptr = &a; b = *ptr; // Definition of a, b // and pointer ptr. // Let ptr point to a. This assigns the value of a to b, since ptr points to a. The assignment b = a; would return the same result. The expression *ptr represents the object a, and can be used wherever a could be used. The star character * used for defining pointer variables is not an operator but merely imitates the later use of the pointer in expressions. Thus, the definition long *ptr; has the following meaning: ptr is a long* (pointer to long) type and *ptr is a long type. The indirection operator * has high precedence, just like the address operator &. Both operators are unary, that is, they have only one operand. This also helps distinguish the redirection operator from the binary multiplication operator *, which always takes two operands. 䊐 L-values An expression that identifies an object in memory is known as an L-value in C++. The term L-value occurs commonly in compiler error messages and is derived from the assignment. The left operand of the = operator must always designate a memory address. Expressions other than an L-value are often referred to as R-values. A variable name is the simplest example of an L-value. However, a constant or an expression, such as x + 1, is an R-value. The indirection operator is one example of an operator that yields L-values. Given a pointer variable p, both p and *p are L-values, as *p designates the object to which p points. 234 ■ CHAPTER 12 ■ REFERENCES AND POINTERS POINTERS AS PARAMETERS Sample function // pointer2.cpp // Definition and call of function swap(). // Demonstrates the use of pointers as parameters. // ---------------------------------------------------#include using namespace std; void swap( float *, float *); // Prototype of swap() int main() { float x = 11.1F; float y = 22.2F; . . . swap( &x, &y ); . . // p2 = &y . } // p1 = &x void swap( float *p1, float *p2) { float temp; // Temporary variable temp = *p1; *p1 = *p2; *p2 = temp; } // At the above call p1 points // to x and p2 to y. POINTERS AS PARAMETERS ■ 235 䊐 Objects as Arguments If an object is passed as an argument to a function, two possible situations occur: ■ ■ the parameter in question is the same type as the object passed to it. The function that is called is then passed a copy of the object (passing by value) the parameter in question is a reference. The parameter is then an alias for the argument, that is, the function that is called manipulates the object passed by the calling function (passing by reference). In the first case, the argument passed to the function cannot be manipulated by the function. This is not true for passing by reference. However, there is a third way of passing by reference—passing pointers to the function. 䊐 Pointers as Arguments How do you declare a function parameter to allow an address to be passed to the function as an argument? The answer is quite simple: The parameter must be declared as a pointer variable. If, for example, the function func() requires the address of an int value as an argument, you can use the following statement Example: long func( int *iPtr ) { // Function block } to declare the parameter iPtr as an int pointer. If a function knows the address of an object, it can of course use the indirection operator to access and manipulate the object. In the program on the opposite page, the function swap() swaps the values of the variables x and y in the calling function. The function swap() is able to access the variables since the addresses of these variables, that is &x and &y, are passed to it as arguments. The parameters p1 and p2 in swap() are thus declared as float pointers. The statement swap( &x, &y); initializes the pointers p1 and p2 with the addresses of x or y. When the function manipulates the expressions *p1 and *p2, it really accesses the variables x and y in the calling function and exchanges their values. ■ CHAPTER 12 ■ exercise s 236 REFERENCES AND POINTERS EXERCISES Listing for exercise 3 // A version of swap() with incorrect logic. // Find the error! void swap(float *p1, float *p2) { float *temp; // Temporary variable temp = p1; p1 = p2; p2 = temp; } Solutions of quadratic equations The quadratic equation: a*x2 + b*x + c = 0 has real solutions: x12 = (-b ± √(b2 - 4ac)) / 2a if the discriminant satisfies: b2 -4ac >= 0 If the value of (b2 - 4ac) is negative, no real solution exists. Test values Quadratic Equation Solutions 2x2 x1 = 1.5, X1 = 3.0, - 2x - 1.5 = 0 x2 - 6x + 9 = 0 2x2 + 2 = 0 none x2 = -0.5 x2 = 3.0 EXERCISES ■ 237 Exercise 1 What happens if the parameter in the sample function strToUpper() is declared as a string& instead of a string? Exercise 2 Write a void type function called circle()to calculate the circumference and area of a circle.The radius and two variables are passed to the function, which therefore has three parameters: Parameters: ✓ A read-only reference to double for the radius and two references to double that the function uses to store the area and circumference of the circle. NOTE Given a circle with radius r: Area = π * r * r and circumference = 2 * π * r where π = 3.1415926536 Test the function circle() by outputting a table containing the radius, the circumference, and the area for the radii 0.5, 1.0, 1.5, . . . , 10.0. Exercise 3 a. The version of the function swap() opposite can be compiled without producing any error messages. However, the function will not swap the values of x and y when swap(&x,&y); is called.What is wrong? b. Test the correct pointer version of the function swap() found in this chapter.Then write and test a version of the function swap() that uses references instead of pointers. Exercise 4 Create a function quadEquation() that calculates the solutions to quadratic equations.The formula for calculating quadratic equations is shown opposite. Arguments: Returns: The coefficients a, b, c and two pointers to both solutions. false, if no real solution is available, otherwise true. Test the function by outputting the quadratic equations on the opposite page and their solutions. ■ CHAPTER 12 ■ solutions 238 REFERENCES AND POINTERS SOLUTIONS Exercise 1 The call to function strToUpper() is left unchanged. But instead of passing by reference, a passing by value occurs, i.e., the function manipulates a local copy. Thus, only a local copy of the string is changed in the function, but the string in the calling function remains unchanged. Exercise 2 // ---------------------------------------------------// circle.cpp // Defines and calls the function circle(). // ---------------------------------------------------#include #include #include using namespace std; // Prototype of circle(): void circle( const double& rad, double& um, double& fl); const double startRadius = 0.5, endRadius = 10.0, step = 0.5; string header = "\n line( 50, '-'); // Start, end and // step width of // the table ***** Computing Circles ***** \n", int main() { double rad, circuit, plane; cout << header << endl; cout << setw(10) << "Radius" << setw(20) << "Circumference" << setw(20) << "Area\n" << line << endl; cout << fixed; // Floating point presentation for( rad = startRadius; rad < endRadius + step/2; rad += step) { circle( rad, circuit, plane); cout << setprecision(1)<< setw(8) << rad << setprecision(5)<< setw(22) << circuit << setw(20) << plane < using namespace std; void swap( float*, float*); void swap( float&, float&); // Prototypes of swap() int main() { float x = 11.1F; float y = 22.2F; cout << "x and y before swapping: << x << " " << y << endl; swap( &x, &y); " // Call pointer version. cout << "x and y after 1. swapping: " << x << " " << y << endl; swap( x, y); // Call reference version. cout << "x and y after 2. swapping: " << x << " " << y << endl; return 0; } void swap(float *p1, float *p2) { float temp; temp = *p1; *p1 = *p2; *p2 = temp; } // Pointer version // Temporary variable // Above call points p1 // to x and p2 to y. 239 240 ■ CHAPTER 12 REFERENCES AND POINTERS void swap(float& a, float& b) { float temp; temp = a; a = b; b = temp; // Reference version // Temporary variable // For above call // a equals x and b equals y } Exercise 4 // // // // // // // // ---------------------------------------------------quadEqu.cpp Defines and calls the function quadEquation(), which computes the solutions of quadratic equations a*x*x + b*x + c = 0 The equation and its solutions are printed by the function printQuadEquation(). ---------------------------------------------------- #include #include #include #include using namespace std; // For the square root sqrt() string header = " *** Solutions of Quadratic Equations ***\n", line( 50, '-'); // ----- Prototypes ----// Computing solutions: bool quadEquation( double a, double b, double c, double* x1Ptr, double* x2Ptr); // Printing the equation and its solutions: void printQuadEquation( double a, double b, double c); int main() { cout << header << endl; printQuadEquation( 2.0, -2.0, -1.5); printQuadEquation( 1.0, -6.0, 9.0); printQuadEquation( 2.0, 0.0, 2.0); return 0; } SOLUTIONS ■ // Prints the equation and its solutions: void printQuadEquation( double a, double b, double c) { double x1 = 0.0, x2 = 0.0; // For solutions cout << << << << line << '\n' "\nThe quadratic equation:\n\t " a << "*x*x + " << b << "*x + " << endl; c << " = 0" if( quadEquation( a, b, c, &x1, &x2) ) { cout << "has real solutions:" << "\n\t x1 = " << x1 << "\n\t x2 = " << x2 << endl; } else cout << "has no real solutions!" << endl; cout << "\nGo on with return. \n\n"; cin.get(); } bool quadEquation( double a, double b, double c, double* x1Ptr, double* x2Ptr) // Computes the solutions of the quadratic equation: // a*x*x + b*x + c = 0 // Stores the solutions in the variables to which // x1Ptr and x2Ptr point. // Returns: true, if a solution exists, // otherwise false. { bool return_flag = false; double help = b*b - 4*a*c; if( help >= 0) { help = sqrt( help); // There are real solutions. *x1Ptr = (-b + help) / (2*a); *x2Ptr = (-b - help) / (2*a); return_flag = true; } return return_flag; } 241 This page intentionally left blank chapter 13 Defining Classes This chapter describes how classes are defined and how instances of classes, that is, objects, are used. In addition, structs and unions are introduced as examples of special classes. 243 244 ■ CHAPTER 13 ■ DEFINING CLASSES THE CLASS CONCEPT Real World A Car Abstraction Class CAR Properties (Data Members): Date when built Capacity (PS) Serial number ... Methods (Member functions): to run, to brake, to park, to turn off ... Instantiation Objects car1 car2 Properties: Date when built = 1990 Capacity = 100 Chassis number = 11111 ... Properties: Date when built = 2000 Capacity = 200 Chassis number = 22222 ... Methods ... Methods ... ••• THE CLASS CONCEPT ■ 245 Classes are the language element in C++ most important to the support object-oriented programming (OOP). A class defines the properties and capacities of an object. 䊐 Data Abstraction Humans use abstraction in order to manage complex situations. Objects and processes are reduced to basics and referred to in generic terms. Classes allow more direct use of the results of this type of abstraction in software development. The first step towards solving a problem is analysis. In object-oriented programming, analysis comprises identifying and describing objects and recognizing their mutual relationships. Object descriptions are the building blocks of classes. In C++, a class is a user-defined type. It contains data members, which describe the properties of the class, and member functions, or methods, which describe the capacities of the objects. Classes are simply patterns used to instantiate, or create, objects of the class type. In other words, an object is a variable of a given class. 䊐 Data Encapsulation When you define a class, you also specify the private members, that is, the members that are not available for external access, and the public members of that class. An application program accesses objects by using the public methods of the class and thus activating its capacities. Access to object data is rarely direct, that is, object data is normally declared as private and then read or modified by methods with public declarations to ensure correct access to the data. One important aspect of this technique is the fact that application programs need not be aware of the internal structure of the data. If needed, the internal structure of the program data can even be modified. Provided that the interfaces of the public methods remain unchanged, changes like these will not affect the application program. This allows you to enhance an application by programming an improved class version without changing a single byte of the application. An object is thus seen to encapsulate its private structure, protecting itself from external influences and managing itself by its own methods. This describes the concept of data encapsulation concisely. 246 ■ CHAPTER 13 ■ DEFINING CLASSES DEFINING CLASSES Definition scheme class Demo { private: // Private data members and methods here public: // Public data members and methods here }; Example of a class // account.h // Defining the class Account. // --------------------------------------------------#ifndef _ACCOUNT_ // Avoid multiple inclusions. #define _ACCOUNT_ #include #include using namespace std; class Account { private: string name; unsigned long nr; double balance; // Sheltered members: // Account holder // Account number // Account balance public: //Public interface: bool init( const string&, unsigned long, double); void display(); }; #endif // _ACCOUNT_ DEFINING CLASSES ■ 247 A class definition specifies the name of the class and the names and types of the class members. The definition begins with the keyword class followed by the class name. The data members and methods are then declared in the subsequent code block. Data members and member functions can belong to any valid type, even to another previously defined class. At the same time, the class members are divided into: ■ ■ private members, which cannot be accessed externally public members, which are available for external access. The public members form the so-called public interface of the class. The opposite page shows a schematic definition of a class. The private section generally contains data members and the public section contains the access methods for the data. This provides for data encapsulation. The following example includes a class named Account used to represent a bank account. The data members, such as the name of the account holder, the account number, and the account balance, are declared as private. In addition, there are two public methods, init() for initialization purposes and display(), which is used to display the data on screen. The labels private: and public: can be used at the programmer’s discretion within a class: ■ ■ you can use the labels as often as needed, or not at all, and in any order. A section marked as private: or public: is valid until the next public: or private: label occurs the default value for member access is private. If you omit both the private and public labels, all the class members are assumed to be private. 䊐 Naming Every piece of software uses a set of naming rules. These rules often reflect the target platform and the class libraries used. For the purposes of this book, we decided to keep to standard naming conventions for distinguishing classes and class members. Class names begin with an uppercase letter and member names with a lowercase letter. Members of different classes can share the same name. A member of another class could therefore also be named display(). 248 ■ CHAPTER 13 ■ DEFINING CLASSES DEFINING METHODS Methods of class Account // account.cpp // Defines methods init() and display(). // --------------------------------------------------#include "account.h" // Class definition #include #include using namespace std; // The method init() copies the given arguments // into the private members of the class. bool Account::init(const string& i_name, unsigned long i_nr, double i_balance) { if( i_name.size() < 1) // No empty name return false; name = i_name; nr = i_nr; balance = i_balance; return true; } // The method display() outputs private data. void Account::display() { cout << fixed << setprecision(2) << "--------------------------------------\n" << "Account holder: " << name << '\n' << "Account number: " << nr << '\n' << "Account balance: " << balance << '\n' << "--------------------------------------\n" << endl; } DEFINING METHODS ■ 249 A class definition is not complete without method definitions. Only then can the objects of the class be used. 䊐 Syntax When you define a method, you must also supply the class name, separating it from the function name by means of the scope resolution operator ::. Syntax: type class_name::function_name(parameter_list) { . . . } Failure to supply the class name results in a global function definition. Within a method, all the members of a class can be designated directly using their names. The class membership is automatically assumed. In particular, methods belonging to the same class can call each other directly. Access to private members is only possible within methods belonging to the same class. Thus, private members are completely controlled by the class. Defining a class does not automatically allocate memory for the data members of that class. To allocate memory, you must define an object. When a method is called for a given object, the method can then manipulate the data of this object. 䊐 Modular Programming A class is normally defined in several source files. In this case, you will need to place the class definition in a header file. If you place the definition of the class Account in the file Account.h, any source file including the header file can use the class Account. Methods must always be defined within a source file. This would mean defining the methods for the class Account in a source file named Account.cpp, for example. The source code of the application program, for example, the code containing the function main, is independent of the class and can be stored in separate source files. Separating classes from application programs facilitates re-use of classes. In an integrated development environment, a programmer will define a project to help manage the various program modules by inserting all the source files into the project. When the project is compiled and linked, modified source files are automatically re-compiled and linked to the application program. 250 ■ CHAPTER 13 ■ DEFINING CLASSES DEFINING OBJECTS The objects current and savings in memory savings name "Cheers, Mary" nr 1234567 balance 2002.22 current name "Dylan, Bob" nr 87654321 balance –1300.13 DEFINING OBJECTS ■ 251 Defining a class also defines a new type for which variables, that is, objects, can be defined. An object is also referred to as an instance of a class. 䊐 Defining Objects An object is defined in the usual way by supplying the type and the object name. Syntax: class_name object_name1 [, object_name2,...] The following statement defines an object current of type Account: Example: Account current; // or: class Account ... Memory is now allocated for the data members of the current object. The current object itself contains the members name, nr, and balance. 䊐 Objects in Memory If multiple objects of the same class type are declared, as in Example: Account current, savings; each object has its own data members. Even the object savings contains the members name, nr, and balance. However, these data members occupy a different position in memory than the data members belonging to current. The same methods are called for both objects. Only one instance of the machine code for a method exists in memory—this applies even if no objects have been defined for the class. A method is always called for a particular instance and then manipulates the data members of this object. This results in the memory content as shown on the opposite page, when the method init() is called for each object with the values shown. 䊐 Initializing Objects The objects belonging to the Account class were originally defined but not initialized. Each member object is thus defined but not explicitly initialized. The string name, is empty, as it is thus defined in the class string. The initial values of the members nr and balance are unknown, however. As is the case for other variables, these data members will default to 0 if the object is declared global or static. You can define exactly how an object is created and destroyed. These tasks are performed by constructors and destructors. Constructors are specifically responsible for initializing objects—more details are given later. 252 ■ CHAPTER 13 ■ DEFINING CLASSES USING OBJECTS Sample program // account_t.cpp // Uses objects of class Account. // --------------------------------------------------#include "Account.h" int main() { Account current1, current2; current1.init("Cheers, Mary", 1234567, -1200.99); current1.display(); // current1.balance += 100; // Error: private member current2 = current1; current2.display(); // ok: Assignment of // objects is possible. // ok // New values for current2 current2.init("Jones, Tom", 3512347, 199.40); current2.display(); Account& mtr = current1; mtr.display(); return 0; } // // // // // To use a reference: mtr is an alias name for object current1. mtr can be used just as object current1. USING OBJECTS ■ 253 䊐 Class Member Access Operator An application program that manipulates the objects of a class can access only the public members of those objects. To do so, it uses the class member access operator (in short: dot operator). Syntax: object.member Where member is a data member or a method. Example: Account current; current.init("Jones, Tom",1234567,-1200.99); The expression current.init represents the public method init of the Account class. This method is called with three arguments for current. The init() call cannot be replaced by direct assignments. Example: current.name = "Dylan, Bob"; current.nr = 1234567; current.balance = -1200.99; // Error: // private // members Access to the private members of an object is not permissible outside the class. It is therefore impossible to display single members of the Account class on screen. Example: cout << current.balance; current.display(); // Error // ok The method display() displays all the data members of current. A method such as display() can only be called for one object. The statement display(); would result in an error message, since there is no global function called display(). What data would the function have to display? 䊐 Assigning Objects The assignment operator = is the only operator that is defined for all classes by default. However, the source and target objects must both belong to the same class. The assignment is performed to assign the individual data members of the source object to the corresponding members of the target object. Example: Account current1, current2; current2.init("Marley, Bob",350123, 1000.0); current1 = current2; This copies the data members of current2 to the corresponding members of current1. 254 ■ CHAPTER 13 ■ DEFINING CLASSES POINTERS TO OBJECTS Sample program // ptrObj.cpp // Uses pointers to objects of class Account. // --------------------------------------------------#include "Account.h" // Includes , bool getAccount( Account *pAccount); // Prototype int main() { Account current1, current2, *ptr = ¤t1; ptr->init("Cheer, Mary", 3512345, 99.40); ptr->display(); ptr = ¤t2; // if( getAccount( ptr)) // ptr->display(); // else cout << "Invalid input!" return 0; // current1.init(...) // current1.display() Let ptr point to current2 Input and output a new account. << endl; } // -------------------------------------------------// getAccount() reads data for a new account // and adds it into the argument. bool getAccount( Account *pAccount ) { string name, line(50,'-'); // Local variables unsigned long nr; double startcapital; cout << line << '\n' << "Enter data for a new account: \n" << "Account holder: "; if( !getline(cin,name) || name.size() == 0) return false; cout << "Account number: "; if( !(cin >> nr)) return false; cout << "Starting capital: "; if( !(cin >> startcapital)) return false; // All input ok pAccount->init( name, nr, startcapital); return true; } POINTERS TO OBJECTS ■ 255 An object of a class has a memory address—just like any other object. You can assign this address to a suitable pointer. Example: Account savings("Mac, Rita",654321, 123.5); Account *ptrAccount = &savings; This defines the object savings and a pointer variable called ptrAccount. The pointer ptrAccount is initialized so that it points to the object savings. This makes *ptrAccount the object savings itself. You can then use the statement Example: (*ptrAccount).display(); to call the method display() for the object savings. Parentheses must be used in this case, as the operator . has higher precedence than the * operator. 䊐 Arrow Operator You can use the class member access operator -> (in short: arrow operator) instead of a combination of * and . . Syntax: objectPointer->member This expression is equivalent to (*objectPointer).member The operator -> is made up of a minus sign and the greater than sign. Example: ptrAccount->display(); This statement calls the method display() for the object referenced by ptrAccount, that is, for the object savings. The statement is equivalent to the statement in the previous example. The difference between the class member access operators . and -> is that the left operand of the dot operator must be an object, whereas the left operand of the arrow operator must be a pointer to an object. 䊐 The Sample Program Pointers to objects are often used as function parameters. A function that gets the address of an object as an argument can manipulate the referenced object directly. The example on the opposite page illustrates this point. It uses the function getAccount() to read the data for a new account. When called, the address of the account is passed: getAccount(ptr) // or: getAccount(¤t1) The function can then use the pointer ptr and the init() method to write new data to the referenced object. 256 ■ CHAPTER 13 ■ DEFINING CLASSES structs Sample program // structs.cpp // Defines and uses a struct. // --------------------------------------------------#include #include #include using namespace std; struct Representative // Defining struct Representative { string name; // Name of a representative. double sales; // Sales per month. }; inline void print( const Representative& v) { cout << fixed << setprecision(2) << left << setw(20) << v.name << right << setw(10) << v.sales << endl; } int main() { Representative rita, john; rita.name = "Strom, Rita"; rita.sales = 37000.37; john.name = "Quick, John"; john.sales = 23001.23; rita.sales += 1700.11; // More Sales cout << " Representative Sales\n" << "-------------------------------" << endl; print( rita); print( john); cout << "\nTotal of sales: " << rita.sales + john.sales << endl; Representative *ptr = &john; // Pointer ptr. // Who gets the if( john.sales < rita.sales) // most sales? ptr = &rita; cout << "\nSalesman of the month: " << ptr->name << endl; // Representative's name // pointed to by ptr. return 0; } structs ■ 257 䊐 Records In a classical, procedural language like C, multiple data that belong together logically are put together to form a record. Extensive data such as the data for the articles in an automobile manufacturer’s stocks can be organized for ease of viewing and stored in files. From the viewpoint of an object-oriented language, a record is merely a class containing only public data members and no methods. Thus, you can use the class keyword to define the structure of a record in C++. Example: class Date { public: short month, day, year; }; However, it is common practice to use the keyword struct, which is also available in the C programming language, to define records. The above definition of Date with the members day, month, and year is thus equivalent to: Example: struct Date { short month, day, year; }; 䊐 The Keywords class and struct You can also use the keyword struct to define a class, such as the class Account. Example: struct Account { private: public: // // . . . . . . as before }; The keywords class and struct only vary with respect to data encapsulation; the default for access to members of a class defined as a struct is public. In contrast to a class defined using the class keyword, all the class members are public unless a private label is used. This allows the programmer to retain C compatibility. Example: Date future; future.year = 2100; // ok! Public data Records in the true sense of the word, that is, objects of a class containing only public members, can be initialized by means of a list during definition. Example: Date birthday = { 1, 29, 1987}; The first element in the list initializes the first data member of the object, and so on. 258 ■ CHAPTER 13 ■ DEFINING CLASSES UNIONS An object of union WordByte in memory Low byte b[0] w (16 bit word) High byte b[1] Defining and using union WordByte // unions.cpp // Defines and uses a union. // --------------------------------------------------#include using namespace std; union WordByte { private: unsigned short w; // 16 bits unsigned char b[2]; // Two bytes: b[0], b[1] public: // Word- and byte-access: unsigned short& word() { return w; } unsigned char& lowByte() { return b[0]; } unsigned char& highByte(){ return b[1]; } }; int main() { WordByte wb; wb.word() = 256; cout << "\nWord: " << (int)wb.word(); cout << "\nLow-byte: " << (int)wb.lowByte() << "\nHigh-byte: " << (int)wb.highByte() << endl; return 0; } Screen output of the program Word: 256 Low-Byte: 0 High-Byte: 1 UNIONS ■ 259 䊐 Memory Usage In normal classes, each data member belonging to an object has its own separate memory space. However, a union is a class whose members are stored in the same memory space. Each data member has the same starting address in memory. Of course, a union cannot store various data members at the same address simultaneously. However, a union does provide for more versatile usage of memory space. 䊐 Definition Syntactically speaking, a union is distinguished from a class defined as a class or struct only by the keyword union. Example: union Number { long n; double x; }; Number number1, number2; This example defines the union Number and two objects of the same type. The union Number can be used to store either integral or floating-point numbers. Unless a private label is used, all union members are assumed to be public. This is similar to the default setting for structures. This allows direct access to the members n and x in the union Number. Example: number1.n = 12345; // Storing an integer number1.n *= 3; number2.x = 2.77; // and multiply by 3. // Floating point number The programmer must ensure that the current content of the union is interpreted correctly. This is normally achieved using an additional type field that identifies the current content. The size of a union type object is derived from the longest data member, as all data members begin at the same memory address. If we look at our example, the union Number, this size is defined by the double member, which defaults to 8 == sizeof(double) byte. The example opposite defines the union WordByte that allows you to read or write to a 16-bit memory space byte for byte or as a unit. ■ CHAPTER 13 ■ exercise 260 DEFINING CLASSES EXERCISE Struct tm in header file ctime struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; // // // // // // // // // 0 - 59(60) 0 - 59 0 - 23 Day of month: 1 - 31 Month: 0 - 11 (January == 0) Years since 1900 (Year - 1900) Weekday: 0 - 6 (Sunday == 0) Day of year: 0 - 365 Flag for summer-time Sample calls to functions time( ) and localtime( ) #include #include using namespace std; struct tm *ptr; time_t sec; . . . time(&sec); ptr = localtime(&sec); // Pointer to struct tm. // For seconds. // // // // To get the present time. To initialize a struct of type tm and return a pointer to it. cout << "Today is the " << ptr->tm_yday + 1 << ". day of the year " << ptr->tm_year << endl; . . . EXERCISE ■ 261 Exercise A program needs a class to represent the date. ■ Define the class Date for this purpose using three integral data members for day, month, and year.Additionally, declare the following methods: void init( int month, int day, int year); void init(void); void print(void); ■ ✓ Store the definition of the class Date in a header file. Implement the methods for the class Date in a separate source file: 1. The method print() outputs the date to standard output using the format Month-Day-Year. 2. The method init() uses three parameters and copies the values passed to it to corresponding members. A range check is not required at this stage, but will be added later. 3. The method init() without parameters writes the current date to the corresponding members. NOTE Use the functions declared in ctime time_t time(time_t *ptrSec) struct tm *localtime(const time_t *ptrSec); ■ The structure tm and sample calls to this function are included opposite.The type time_t is defined as long in ctime. The function time() returns the system time expressed as a number of seconds and writes this value to the variable referenced by ptrSec. This value can be passed to the function localtime() that converts the number of seconds to the local type tm date and returns a pointer to this structure. Test the class Date using an application program that once more is stored in a separate source file.To this end, define two objects for the class and display the current date. Use object assignments and—as an additional exercise—references and pointers to objects. ■ CHAPTER 13 solution 262 DEFINING CLASSES ■ SOLUTION // // // // ---------------------------------------------------date.h First Definition of class Date. ---------------------------------------------------- #ifndef _DATE_ #define _DATE_ class Date { private: short month, day, year; // Avoid multiple inclusion. // Sheltered members: public: // Public interface: void init(void); void init( int month, int day, int year); void print(void); }; #endif // // // // // _DATE_ ---------------------------------------------------date.cpp Implementing the methods of class Date. ---------------------------------------------------- #include "date.h" #include #include using namespace std; // --------------------------------------------------void Date::init(void) // Get the present date and { // assign it to data members. struct tm *ptr; // Pointer to struct tm. time_t sec; // For seconds. time(&sec); ptr = localtime(&sec); // Get the present date. // Initialize a struct of // type tm and return a // pointer to it. month = (short) ptr->tm_mon + 1; day = (short) ptr->tm_mday; year = (short) ptr->tm_year + 1900; } SOLUTION // --------------------------------------------------void Date::init( int m, int d, int y) { month = (short) m; day = (short) d; year = (short) y; } // --------------------------------------------------void Date::print(void) // Output the date { cout << month << '-' << day << '-' << year << endl; } // ---------------------------------------------------// date_t.cpp // Using objects of class Date. // ---------------------------------------------------#include "date.h" #include using namespace std; int main() { Date today, birthday, aDate; today.init(); birthday.init( 12, 11, 1997); cout << "Today's date: "; today.print(); cout << "\n Felix' birthday: "; birthday.print(); cout << "----------------------------------\n" "Some testing outputs:" << endl; aDate = today; aDate.print(); // Assignment ok Date *pDate = &birthday; pDate->print(); // Pointer to birthday Date &holiday = aDate; holiday.init( 1, 5, 2000); aDate.print(); // Reference to aDate. // Writing to aDate. // holiday.print(); return 0; } ■ 263 This page intentionally left blank chapter 14 Methods This chapter describes ■ how constructors and destructors are defined to create and destroy objects ■ how inline methods, access methods, and read-only methods can be used ■ the pointer this, which is available for all methods, and ■ what you need to pay attention to when passing objects as arguments or returning objects. 265 266 ■ CHAPTER 14 ■ METHODS CONSTRUCTORS Class Account with constructors // account.h // Defining class Account with two constructors. // --------------------------------------------------#ifndef _ACCOUNT_ #define _ACCOUNT_ #include using namespace std; class Account { private: // Sheltered members: string name; // Account holder unsigned long nr; // Account number double state; // State of the account public: // Public interface: Account( const string&, unsigned long, double ); Account( const string& ); bool init( const string&, unsigned long, double); void display(); }; #endif // _ACCOUNT_ Defining the constructors // Within file account.cpp: Account::Account( const string& a_name, unsigned long a_nr, double a_state) { nr = a_nr; name = a_name; state = a_state; } Account::Account( const string& a_name ) { name = a_name; nr = 1111111; state = 0.0; } CONSTRUCTORS ■ 267 䊐 The Task of a Constructor Traditional programming languages only allocate memory for a variable being defined. The programmer must ensure that the variable is initialized with suitable values. An object of the class Account, as described in the previous chapter, does not possess any valid values until the method init() is called. Non-initialized objects can lead to serious runtime errors in your programs. To avoid errors of this type, C++ performs implicit initialization when an object is defined. This ensures that objects will always have valid data to work on. Initialization is performed by special methods known as constructors. 䊐 Declaration Constructors can be identified by their names. In contrast to other member functions, the following applies: ■ ■ the name of the constructor is also the class name a constructor does not possess a return type—not even void. Constructors are normally declared in the public section of a class. This allows you to create objects wherever the class definition is available. Constructors can be overloaded, just like other functions. Constructors belonging to a class must be distinguishable by their signature (that is, the number, order, and type of parameters). This allows for different methods of object initialization. The example opposite shows an addition to the Account class. The class now has two constructors. 䊐 Definition Since a constructor has the same name as its class, the definition of a constructor always begins with Class_name::Class_name In the definition itself, the arguments passed can be checked for validity before they are copied to the corresponding data members. If the number of arguments is smaller than the number of data members, the remaining members can be initialized using default values. Constructors can also perform more complex initialization tasks, such as opening files, allocating memory, and configuring interfaces. 268 ■ CHAPTER 14 ■ METHODS CONSTRUCTOR CALLS Sample program // account2_t.cpp // Using the constructors of class Account. // --------------------------------------------------#include "account.h" int main() { Account giro("Cheers, Mary", 1234567, -1200.99 ), save("Lucky, Luke"); Account depot; giro.display(); save.display(); // Error: no default constructor // defined. // To output Account temp("Funny, Susy", 7777777, 1000000.0); save = temp; // ok: Assignment of // objects possible. save.display(); // Or by the presently available method init(): save.init("Lucky, Luke", 7654321, 1000000.0); save.display(); return 0; } CONSTRUCTOR CALLS ■ 269 Unlike other methods, constructors cannot be called for existing objects. For this reason, a constructor does not have a return type. Instead, a suitable constructor is called once only when an object is created. 䊐 Initialization When an object is defined, initial values can follow the object name in parentheses. Syntax: class object( initializing_list); During initialization the compiler looks for a constructor whose signature matches the initialization list. After allocating sufficient memory for the object, the constructor is called. The values in the initialization list are passed as arguments to the constructor. Example: account nomoney("Poor, Charles"); This statement calls the constructor with one parameter for the name. The other data members will default to standard values. If the compiler is unable to locate a constructor with a suitable signature, it will not create the object but issue an error message. Example: account somemoney("Li, Ed",10.0); // Error! The class Account does not contain a constructor with two parameters. If a constructor with only one parameter is defined in the class, the statement can be written with an equals sign =. Example: account nomoney = "Poor, Charles"; This statement is equivalent to the definition in the example before last. Initialization with parentheses or the = sign was introduced previously for fundamental types. For example, int i(0); is equivalent to int i =0;. 䊐 Default Constructor A constructor without parameters is referred to as a default constructor. The default constructor is only called if an object definition does not explicitly initialize the object. A default constructor will use standard values for all data members. If a class does not contain a constructor definition, the compiler will create a minimal version of the default constructor as a public member. However, this constructor will not perform initialization. By contrast, if a class contains at least one constructor, a default constructor must be defined explicitly, if it is needed. The definition of the Account class does not specify a default constructor; thus a new account object can be created with initialization only. 270 ■ CHAPTER 14 ■ METHODS DESTRUCTORS Sample program // demo.cpp // Outputs constructor and destructor calls. // --------------------------------------------------#include #include using namespace std; int count = 0; // Number of objects. class Demo { private: string name; public: Demo( const string& ); // Constructor ~Demo(); // Destructor }; Demo::Demo( const string& str) { ++count; name = str; cout << "I am the constructor of "<< name << '\n' << "This is the " << count << ". object!\n" } Demo:: ~Demo() // Defining the destructor { cout << "I am the destructor of " << name << '\n' << "The " << count << ". object " << "will be destroyed " << endl; --count; } // -- To initialize and destroy objects of class Demo -Demo globalObject("the global object"); int main() { cout << "The first statement in main()." << endl; Demo firstLocalObject("the 1. local object"); { Demo secLocalObject("the 2. local object"); static Demo staticObject("the static object"); cout << "\nLast statement within the inner block" << endl; } cout << "Last statement in main()." << endl; return 0; } DESTRUCTORS ■ 271 䊐 Cleaning Up Objects Objects that were created by a constructor must also be cleaned up in an orderly manner. The tasks involved in cleaning up include releasing memory and closing files. Objects are cleaned up by a special method called a destructor, whose name is made up of the class name preceded by ∼ (tilde). 䊐 Declaration and Definition Destructors are declared in the public section and follow this syntax: Syntax: ∼class_name(void); Just like the constructor, a destructor does not have a return type. Neither does it have any parameters, which makes the destructor impossible to overload. Each class thus has one destructor only. If the class does not define a destructor, the compiler will create a minimal version of a destructor as a public member, called the default destructor. It is important to define a destructor if certain actions performed by the constructor need to be undone. If the constructor opened a file, for example, the destructor should close that file. The destructor in the Account class has no specific tasks to perform. The explicit definition is thus: Account::∼Account(){} // Nothing to do The individual data members of an object are always removed in the order opposite of the order in which they were created. The first data member to be created is therefore cleaned up last. If a data member is also a class type object, the object’s own destructor will be called. 䊐 Calling Destructors A destructor is called automatically at the end of an object’s lifetime: ■ ■ for local objects except objects that belong to the static storage class, at the end of the code block defining the object for global or static objects, at the end of the program. The sample program on the opposite page illustrates various implicit calls to constructors and destructors. 272 ■ CHAPTER 14 ■ METHODS INLINE METHODS Sample class Account // account.h // New definition of class Account with inline methods // ---------------------------------------------------#ifndef _ACCOUNT_ #define _ACCOUNT_ #include #include #include using namespace std; class Account { private: string name; unsigned long nr; double state; public: // Sheltered members: // Account holder // Account number // State of the account //Public interface: // Constructors: implicit inline Account( const string& a_name = "X", unsigned long a_nr = 1111111L, double a_state = 0.0) { name = a_name; nr = a_nr; state = a_state; } ~Account(){ } // Dummy destructor: implicit inline void display(); }; // display() outputs data of class Account. inline void Account::display() // Explicit inline { cout << fixed << setprecision(2) << "--------------------------------------\n" << "Account holder: " << name << '\n' << "Account number: " << nr << '\n' << "Account state: " << state << '\n' << "--------------------------------------\n" << endl; } #endif // _ACCOUNT_ INLINE METHODS ■ 273 A class typically contains multiple methods that fulfill simple tasks, such as reading or updating data members. This is the only way to ensure data encapsulation and class functionality. However, continually calling “short” methods can impact a program’s runtime. In fact, saving a re-entry address and jumping to the called function and back into the calling function can take more time than executing the function itself. To avoid this overhead, you can define inline methods in a way similar to defining inline global functions. 䊐 Explicit and Implicit inline Methods Methods can be explicitly or implicitly defined as inline. In the first case, the method is declared within the class, just like any other method. You simply need to place the inline keyword before the method name in the function header when defining the method. Example: inline void Account::display() { . . . } Since the compiler must have access to the code block of an inline function, the inline function should be defined in the header containing the class definition. Short methods can be defined within the class. Methods of this type are known as implicit inline methods, although the inline keyword is not used. Example: // Within class Account: bool isPositive(){ return state > 0; } 䊐 Constructors and Destructors with inline Definitions Constructors and destructors are special methods belonging to a class and, as such, can be defined as inline. This point is illustrated by the new definition of the Account class opposite. The constructor and the destructor are both implicit inline. The constructor has a default value for each argument, which means that we also have a default constructor. You can now define objects without supplying an initialization list. Example: Account temp; Although we did not explicitly supply values here, the object temp was correctly initialized by the default constructor we defined. 274 ■ CHAPTER 14 ■ METHODS ACCESS METHODS Access methods for the Account class // account.h // Class Account with set- and get-methods. // ---------------------------------------------------#ifndef _ACCOUNT_ #define _ACCOUNT_ #include #include #include using namespace std; class Account { private: string name; unsigned long nr; double state; public: // Sheltered members: // Account holder // Account number // State of the account //Public interface: // constructors, destructor: Account( const string& a_name = "X", unsigned long a_nr = 1111111L, double a_state = 0.0) { name = a_name; nr = a_nr; state = a_state; } ∼Account(){ } // Access methods: const string& getName() { return name; } bool setName( const string& s) { if( s.size() < 1) // No empty name return false; name = s; return true; } unsigned long getNr() { return nr; } void setNr( unsigned long n) { nr = n; } double getState() { return state; } void setState(double x) { state = x; } void display(); }; // inline definition of display() as before. #endif // _ACCOUNT_ ACCESS METHODS ■ 275 䊐 Accessing Private Data Members An object’s data members are normally found in the private section of a class. To allow access to this data, you could place the data members in the public section of the class; however, this would undermine any attempt at data encapsulation. Access methods offer a far more useful way of accessing the private data members. Access methods allow data to be read and manipulated in a controlled manner. If the access methods were defined as inline, access is just as efficient as direct access to the public members. In the example opposite, several access methods have been added to the Account class. You can now use the getName(), getNr(), getState() methods to read the individual data members. As is illustrated in getName(), references should be read-only when used as return values. Direct access for write operations could be possible otherwise. To manipulate data members, the following methods can be used: setName(), setNr(), setState(). This allows you to define a new balance, as follows: Example: save.setState( 2199.0); 䊐 Access Method Benefits Defining access methods for reading and writing to each data member may seem like a lot of work—all that typing, reams of source code, and the programmer has to remember the names and tasks performed by all those methods. So, you may be asking yourself how you benefit from using access methods. There are two important issues: ■ ■ Access methods can prevent invalid access attempts at the onset by performing sanity checks. If a class contains a member designed to represent positive numbers only, an access method can prevent processing negative numbers. Access methods also hide the actual implementation of a class. It is therefore possible to modify the internal structure of your data at a later stage. If you detect that a new data structure will allow more efficient data handling, you can add this modification to a new version of the class. Provided the public interface to the class remains unchanged, an application program can be leveraged by the modification without needing to modify the application itself. You simply recompile the application program. 276 ■ CHAPTER 14 ■ METHODS const OBJECTS AND METHODS Read-only methods in the Account class // account.h // Account class with read-only methods. // ---------------------------------------------------#ifndef _ACCOUNT_ #define _ACCOUNT_ #include #include #include using namespace std; class Account { private: // // Data members: as public: // // Constructors and . . . // as before Sheltered members before Public interface destructor // Get-methods: const string& getName() const { return name; } unsigned long getNr() const { return nr; } double getState() const { return state; } // Set-methods: . . . // as before // Additional methods: void display() const; }; // display() outputs the data of class Account. inline void Account::display() const { cout << fixed << setprecision(2) << "--------------------------------------\n" << "Account holder: " << name << '\n' << "Account number: " << nr << '\n' << "Account state: " << state << '\n' << "--------------------------------------\n" << endl; } #endif // _ACCOUNT_ const OBJECTS AND METHODS ■ 277 䊐 Accessing const Objects If you define an object as const, the program can only read the object. As mentioned earlier, the object must be initialized when you define it for this reason. Example: const Account inv("YMCA, FL", 5555, 5000.0); The object inv cannot be modified at a later stage. This also means that methods such as setName() cannot be called for this object. However, methods such as getName or display() will be similarly unavailable although they only perform read access with the data members. The reason for this is that the compiler cannot decide whether a method performs write operations or only read operations with data members unless additional information is supplied. 䊐 Read-Only Methods Methods that perform only read operations and that you need to call for constant objects must be identified as read-only. To identify a method as read-only, append the const keyword in the method declaration and in the function header for the method definition. Example: unsigned long getNr() const; This declares the getNr() method as a read-only method that can be used for constant objects. Example: cout << "Account number: " << inv.getNr(); Of course, this does not prevent you from calling a read-only method for a non-constant object. The compiler issues an error message if a read-only method tries to modify a data member. This also occurs when a read-only method calls another method that is not defined as const. 䊐 const and Non-const Versions of a Method Since the const keyword is part of the method’s signature, you can define two versions of the method: a read-only version, which will be called for constant objects by default, and a normal version, which will be called for non-const objects. 278 ■ CHAPTER 14 ■ METHODS STANDARD METHODS Sample program // stdMeth.cpp // Using standard methods. // --------------------------------------------------#include #include #include using namespace std; class CD { private: string interpret, title; long seconds; // Time duration of a song public: CD( const string& i="", const string& t="", long s = 0L) { interpret = i; title = t; seconds = s; } const string& getInterpret() const{ return interpret; } const string& getTitle() const { return title; } long getSeconds() const { return seconds; } }; // Generate objects of class CD and output it in tabular form void printLine( CD cd) ; // A row of the table int main() { CD cd1( "Mister X", "Let's dance", 30*60 + 41), cd2( "New Guitars", "Flamenco Collection", 2772 ), cd3 = cd1, // Copy constructor! cd4; // Default constructor. cd4 = cd2; // Assignment! string line( 70,'-'); line += '\n'; cout << line << left << setw(20) << "Interpreter" << setw(30) << "Title" << "Length (Min:Sec)\n" << line << endl; printLine(cd3); // Call by value ==> printLine(cd4); // Copy constructor! return 0; } void printLine( CD cd) { cout << left << setw(20) << cd.getInterpret() << setw(30) << cd.getTitle() << right << setw(5) << cd.getSeconds() / 60 << ':' << setw(2) << cd.getSeconds() % 60 << endl; } STANDARD METHODS ■ 279 Every class automatically contains four standard methods: ■ ■ ■ ■ the default constructor the destructor the copy constructor and the assignment. You can use your own definitions to replace these standard methods. As illustrated by the sample class Account, the compiler only uses the pre-defined default constructor if no other constructor is available. The default constructor and the implicit, minimal version of a destructor were introduced earlier. 䊐 Copy Constructor The copy constructor initializes an object with another object of the same type. It is called automatically when a second, already existing object is used to initialize an object. Example: Account myAccount("Li, Ed", 2345, 124.80); Account yourAccount(myAccount); In this example, the object yourAccount is initialized by calling the copy constructor with the myAccount object. Each member is copied individually, that is, the following initialization process takes place: yourAccount.name = myAccount.name; yourAccount.nr = myAccount.nr; yourAccount.state = myAccount.state; The copy constructor is also called when an object is passed to a function by value. When the function is called, the parameter is created and initialized with the object used as an argument. 䊐 Assignment Assignment has been used in several previous examples. An object can be assigned to another object of the same type. Example: hisAccount = yourAccount; The data members of the yourAccount object are copied to the corresponding members of hisAccount in this case also. In contrast to initialization using the copy constructor, assignment requires two existing objects. Later in the book, you will be introduced to situations where you need to define the copy constructor or an assignment yourself, and the necessary techniques will be discussed. 280 ■ CHAPTER 14 ■ METHODS this POINTER Sample class DayTime // DayTime.h // The class DayTime represents the time in // hours, minutes and seconds. // --------------------------------------------------#ifndef _DAYTIME_ #define _DAYTIME_ class DayTime { private: short hour, minute, second; bool overflow; public: DayTime( int h = 0, int m = 0, int s = 0) { overflow = false; if( !setTime( h, m, s)) // this->setTime(...) hour = minute = second = 0; // hour is equivalent } // to this->hour etc. bool setTime(int hour, int minute, int second = 0) { if( hour >= 0 && hour < 24 && minute >= 0 && minute < 60 && second >= 0 && second < 60 ) { this->hour = (short)hour; this->minute = (short)minute; this->second = (short)second; return true; } else return false; } int getHour() const { return hour; } int getMinute() const { return minute; } int getSecond() const { return second; } int asSeconds() const // daytime in seconds { return (60*60*hour + 60*minute + second); } bool isLess( DayTime t) const // compare *this and t { return asSeconds() < t.asSeconds(); } // this->asSeconds() < t.asSeconds(); }; #endif // _DAYTIME_ this POINTER ■ 281 䊐 Accessing the Current Object A method can access any member of an object without the object name being supplied in every case. A method will always reference the object with which it was called. But how does a method know which object it is currently working with? When a method is called, it is passed a hidden argument containing the address of the current object. The address of the current object is available to the method via the constant pointer this. Given that actObj is the current object of type Class_id, for which a method was called, the pointer this has the following declaration: Class_id* const this = &actObj; The name this is a keyword. As this is a constant pointer, it cannot be redirected. In other words, the pointer this allows you to access the current object only. 䊐 Using the this Pointer You can use the this pointer within a method to address an object member as follows: Example: this->data // Data member: data // Calling member function this->func() The compiler implicitly creates an expression of this type if only a member of the current object is supplied. Example: data = 12; // Corresponds to this->data=12; Write operations of this type are permissible since the pointer this is a constant, but the referenced object is not. However, the above statement would be invalid for a readonly method. The this pointer can be used explicitly to distinguish a method’s local variables from class members of the same name. This point is illustrated by the sample method setTime() on the opposite page. The this pointer is always necessary to access the current object, *this, collectively. This situation often occurs when the current object needs to be returned as a copy or by reference. Then the return statement is as follows: return *this; 282 ■ CHAPTER 14 ■ METHODS PASSING OBJECTS AS ARGUMENTS Calling methods setTime() and isLess() #include "DayTime.h" . . . DayTime depart1( 11, 11, 11), depart2; . . . depart2.setTime(12, 0, 0); if( depart1.isLess( depart2) ) cout << "\nThe 1st plane takes off earlier" << endl; . . . Global function swap() #include "DayTime.h" // Defines the global function swap(): void swap( DayTime& t1, DayTime& t2) // Two { // parameters! DayTime temp(t1); t1 = t2; t2 = temp; // To swap } // t1 and t2. // A call (e.g. in function main()): DayTime arrival1( 14, 10), arrival2( 15, 20); . . . swap( arrival1, arrival2); // To swap . . . Implementing swap() as a method // Defines the method swap(): class DayTime // With a new method swap() { . . . public: void swap( DayTime& t) // One parameter! { // To swap *this and t: DayTime temp(t); t = *this; *this = temp; } }; // A call (e.g. in function main()): #include "DayTime.h" DayTime arrival1( 10, 10), arrival2( 9, 50); . . . arrival1.swap(arrival2); . . . PASSING OBJECTS AS ARGUMENTS ■ 283 䊐 Passing by Value As you already know, passing by value copies an object that was passed as an argument to the corresponding parameter of a function being called. The parameter is declared as an object of the class in question. Example: bool isLess( DayTime t) const; When the method isLess() is called, the copy constructor executes and initializes the created object, t, with the argument. depart1.isLess( depart2) // Copy constructor The function uses a copy of the object depart2. The copy is cleaned up when leaving the function, that is, the destructor is called. 䊐 Passing by Reference The overhead caused by creating and cleaning up objects can be avoided by passing arguments by reference. In this case, the parameter is declared as a reference or pointer. Example: bool isLess( const DayTime& t) const; This new declaration of the isLess() method is preferable to the previous declaration. There is no formal difference to the way the method is called. However, isLess() no longer creates an internal copy, but accesses directly the object being passed. Of course, the object cannot be changed, as the parameter was declared read-only. 䊐 Methods Versus Global Functions Of course, it is possible to write a global function that expects one object as an argument. However, this rarely makes sense since you would normally expect an object’s functionality to be defined in the class itself. Instead, you would normally define a method for the class and the method would perform the task in hand. In this case, the object would not be passed as an argument since the method would manipulate the members of the current object. A different situation occurs where operations with at least two objects need to be performed, such as comparing or swapping. For example, the method isLess() could be defined as a global function with two parameters. However, the function could only access the public interface of the objects. The function swap()on the opposite page additionally illustrates this point. The major advantage of a globally defined function is its symmetry. The objects involved are peers, since both are passed as arguments. This means that conversion rules are applied to both arguments when the function is called. 284 ■ CHAPTER 14 ■ METHODS RETURNING OBJECTS Global function currentTime() #include "DayTime.h" #include using namespace std; // Functions time(), localtime() const DayTime& currentTime() // Returns the { // present time. static DayTime curTime; time_t sec; time(&sec); // Gets the present time. // Initializes the struct struct tm *time = localtime(&sec); // tm with it. curTime.setTime( time->tm_hour, time->tm_min, time->tm_sec ); return curTime; } Sample program // DayTim_t.cpp // Tests class DayTime and function currentTime() // --------------------------------------------------#include "DayTime.h" // Class definition #include using namespace std; const DayTime& currentTime(); // The current time. int main() { DayTime cinema( 20,30); cout << "\nThe movie starts at "; cinema.print(); DayTime now(currentTime()); // Copy constructor cout << "\nThe current time is "; now.print(); cout << "\nThe movie has "; if( cinema.isLess( now) ) cout << "already begun!\n" << endl; else cout << "not yet begun!\n" << endl; return 0; } RETURNING OBJECTS ■ 285 A function can use the following ways to return an object as a return value: It can create a copy of the object, or it can return a reference or pointer to that object. 䊐 Returning a Copy Returning a copy of an object is time-consuming and only makes sense for small-scale objects. Example: DayTime startMeeting() { DayTime start; . . . // Everyone has time at 14:30: start.setTime( 14, 30); return( start); } On exiting the function, the local object start is destroyed. This forces the compiler to create a temporary copy of the local object and return the copy to the calling function. 䊐 Returning a Reference Of course, it is more efficient to return a reference to an object. But be aware that the lifetime of the referenced object must not be local. If this is the case, the object is destroyed on exiting the function and the returned reference becomes invalid. If you define the object within a function, you must use a static declaration. The global function currentTime() on the opposite page exploits this option by returning a reference to the current time that it reads from the system each time the function is called. The sample program that follows this example uses the current time to initialize the new object now and then outputs the time. In order to output the time, an additional method, print(), was added to the class. 䊐 Using Pointers as Return Values Instead of returning a reference, a function can also return a pointer to an object. In this case too, you must ensure that the object still exists after exiting the function. Example: const DayTime* currentTime() // Read-only pointer { // to the current time . . . // Unchanged return &curTime; } ■ CHAPTER 14 ■ exercise s 286 METHODS EXERCISES Class Article Private members: Type Article number: Article name: Sales price: long string double Public members: Article(long, const string&, double); ~Article(); void print(); // Formatted output set- and get-methods for any data member Output from constructor An object of type Article . . . is created. This is the . . .. Article. Output from destructor The object of type Article . . . is destroyed. There are still . . . articles. EXERCISES ■ 287 Exercise 1 A warehouse management program needs a class to represent the articles in stock. ■ ■ Define a class called Article for this purpose using the data members and methods shown opposite. Store the class definition for Article in a separate header file. Declare the constructor with default arguments for each parameter to ensure that a default constructor exists for the class. Access methods for the data members are to be defined as inline. Negative prices must not exist. If a negative price is passed as an argument, the price must be stored as 0.0. Implement the constructor, the destructor, and the method print() in a separate source file. Also define a global variable for the number of Article type objects. The constructor must use the arguments passed to it to initialize the data members, additionally increment the global counter, and issue the message shown opposite. The destructor also issues a message and decrements the global counter. The method print() displays a formatted object on screen.After outputting an article, the program waits for the return key to be pressed. ■ The application program (again use a separate source file) tests the Article class. Define four objects belonging to the Article class type: 1. A global object and a local object in the main function. 2. Two local objects in a function test() that is called twice by main(). One object needs a static definition.The function test() displays these objects and outputs a message when it is terminated. ■ Use articles of your own choice to initialize the objects.Additionally, call the access methods to modify individual data members and display the objects on screen. Test your program. Note the order in which constructors and destructors are called. Supplementary question: Suppose you modify the program by declaring a function called test() with a parameter of type Article and calling the function with an article type object.The counter for the number of objects is negative after running the program.Why? 288 ■ CHAPTER 14 METHODS Methods for class Date Public Methods: Date(); Date( int month, int day, int year); void setDate(); bool setDate( int mn, int da, int yr); int getMonth() const; int getDay() const; int getYear() const; bool isEqual( const Date&) const; bool isLess( const Date&) const; const string& asString() const; void print() const; Converting a number to a string The class stringstream offers the same functionality for reading and writing to character buffer as the classes istream and ostream do.Thus, the operators >> and << , just as all manipulators, are available. // Example: Converting a number to a string. #include // Class stringstream #include // Manipulators double x = 12.3; string str; stringstream iostream; iostream << setw(10) << x; iostream >> str; // // // // // // Number Destination string For conversion number -> string. Add to the stream. Read from the stream. Notices for exercise 3 ■ A year is a leap year if it is divisible by 4 but not by 100. In addition, all multiples of 400 are leap years. February has 29 days in a leap year. ■ Use a switch statement to examine the number of days for months containing less than 31 days. EXERCISES ■ 289 Exercise 2 In the exercises for chapter 13, an initial version of the Date class containing members for day, month, and year was defined. Now extend this class to add additional functionality.The methods are shown on the opposite page. ■ ■ ■ ■ ■ The constructors and the method setDate() replace the init method used in the former version.The default constructor uses default values, for example, 1.1.1, to initialize the objects in question.The setDate() method without any parameters writes the current date to the object. The constructor and the setDate() method with three parameters do not need to perform range checking.This functionality will be added in the next exercise. The methods isEqual() and isLess() enable comparisons with a date passed to them. The method asString() returns a reference to a string containing the date in the format mm-dd-year, e.g. 03-19-2006.You will therefore need to convert any numerical values into their corresponding decimal strings. This operation is performed automatically when you use the << operator to output a number to the standard output cout. In addition to the cin and cout streams, with which you are already familiar, so-called string streams with the same functionality also exist. However, a string stream does not read keyboard input or output data on screen. Instead, the target, or source, is a buffer in main memory.This allows you to perform formatting and conversion in main memory. Use an application program that calls all the methods defined in the class to test the Date class. Exercise 3 The Date class does not ensure that an object represents a valid date.To avoid this issue, add range checking functionality to the class. Range checking is performed by the constructor and the setDate() method with three parameters. ■ ■ ■ First, write a function called isLeapYear() that belongs to the bool type and checks whether the year passed to it is a leap year. Define the function as a global inline function and store it in the header file Date.h. Modify the setDate() method to allow range checking for the date passed to it.The constructor can call setDate(). Test the new version of the Date class.To do so, and to test setDate(...), read a date from the keyboard. ■ CHAPTER 14 ■ solutions 290 METHODS SOLUTIONS Exercise 1 // ---------------------------------------------------// article.h // Defines a simple class, Article. // ---------------------------------------------------#ifndef _ARTICLE_ #define _ARTICLE_ #include using namespace std; class Article { private: long nr; string name; double sp; // Article number // Article name // Selling price public: Article( long nr=0, const string& name="noname", double sp=0.0); ~Article(); void print(); const string& getName() const { return name; } long getNr() const { return nr; } double getSP() const { return sp; } bool setName( const string& s) { if( s.size() < 1) return false; name = s; return true; } void setNr( long n) { nr = n; } void setSP(double v) { sp = v > 0.0 ? v : 0.0; } }; #endif // _ARTICLE_ // No empty name // No negative price SOLUTIONS // -----------------------------------------------------// article.cpp // Defines those methods of Article, which are // not defined inline. // Screen output for constructor and destructor calls. // -----------------------------------------------------#include "Article.h" // Definition of the class #include #include using namespace std; // Global counter for the objects: int count = 0; // -----------------------------------------------------// Define constructor and destructor: Article::Article( long nr, const string& name, double sp) { setNr(nr); setName(name); setSP(sp); ++count; cout << "Created object for the article " << name << ".\n" << "This is the " << count << ". article!\n" } Article::~Article() { cout << "Cleaned up object for the article " << name << ".\n" << "There are still " << --count << " articles!" << endl; } // -----------------------------------------------------// The method print() outputs an article. void Article::print() { long savedFlags = cout.flags(); // To mark the // flags of cout. cout << fixed << setprecision(2) << "-----------------------------------------\n" << "Article data:\n" << " Number ....: " << nr << '\n' << " Name ....: " << name << '\n' << " Sales price: " << sp << '\n' << "-----------------------------------------" << endl; cout.flags(savedFlags); // To restore // old flags. cout << " --- Go on with return --- "; cin.get(); } ■ 291 292 ■ CHAPTER 14 METHODS // -----------------------------------------------------// article_t.cpp // Tests the Article class. // -----------------------------------------------------#include "Article.h" // Definition of the class #include #include using namespace std; void test(); // -- Creates and destroys objects of Article class -Article Article1( 1111,"volley ball", 59.9); int main() { cout << "\nThe first statement in main().\n" << endl; Article Article2( 2222,"gym-shoes", 199.99); Article1.print(); Article2.print(); Article& shoes = Article2; // Another name shoes.setNr( 2233); shoes.setName("jogging-shoes"); shoes.setSP( shoes.getSP() - 50.0); cout << "\nThe shoes.print(); cout << "\nThe test(); cout << "\nThe test(); cout << "\nThe return 0; new values of the shoes object:\n"; first call to test()." << endl; second call to test()." << endl; last statement in main().\n" << endl; } void test() { Article shirt( 3333, "T-Shirt", 29.9); shirt.print(); static Article net( 4444, "volley ball net", 99.0); net.print(); cout << "\nLast statement in function test()" << endl; } SOLUTIONS ■ 293 Answer to the supplementary question: The copy constructor is called on each “passing by value,” although this constructor has not been defined explicitly. In other words, the implicitly defined copy constructor is used and of course does not increment the object counter. However, the explicitly defined destructor, which decrements the counter, is still called for each object. Exercises 2 and 3 // -----------------------------------------------------// Date.h // Defining class Date with optimized // functionality, e.g. range check. // -----------------------------------------------------#ifndef _DATE_ // Avoids multiple inclusions. #define _DATE_ #include