C++ in 24 Hours, Sams Teach Yourself 0672337460, 9780672337468

Sams Teach Yourself C++ in 24 Hours is a hands-on guide to the C++ programming language. Readers are provided with short

328 64 4MB

English Pages 480 [475] Year 2016

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Cover
Title Page
Copyright Page
Table of Contents
Introduction
Part I: Beginning C++
HOUR 1: Writing Your First Program
Using C++
Compiling and Linking the Source Code
Creating Your First Program
HOUR 2: Organizing the Parts of a Program
Reasons to Use C++
The Parts of a Program
Comments
Functions
HOUR 3: Creating Variables and Constants
What Is a Variable?
Defining a Variable
Assigning Values to Variables
Using Type Definitions
Constants
Auto-Typed Variables
HOUR 4: Using Expressions, Statements, and Operators
Statements
Expressions
Operators
If-Else Conditional Statements
Logical Operators
Tricky Expression Values
HOUR 5: Calling Functions
What Is a Function?
Declaring and Defining Functions
Using Variables with Functions
Function Parameters
Returning Values from Functions
Default Function Parameters
Overloading Functions
Auto-Typed Return Values
HOUR 6: Controlling the Flow of a Program
Looping
while Loops
do-while Loops
for Loops
switch Statements
HOUR 7: Storing Information in Arrays and Strings
What Is an Array?
Writing Past the End of Arrays
Initializing Arrays
Multidimensional Arrays
Character Arrays
Copying Strings
Reading Arrays with Foreach Loops
Part II: Classes
HOUR 8: Creating Basic Classes
What Is a Type?
Creating New Types
Classes and Members
Accessing Class Members
Private Versus Public Access
Implementing Member Functions
Creating and Deleting Objects
HOUR 9: Moving into Advanced Classes
const Member Functions
Interface Versus Implementation
Organizing Class Declarations and Function Definitions
Inline Implementation
Classes with Other Classes as Member Data
Part III: Memory Management
HOUR 10: Creating Pointers
Understanding Pointers and Their Usage
The Stack and the Heap
Null Pointer Constant
HOUR 11: Developing Advanced Pointers
Creating Objects on the Heap
Deleting Objects
Accessing Data Members Using Pointers
Member Data on the Heap
The this Pointer
Stray or Dangling Pointers
const Pointers
const Pointers and const Member Functions
HOUR 12: Creating References
What is a Reference?
Creating a Reference
Using the Address of Operator on References
What Can Be Referenced?
Null Pointers and Null References
Passing Function Arguments by Reference
Understanding Function Headers and Prototypes
Returning Multiple Values
HOUR 13: Developing Advanced References and Pointers
Passing by Reference for Efficiency
Passing a const Pointer
References as an Alternative to Pointers
When to Use References and When to Use Pointers
References to Objects Not in Scope
Returning a Reference to an Object on the Heap
Pointer, Pointer, Who Has the Pointer?
Part IV: Advanced C++
HOUR 14: Calling Advanced Functions
Overloaded Member Functions
Using Default Values
Initializing Objects
The Copy Constructor
Compile-Time Constant Expressions
HOUR 15: Using Operator Overloading
Operator Overloading
Conversion Operators
Part V: Inheritance and Polymorphism
HOUR 16: Extending Classes with Inheritance
What Is Inheritance?
Private Versus Protected
Constructors and Destructors
Passing Arguments to Base Constructors
Overriding Functions
HOUR 17: Using Polymorphism and Derived Classes
Polymorphism Implemented with Virtual Member Functions
How Virtual Member Functions Work
HOUR 18: Making Use of Advanced Polymorphism
Problems with Single Inheritance
Abstract Data Types
Part VI: Special Topics
HOUR 19: Storing Information in Linked Lists
Linked Lists and Other Structures
Linked List Case Study
Linked Lists as Objects
HOUR 20: Using Special Classes, Functions, and Pointers
Static Member Data
Static Member Functions
Containment of Classes
Friend Classes and Functions
HOUR 21: Using New Features of C++14
The Newest Version of C++
Using auto in Function Return Types
Improved Numeric Literals
The constexpr Keyword
Lambda Expressions
HOUR 22: Employing Object-Oriented Analysis and Design
The Development Cycle
Simulating an Alarm System
PostMaster: A Case Study
HOUR 23: Creating Templates
What Are Templates?
Instances of the Template
Template Definition
Using Template Items
HOUR 24: Dealing with Exceptions and Error Handling
Bugs, Errors, Mistakes, and Code Rot
Handling the Unexpected
Exceptions
Using try and catch Blocks
Writing Professional-Quality Code
Part VII: Appendixes
APPENDIX A: Binary and Hexadecimal
Other Bases
Around the Bases
Hexadecimal
APPENDIX B: Glossary
A
B
C
D
E
F
G
H
I
L
M
O
P
R
S
T
U
V
W
APPENDIX C: This Book’s Website
APPENDIX D: Using the MinGW C++ Compiler on Windows
Downloading MinGW-w64
Setting the Path Environment Variable
Testing Your Installation
Index
A
B
C
D
E
F
G
H
I
J-K
L
M
N
O
P
Q-R
S
T
U
V
W-X-Y-Z
Recommend Papers

