,Object-Oriented Programming in C++,
Fourth Edition
Robert Lafore
800 East 96th St., Indianapolis, Indiana 46240 USA
,Copyright 2002 by Sams Publishing EXECUTIVE EDITOR
All rights reserved. No part of this book shall be reproduced, stored in a Michael Stephens
retrieval system, or transmitted by any means, electronic, mechanical, photo- ACQUISITIONS EDITOR
copying, recording, or otherwise, without written permission from the pub- Michael Stephens
lisher. No patent liability is assumed with respect to the use of the information
contained herein. Although every precaution has been taken in the preparation MANAGING EDITOR
of this book, the publisher and author assume no responsibility for errors or Matt Purcell
omissions. Nor is any liability assumed for damages resulting from the use of PROJECT EDITORS
the information contained herein. Angela Boley
International Standard Book Number: 0-672-32308-7 Christina Smith
Library of Congress Catalog Card Number: 2001094813 INDEXER
Printed in the United States of America Rebecca Salerno
First Printing: December 2001 PROOFREADER
04 03 02 01 4 3 2 1 Matt Wynalda
TECHNICAL EDITOR
Trademarks Mark Cashman
All terms mentioned in this book that are known to be trademarks or service
marks have been appropriately capitalized. Sams Publishing cannot attest to TEAM COORDINATOR
the accuracy of this information. Use of a term in this book should not be Pamalee Nelson
regarded as affecting the validity of any trademark or service mark.
MEDIA DEVELOPER
Dan Scherf
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as INTERIOR DESIGNER
possible, but no warranty or fitness is implied. The information provided is on Gary Adair
an “as is” basis. The author and the publisher shall have neither liability nor
responsibility to any person or entity with respect to any loss or damages COVER DESIGNER
arising from the information contained in this book. Alan Clements
PAGE LAYOUT
Ayanna Lacey
, Overview
Introduction 1
1 The Big Picture 9
2 C++ Programming Basics 29
3 Loops and Decisions 75
4 Structures 131
5 Functions 161
6 Objects and Classes 215
7 Arrays and Strings 263
8 Operator Overloading 319
9 Inheritance 371
10 Pointers 429
11 Virtual Functions 503
12 Streams and Files 567
13 Multifile Programs 633
14 Templates and Exceptions 681
15 The Standard Template Library 725
16 Object-Oriented Software Development 801
A ASCII Chart 849
B C++ Precedence Table and Keywords 859
C Microsoft Visual C++ 863
D Borland C++Builder 871
E Console Graphics Lite 881
F STL Algorithms and Member Functions 895
G Answers to Questions and Exercises 913
H Bibliography 977
Index 981
Fourth Edition
Robert Lafore
800 East 96th St., Indianapolis, Indiana 46240 USA
,Copyright 2002 by Sams Publishing EXECUTIVE EDITOR
All rights reserved. No part of this book shall be reproduced, stored in a Michael Stephens
retrieval system, or transmitted by any means, electronic, mechanical, photo- ACQUISITIONS EDITOR
copying, recording, or otherwise, without written permission from the pub- Michael Stephens
lisher. No patent liability is assumed with respect to the use of the information
contained herein. Although every precaution has been taken in the preparation MANAGING EDITOR
of this book, the publisher and author assume no responsibility for errors or Matt Purcell
omissions. Nor is any liability assumed for damages resulting from the use of PROJECT EDITORS
the information contained herein. Angela Boley
International Standard Book Number: 0-672-32308-7 Christina Smith
Library of Congress Catalog Card Number: 2001094813 INDEXER
Printed in the United States of America Rebecca Salerno
First Printing: December 2001 PROOFREADER
04 03 02 01 4 3 2 1 Matt Wynalda
TECHNICAL EDITOR
Trademarks Mark Cashman
All terms mentioned in this book that are known to be trademarks or service
marks have been appropriately capitalized. Sams Publishing cannot attest to TEAM COORDINATOR
the accuracy of this information. Use of a term in this book should not be Pamalee Nelson
regarded as affecting the validity of any trademark or service mark.
MEDIA DEVELOPER
Dan Scherf
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as INTERIOR DESIGNER
possible, but no warranty or fitness is implied. The information provided is on Gary Adair
an “as is” basis. The author and the publisher shall have neither liability nor
responsibility to any person or entity with respect to any loss or damages COVER DESIGNER
arising from the information contained in this book. Alan Clements
PAGE LAYOUT
Ayanna Lacey
, Overview
Introduction 1
1 The Big Picture 9
2 C++ Programming Basics 29
3 Loops and Decisions 75
4 Structures 131
5 Functions 161
6 Objects and Classes 215
7 Arrays and Strings 263
8 Operator Overloading 319
9 Inheritance 371
10 Pointers 429
11 Virtual Functions 503
12 Streams and Files 567
13 Multifile Programs 633
14 Templates and Exceptions 681
15 The Standard Template Library 725
16 Object-Oriented Software Development 801
A ASCII Chart 849
B C++ Precedence Table and Keywords 859
C Microsoft Visual C++ 863
D Borland C++Builder 871
E Console Graphics Lite 881
F STL Algorithms and Member Functions 895
G Answers to Questions and Exercises 913
H Bibliography 977
Index 981