Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Object-Oriented Programming in C++, 3rd Edition docx
Nội dung xem thử
Mô tả chi tiết
Object-Oriented Programming in C++, Third Edition
(Publisher: Macmillan Computer Publishing)
Author(s): Robert Lafore
ISBN: 157169160x
Publication Date: 11/25/98
Introduction
About the Author
Preface
CHAPTER 1—THE BIG PICTURE
Why Do We Need Object-Oriented Programming?
Procedural Languages
The Object-Oriented Approach
Characteristics of Object-Oriented Languages
Objects
Classes
Inheritance
Reusability
Creating New Data Types
Polymorphism and Overloading
C++ and C
Laying the Groundwork
Summary
Questions
CHAPTER 2—C++ PROGRAMMING BASICS
Getting Started
Basic Program Construction
Functions
Program Statements
Whitespace
Output Using cout
String Constants
Directives
Preprocessor Directives
Header Files
The using Directive
Comments
Comment Syntax
When to Use Comments
Alternative Comment Syntax
Integer Variables
Defining Integer Variables
Object-Oriented Programming in C++, Third Edition
(Publisher: Macmillan Computer Publishing)
Author(s): Robert Lafore
ISBN: 157169160x
Publication Date: 11/25/98
Introduction
About the Author
Preface
CHAPTER 1—THE BIG PICTURE
Why Do We Need Object-Oriented Programming?
Procedural Languages
The Object-Oriented Approach
Characteristics of Object-Oriented Languages
Objects
Classes
Inheritance
Reusability
Creating New Data Types
Polymorphism and Overloading
C++ and C
Laying the Groundwork
Summary
Questions
CHAPTER 2—C++ PROGRAMMING BASICS
Getting Started
Basic Program Construction
Functions
Program Statements
Whitespace
Output Using cout
String Constants
Directives
Preprocessor Directives
Header Files
The using Directive
Comments
Comment Syntax
When to Use Comments
Alternative Comment Syntax
Integer Variables
Defining Integer Variables
Declarations and Definitions
Variable Names
Assignment Statements
Integer Constants
Output Variations
The endl Manipulator
Other Integer Types
Character Variables
Character Constants
Initialization
Escape Sequences
Input with cin
Variables Defined at Point of Use
Cascading <<
Expressions
Precedence
Floating Point Types
Type float
Type double and long double
Floating-Point Constants
The const Qualifier
The #define Directive
Type bool
The setw Manipulator
Cascading the Insertion Operator
Multiple Definitions
The IOMANIP Header File
Variable Type Summary
unsigned Data Types
Type Conversion
Automatic Conversions
Casts
Arithmetic Operators
The Remainder Operator
Arithmetic Assignment Operators
Increment Operators
Library Functions
Header Files
Library Files
Header Files and Library Files
Two Ways to Use #include
Summary
Questions
Exercises
CHAPTER 3—LOOPS AND DECISIONS
Relational Operators
Loops
The for Loop
Debugging Animation
for Loop Variations
The while Loop
Precedence: Arithmetic and Relational Operators
The do Loop
When to Use Which Loop
Decisions
The if Statement
The if...else Statement
The else...if Construction
The switch Statement
The Conditional Operator
Logical Operators
Logical and Operator
Logical OR Operator
Logical NOT Operator
Precedence Summary
Other Control Statements
The break Statement
The continue Statement
The goto Statement
Summary
Questions
Exercises
CHAPTER 4—STRUCTURES
Structures
A Simple Structure
Declaring the Structure
Defining a Structure Variable
Accessing Structure Members
Other Structure Features
A Measurement Example
Structures Within Structures
A Card Game Example
Structures and Classes
Enumerations
Days of the Week
One Thing or Another
Organizing the Cards
Specifying Integer Values
Not Perfect
Other Examples
Summary
Questions
Exercises
CHAPTER 5—FUNCTIONS
Simple Functions
The Function Declaration
Calling the Function
The Function Definition
Comparison with Library Functions
Eliminating the Declaration
Passing Arguments to Functions
Passing Constants
Passing Variables
Passing by Value
Structures as Arguments
Names in the Declaration
Returning Values from Functions
The return Statement
Returning Structure Variables
Reference Arguments
Passing Simple Data Types by Reference
A More Complex Pass by Reference
Passing Structures by Reference
Notes on Passing by Reference
Overloaded Functions
Different Numbers of Arguments
Different Kinds of Arguments
Inline Functions
Default Arguments
Variables and Storage Classes
Automatic Variables
External Variables
Static Variables
Storage
Returning by Reference
Function Calls on the Left of the Equal Sign
Don’t Worry Yet
const Function Arguments
Summary
Questions
Exercises
CHAPTER 6—OBJECTS AND CLASSES
A Simple Class
Classes and Objects
Declaring the Class
Using the Class
Calling Member Functions
C++ Objects As Physical Objects
Widget Parts as Objects
Circles as Objects
C++ Objects As Data Types
Constructors
A Counter Example
A Graphics Example
Destructors
Objects as Function Arguments
Overloaded Constructors
Member Functions Defined Outside the Class
Objects As Arguments
The Default Copy Constructor
Returning Objects from Functions
Arguments and Objects
A Card–Game Example
Structures and Classes
Classes, Objects, and Memory
Static Class Data
Uses of Static Class Data
An Example of Static Class Data
Separate Declaration and Definition
const and Classes
const Member Functions
const Objects
What Does It All Mean?
Summary
Questions
Exercises
CHAPTER 7—ARRAYS AND STRINGS
Array Fundamentals
Defining Arrays
Array Elements
Accessing Array Elements
Averaging Array Elements
Initializing Arrays
Multidimensional Arrays
Passing Arrays to Functions
Function Declaration with Array Argument
Arrays of Structures
Arrays As Class Member Data
Arrays of Objects
Arrays of English Distances
Arrays of Cards
C-Strings
C-string Variables
Avoiding Buffer Overflow
String Constants
Reading Embedded Blanks
Reading Multiple Lines
Copying a String the Hard Way
Copying a String the Easy Way
Arrays of Strings
Strings As Class Members
A User-Defined String Type
The Standard C++ string Class
Defining and Assigning string Objects
Input/Output with string Objects
Finding string Objects
Modifying string Objects
Comparing string Objects
Accessing Characters in string Objects
Other string Functions
Summary
Questions
Exercises
CHAPTER 8—OPERATOR OVERLOADING
Overloading Unary Operators
The operator Keyword
Operator Arguments
Operator Return Values
Nameless Temporary Objects
Postfix Notation
Overloading Binary Operators
Arithmetic Operators
Concatenating Strings
Multiple Overloading
Comparison Operators
Arithmetic Assignment Operators
The Subscript Operator [ ]
Data Conversion
Conversions Between Basic Types
Conversions Between Objects and Basic Types
Conversions Between Objects of Different Classes
Conversions: When to Use What
Pitfalls of Operator Overloading and Conversion
Use Similar Meanings
Use Similar Syntax
Show Restraint
Avoid Ambiguity
Not All Operators Can Be Overloaded
Keywords explicit and mutable
Preventing Conversions with explicit
Changing const Object Data Using mutable
Summary
Questions
Exercises
CHAPTER 9—INHERITANCE
Derived Class and Base Class
Specifying the Derived Class
Accessing Base Class Members
The protected Access Specifier
Derived Class Constructors
Overriding Member Functions
Which Function Is Used?
Scope Resolution with Overridden Functions
Inheritance in the English Distance Class
Operation of ENGLEN
Constructors in DistSign
Member Functions in DistSign
Abetting Inheritance
Class Hierarchies
“Abstract” Base Class
Constructors and Member Functions
Inheritance and Graphics Shapes
Public and Private Inheritance
Access Combinations
Access Specifiers: When to Use What
Levels of Inheritance
Multiple Inheritance
Member Functions in Multiple Inheritance
private Derivation in EMPMULT
Constructors in Multiple Inheritance
Ambiguity in Multiple Inheritance
Containership: Classes Within Classes
Inheritance and Program Development
Summary
Questions
Exercises
CHAPTER 10—POINTERS
Addresses and Pointers
The Address-of Operator &
Pointer Variables
Syntax Quibbles
Accessing the Variable Pointed To
Pointer to void
Pointers and Arrays
Pointer Constants and Pointer Variables
Pointers and Functions
Passing Simple Variables
Passing Arrays
Sorting Array Elements
Pointers and C-type Strings
Pointers to String Constants
Strings As Function Arguments
Copying a String Using Pointers
Library String Functions
The const Modifier and Pointers
Arrays of Pointers to Strings
Memory Management: new and delete
The new Operator
The delete Operator
A String Class Using new
Pointers to Objects
Referring to Members
Another Approach to new
An Array of Pointers to Objects
A Linked List Example
A Chain of Pointers
Adding an Item to the List
Displaying the List Contents
Self-Containing Classes
Augmenting linklist
Pointers to Pointers
Sorting Pointers
The person** Data Type
Comparing Strings
A Parsing Example
Parsing Arithmetic Expressions
The PARSE Program
Simulation: A HORSE Race
Debugging Pointers
Summary
Questions
Exercises
CHAPTER 11—VIRTUAL FUNCTIONS
Finding An object’s class with TYPEID( )11
Virtual Functions
Normal Member Functions Accessed with Pointers
Virtual Member Functions Accessed with Pointers
Late Binding
Abstract Classes and Pure Virtual Functions
Virtual Functions and the person Class
Virtual Functions in a Graphics Example
Virtual Destructors
Virtual Base Classes
Friend Functions
Friends As Bridges
Breaching the Walls
English Distance Example
friends for Functional Notation
friend Classes
Static Functions
Accessing static Functions
Numbering the Objects
Investigating Destructors
Assignment and Copy Initialization
Overloading the Assignment Operator
The Copy Constructor
A Memory-Efficient String Class
The this Pointer
Accessing Member Data with this
Using this for Returning Values
Revised strimem Program
Dynamic Type Information
Checking the Type of a Class with dynamic_cast
Changing Pointer Types with dynamic_cast
The typeid Operator
Summary
Questions
Exercises
CHAPTER 12—STREAMS AND FILES
Stream Classes
Advantages of Streams
The Stream Class Hierarchy
The ios Class
The istream Class
The ostream Class
The iostream and the _withassign Classes
Stream Errors
Error-Status Bits
Inputting Numbers
Too Many Characters
No-Input Input
Inputting Strings and Characters
Error-Free Distances
Disk File I/O with Streams
Formatted File I/O
Strings with Embedded Blanks
Character I/O
Binary I/O
The reinterpret_cast Operator
Closing Files
Object I/O
I/O with Multiple Objects
File Pointers
Specifying the Position
Specifying the Offset
The tellg() Function
Error Handling in File I/O
Reacting to Errors
Analyzing Errors
File I/O with Member Functions
Objects That Read and Write Themselves
Classes That Read and Write Themselves
Overloading the Extraction and Insertion Operators
Overloading for cout and cin
Overloading for Files
Memory As a Stream Object
Command-Line Arguments
Printer Output
Summary
Questions
Exercises
CHAPTER 13—MULTIFILE PROGRAMS
Reasons for Multifile Programs
Class Libraries
Organization and Conceptualization
Creating a Multifile Program
Header Files
Directory
Projects
A Very Long Number Class
Numbers As Strings
The Class Specifier
The Member Functions
The Application Program
A High-Rise elevator Simulation
Running the ELEV Program
Designing the System
Listings for ELEV
Elevator Strategy
A Water-Distribution System
Components of a Water System
Flow, Pressure, and Back Pressure
Component Input and Output
Making Connections
Simplifying Assumptions
Program Design
Programming the Connections
Base and Derived Classes
The Component Base Class
The Flows-Into Operator
Derived Classes
The Switch Class
The PIPE_APP.CPP File
Summary
Questions
Projects
CHAPTER 14—TEMPLATES AND EXCEPTIONS
Function Templates
A Simple Function Template
Function Templates with Multiple Arguments
Class Templates
Class Name Depends on Context
A Linked List Class Using Templates
Storing User-Defined Data Types
Exceptions
Why Do We Need Exceptions?
Exception Syntax
A Simple Exception Example
Multiple Exceptions
Exceptions with the Distance Class
Exceptions with Arguments
Extracting Data from the Exception Object
The bad_alloc Class
Exception Notes
Summary
Questions
Exercises
CHAPTER 15—THE STANDARD TEMPLATE LIBRARY
Introduction to the STL
Containers
Algorithms
Iterators
Potential Problems with the STL
Algorithms
The find() Algorithm
The count() Algorithm
The sort() Algorithm
The search() Algorithm
The merge() Algorithm
Function Objects
The for_each() Algorithm
The transform() Algorithm
Sequential Containers
Vectors
Lists
Deques
Iterators
Iterators as Smart Pointers
Iterators as an Interface
Matching Algorithms with Containers
Iterators at Work
Specialized Iterators
Iterator Adapters
Stream Iterators
Associative Containers
Sets and Multisets
Maps and Multimaps
Storing User-Defined Objects
A Set of person Objects
A List of person Objects
Function Objects
Predefined Function Objects
Writing Your Own Function Objects
Function objects Used to Modify Container Behavior
Summary
Questions
Exercises
CHAPTER 16—OBJECT-ORIENTED DESIGN
Our Approach to OOD
CRC Cards
Use Cases
Class Diagrams
The Programming Problem
Hand-Written Forms
Assumptions
The CRC Modeling Team
Members of the Team
The Problem Summary Statement
Constructing the CRC Cards
Classes
Responsibilities
Collaborators
The Tenant CRC Card
The Expense CRC Card
The Rent Input Screen CRC card
The Rent Record CRC Card
The Expense Input Screen CRC Card
he Expense Record CRC Card
The Annual Report CRC Card
The User Interface CRC Card
The Scribe
Use Cases
Use Case 1: User Inputs an Expense
Use Case 2: The User Inputs a Rent
Trouble with the “User Inputs a Rent” Use Case
The Remaining Use Cases
Simplifications
Class Relationships
Attribute
Association
Navigability
Aggregation
Composition
Objects and Classes
Multiplicity
Generalization
Coupling and Cohesion
Class Diagrams
Arranging the CRC Cards
Associations in Landlord
Aggregations in Landlord
Writing the Program
The Header file
The .cpp Files
More Simplifications
Interacting with the Program
Prototyping
Final Thoughts
Summary
Questions
Projects
Appendix A
Appendix B
Appendix C
Appendix D
Appendix E
Appendix F
Appendix G
Appendix H
Index
Previous Table of Contents Next
Introduction
Object-Oriented Programming (OOP) is the most dramatic innovation in software development in
the last decade. It ranks in importance with the development of the first higher-level languages at
the dawn of the computer age. Sooner or later, every programmer will be affected by the objectoriented approach to program design.
Advantages of OOP
Why is everyone so excited about OOP? The chief problem with computer programs is complexity.
Large programs are probably the most complicated entities ever created by humans. Because of this
complexity, programs are prone to error, and software errors can be expensive and even life
threatening (in air-traffic control, for example). Object-Oriented Programming offers a new and
powerful way to cope with this complexity. Its goal is clearer, more reliable, more easily
maintained programs.
Languages and Development Platforms
Of the Object-Oriented Programming languages, C++ is by far the most widely used. (Java, a recent
addition to the field of OO languages, lacks certain features, such as pointers, that make it less
powerful and versatile than C++.)
In past years the standards for C++ have been in a state of evolution. This meant that each compiler
vendor handled certain details differently. However, in November 1997, the ANSI/ISO C++
standards committee approved the final draft of what is now known as Standard C++. (ANSI stands
for American National Standards Institute, and ISO stands for International Standards Institute.)
Standard C++ adds many new features to the language, such as the Standard Template Library
(STL). In this book we follow Standard C++ (except for a few places which we’ll note as we go
along).
The most popular development environments for C++ are manufactured by Microsoft and Borland
and run on the various flavors of Microsoft Windows. In this book we’ve attempted in ensure that
all example programs run on the current versions of both Borland and Microsoft compilers. (See
Appendixes C and D for more on these compilers.)
What this Book Does
This book teaches Object-Oriented Programming with the C++ programming language, using either
Microsoft or Borland compilers. It is suitable for professional programmers, students, and kitchen-