C++ in 24 Hours, Sams Teach Yourself
 0672337460, 9780672337468

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Contents at a Glance

SamsTeach Yourself

Part I: Beginning C++

When you only have time for the answers™

1 Writing Your First Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 Organizing the Parts of a Program.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3 Creating Variables and Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4 Using Expressions, Statements, and Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

Whatever your need and whatever your time frame, there’s a Sams Teach Yourself book for you. With a Sams Teach Yourself book as your guide, you can quickly get up to speed on just about any new product or technology—in the absolute shortest period of time possible. Guaranteed. Learning how to do new things with your computer shouldn’t be tedious or time-consuming. Sams Teach Yourself makes learning anything quick, easy, and even a little bit fun.

5 Calling Functions.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 6 Controlling the Flow of a Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 7 Storing Information in Arrays and Strings.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

Part II: Classes 8 Creating Basic Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 9 Moving into Advanced Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

Part III: Memory Management 10 Creating Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 11 Developing Advanced Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

Java in 24 Hours

12 Creating References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

Rogers Cadenhead

13 Developing Advanced References and Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

ISBN-13: 978-0-672-33702-4

Part IV: Advanced C++ 14 Calling Advanced Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 15 Using Operator Overloading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

Part V: Inheritance and Polymorphism 16 Extending Classes with Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 17 Using Polymorphism and Derived Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 18 Making Use of Advanced Polymorphism. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

Part VI: Special Topics 19 Storing Information in Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 20 Using Special Classes, Functions, and Pointers.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 21 Using New Features of C++14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 22 Employing Object-Oriented Analysis and Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 23 Creating Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373

jQuery and JavaScript in 24 Hours

JavaScript in 24 Hours

Visual Basic 2015 in 24 Hours

Brad Dayley

Phil Ballard

James Foxall

ISBN-13: 978-0-672-33734-5

ISBN-13: 978-0-672-33738-3

ISBN-13: 978-0-672-33745-1

HTML, CSS and JavaScript All in One Julie C. Meloni ISBN-13: 978-0-672-33714-7

24 Dealing with Exceptions and Error Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

Part VII: Appendixes A Binary and Hexadecimal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 B Glossary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 C This Book’s Website. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427

Sams Teach Yourself books are available at most retail and online bookstores. For more information or to order direct, visit our online bookstore at informit.com/teachyourself. Online editions of all Sams Teach Yourself titles are available by subscription from Safari Books Online at safari.informit.com.

D Using the MinGW C++ Compiler on Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

7/12/16 4:09 PM

Rogers Cadenhead Jesse Liberty

SamsTeach Yourself

C++

24 Hours in

SIXTH EDITION

800 East 96th Street, Indianapolis, Indiana, 46240 USA

Sams Teach Yourself C++ in 24 Hours Copyright © 2017 by Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms, and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/. 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. ISBN-13: 978-0-672-33746-8 ISBN-10: 0-672-33746-0 Library of Congress Control Number: 2016945006 First Printing August 2016

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.

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 authors 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 or programs accompanying it.

Special Sales For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at [email protected] or (800) 382-3419. For government sales inquiries, please contact [email protected].  For questions about sales outside the U.S., please contact [email protected]

Editor Mark Taber Project Editor Lori Lyons Project Manager Prashanthi Nadipalli Copy Editor Christopher Morris Technical Editor Jon Upchurch

Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Part I: Beginning C++ HOUR 1 Writing Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 Organizing the Parts of a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3 Creating Variables and Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4 Using Expressions, Statements, and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 5 Calling Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 6 Controlling the Flow of a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 7 Storing Information in Arrays and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

Part II: Classes HOUR 8 Creating Basic Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 9 Moving into Advanced Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

Part III: Memory Management HOUR 10 Creating Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 11 Developing Advanced Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 12 Creating References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 13 Developing Advanced References and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

Part IV: Advanced C++ HOUR 14 Calling Advanced Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 15 Using Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

