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

Moving from C to C++
Nội dung xem thử
Mô tả chi tiết
Moving from C to C++
Discussing Programming Problems, Why
They Exist, and How C++ Solves Them
Arunesh Goyal
Moving from C to C++: Discussing Programming Problems, Why They Exist, and How C++
Solves Them Copyright © 2013 by Arunesh Goyal This work is subject to copyright. All rights are
reserved by the Publisher, whether the whole or part of the material is concerned, specifically the
rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on
microfilms or in any other physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar methodology now known or
hereafter developed. Exempted from this legal reservation are brief excerpts in connection with
reviews or scholarly analysis or material supplied specifically for the purpose of being entered and
executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this
publication or parts thereof is permitted only under the provisions of the Copyright Law of the
Publisher’s location, in its current version, and permission for use must always be obtained from
Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center.
Violations are liable to prosecution under the respective Copyright Law.
ISBN-13 (pbk): 978-1-43026094-3
ISBN-13 (electronic): 978-1-4302-6095-0
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol
with every occurrence of a trademarked name, logo, or image we use the names, logos, and images
only in an editorial fashion and to the benefit of the trademark owner, with no intention of
infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they
are not identified as such, is not to be taken as an expression of opinion as to whether or not they are
subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for
any errors or omissions that may be made. The publisher makes no warranty, express or implied, with
respect to the material contained herein.
President and Publisher: Paul Manning Lead Editor: Saswata Mishra
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan
Ertel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James
Markham, Saswata Mishra, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas
Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Tom Welsh, Steve
Weiss Coordinating Editor: Anamika Panchoo Copy Editor: Mary Behr
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring
Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
[email protected], or visit www.springeronline.com. Apress Media, LLC
is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional
use. eBook versions and licenses are also available for most titles. For more information, reference
our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to
readers at www.apress.com. For detailed information about how to locate your book’s source
code, go to www.apress.com/source-code/.
For Maa, Paa, & Pals! :~
Contents at a Glance
About the Author
Acknowledgments
Introduction
Chapter 1: Introduction to Objects
Chapter 2: Making and Using Objects
Chapter 3: The C in C++
Chapter 4: Data Abstraction
Chapter 5: Hiding the Implementation
Chapter 6: Initialization and Cleanup
Chapter 7: Function Overloading and Default Arguments
Chapter 8: Constants
Chapter 9: Inline Functions
Chapter 10: Name Control
Chapter 11: References and the Copy Constructor
Chapter 12: Operator Overloading
Chapter 13: Dynamic Object Creation
Chapter 14: Inheritance and Composition
Chapter 15: Polymorphism and Virtual Functions
Chapter 16: Introduction to Templates
Chapter 17: Exception Handling
Chapter 18: Strings in Depth
Chapter 19: iostreams
Chapter 20: Runtime Type Identification (RTTI)
Chapter 21: Multiple Inheritance (MI)
Index
Contents
About the Author
Acknowledgments
Introduction
Chapter 1: Introduction to Objects
The Progress of Abstraction
An Object Has An Interface
The Hidden Implementation
Reusing the Implementation
Inheritance: Reusing the Interface
Is-a vs. is-like-a Relationships
Interchangeable Objects with Polymorphism
Creating and Destroying Objects
Exception Handling: Dealing with Errors
Analysis and Design
Phase 0: Make a Plan
Phase 1: What are we making?
Phase 2: How will we build it?
Phase 3: Build the Core
Phase 4: Iterate the Use Cases
Phase 5: Evolution
Plans Pay Off
Extreme Programming
Write Tests First
Pair Programming
Why C++ Succeeds
A Better C
You’re Already on the Learning Curve
Efficiency
Systems Are Easier to Express and Understand
Maximal Leverage with Libraries
Source-Code Reuse with Templates
Error Handling
Programming in the Large
Strategies for Transition
Guidelines
Management Obstacles
Review Session
Chapter 2: Making and Using Objects
The Process of Language Translation
Interpreters
Compilers
The Compilation Process
Static Type Checking
Tools for Separate Compilation
Declarations vs. Definitions
Function Declaration Syntax
Function Definitions
Variable Declaration Syntax
Including Headers
Standard C++ include Format
Linking
Using Libraries
How the Linker Searches a Library
Secret Additions
Using Plain C Libraries
Your First C++ Program
Using the iostream Class
Namespaces
Fundamentals of Program Structure
Running the Compiler
More About iostream
Character Array Concatenation
Reading Input
Calling Other Programs
Introducing Strings
Reading and Writing Files
Introducing Vector
Review Session
Chapter 3: The C in C++
Creating Functions
Function Return Values
Using the C Function Library
Creating Your Own Libraries with the Librarian
Controlling Execution
True and False
Using if-else
Using while
Using do-while
Using for
The break and continue Keywords
Using switch
Using and Misusing goto
Recursion
Introduction to Operators
Precedence
Auto-Increment and Auto-Decrement
Introduction to Data Types
Basic Built-in Types
Using bool, true, and false
Using Specifiers
Introduction to Pointers
Modifying the Outside Object
Introduction to C++ References
Pointers and References as Modifiers
Understanding Scoping
Defining Variables on the Fly
Specifying Storage Allocation
Global Variables
Local Variables
The static Keyword
The extern Keyword
Linkage
Constants
Constant Values
The volatile Qualifier
Operators and Their Use
Assignment
Mathematical Operators
Introduction to Preprocessor Macros
Relational Operators
Logical Operators
Bitwise Operators
Shift Operators
Unary Operators
The Ternary Operator
The Comma Operator
Common Pitfalls when Using Operators
Casting Operators
C++ Explicit Casts
sizeof—An Operator by Itself
The asm Keyword
Explicit Operators
Composite Type Creation
Aliasing Names with typedef
Combining Variables with struct
Pointers and structs
Clarifying Programs with enum
Type Checking for Enumerations
Saving Memory with union
Using Arrays
Pointers and Arrays
Exploring Floating-Point Format
Pointer Arithmetic
Debugging Hints
Debugging Flags
Turning Variables and Expressions into Strings
The C assert( ) Macro
Function Addresses
Defining a Function Pointer
Complicated Declarations and Definitions
Using a Function Pointer
Arrays of Pointers to Functions
Make Activities
Macros
Suffix Rules
Default Targets
An Example makefile
The I/O System
Review Session
Chapter 4: Data Abstraction
A Tiny C-like Library
Dynamic Storage Allocation
Bad Guesses
What’s wrong?
The Basic Object
What’s an object?
Abstract Data Typing
Object Details
Header File Etiquette
Importance of Header Files
The Multiple-Declaration Problem
The Preprocessor Directives:#define, #ifdef, #endif
A Standard for Header Files
Namespaces in Headers
Using Headers in Projects
Nested Structures
Global Scope Resolution
Review Session
Chapter 5: Hiding the Implementation
Setting Limits
C++ Access Control
Another Access Specifier: protected
Friends
Nested Friends
Is it pure?
Object Layout
The Class
Modifying Stash to Use Access Control
Modifying Stack to Use Access Control
Handle Classes
Hiding the Implementation
Reducing Recompilation
Review Session
Chapter 6: Initialization and Cleanup
Guaranteed Initialization with the Constructor
Guaranteed Cleanup with the Destructor
Elimination of the Definition Block
for loops
Storage Allocation
Stash with Constructors and Destructors
Stack with Constructors and Destructors
Aggregate Initialization
Default Constructors
Review Session
Chapter 7: Function Overloading and Default Arguments
More Name Decoration
Overloading on Return Values
Type-Safe Linkage
Overloading Example
Unions
Default Arguments
Placeholder Arguments
Choosing Overloading vs. Default Arguments
Review Session
Chapter 8: Constants
Value Substitution
const in Header Files
Safety consts
Aggregates
Differences with C
Pointers
Pointer to const
const Pointer
Assignment and Type Checking
Character Array Literals
Function Arguments and Return Values
Passing by const Value
Returning by const Value
Temporaries
Passing and Returning Addresses
Standard Argument Passing
Classes
const in Classes
The Constructor Initializer List
“Constructors” for Built-in Types
Compile-Time Constants in Classes
The “enum hack” in Old Code
const Objects and Member Functions
Mutable: Bitwise vs. Logical const
ROMability
The volatile Keyword
Review Session
Chapter 9: Inline Functions
Preprocessor Pitfalls
Macros and Access
Inline Functions
Inlines Inside Classes