Object-Oriented_Programming_in_C __4th_Edition__Robert_Lafore.pdf

(5761 KB) Pobierz
Object-Oriented Programming in C++,
Fourth Edition
Robert Lafore
800 East 96th St., Indianapolis, Indiana 46240 USA
Copyright
©
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
06
05
04
9 8
7
6
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
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
Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in quan-
tity for bulk purchases or special sales. For more information, please contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
1-317-428-3341
international@pearsontechgroup.com
P
AGE
L
AYOUT
Ayanna Lacey
Overview
Introduction
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
A
B
C
D
E
F
G
H
The Big Picture
9
C++ Programming Basics
29
Loops and Decisions
75
Structures
131
Functions
161
Objects and Classes
215
Arrays and Strings
263
Operator Overloading
319
Inheritance
371
Pointers
429
Virtual
Functions
503
Streams and
Files
567
Multifile Programs
633
Templates and
Exceptions
681
The Standard Template Library
725
Object-Oriented Software Development
801
ASCII Chart
849
C++ Precedence Table and Keywords
859
Microsoft Visual C++
863
Borland C++Builder
871
Console Graphics Lite
881
STL Algorithms and Member
Functions
895
Answers to Questions and
Exercises
913
Bibliography
Index
981
977
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
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
Variable Names ................................................................................40
Assignment Statements ....................................................................40
2
Integer Constants ..............................................................................41
Output Variations ..............................................................................41
The
endl
Manipulator ......................................................................41
Other Integer Types ..........................................................................42
Character Variables ................................................................................42
Character Constants..........................................................................43
Initialization......................................................................................44
Escape
Sequences ............................................................................44
Input with
cin
........................................................................................45
Variables Defined at Point of Use ....................................................47
Cascading
<<
....................................................................................47
Expressions
......................................................................................47
Precedence ........................................................................................47
Floating
Point Types ..............................................................................48
Type
float
........................................................................................48
Type
double
and
long double
........................................................49
Floating-Point
Constants ..................................................................50
The
const
Qualifier ..........................................................................51
The
#define
Directive ......................................................................51
Type
bool
..............................................................................................51
The
setw
Manipulator............................................................................52
Cascading the Insertion Operator ....................................................54
Multiple Definitions ........................................................................54
The
IOMANIP
Header
File
..................................................................54
Variable Type Summary ........................................................................54
unsigned
Data Types ........................................................................55
Type Conversion ....................................................................................56
Automatic Conversions ....................................................................57
Casts..................................................................................................58
Arithmetic Operators ............................................................................60
The Remainder Operator ..................................................................61
Arithmetic Assignment Operators ....................................................61
Increment Operators ........................................................................63
Library
Functions
..................................................................................65
Header
Files......................................................................................66
Library
Files
....................................................................................66
Header
Files
and Library
Files
........................................................67
Two Ways to Use
#include
..............................................................67
Summary ................................................................................................68
Questions................................................................................................69
Exercises
................................................................................................71
Zgłoś jeśli naruszono regulamin