Part V: Inheritance and Polymorphism HOUR 16 Extending Classes with Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 17 Using Polymorphism and Derived Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 18 Making Use of Advanced Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

iv

Sams Teach Yourself C++ in 24 Hours

Part VI: Special Topics HOUR 19 Storing Information in Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 20 Using Special Classes, Functions, and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 21 Using New Features of C++14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 22 Employing Object-Oriented Analysis and Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 23 Creating Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 24 Dealing with Exceptions and Error Handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

Part VII: Appendixes A Binary and Hexadecimal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 B Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419 C This Book’s Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 D Using the MinGW C++ Compiler on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439

Table of Contents Introduction

1

Part I: Beginning C++ HOUR 1: Writing Your First Program Using C++

5

.................................................................................................

5

Compiling and Linking the Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Creating Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 HOUR 2: Organizing the Parts of a Program

13

Reasons to Use C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 The Parts of a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 HOUR 3: Creating Variables and Constants

27

What Is a Variable? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Defining a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Using Type Definitions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Auto-Typed Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 HOUR 4: Using Expressions, Statements, and Operators Statements

..............................................................................................

43 43

Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 If-Else Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Tricky Expression Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

vi

Sams Teach Yourself C++ in 24 Hours

HOUR 5: Calling Functions

61

What Is a Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Declaring and Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Using Variables with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Function Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Returning Values from Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Default Function Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Overloading Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Auto-Typed Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 HOUR 6: Controlling the Flow of a Program

79

Looping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 do-while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 HOUR 7: Storing Information in Arrays and Strings What Is an Array?

95

...................................................................................

95

Writing Past the End of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Initializing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Character Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Copying Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Reading Arrays with Foreach Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Part II: Classes HOUR 8: Creating Basic Classes

109

What Is a Type? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Creating New Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Classes and Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Accessing Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Private Versus Public Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Implementing Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Creating and Deleting Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

Table of Contents

HOUR 9: Moving into Advanced Classes

vii

123

const Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Interface Versus Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Organizing Class Declarations and Function Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Inline Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Classes with Other Classes as Member Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

Part III: Memory Management HOUR 10: Creating Pointers

135

Understanding Pointers and Their Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 The Stack and the Heap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Null Pointer Constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 HOUR 11: Developing Advanced Pointers

155

Creating Objects on the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Deleting Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Accessing Data Members Using Pointers

..................................................

157

Member Data on the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 The this Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Stray or Dangling Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 const Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 const Pointers and const Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 HOUR 12: Creating References

167

What is a Reference? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Creating a Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Using the Address of Operator on References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 What Can Be Referenced? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Null Pointers and Null References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Passing Function Arguments by Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Understanding Function Headers and Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Returning Multiple Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 HOUR 13: Developing Advanced References and Pointers

183

Passing by Reference for Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Passing a const Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

viii

Sams Teach Yourself C++ in 24 Hours

References as an Alternative to Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 When to Use References and When to Use Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 References to Objects Not in Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Returning a Reference to an Object on the Heap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Pointer, Pointer, Who Has the Pointer? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

Part IV: Advanced C++ HOUR 14: Calling Advanced Functions

199

Overloaded Member Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Using Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Initializing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 The Copy Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Compile-Time Constant Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 HOUR 15: Using Operator Overloading

213

Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Conversion Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

Part V: Inheritance and Polymorphism HOUR 16: Extending Classes with Inheritance

231

What Is Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Private Versus Protected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Constructors and Destructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Passing Arguments to Base Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Overriding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 HOUR 17: Using Polymorphism and Derived Classes

251

Polymorphism Implemented with Virtual Member Functions . . . . . . . . . . . . . . . . . . . . . 251 How Virtual Member Functions Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 HOUR 18: Making Use of Advanced Polymorphism

267

Problems with Single Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

Table of Contents

ix

Part VI: Special Topics HOUR 19: Storing Information in Linked Lists

287

Linked Lists and Other Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Linked List Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Linked Lists as Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 HOUR 20: Using Special Classes, Functions, and Pointers

301

Static Member Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 Static Member Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 Containment of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Friend Classes and Functions

..................................................................

HOUR 21: Using New Features of C++14

312

331

The Newest Version of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Using auto in Function Return Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 Improved Numeric Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 The constexpr Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 HOUR 22: Employing Object-Oriented Analysis and Design

343

The Development Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Simulating an Alarm System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 PostMaster: A Case Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 HOUR 23: Creating Templates

373

What Are Templates? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Instances of the Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Template Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Using Template Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 HOUR 24: Dealing with Exceptions and Error Handling

391

Bugs, Errors, Mistakes, and Code Rot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 Handling the Unexpected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 Using try and catch Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 Writing Professional-Quality Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403

x

