little trouble. These programs usually don't use all the new features of C++, but they do work. In this way, C++ allows
Practical C++ Programming Steve Oualline O'Reilly & Associates, Inc. Beijing · Cambridge · Köln · Paris · Sebastopol · Taipei · Tokyo
Page iv
Practical C++ Programming by Steve Oualline Copyright © 1995 O'Reilly & Associates, Inc. All rights reserved. Printed in the United States of America. Editors: Adrian Nye and Dale Dougherty Production Editor: Nicole Gipson Printing History: August 1995 First Edition. January 1997: Minor corrections. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks and The Java Series is a trademark of O'Reilly & Associates, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
This book is printed on acid-free paper with 85% recycled content, 15% post-consumer waste. O'Reilly & Associates is committed to using paper with the highest recycled content available consistent with high quality. ISBN. 1-56592-139-9
[12/98] Page v
Table of Contents Preface
xv
I: The Basics
1
1: What Is C++? 3
3
A Brief History of C++
3
C++ Organization
4
How to Learn C++
6
2: The Basics of Program Writing
9
Programs from Conception to Execution
12
Creating a Real Program
13
Creating a Program Using a Command-Line Compiler
13
Creating a Program Using an Integrated Development Environment
16
Getting Help in UNIX
32
Getting Help in an Integrated Development Environment
33
Programming Exercises
33
3: Style
35
Comments
36
C++ Code 4
41
Naming Style
42
Coding Religion
43
Indentation and Code Format
43
Page vi
Clarity
44
44 Simplicity
45
Consistency and Organization
46
Further Reading
46
Summary
46
4: Basic Declarations and Expressions
49
The Elements of a Program
49
Basic Program Structure
50
Simple Expressions
51
The cout Output Class
53
Variables and Storage
53
Variable Declarations
54
Integers
55
Assignment Statements
56
Floating Point Numbers
57
Floating Point Versus Integer Divide
58
Characters
59
Programming Exercises
60
Answers Chapter Questions
61
5: Arrays, Qualifiers, and Reading Numbers
63
Arrays
63
Strings
64
Reading Error: Copy constructor for 'no_copy' called. Exiting\n"; exit(8); } };
This works, sort of. The problem is that errors are detected at runtime instead of compile time. You want to catch errors as soon as possible, so this solution is at best a hack. However, you can prevent the compiler from automatically calling the copy constructor. The trick is to declare it private. That's your way of saying to the world, ''Yes, there is a copy constructor, but no one can ever use it." class no_copy { // Body of the class private: // There is no copy constructor no_copy(const no_copy &old_class); };
Now when the compiler attempts to use the copy constructor you will get an error message like "Error: Attempt to access private member function." Note: Since the copy constructor is never called, you never have to define the body of this function.
Programming Exercises Exercise 13-1: Write a parity class. This class allows the program to put any number of items into it and returns TRUE if an even number of items is put in and FALSE if an odd number is used. Member functions: void parity::put(void); int parity::test(void);
// Count another element // Return TRUE(1) if an even number of
puts have been done. Return FALSE(O) for an odd number.
Page 215
Exercise 13-2: Write a "checkbook" class. You put a list of numbers into this class and get a total out. Member functions: void check::add_item(int amount); int check::total(void);
// Add a new entry to the checkbook // Return the total of all items
Exercise 13-3: Write a class to implement a simple queue. A queue is very similar to a stack except the The number of active stacks is "