Object-Oriented Programming in C++ (4th Edition) by Robert Lafore.pdf

(13381 KB) Pobierz
Object-Oriented Programming in C++ (4th Edition)
113250865.004.png
Object-Oriented Programming in C++,
Fourth Edition
Robert Lafore
800 East 96th St.,Indianapolis,Indiana 46240 USA
113250865.005.png 113250865.006.png 113250865.007.png
2002 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical, photo-
copying, recording, or otherwise, without written permission from the pub-
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
of this book, the publisher and author assume no responsibility for errors or
omissions. Nor is any liability assumed for damages resulting from the use of
the information contained herein.
International Standard Book Number: 0-672-32308-7
Library of Congress Catalog Card Number: 2001094813
Printed in the United States of America
First Printing: December 2001
04 03 02 01
E XECUTIVE E DITOR
Michael Stephens
A CQUISITIONS E DITOR
Michael Stephens
M ANAGING E DITOR
Matt Purcell
P ROJECT E DITORS
Angela Boley
Christina Smith
I NDEXER
Rebecca Salerno
4321
P ROOFREADER
Matt Wynalda
Trademarks
All terms mentioned in this book that are known to be trademarks or service
marks have been appropriately capitalized. Sams Publishing cannot attest to
the accuracy of this information. Use of a term in this book should not be
regarded as affecting the validity of any trademark or service mark.
T ECHNICAL E DITOR
Mark Cashman
T EAM C OORDINATOR
Pamalee Nelson
M EDIA D EVELOPER
Dan Scherf
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as
possible, but no warranty or fitness is implied. The information provided is on
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
arising from the information contained in this book.
I NTERIOR D ESIGNER
Gary Adair
C OVER D ESIGNER
Alan Clements
P AGE L AYOUT
Ayanna Lacey
Copyright
113250865.001.png
Overview
Introduction 1
1he Big Picture 9
2 C++ Programming Basics 29
3 Loops and Decisions 75
4tructures 131
5 Functions 161
6 Objects and Classes 215
7Arrays 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
CMicrosoft 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
113250865.002.png
Contents
Introduction 1
1 The Big Picture 9
Why Do We Need Object-Oriented Programming? ..............................10
Procedural Languages ......................................................................10
The Object-Oriented Approach ........................................................13
Characteristics of Object-Oriented Languages......................................16
Objects ..............................................................................................16
Classes ..............................................................................................18
Inheritance ........................................................................................18
Reusability ........................................................................................21
Creating New Data Types ................................................................21
Polymorphism and Overloading ......................................................21
C++ and C..............................................................................................22
Laying the Groundwork ........................................................................23
The Unified Modeling Language (UML) ..............................................23
Summary ................................................................................................25
Questions................................................................................................25
2 C++ Programming Basics 29
Getting Started ......................................................................................30
Basic Program Construction ..................................................................30
Functions ..........................................................................................31
Program Statements..........................................................................32
Whitespace........................................................................................33
Output Using cout ................................................................................33
String Constants................................................................................34
Directives ..............................................................................................35
Preprocessor Directives ....................................................................35
Header Files......................................................................................35
The using Directive..........................................................................36
Comments ..............................................................................................36
Comment Syntax ..............................................................................36
When to Use Comments ..................................................................37
Alternative Comment Syntax ..........................................................37
Integer Variables ....................................................................................38
Defining Integer Variables ................................................................38
Declarations and Definitions ............................................................40
Va riable Names ................................................................................40
Assignment Statements ....................................................................40
113250865.003.png
Zgłoś jeśli naruszono regulamin