Sams Teach Yourself C++ in 24 Hours

Part VII: Appendixes APPENDIX A: Binary and Hexadecimal

411

Other Bases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412 Around the Bases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412 Hexadecimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 APPENDIX B: Glossary

419

APPENDIX C: This Book’s Website

427

APPENDIX D: Using the MinGW C++ Compiler on Windows

429

Downloading MinGW-w64. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 Setting the Path Environment Variable

....................................................

431

Testing Your Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433

Index

439

About the Authors Rogers Cadenhead is a writer, computer programmer, and web developer who has written more than 25 books on Internet-related topics, including Sams Teach Yourself Java in 21 Days and Absolute Beginner’s Guide to Minecraft Mods Programming. He publishes the Drudge Retort and other websites that receive more than 22 million visits a year. This book’s official website is at http://cplusplus.cadenhead.org. Jesse Liberty is the author of numerous books on software development, including best-selling titles on C++ and .NET. He is the president of Liberty Associates, Inc. (www.libertyassociates.com), where he provides custom programming, consulting, and training.

We Want to Hear from You! As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we’re doing right, what we could do better, what areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass our way. We welcome your comments. You can email or write to let us know what you did or didn’t like about this book—as well as what we can do to make our books better. Please note that we cannot help you with technical problems related to the topic of this book. When you write, please be sure to include this book’s title and author as well as your name and email address. We will carefully review your comments and share them with the author and editors who worked on the book. Email: Mail:

[email protected] Sams Publishing ATTN: Reader Feedback 800 East 96th Street Indianapolis, IN 46240 USA

Reader Services Visit our website and register this book at informit.com/register for convenient access to any updates, downloads, or errata that might be available for this book.

Introduction

Congratulations! By reading this sentence, you are already 20 seconds closer to learning C++, one of the most important programming languages in the world. If you continue for another 23 hours, 59 minutes, and 40 seconds, you will master the fundamentals of the C++ programming language. Twenty-four one-hour lessons cover important features such as managing I/O, creating loops and arrays, using object-oriented programming with templates, and creating C++ programs. All of this has been organized into well-structured, easy-to-follow lessons. There are working projects that you create—complete with output and an analysis of the code—to illustrate the topics of the hour. Syntax examples are clearly marked for handy reference. To help you become more proficient, each hour ends with a set of common questions and answers.

Who Should Read This Book? You don’t need any previous experience in programming to learn C++ with this book. It starts with the basics and teaches you both the language and the concepts involved with programming C++. Whether you are just beginning or already have some experience programming, you will find that this book makes learning C++ fast and easy.

Should I Learn C First? No, you don’t need to learn C first. C++ is a much more powerful and versatile language that was created by Bjarne Stroustrup as a successor to C. Learning C first can lead you into some programming habits that are more error-prone than what you’ll do in C++. This book does not assume that readers are familiar with C.

2

Introduction

Why Should I Learn C++? You could be learning a lot of other languages, but C++ is valuable to learn because it has stood the test of time and continues to be a popular choice for modern programming. Despite being created in 1979, C++ is still being used for professional software today because of the power and flexibility of the language. There’s even a new version of the language, called C++14, that makes it even more useful. Because other languages such as Java were inspired by C++, learning the language can provide you insight into them, as well. Mastering C++ gives you portable skills that you can use on just about any platform on the market today, from desktop computers to Linux servers, mobile devices, videogame consoles, and mainframes.

What If I Don’t Want This Book? I’m sorry you feel that way, but these things happen sometimes. Please reshelve this book with the front cover facing outward on an endcap with access to a lot of the store’s foot traffic.

Conventions Used in This Book This book contains special elements as described here.

NOTE These boxes provide additional information to the material you just read.

CAUTION These boxes focus your attention on problems or side effects that can occur in specific situations.

TIP These boxes give you tips and highlight information that can make your C++ programming more efficient and effective.

When you see this symbol, you know that what you see next will show the output from a code listing/example.

Conventions Used in This Book

3

This book uses various typefaces: X

To help you distinguish C++ code from regular English, actual C++ code is typeset in a special monospace font.

X

Placeholders—words or characters temporarily used to represent the real words or characters you would type in code—are typeset in italic monospace.

X

New or important terms are typeset in italic.

X

In the listings in this book, each real code line is numbered. If you see an unnumbered line in a listing, you’ll know that the unnumbered line is really a continuation of the preceding numbered code line (some code lines are too long for the width of the book). In this case, you should type the two lines as one; do not divide them.

This page intentionally left blank

HOUR 1

Writing Your First Program

What You’ll Learn in This Hour: X

