Table Of ContentC
++
FAQs
S E C O N D E D I T I O N
This page intentionally left blank
C
++
FAQs
S E C O N D E D I T I O N
Marshall Cline
Greg Lomow
Mike Girou
ADDISON–WESLEY
Boston • San Francisco • New York • Toronto • Montreal
London • Munich • Paris • Madrid
Capetown • Sydney • Tokyo • Singapore • Mexico City
Many ofthe designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks.Where those designations appear in this book,and we were aware ofa trademark claim,the designations
have been printed with initial caps or in all caps.
The authors and publisher have taken care in the preparation of this book,but make no expressed or
implied warranty ofany kind and assume no responsibility for errors or omissions.No liability is assumed
for incidental or consequential damages in connection with or arising out ofthe use ofthe information or
programs contained herein.
The publisher offers discounts on this book when ordered in quantity for special sales.For more informa-
tion,please contact:
U.S. Corporate and Government Sales
(800) 382-3419
[email protected]
For sales outside of the U.S., please contact:
International Sales
[email protected]
Visit Addison-Wesley on the Web:www.awprofessional.com
Library ofCongress Cataloging-in-Publication Data
Cline,Marshall P.
C++ FAQs / Marshall Cline,Greg Lomow,Mike Girou.—2nd ed.
p. cm.
Includes index.
ISBN 0-201-30983-1
1.C++ (Computer program language) I.Lomow,Greg A. II.Girou,Mike. III.Title.
QA76.73.C153C55 1999
005.13'3—dc21 98–38674
CIP
Copyright © 1999 by Pearson Education,Inc.
All rights reserved.No part ofthis publication may be reproduced,stored in a retrieval system,or transmit-
ted,in any form,or by any means,electronic,mechanical,photocopying,recording,or otherwise,without
the prior consent ofthe publisher.Printed in the United States ofAmerica.Published simultaneously in
Canada.
ISBN 0-201-30983-1
Text printed in the United States at Demand Print Center in Old Tappan, New Jersey.
15th Printing February 2010
For MaryElaine,David,Elizabeth,Gabrielle,
Peter,Katherine,and Brandon.
—Marshall
For Barb,Lonnie,and Mac; and to the caregivers and
animals at PIGS,A Sanctuaryand Farm Sanctuary.
—Greg
For Beverly,Christian,James,Kelly,and Tamie.
—Mike
This page intentionally left blank
Contents
Acknowledgments xxxiii
Part I Preliminaries 1
Chapter 1 Introduction 3
FAQ 1.01:What is the purpose ofthis chapter? 3
FAQ 1.02:What are C++ FAQs? 3
FAQ 1.03:Who is the target audience for this book? 4
FAQ 1.04:Is this a book about C++ per se? 4
FAQ 1.05:Why do developers need a guidebook for C++ and OO
technology? 5
FAQ 1.06:What kind ofguidance is given in the answers to these FAQs? 5
FAQ 1.07:What is the electronic FAQ and why buy this book when the
electronic FAQ is free? 6
FAQ 1.08:Why should you buy this edition ifyou already have a copy of
the first edition? 7
FAQ 1.09:What conventions are used in this book? 7
Chapter 2 Basic C++ Syntax and Semantics 11
FAQ 2.01:What is the purpose ofthis chapter? 11
FAQ 2.02:What are the basics ofmain()? 11
FAQ 2.03:What are the basics offunctions? 12
FAQ 2.04:What are the basics ofdefault parameters? 13
FAQ 2.05:What are the basics oflocal (auto) objects? 13
vii
viii Contents
FAQ 2.06:What are the basics ofconstructing objects using explicit
parameters? 15
FAQ 2.07:What are the basics ofdynamically allocated (new) objects? 16
FAQ 2.08:What are the basics oflocal objects within inner scopes? 17
FAQ 2.09:What are the basics ofpassing objects by reference? 18
FAQ 2.10:What are the basics ofpassing objects by value? 19
FAQ 2.11:What are the basics ofpassing objects by pointer? 20
FAQ 2.12:What are the basics ofstream output? 21
FAQ 2.13:What are the basics ofstream input? 22
FAQ 2.14:What are the basics ofusing classes that contain overloaded
operators? 24
FAQ 2.15:What are the basics ofusing container classes? 24
FAQ 2.16:What are the basics ofcreating class header files? 26
FAQ 2.17:What are the basics ofdefining a class? 27
FAQ 2.18:What are the basics ofdefining member functions? 28
FAQ 2.19:What are the basics ofadding a constructor to a class? 29
FAQ 2.20:What are the basics ofadding a destructor to a class? 31
FAQ 2.21:What are the basics ofdefining a class that contains a pointer to
an object allocated from the heap? 32
FAQ 2.22:What are the basics ofglobal objects? 35
FAQ 2.23:What are the basics ofthrowing and catching exceptions? 37
FAQ 2.24:What are the basics ofinheritance and dynamic binding? 39
Chapter 3 Understanding the
Management Perspective 43
FAQ 3.01:What is the purpose ofthis chapter? 43
FAQ 3.02:What is the core message ofthis chapter (and this book)? 44
FAQ 3.03:Why are the managers in charge rather than the developers who
understand technology? 45
FAQ 3.04:How can someone manage something they don’t
understand? 45
FAQ 3.05:What is the most common mistake on C++ and OO
projects? 46
FAQ 3.06:What’s the “Software Peter Principle”? 47
FAQ 3.07:Should an organization use OO on all its projects? 47
Contents ix
FAQ 3.08:Can OO be ignored until it goes away? 48
FAQ 3.09:What OO language is best? 48
FAQ 3.10:What is the right approach to processes and tools? 49
FAQ 3.11:What is the right approach with off-the-shelfclass libraries and
frameworks? 50
Chapter 4 The Architectural Perspective 51
FAQ 4.01:What is the purpose ofthis chapter? 51
FAQ 4.02:Why is software architecture important? 52
FAQ 4.03:What should the architecture be based on,the problem being
solved or the problem domain? 53
FAQ 4.04:Should the software architecture be based on the policy ofthe
problem? 53
FAQ 4.05:Do customers ever change their requirements? 54
FAQ 4.06:Are stable requirements desirable? 54
FAQ 4.07:What is the key to planning for change? 55
FAQ 4.08:What is a framework? 55
FAQ 4.09:What is the “inversion ofcontrol”exhibited by
frameworks? 56
FAQ 4.10:What is an extensible,domain-specific framework? 56
FAQ 4.11:What characteristics make a framework extensible yet
domain-specific? 57
FAQ 4.12:What happens ifthe domain analysis is incorrect? 58
FAQ 4.13:How much effort should be expended to support change—
that is,how much is extensibility worth? 58
FAQ 4.14:How does an architect make the software architecture
flexible? 59
FAQ 4.15:What is the secret to achieving reuse? 59
Part II Object-Oriented Design 61
Chapter 5 Object-Oriented Fundamentals 63
FAQ 5.01:What is the purpose ofthis chapter? 63
FAQ 5.02:Why is the world adopting OO technology? 63