How and why C++ was invented

X

How to find a C++ compiler

X

How to create and compile your first program

X

How to link and run the program

Using C++ In 1979, a Danish computer scientist at Bell Labs in the United States began work on an enhancement to the C programming language. Bjarne Stroustrop explained on his personal website that he wanted a language “in which I could write programs that were both efficient and elegant.” A lot of other people wanted that too. Stroustrop’s creation, which he dubbed C++, has held a spot among the world’s top programming languages for decades. Many programming trends have come and gone over the years, but this language remains a contemporary and useful choice for software development on desktop computers, servers, embedded devices like phones, and many other computing environments. C++ is a portable language that works equally well on Microsoft Windows, Apple Mac OS, Linux, and UNIX systems. The best way to learn the language is to write programs without regard to the operating system the program runs on. Sams Teach Yourself C++ in 24 Hours offers a hands-on introduction to the language that makes absolutely no assumptions about your operating system. The book can achieve this because it covers standard C++ (also called ANSI/ISO C++), the internationally agreed-upon version of the language, which is portable to any platform and development environment. The code presented throughout the book is standard ANSI/ISO C++ and should work with any development environment for C++ that’s up-to-date.

6

HOUR 1: Writing Your First Program

New features that are introduced in C++14, the language’s current version, also are covered. Popular C++ development environments support this version, which has 14 as part of its name because it was released in 2014. C++ programs are developed by a set of tools that work together called the compiler and linker. A compiler turns C++ programs into a form that can be run. The compiler translates a program from a human-readable form called source code into a machine-runnable form called machine code. The compiler produces an object file. From the object file, a linker builds an executable file that can be run. There are several popular environments for C++ programming that you might have used before or know how to obtain. Some of these are GCC (the GNU Compiler Collection), Microsoft Visual Studio, NetBeans, and Embarcadero C++ Builder. If you have an up-to-date C++ compiler on your system and know the basics of how to use it, you will have no trouble completing the programming projects in this book. If you don’t have a C++ compiler, don’t know how to use a compiler, or don’t know how to find one, relax. You can learn how to download and install an excellent free compiler, GCC, in Appendix D, “Using the MinGW C++ Compiler on Windows.” Do that now if you need a compiler before you proceed in this book. Microsoft Visual Studio also supports C++ programming, and a free version called Visual Studio Community is available from the website www.visualstudio.com. Some guidance is offered in this book for people learning C++ with Visual Studio.

Compiling and Linking the Source Code Before you create your first C++ program later this hour, it’s worthwhile to understand the process. C++ programs begin as source code, which is just text typed into an editor such as Windows WordPad, Gedit, Emacs, or Vi. Although Microsoft Word and other word processors can save files as plain text, you should use a simpler editor for programming because you don’t need all the formatting and presentation capabilities of a word processor. Source code consists of plain text with no special formatting.

NOTE There’s a free text editor for Windows called Notepad++ that provides excellent support for C++ programming. It includes features you’ll come to appreciate as you get experience creating your own programs, such as code highlighting, which displays different elements of the language in different colors. You can download it from https://notepad-plus-plus.org.

Creating Your First Program

7

The source code files you create for C++ can be given filenames ending with the extensions

.cpp, .cxx, .cp, or .c. This book names all source code files with the .cpp extension, the most common choice for C++ programmers and the default for some compilers. Most C++ compilers don’t care about the extension given to source code, but using .cpp consistently helps you identify source code files. Source code is the human-readable form of a C++ program. It can’t be run until it is compiled and linked. After your source code is compiled, an object file is produced. This file is turned into an executable program by a linker. C++ programs are created by linking together one or more object files with one or more libraries. A library is a collection of linkable files that provide useful functions and classes that you can rely on in your programs. A function is a block of code that performs a task, such as multiplying two numbers or displaying text. A class is the definition of a new type of data and related functions. Here are the steps to create a C++ program:

1. Create a source code file with a text editor. 2. Use a compiler to convert the source code into an object file. 3. Use a linker to link the object file and any necessary libraries to produce an executable program.

4. Type the name of the executable to run it. The GCC compiler can handle compiling and linking in a single step.

Creating Your First Program Now that you’ve been introduced to the process, it’s time to create your first C++ program and give the compiler a test drive. Launch the text editor you’re using to create programs and open a new file. The first program that you create will display text on the screen. Type the text of Listing 1.1 into the editor. Ignore the numbers along the left side of the listing and the colons that follow them—these are there simply for reference purposes in this book. As you type, make sure to enter the punctuation on each line properly, such as the :: and