100% found this document useful (1 vote)
21 views

(Ebook) An Introduction to Programming with C++, 6th Edition by Diane Zak ISBN 9780538466523, 0538466529 pdf download

The document is a promotional listing for various programming ebooks, including 'An Introduction to Programming with C++' by Diane Zak, along with several editions of Visual Basic programming books. It provides ISBN numbers and download links for each ebook. Additionally, it includes a brief overview of the contents and structure of the C++ programming book.

Uploaded by

selshkozue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
21 views

(Ebook) An Introduction to Programming with C++, 6th Edition by Diane Zak ISBN 9780538466523, 0538466529 pdf download

The document is a promotional listing for various programming ebooks, including 'An Introduction to Programming with C++' by Diane Zak, along with several editions of Visual Basic programming books. It provides ISBN numbers and download links for each ebook. Additionally, it includes a brief overview of the contents and structure of the C++ programming book.

Uploaded by

selshkozue
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

(Ebook) An Introduction to Programming with C++,

6th Edition by Diane Zak ISBN 9780538466523,


0538466529 download

https://ebooknice.com/product/an-introduction-to-programming-
with-c-6th-edition-2427428

Explore and download more ebooks at ebooknice.com


We have selected some products that you may be interested in
Click the link to download now or visit ebooknice.com
for more options!.

(Ebook) An Introduction to Programming with C++ by Diane Zak


ISBN 9781285860114, 128586011X

https://ebooknice.com/product/an-introduction-to-programming-with-c-5291684

(Ebook) Biota Grow 2C gather 2C cook by Loucas, Jason; Viles,


James ISBN 9781459699816, 9781743365571, 9781925268492,
1459699815, 1743365578, 1925268497

https://ebooknice.com/product/biota-grow-2c-gather-2c-cook-6661374

(Ebook) Programming with Microsoft Visual Basic 2010, 5th


Edition by Diane Zak ISBN 1111529434

https://ebooknice.com/product/programming-with-microsoft-visual-
basic-2010-5th-edition-2357262

(Ebook) Programming with Microsoft Visual Basic 2012 by Diane


Zak ISBN 9781285077925, 128507792X

https://ebooknice.com/product/programming-with-microsoft-visual-
basic-2012-6809572
(Ebook) Programming with Microsoft Visual Basic 2015 by Diane
Zak ISBN 9781285860268, 1285860268

https://ebooknice.com/product/programming-with-microsoft-visual-
basic-2015-5312274

(Ebook) Programming with Microsoft Visual Basic 2017 by Diane


Zak ISBN 9781337102124, 1337102121

https://ebooknice.com/product/programming-with-microsoft-visual-
basic-2017-6732858

(Ebook) Clearly Visual Basic: Programming with Microsoft Visual


Basic 2012 by Diane Zak ISBN 9781285084107, 1285084101

https://ebooknice.com/product/clearly-visual-basic-programming-with-
microsoft-visual-basic-2012-4644122

(Ebook) Clearly Visual Basic: Programming with Microsoft Visual


Basic 2010 by Diane Zak ISBN 9781111530150, 1111530157

https://ebooknice.com/product/clearly-visual-basic-programming-with-
microsoft-visual-basic-2010-2382420

(Ebook) Primary Mathematics Workbook 2B by Jennifer Hoerst ISBN


9780761469933, 0761469931

https://ebooknice.com/product/primary-mathematics-workbook-2b-23520620
AN INTRODUCTION TO
PROGRAMMING WITH C++
This page intentionally left blank
SIXTH EDITION

AN INTRODUCTION TO
PROGRAMMING WITH C++

DIANE ZAK

Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
An Introduction to Programming with C++, © 2011 Course Technology, Cengage Learning
Sixth Edition
ALL RIGHTS RESERVED. No part of this work covered by the copyright herein
Diane Zak may be reproduced, transmitted, stored or used in any form or by any means—
Executive Editor: Marie Lee graphic, electronic, or mechanical, including but not limited to photocopying,
recording, scanning, digitizing, taping, Web distribution, information networks,
Acquisitions Editor: Amy Jollymore
or information storage and retrieval systems, except as permitted under
Freelance Product Manager: Tricia Coia Section 107 or 108 of the 1976 United States Copyright Act—without the prior
Senior Content Project Manager: Jill Braiewa written permission of the publisher.
Editorial Assistant: Zina Kresin
For product information and technology assistance, contact us at
Art Director: Marissa Falco
Cengage Learning Customer & Sales Support, 1-800-354-9706
Text Designer: Shawn Girsberger For permission to use material from this text or product,
Print Buyer: Julio Esperas submit all requests online at cengage.com/permissions
Further permissions questions can be emailed to
Proofreader: Andy Smith, Green Pen QA
[email protected]
Indexer: Michael Brackney
Compositor: Integra Software Services
Library of Congress Control Number: 2009941969
ISBN-13: 978-0-538-46652-3
ISBN-10: 0-538-46652-9

Course Technology
20 Channel Center Street
Boston, MA 02210
USA

Some of the product names and company names used in this book have been
used for identification purposes only and may be trademarks or registered
trademarks of their respective manufacturers and sellers.

Course Technology, a part of Cengage Learning, reserves the right to revise this
publication and make changes from time to time in its content without notice.

Cengage Learning is a leading provider of customized learning solutions with


office locations around the globe, including Singapore, the United Kingdom,
Australia, Mexico, Brazil, and Japan. Locate your local office at:
www.cengage.com/global

Cengage Learning products are represented in Canada by Nelson Education, Ltd.

To learn more about Course Technology, visit


www.cengage.com/coursetechnology

Purchase any of our products at your local college store or at our preferred
online store: www.CengageBrain.com

Printed in the United States of America


1 2 3 4 5 6 7 16 15 14 13 12 11 10
This page intentionally left blank
Brief Contents
vi

P ref ac e . . . . . . . . . . . . . . . . . . . . . . xiv

CH AP T ER 1 An I n t rod u c t i on to P ro g r a mmi n g . . . . . . . . . . . . 1

CH AP T ER 2 B eg i n n i n g t h e P ro b l e m- S o l v i n g P ro ce s s . . . . . . . . 22

CH AP T ER 3 Vari abl es an d C o n s ta n ts . . . . . . . . . . . . . . 51

CH AP T ER 4 C om p l et i n g t h e P ro b l e m- S o l vi n g P ro ce s s . . . . . . . 77

CH AP T ER 5 Th e Sel ec t i on S tr u ctu re . . . . . . . . . . . . . . 119

CH AP T ER 6 M ore on t h e Sel e cti o n S tr u ctu re . . . . . . . . . . . 163

CH AP T ER 7 Th e R ep et i t i on S tr u ctu re . . . . . . . . . . . . . . 213

CH AP T ER 8 M ore on t h e R ep e ti ti o n S tr u ctu re . . . . . . . . . . 264

CH AP T ER 9 Val u e-R et u rn i n g F u n cti o n s . . . . . . . . . . . . . 308

CH AP T ER 10 Voi d Fu n c t i on s . . . . . . . . . . . . . . . . . . 370

CH AP T ER 11 O n e-D i m en si on a l A r r a y s . . . . . . . . . . . . . . 419

CH AP T ER 12 Tw o-D i m en si on a l A r r a y s . . . . . . . . . . . . . . 486

CH AP T ER 13 St ri n g s . . . . . . . . . . . . . . . . . . . . . . 524

CH AP T ER 14 Seq u en t i al Ac c e s s F i l e s . . . . . . . . . . . . . . 582

AP P EN D IX A A n sw ers t o M i n i - Q u i z z e s a n d L a b s . . . . . . . . . . 626

AP P EN D IX B C + + Keyw ord s . . . . . . . . . . . . . . . . . . 690


AP P EN D IX C ASC I I C od es . . . . . . . . . . . . . . . . . . . 691

AP P EN D IX D H ow t o U se M i c ro s o ft V i s u a l C + + . . . . . . . . . . 693

AP P EN D IX E H ow t o U se D ev- C + + . . . . . . . . . . . . . . . . 694
vii
AP P EN D IX F C l asses an d O b j e cts . . . . . . . . . . . . . . . . 695

I n d ex . . . . . . . . . . . . . . . . . . . . . . 721
Contents
viii

P ref ac e . . . . . . . . . . . . . . . . . . . . . . xiv

CH AP T ER 1 A n I n t rod u c t i on to P ro gr a mmi n g . . . . . . . . . . . . 1
Programming a Computer . . . . . . . . . . . . . . . . . . . . . . . 2
The Programmer’s Job . . . . . . . . . . . . . . . . . . . . . . . 2
Do I Have What It Takes to Be a Programmer? . . . . . . . . . . . . . 2
Employment Opportunities . . . . . . . . . . . . . . . . . . . . . 3
A Brief History of Programming Languages . . . . . . . . . . . . . . . . 4
Machine Languages . . . . . . . . . . . . . . . . . . . . . . . . 4
Assembly Languages . . . . . . . . . . . . . . . . . . . . . . . . 4
High-Level Languages. . . . . . . . . . . . . . . . . . . . . . . . 4
Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The Sequence Structure . . . . . . . . . . . . . . . . . . . . . . 6
The Selection Structure . . . . . . . . . . . . . . . . . . . . . . . 7
The Repetition Structure . . . . . . . . . . . . . . . . . . . . . . 9
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

CH AP T ER 2 B eg i n n i n g t h e P ro b l e m- S o l v i n g P ro ce s s . . . . . . . . 22
Problem Solving . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Solving Everyday Problems . . . . . . . . . . . . . . . . . . . . . . . 23
Creating Computer Solutions to Problems . . . . . . . . . . . . . . . . 24
Step 1—Analyze the Problem . . . . . . . . . . . . . . . . . . . . . . 25
Step 2—Plan the Algorithm . . . . . . . . . . . . . . . . . . . . . . . 27
Step 3—Desk-Check the Algorithm . . . . . . . . . . . . . . . . . . . 31
The Gas Mileage Problem . . . . . . . . . . . . . . . . . . . . . . . . 34
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

CH AP T ER 3 Vari abl es an d Co n s ta n ts . . . . . . . . . . . . . . 51
Beginning Step 4 in the Problem-Solving Process . . . . . . . . . . . . . 52
Internal Memory . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Selecting a Name for a Memory Location . . . . . . . . . . . . . . . . 53
Revisiting the Treyson Mobley Problem . . . . . . . . . . . . . . . . 54
Selecting a Data Type for a Memory Location . . . . . . . . . . . . . . 55
How Data Is Stored in Internal Memory . . . . . . . . . . . . . . . . 57
Selecting an Initial Value for a Memory Location . . . . . . . . . . . . . 60
Declaring a Memory Location . . . . . . . . . . . . . . . . . . . . . . 62
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 ix
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

CH AP T ER 4 C om p l et i n g t h e P ro b l e m- S o l v i n g P ro ce s s . . . . . . . . 77
Finishing Step 4 in the Problem-Solving Process . . . . . . . . . . . . . 78
Getting Data from the Keyboard . . . . . . . . . . . . . . . . . . . . . 79
Displaying Messages on the Computer Screen . . . . . . . . . . . . . . 81
Arithmetic Operators in C++ . . . . . . . . . . . . . . . . . . . . . . 83
Type Conversions in Arithmetic Expressions . . . . . . . . . . . . . 84
The static_cast Operator . . . . . . . . . . . . . . . . . . . . 86
Assignment Statements . . . . . . . . . . . . . . . . . . . . . . . . 87
Step 5—Desk-Check the Program . . . . . . . . . . . . . . . . . . . . 90
Step 6—Evaluate and Modify the Program . . . . . . . . . . . . . . . . 92
Arithmetic Assignment Operators . . . . . . . . . . . . . . . . . . . . 97
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

CH AP T ER 5 Th e Sel ec t i on S tr u ctu re . . . . . . . . . . . . . . 119


Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Flowcharting a Selection Structure . . . . . . . . . . . . . . . . . . . 123
Coding a Selection Structure in C++. . . . . . . . . . . . . . . . . . . 125
Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . 127
Swapping Numeric Values . . . . . . . . . . . . . . . . . . . . . . 128
Displaying the Sum or Difference . . . . . . . . . . . . . . . . . . 130
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Using the Truth Tables . . . . . . . . . . . . . . . . . . . . . . . 134
Calculating Gross Pay . . . . . . . . . . . . . . . . . . . . . . . 135
Pass/ Fail Program . . . . . . . . . . . . . . . . . . . . . . . . . 137
Converting a Character to Uppercase or Lowercase . . . . . . . . . . . . 140
Formatting Numeric Output . . . . . . . . . . . . . . . . . . . . . . . 141
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

CH AP T ER 6 M ore on t h e Sele cti o n S tr u ctu re . . . . . . . . . . . 163


Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Flowcharting a Nested Selection Structure . . . . . . . . . . . . . . . . 168
Coding a Nested Selection Structure . . . . . . . . . . . . . . . . . . 170
CONTENTS

Logic Errors in Selection Structures . . . . . . . . . . . . . . . . . . . 173


First Logic Error: Using a Compound Condition Rather Than a
Nested Selection Structure . . . . . . . . . . . . . . . . . . . . . 175
Second Logic Error: Reversing the Outer and Nested Decisions . . . . 177
Third Logic Error: Using an Unnecessary Nested Selection
Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
x Multiple-Alternative Selection Structures . . . . . . . . . . . . . . . . . 180
The switch Statement . . . . . . . . . . . . . . . . . . . . . . . 183
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

CH AP T ER 7 Th e R ep et i t i on S tr u ctu re . . . . . . . . . . . . . . 213
Repeating Program Instructions . . . . . . . . . . . . . . . . . . . . . 214
Using a Pretest Loop to Solve a Real-World Problem . . . . . . . . . . . 216
Flowcharting a Pretest Loop . . . . . . . . . . . . . . . . . . . . . . 219
The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . 221
Using Counters and Accumulators . . . . . . . . . . . . . . . . . . . . 224
The Sales Express Program . . . . . . . . . . . . . . . . . . . . . 225
Counter-Controlled Pretest Loops . . . . . . . . . . . . . . . . . . . . 228
The for Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 231
The Holmes Supply Program. . . . . . . . . . . . . . . . . . . . . 233
The Colfax Sales Program . . . . . . . . . . . . . . . . . . . . . . 236
Another Version of the Miller Incorporated Program . . . . . . . . . . 238
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

CH AP T ER 8 M ore on t h e R epe ti ti o n S tr u ctu re . . . . . . . . . . 264


Posttest Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Flowcharting a Posttest Loop . . . . . . . . . . . . . . . . . . . . . . 267
The do while Statement . . . . . . . . . . . . . . . . . . . . . . . 270
Nested Repetition Structures . . . . . . . . . . . . . . . . . . . . . . 273
The Asterisks Program . . . . . . . . . . . . . . . . . . . . . . . . . 275
The Savings Calculator Program. . . . . . . . . . . . . . . . . . . . . 283
The pow Function . . . . . . . . . . . . . . . . . . . . . . . . . 284
Coding the Savings Calculator Program . . . . . . . . . . . . . . . 286
Modifying the Savings Calculator Program . . . . . . . . . . . . . . . . 287
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300

CH AP T ER 9 Val u e-R et u rn i n g F u n cti o n s . . . . . . . . . . . . . 308


Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
The Hypotenuse Program . . . . . . . . . . . . . . . . . . . . . . . . 310
Finding the Square Root of a Number . . . . . . . . . . . . . . . . 310
The Random Addition Problems Program . . . . . . . . . . . . . . . . . 313
Generating Random Integers . . . . . . . . . . . . . . . . . . . . 314
Creating Program-Defined Value-Returning Functions . . . . . . . . . . . 322
Calling a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Function Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . 330
The Plano Elementary School Program . . . . . . . . . . . . . . . . . . 333
The Area Calculator Program . . . . . . . . . . . . . . . . . . . . . . 336 xi
The Scope and Lifetime of a Variable . . . . . . . . . . . . . . . . . . 340
The Bonus Calculator Program. . . . . . . . . . . . . . . . . . . . 340
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362

CH AP T ER 10 Voi d Fu n c t i on s . . . . . . . . . . . . . . . . . . 370
Void Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Passing Variables to a Function . . . . . . . . . . . . . . . . . . . . . 376
Reviewing Passing Variables by Value . . . . . . . . . . . . . . . . 377
Passing Variables by Reference . . . . . . . . . . . . . . . . . . . 379
The Salary Program . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406

CH AP T ER 11 O n e-D i m en si on a l A r r a y s . . . . . . . . . . . . . . 419
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . 420
Declaring and Initializing a One-Dimensional Array . . . . . . . . . . . 422
Entering Data into a One-Dimensional Array . . . . . . . . . . . . . . 424
Displaying the Contents of a One-Dimensional Array . . . . . . . . . . 426
Coding the XYZ Company’s Sales Program . . . . . . . . . . . . . . 427
Passing a One-Dimensional Array to a Function . . . . . . . . . . . . . . 433
The Moonbucks Coffee Program—Calculating a Total and Average . . . . . 436
The KL Motors Program—Searching an Array . . . . . . . . . . . . . . . 439
The Hourly Rate Program—Accessing an Individual Element . . . . . . . . 442
The Random Numbers Program . . . . . . . . . . . . . . . . . . . . . 444
Sorting the Data Stored in a One-Dimensional Array . . . . . . . . . . . . 454
Parallel One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . 461
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478

CH AP T ER 12 Tw o-D i m en si on a l A r r a y s . . . . . . . . . . . . . . 486
Using Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . 487
Declaring and Initializing a Two-Dimensional Array . . . . . . . . . . . 489
Entering Data into a Two-Dimensional Array . . . . . . . . . . . . . . 491
CONTENTS

Displaying the Contents of a Two-Dimensional Array . . . . . . . . . . 494


Coding the Caldwell Company’s Orders Program . . . . . . . . . . . 495
Accumulating the Values Stored in a Two-Dimensional Array . . . . . . . . 498
Searching a Two-Dimensional Array . . . . . . . . . . . . . . . . . . . 500
Passing a Two-Dimensional Array to a Function . . . . . . . . . . . . . . 507
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
xii Key Term . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

CH AP T ER 13 St ri n g s . . . . . . . . . . . . . . . . . . . . . . 524
The string Data Type. . . . . . . . . . . . . . . . . . . . . . . . . 525
The Creative Sales Program . . . . . . . . . . . . . . . . . . . . . . 526
The getline Function . . . . . . . . . . . . . . . . . . . . . . . 527
The ignore Function . . . . . . . . . . . . . . . . . . . . . . . 531
The ZIP Code Program . . . . . . . . . . . . . . . . . . . . . . . . . 535
Determining the Number of Characters Contained in
a string Variable . . . . . . . . . . . . . . . . . . . . . . . . . 535
Accessing the Characters Contained in a string Variable . . . . . . . 538
The Rearranged Name Program . . . . . . . . . . . . . . . . . . . . . 544
Searching the Contents of a string Variable. . . . . . . . . . . . . 544
The Annual Income Program . . . . . . . . . . . . . . . . . . . . . . 547
Removing Characters from a string Variable . . . . . . . . . . . . 548
Replacing Characters in a string Variable. . . . . . . . . . . . . . 551
The Social Security Number Program . . . . . . . . . . . . . . . . . . 553
Inserting Characters Within a string Variable . . . . . . . . . . . . 554
The Company Name Program . . . . . . . . . . . . . . . . . . . . . . 556
Duplicating a Character Within a string Variable . . . . . . . . . . . 557
Concatenating Strings . . . . . . . . . . . . . . . . . . . . . . . 558
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574

CH AP T ER 14 Seq u en t i al Ac c e s s F i l e s . . . . . . . . . . . . . . 582
File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
The CD Collection Program . . . . . . . . . . . . . . . . . . . . . . . 583
Creating File Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Opening a Sequential Access File . . . . . . . . . . . . . . . . . . . . 586
Determining Whether a File Was Opened Successfully . . . . . . . . . 589
Writing Data to a Sequential Access File . . . . . . . . . . . . . . . . . 590
Reading Information from a Sequential Access File . . . . . . . . . . . . 592
Testing for the End of a Sequential Access File . . . . . . . . . . . . . . 594
Closing a Sequential Access File . . . . . . . . . . . . . . . . . . . . 595
Coding the CD Collection Program. . . . . . . . . . . . . . . . . . . . 596
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
Key Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618
AP P EN D IX A A n sw ers t o M i n i - Q u i z z e s a n d L a b s . . . . . . . . . . 626

AP P EN D IX B C + + Keyw ord s . . . . . . . . . . . . . . . . . . 690

AP P EN D IX C ASC I I C od es . . . . . . . . . . . . . . . . . . . 691
xiii
AP P EN D IX D H ow t o U se M i c ro s o ft V i s u a l C + + . . . . . . . . . . 693

AP P EN D IX E H ow t o U se D ev- C + + . . . . . . . . . . . . . . . . 694

AP P EN D IX F C l asses an d O b j e cts . . . . . . . . . . . . . . . . 695


Object-Oriented Terminology . . . . . . . . . . . . . . . . . . . . . . 696
Defining a Class in C++ . . . . . . . . . . . . . . . . . . . . . . . . 697
Instantiating an Object and Referring to a Public Member . . . . . . . . . 700
Example 1—A Class that Contains Public Data Members Only . . . . . . . 702
Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Example 2—A Class that Contains a Private Data Member
and Public Member Methods . . . . . . . . . . . . . . . . . . . . . . 706
Example 3—Using a Class that Contains Two Constructors . . . . . . . . 709
Example 4—A Class that Contains Overloaded Methods . . . . . . . . . . 712
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 716
Key Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Review Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 718
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720

I n d ex . . . . . . . . . . . . . . . . . . . . . . 721
Preface
xiv

An Introduction to Programming with C++, Sixth Edition uses the C++ pro-
gramming language to teach programming concepts. This book is designed for
a beginning programming course. Although the book provides instructions for
using the Microsoft® Visual C++® and Dev-C++ compilers, it can be used with
most C++ compilers, often with little or no modification.

Organization and Coverage


An Introduction to Programming with C++, Sixth Edition contains 14 chapters
and several appendices. In order to provide the most up-to-date instructions for
using the Microsoft Visual C++ and Dev-C++ compilers, Appendices D and E
are available online. You can obtain the appendices by connecting to the Course
Technology Web site (www.cengage.com/coursetechnology) and then navigating
to the page for this book. In the chapters, students with no previous program-
ming experience learn how to plan and create well-structured programs. By
the end of the book, students will have learned how to write programs using
the sequence, selection, and repetition structures, as well as how to create
and manipulate functions, sequential access files, arrays, strings, classes, and
objects.

Approach
An Introduction to Programming with C++, Sixth Edition is distinguished from
other textbooks because of its unique approach, which motivates students by
demonstrating why they need to learn the concepts and skills presented. Each
chapter begins with an introduction to one or more programming concepts.
The concepts are illustrated with code examples and sample programs. The
sample programs allow the student to observe how the current concept can be
used before they are introduced to the next concept. The concepts are taught
using standard C++ commands. Following the concept portion in each chapter
(except Chapter 1) are five labs: Stop and Analyze, Plan and Create, Modify,
Desk-Check, and Debug. Each lab teaches students how to apply the chapter
concepts; however, each does so in a different way.

Features
An Introduction to Programming with C++, Sixth Edition is an exceptional text-
book because it also includes the following features:
READ THIS BEFORE YOU BEGIN This section is consistent with Course
Technology’s unequaled commitment to helping instructors introduce
Organization and Coverage

technology into the classroom. Technical considerations and assumptions


about hardware, software, and default settings are listed in one place to help
instructors save time and eliminate unnecessary aggravation.
LABS Each chapter contains five labs that teach students how to apply
the concepts taught in the chapter to real-world problems. In the first
lab, which is the Stop and Analyze lab, students are expected to stop
xv
and analyze an existing program. Students plan and create a program in the
Plan and Create lab, which is the second lab. The third lab is the Modify lab.
This lab requires students to modify an existing program. The fourth lab is
the Desk-Check lab, in which students follow the logic of a program by desk-
checking it. The fifth lab is the Debug lab. This lab gives students an opportu-
nity to find and correct the errors in an existing program.
STANDARD C++ SYNTAX Like the previous edition of the book, this edition
uses the standard C++ syntax in the examples, sample programs, and exer-
cises in each chapter.
TIP These notes provide additional information about the current con-
cept. Examples include alternative ways of writing statements, warnings
about common mistakes made when using a particular command, and
reminders of related concepts learned in previous chapters.
PSEUDOCODE AND FLOWCHARTS Although pseudocode is the primary
tool used when planning the programs in each chapter, flowcharts also are
provided for many of the programs. If the flowchart is not in the chapter
itself, the student is directed to the Cpp6\Chapxx\ChxxFlowcharts.pdf file,
where xx is the chapter number.
MINI-QUIZZES Mini-quizzes are strategically placed to test students’
knowledge at various points in each chapter. Answers to the quiz questions
are provided in Appendix A, allowing students to determine whether they
have mastered the material covered thus far before continuing with the
chapter.
SUMMARY A Summary section follows the labs in each chapter. The Sum-
mary section recaps the programming concepts and commands covered in
the chapter.
KEY TERMS Following the Summary section in each chapter is a list-
ing of the key terms introduced throughout the chapter, along with their
definitions.
REVIEW QUESTIONS Review Questions follow the Key Terms section in
each chapter. The Review Questions test the students’ understanding of what
they learned in the chapter.
PAPER AND PENCIL EXERCISES The Review Questions are fol-
lowed by Pencil and Paper Exercises, which are designated as TRY
THIS, MODIFY THIS, INTRODUCTORY, INTERMEDIATE,
ADVANCED, and SWAT THE BUGS. The answers to the TRY THIS Exer-
cises are provided at the end of the chapter. The ADVANCED Exercises pro-
vide practice in applying cumulative programming knowledge or allow
students to explore alternative solutions to programming tasks. The SWAT
THE BUGS Exercises provide an opportunity for students to detect and cor-
rect errors in one or more lines of code.
P R E FA C E Organization and Coverage

COMPUTER EXERCISES The Computer Exercises provide students


with additional practice of the skills and concepts they learned in the
chapter. The Computer Exercises are designated as TRY THIS,
MODIFY THIS, INTRODUCTORY, INTERMEDIATE, ADVANCED, and
SWAT THE BUGS. The answers to the TRY THIS Exercises are provided at
the end of the chapter. The ADVANCED Exercises provide practice in apply-
xvi ing cumulative programming knowledge or allow students to explore alterna-
tive solutions to programming tasks. The SWAT THE BUGS Exercises
provide an opportunity for students to detect and correct errors in an exist-
ing program.

New to This Edition!


STD NAMESPACE Rather than including a using directive for each
standard object used in a program, all programs now contain the using
namespace std; directive.
STRING CLASS The string class is now covered in Chapter 13. In the
chapter, students learn how to declare and utilize string variables and
string named constants in a program. They also learn how to concatenate
strings and use many of the functions available in the string class.
CHAPTERS 3 AND 4 Chapters 3 and 4 from the previous edition of the
book have been redesigned to make the material easier for students to
comprehend. Chapter 3 now covers only variables and named constants,
which are challenging concepts for beginner programmers. Chapter 4
shows the student how to get numeric and character input from the
keyboard, write assignment statements, and display information on
the computer screen. Chapter 4 also covers the last three steps in the
problem-solving process.
ARRAYS One-dimensional arrays and two-dimensional arrays are now cov-
ered in separate chapters. One-dimensional arrays are covered in Chapter 11,
and two-dimensional arrays are covered in Chapter 12.
APPENDIX A The answers to both the Mini-Quiz questions and the Labs are
now located in one convenient place in the book: Appendix A.
APPENDICES D AND E Appendix D contains the instructions for using the
Microsoft Visual C++ compiler, and Appendix E contains the instructions
for using the Dev-C++ compiler. In order to provide the most up-to-date
instructions for using both compilers, Appendices D and E are available
online. You can obtain the appendices by connecting to the Course Technol-
ogy Web site (www.cengage.com/coursetechnology) and then navigating to the
page for this book.
APPENDIX F Appendix F covers Classes and Objects. This topic was origi-
nally covered in Chapter 14 in the previous edition of the book.
NEW EXAMPLES, SAMPLE PROGRAMS, AND EXERCISES Each chapter
has been updated with new examples, sample programs, and exercises.
NEW FORMAT The book has a new, more convenient format. All of the
questions and exercises are now located at the end of the chapter.
Instructor Resources and Supplements

Instructor Resources and Supplements


All of the resources available with this book are provided to the instructor on
a single CD-ROM. Many also can be found on the Course Technology Web
site (www.cengage.com/coursetechnology).
ELECTRONIC INSTRUCTOR’S MANUAL The Instructor’s Manual that
accompanies this textbook includes additional instructional material to assist xvii
in class preparation, including Sample Syllabi, Chapter Outlines, Technical
Notes, Lecture Notes, Quick Quizzes, Teaching Tips, Discussion Topics, and
Additional Case Projects.
EXAMVIEW ® This textbook is accompanied by ExamView, a powerful test-
ing software package that allows instructors to create and administer printed,
computer (LAN-based), and Internet exams. ExamView includes hundreds
of questions that correspond to the topics covered in this text, enabling
students to generate detailed study guides that include page references for
further review. The computer-based and Internet testing components allow
students to take exams at their computers, and also save the instructor time
by grading each exam automatically.
MICROSOFT ® POWERPOINT ® PRESENTATIONS This book offers Micro-
soft PowerPoint slides for each chapter. These are included as a teaching aid
for classroom presentation, to make available to students on the network for
chapter review, or to be printed for classroom distribution. Instructors can
add their own slides for additional topics they introduce to the class.
DATA FILES Data Files are necessary for completing the Labs and Com-
puter Exercises in this book. The Data Files are provided on the Instructor
Resources CD-ROM and also may be found on the Course Technology Web
site at www.cengage.com/coursetechnology.
SOLUTION FILES Solutions to the Labs, Review Questions, Pencil and Paper
Exercises, and Computer Exercises are provided on the Instructor Resources
CD-ROM and also may be found on the Course Technology Web site at
www.cengage.com/coursetechnology. The solutions are password protected.
FIGURE FILES The sample programs that appear in the figures throughout
the book are provided on the Instructor Resources CD-ROM.
DISTANCE LEARNING Course Technology offers online WebCT and
Blackboard courses for this text to provide the most complete and dynamic
learning experience possible. When you add online content to one of your
courses, you’re adding a lot: automated tests, topic reviews, quick quizzes,
and additional case projects with solutions. For more information on how to
bring distance learning to your course, contact your local Course Technology
sales representative.
P R E FA C E Got a Job in Computing . . . ?

Acknowledgments
Writing a book is a team effort rather than an individual one. I would like
to take this opportunity to thank my team, especially Tricia Coia (Freelance
Product Manager), Jill Braiewa (Senior Content Project Manager), Sreejith
Govindan (Full Service Project Manager), and Nicole Ashton (Quality Assur-
xviii ance). Thank you for your support, enthusiasm, patience, and hard work; it
made a difficult task much easier. Last, but certainly not least, I want to thank
Matthew Alimagham (Spartanburg Technical College) and Linda Cohen
(Forsyth Technical Community College) for their invaluable ideas and com-
ments. And an extra special thank you to Bill Tucker (Austin Community
College) for going way above and beyond to help me on this project. Your
attention to detail and your willingness to share your ideas and your experi-
ences with the previous edition of the book were very much appreciated.
Diane Zak

Got a Job in Computing . . . ?


We hope you enjoyed the Q&A on the inside front cover of this book. If you,d
like to suggest that we interview someone you know, a recent graduate who has
landed an interesting job in computing, please send your suggestions via e-mail
to Amy Jollymore, Acquisitions Editor, at [email protected].
Read This Before
You Begin
xix

Technical Information

Data Files
You will need data files to complete the Labs and Computer Exercises in this
book. Your instructor may provide the data files to you. You may obtain the
files electronically on the Course Technology Web site (www.cengage.com/
coursetechnology).
Each chapter in this book has its own set of data files, which are stored in a
separate folder within the Cpp6 folder. The files for Chapter 4 are stored in
the Cpp6\Chap04 folder. Similarly, the files for Chapter 5 are stored in the
Cpp6\Chap05 folder. Throughout this book, you will be instructed to open
files from or save files to these folders.
You can use a computer in your school lab or your own computer to com-
plete the Labs and Computer Exercises in this book.

Using Your Own Computer


To use your own computer to complete the Labs and Computer Exercises
in this book, you will need a C++ compiler. The book was written and Qual-
ity Assurance tested using Microsoft Visual C++ 2010. It also was tested
using Dev-C++. However, the book can be used with most C++ compilers,
often with little or no modification. If your book came with a copy of Micro-
soft Visual C++, then you may install that on your computer and use it to
complete the material.

Visit Our Web Site


Additional materials designed for this textbook might be available through
the Course Technology Web site, www.cengage.com/coursetechnology. Search
this site for more details.
Random documents with unrelated
content Scribd suggests to you:
“Friends, I account the fortune of my wife
Happier than mine, albeit it seems not so.
For nought of grief shall touch her any more,
And glorious rest she finds from many toils.
But I, unmeet to live, my doom outrun,
Shall drag out bitter days: I know it now.
How shall I bear to enter this my home?“[29]

The bystanders try to persuade him to go in, but he lingers through


the beautiful choral ode that is raised in praise of Alcestis. They sing
of the worship and honour that will be paid at her tomb as at a
shrine; and as the long hymn is drawing to a close, Heracles is seen
to be returning, leading a woman closely veiled. The king, standing
in quiet despair, utters no word of greeting to his guest, and the
Chorus wait in silent wonder for an explanation. A strange awe falls
upon them; and Heracles, beginning in gentle gravity to reproach
the king for want of confidence in him, turns presently to the veiled
figure at his side. Will the king take and guard this maid for him,
until he shall return from Thrace? She is a prize awarded him for
great toil, and Admetus will do well to care for her.
But the king recoils at the thought. How can he receive a young and
beautiful woman into his house without pain to himself and shame
to her? He protests that it is unthinkable, and begs Heracles to take
her elsewhere. She would be a constant reminder of his grief, and
an insult to the memory of his wife. Until this moment he has hardly
glanced at the silent figure by the hero’s side, except to notice that
her rich vestments proclaim her young. But something in her
appearance seizes his attention; and he proceeds, rapidly and in
great agitation:
“But, woman, thou,
Whoso thou art, know that thy body’s stature
Is as Alcestis, and thy form as hers.
Ah me!—lead, for the Gods’ sake, from my sight
This woman!—Take not my captivity captive.
For as I look on her, methinks I see
My wife. She stirs my heart with turmoil: fountains
Of tears burst from mine eyes. O wretched I!
Now taste I first this grief’s full bitterness.“[29]

It is Alcestis’ very self, won back from death as Apollo had promised;
but with the awful silence of the tomb still upon her. Heracles places
her hand in that of the reluctant and incredulous king, while he
draws aside her veil:
“Yea, guard her. Thou shalt call
The child of Zeus one day a noble guest.
Look on her, if in aught she seems to thee
Like to thy wife. Step forth from grief to bliss.“[29]

28. From The Life and Death of Jason, by William Morris


(Longmans).

29. From the Alcestis of Euripides, translated by Dr. A. S. Way


(Loeb Classical Library: London, Heinemann).
Euripides: Medea

O nly eighteen dramas are extant of the seventy-five which


Euripides is known to have written. And an interesting small fact
is that the two earliest of these surviving dramas are the Alcestis
and the Medea, produced respectively in 438 B.C. and 431 B.C. Each
of the two has a woman for the protagonist, and both have love for
their central theme. To that extent therefore they are similar, and
represent certain clear features of Euripidean drama as a whole.
We have already noted the poet’s interest in womanhood: his keen
and careful study of feminine character. He was no less occupied
with the influence of love in human life; but on both themes he was
clear-eyed and penetrative, aspiring always to the ‘white star of
truth.’ Therefore we do not find in his drama a troop of faultless
women, moving in an atmosphere of romantic glamour; nor a
treatment of love which reveals only the more beautiful aspects of it.
He seems to have been content to acknowledge, as for instance in
the Alcestis and the lost Andromeda, that life’s flowers do
sometimes, given the right conditions, come to fair fruition. But he
saw how often they are warped and blighted; and though he would
not hide the grimmer facts, he was always careful to seek and show
the cause of the aberration. Hence, though the truth of his
presentation is sometimes merciless, and may have given colour to
the contemporary gossip which called him a ‘woman-hater,’ one
glance below the surface of his thought shows him to have been
inspired by a nobler chivalry than that which is content to veil the
facts of life in romantic illusion. So we find that although both the
Alcestis and the Medea are preoccupied with the theme of love,
there is a vivid contrast in the treatment of the theme, despite
certain resemblances between the two dramas. It is true that both of
the heroines are pre-eminent in devotion to the men with whom
they are mated; and that the hero in each case moves on a plane
from which he cannot reach the height of his wife’s spirit. But whilst
on the one hand love takes possession of a gentle nature, and
favoured by every circumstance of character and environment
triumphs over death itself, in the case of Medea a wild soul spends
itself recklessly for the object of its love, beats impotently against
injustice, loses hold on sanity and sweet human ties, and is
transformed into an avenging fury.
The story of Medea belongs to the old Argo legend, which was made
into poetry by Apollonius Rhodius in the first century before Christ,
and by our own Victorian poet Morris in The Life and Death of Jason.
Jason, the exiled heir to the throne of Iolchos, was reared by the
centaur Chiron. Arrived at manhood, he determined to claim his right
from his usurping uncle Pelias; and travelling to Iolchos on foot, he
presented himself before the king minus a sandal. Now Pelias had
been warned against a man who should come to him with one foot
bare; and, moreover, he had no intention of yielding up the throne
to his nephew. He therefore cast about for some means of ridding
himself of Jason, and hit upon the plan of sending him on a wild and
dangerous quest—to seek and bring the Golden Fleece from the
barbarous land of Colchis. Jason gladly undertook the task: gathered
the Greek heroes together and sailed with them in the good ship
Argo.
After a perilous voyage, the heroes arrived at Colchis, and Jason
made known their quest to the king Aeêtes. But they soon found
that they had no hope of success. Aeêtes was false to them, made
impossible conditions, and plotted against their life. Disaster seemed
imminent, when there came a deliverance so glorious that it seemed
like the interposition of a god. It was the quick wit of a girl,
prompted by love. Medea, the young daughter of Aeêtes, had seen
and loved the brave Greek prince whom her father now plotted to
destroy. She was an ardent and impulsive creature; and she
determined to save Jason. By the magic lore that she possessed, she
secretly enabled him to overcome the fire-breathing oxen, and the
earth-sown army that her father sent against him. Then, realizing
too late that she had incurred the unpitying rage of her father, she
fled at night from the palace, to take refuge with the Greek heroes.
She kissed her bed, and her hands on the walls with loving caress
Lingered; she kissed the posts of the doors; and one long tress
She severed, and left it her bower within, for her mother to be
A memorial of maidenhood’s days, and with passionate voice
moaned she.[30]

Under cover of the darkness, she led Jason to the forest-precinct


where the Fleece was hidden; and by her charms she lulled the
sleepless dragon that guarded it. She even betrayed to him her
brother Absyrtus, driven by the danger of a horrible death for
herself, her lover and his comrades; and then, claiming from Jason a
solemn oath of marriage when they should come to Hellas, she
sailed with him on the Argo. Aeêtes pursued them in fierce wrath;
and the gods, offended for the murder of Absyrtus, vexed them with
storms. But at length they came to the island of Circe; and she, for
the sake of her kinship with Medea, purified them of the murder of
Absyrtus and set them on their way again. At Phæacia, where they
were driven for harbourage, Aeêtes overtook them, threatening war
with King Alcinous if he did not yield up his fugitive daughter. It was
then that the great wise queen Arete pleaded for Medea in gentle
charity:
“In madness she sinned at the first, when she gave him the charm
that should tame
The bulls; and with wrong to amend that wrong,—Ay, oftimes the
same
In our sinning we do!—she straightway essayed; and shrinking in
fear
From her proud sire’s tyrannous wrath, she fled. Now the man, as I
hear,
This Jason, is hound by mighty oaths which his own lips said,
When he pledged him to make her, his halls within, his wife true-
wed.“[30]

Alcinous yielded to his wife’s entreaties on one condition—that Jason


and Medea should be married forthwith; for then he could return
answer to Aeêtes that he would not separate husband and wife.
Thus the two were hurriedly wedded; and sailed in safety from
Phæacia, to encounter many a terrible adventure before they
reached Iolchos at last, triumphing in the possession of the Fleece.
They gained great glory from their enterprise, but little else. For
Pelias would not yield the throne to Jason; and it seemed to Medea
that all she had wrought had been in vain. She brooded over Jason’s
wrongs, chafing at the restraint imposed on her in her new life, and
eager to strike for the kingship on his behalf. At last she evolved a
plan by which she thought Pelias might be removed from their path,
and the throne secured for Jason. Promising the old king renewed
youth by means of her enchantments, she induced him to submit to
death at the hands of his daughters. Then, in the storm of
indignation which arose against her, she and Jason and their two
young children fled to Corinth.
So the legend runs to the point where Euripides takes it up. In crude
outline it is savage and incredible; and yet it contains all the
elements which in the hands of idealistic poets have made a story of
enthralling romantic beauty. In the Medea, however, the poet has
avoided so far as might be both the barbarity of the legend and its
potential charm. He has treated only the final catastrophe—the
abandonment of Medea by Jason and her dreadful vengeance upon
him. And although he could not escape from the data: although he is
compelled to handle some of the most barbarous of them, he has
translated them from terms of glimmering wonder and breathless
excitement into the language of reality. He has brought Medea out of
the region of myth, where she dwelt in eerie and tempestuous
beauty, into the stream of human existence. The marvellous and the
superhuman drop away, save for a fragment or two in the
framework of the Drama; and Medea becomes simply a woman,
struggling against her own wild heart and the injustice of her
oppressors.
The Drama opens with the monologue of Medea’s old nurse, from
which we learn all that is vital to an understanding of the action.
Jason has forsaken Medea and is about to marry with Glaucé, the
young daughter of Creon, King of Corinth. Medea is sick with misery
and is lying in the house prostrate on her bed. Two things the old
woman makes quite clear, as she stands talking outside: that the
chief cause of Medea’s grief is shame at her betrayal; and that
already the storm of passion is tending toward madness. When an
attendant comes in, bringing Jason’s children back from their play,
there is a clear hint of the catastrophe. The man tells of a rumour
that he has heard: Creon has ordered the banishment of their
mistress and her boys. The nurse breaks into a wail of
commiseration, and then clearly states her fear for the effect of this
new wrong upon Medea’s mind. She sends the little ones in before
she speaks the dread she has that their mother may lift her hand
against their lives; and almost immediately afterward the frenzied
voice of Medea is heard, calling bitter curses upon her unfathered
children.
There gather gradually the ladies of Corinth who form the Chorus.
They are deeply sympathetic; and they give pitying answers to the
nurse’s tale; while within the house, at intervals, Medea’s voice is
heard, wailing her grief and anger, and the old remorse that has
reawakened for her brother’s death.
“Virgin of Righteousness,
Virgin of hallowed Troth,
Ye marked me when with an oath
I bound him; mark no less
That oath’s end. Give me to see
Him and his bride, who sought
My grief when I wronged her not,
Broken in misery,
And all her house.“[31]

The scene is one of weird impressiveness. So far, Medea has not


appeared; but her cries within the house, the appearance of her
children, the indignant fidelity of the old servants, the beautiful lyrics
of the Chorus, and, above all, the knowledge we possess that
another blow is about to fall on her, produce a cumulative effect
which makes the moment of her entrance intensely dramatic. Yet
she begins her speech quietly, almost in apology for her former
unrestraint. She strives for self-control while she puts her case
before the Corinthian women and begs their help. For a moment or
two she succeeds, pathetically acknowledging her foreign birth and
the flaw it intrudes in the legality of her marriage. But at this
thought, emotion sweeps over her again:
“...I dazzle where I stand,
The cup of all life shattered in my hand,
Longing to die—O friends! He, even he,
Whom to know well was all the world to me,
The man I loved, hath proved most evil.“[31]

She pours out her heart to the listeners; and it is not a mere selfish
recital of her own sorrow. The brain that had been clear and quick to
save her lover in the extremity of danger has not lost its power. She
sees the base act of Jason in its broad aspect, as a wrong to
womankind; and she rises from the contemplation of her personal
suffering to the thought that this, after all, is but one of the many
evils that subjection brings upon women. But the greatest evil—the
helpless creature goaded to crime by injustice—is present to her at
this moment only as a blind craving for revenge. It will seize and
carry her on to its culmination as the sweetest thing that life now
holds; but it will finally reveal itself, since she cannot but face the
truth, as the last and deepest wrong, that has cancelled her
humanity. The light of that thought has not yet dawned; and will not
until the storm of passion has wrought sheer havoc. All her fervent
nature is possessed by the idea of vengeance; and seeing that her
friends pity and sympathize, she pledges them not to betray her.
Their willing promise is only just in time, for they are interrupted by
the arrival of the king, guarded by armed attendants whose very
presence is a menace. Creon is old, and has grown hard and
tyrannous with age. He has long desired a great match for his only
daughter, hoping to see his line established on the throne of Corinth
before his death. To him the marriage with the Argonaut hero is not
only a prudent step, likely to bring him reflected glory; but a thing
perfectly right in itself, because perfectly legal. By the letter of the
law, which forbade a Greek to marry a ‘barbarian,’ Medea was not
Jason’s wife; and the letter of the law merely was of concern to
Creon. To him Medea was an uncivilized creature from outland parts:
a being without rights, who might safely be ignored; and having won
over Jason, the match was arranged and the preliminary formalities
concluded. Not until a rumour reached him that Medea in her wrath
had solemnly cursed his child and him, did any thought of her
disturb him. Then, fearing that she might indeed do his daughter
some injury, or at the least might move public opinion in her favour,
he determined upon instant banishment for her and her two young
sons. Without a word to soften or explain his action, he stands
before Medea now, and curtly orders her to prepare for departure.
The blow is so crushing that for a moment Medea seems to sink
under it; she can think of nothing but to ask what crime of hers has
merited this punishment. But when Creon cynically replies that there
has been no crime, and that the measure is one of precaution
merely, to guard himself against her reputation for magic-lore, she
rallies her wit and meets him on his own ground. Half ironically, she
repudiates the damning possession of brains, and bids him set his
mind at rest.
“’Tis not the first nor second time, O King,
That fame hath hurt me....
Come unto fools with knowledge of new things,
They deem it vanity, not knowledge....
Ah, I am not so wondrous wise!—And now,
To thee, I am terrible! What fearest thou?
What dire deed? Do I tread so proud a path—
Fear me not thou!—that I should brave the wrath
Of princes?“[31]

Creon sees that she is trying to placate him, and harshly repeats his
decree. He even threatens her, when she continues her entreaties,
with force from his soldiery; and Medea, shrinking in horror from the
thought of personal violence, instantly ceases her petition. She
pretends to yield; and in feigned humility, begs on her knees for one
day’s respite. Creon, partly deceived, and entirely convinced that she
can do no harm in so short a time, reluctantly consents. But he has
hardly gone when Medea breaks into a torrent of speech which, in
its fierce exultation over Creon, its wild leap to the height of daring
and its rallying cry to her own spirit, comes very near to madness.
All the shapeless thoughts of vengeance on which she had brooded
spring into vivid life as she rapidly cons now this plot, now that, to
reach her end. Of the end itself there can be no doubt; she must kill
these three—the king, and Jason and his bride—in the few hours left
to her. And for this she will need every resource of strategy and
courage.
“Awake thee now, Medea! Whatso plot
Thou hast, or cunning, strive and falter not.
On to the peril-point! Now comes the strain
Of daring. Shall they trample thee again?“[31]
No wonder that the Chorus sing, as she rushes into the house, of a
strange reversal of all the order of nature; of woman made terrible
because man has forgotten God. They take up the story of Medea’s
broken life: of the wonder and the pity of it: of her distant home: of
her surpassing love for Jason, and of her betrayal. In the beauty and
grace of the songs the emotional strain is lightened: but they have a
further purpose. For while they tell the old story over in tender
phrases, Jason himself enters and Medea again comes out of the
house. The two stand face to face at last and the crux of the drama
is reached. Jason is the first to speak; and one feels all the spirit of
the man in his opening words—cold, ambitious, prudent, with ideals
faded and every generous emotion dead. He protests that he has
acted from motives of policy and considerations of their best
interest: for the welfare of Medea and their children as well as for
himself. The new marriage was the only way, in a land to which they
were strangers, to secure a home for them all, and princely
connexions for his sons. But Medea has spoiled everything by her
ungovernable anger: and he has come, since nothing else is possible
now, to make provision for the children in their exile.
The speech is clear, terse, moderate in tone, and pitilessly logical
from Jason’s point of view. From that point, too, it is not unkind: he
wishes to do what may be done to soften their lot. But to the
woman who loves him his words are a mere blur of sound, the logic
meaningless, the untroubled manner a thing of contempt. In tone
and look and gesture one fact is certain—that her husband has
ceased to love her, and is content to cast her off. It has clamoured in
her ears while he spoke, drowning every other sound; and when she
replies it is that which prompts her. It inspires her great indictment—
the case for the woman against injustice throughout all time—and it
evokes a shuddering recoil from baseness which she feels to be
literally a pollution.
“Evil—most Evil ...
I will begin with that, ‘twixt me and thee,
That first befell. I saved thee. I saved thee—
... And hast thou then
Accepted all—O evil yet again!—
And cast me off and taken for thy bride
Another? And with children at thy side!
... Is sworn faith so low
And weak a thing? I understand it not.
Are the old gods dead? Are the old laws forgot,
And new laws made? ...
... O great God, shall gold withal
Bear thy clear mark, to sift the base and fine,
And o’er man’s living visage runs no sign
To show the lie within, ere all too late?“[31]

Jason’s anger is stung by her denunciation, but his purpose is quite


unmoved. He flings a veiled insult at her love; and as he elaborates
the reasons for his action, with no little skill and plausibility, we feel
that with every word the conflict becomes more deadly. In apparent
good faith, but with intolerable effrontery to the injured woman, he
claims to have repaid that old debt, if indeed it were a debt. He has
given her a home in an ordered country and her name has been
linked in the glory of his. As to the marriage with Glaucé—with a
sneer at the bare idea of sentiment—the affair is a bargain, with
consideration given and received on each side. Let Medea look at the
matter for one instant with the eyes of reason, and she herself will
acknowledge that he has acted wisely.
But the very root of the tragedy lay there. Medea could no more
detach herself from the emotion that possessed her than Jason
could revive the tenderness that filled him when he lifted the sweet
wild fugitive on board the Argo. So they stand, typifying the eternal
struggle between the passionate heart and the arrogant brain; and
striking at each other in baffled rage across the gulf between them.
Jason makes one last offer of help, but it is vehemently refused, and
with a final thrust at Medea’s savagery, he leaves her. When he has
gone, the inevitable reaction comes. The Chorus, interpreting her
mood, sing musingly of the pains of exile, and of her lonely state.
She realizes that she has flung away her only chance of help, and
she sees herself in a few hours expelled from Corinth without one
friend to shelter her. Despair is settling upon her when a curious
incident occurs, suddenly reviving hope and making the path clear
for her revenge. It is the arrival of Ægeus, King of Athens. He is
travelling back from Apollo’s shrine at Delphi, where he has been to
renew an old petition that the god would give him children. Medea,
thinking rapidly, questions him of his errand. She sees a possibility of
succour; and putting all her wrongs before him, she begs him to give
her refuge at Athens. He shall not fail of a reward, for she has magic
arts which will secure to him his long desire for children. Ægeus is
indignant at her wrongs, and promises to succour her if she comes
to him; but knowing what she is about to do, she cunningly extorts
an oath from him. He gives it willingly, and as he departs Medea
breaks into a cry of exultation:
“God, and God’s Justice, and ye blinding Skies,
At last the victory dawneth!“[31]
MEDEA & ABSYRTUS

Herbert Draper

By permission of the Corporation Art Gallery of Bradford

Quickly she lays her plan. She will recall Jason, feign repentance,
and send the children to the bride with gifts—marvellous raiment
and jewels which will hide under their beauty an agonizing death for
Glaucé. But that done—she pauses in horror, the sweetness of
revenge dashed by the thought of what must follow. Then, she must
lift her hand to slay her children, before they can be caught and
killed for their mother’s crime. There is a short altercation with the
friendly women about her, who make a futile effort to restrain her.
But brushing aside their remonstrance, she sends the nurse for
Jason, and in a scene which vibrates with dramatic power, she
pretends to make peace with him, and puts the frightful revenge in
motion. Jason, completely deceived, promises that the children shall
be taken to Glaucé, to present their gifts and beg for leave to stay in
Corinth. But twice, as the little ones stand waiting, the motherhood
in Medea rebels against the fury that is driving her. Tears that she
cannot check rush into her eyes, and she almost forgets her rôle, as
she clasps them to her.
“Shall it be
A long time more, my children, that ye live
To reach to me those dear, dear arms? ... Forgive.“[31]

And again when Jason, softened by her submission, is promising to


lead them up to an honoured manhood, a sudden movement of
Medea arrests him. He cannot understand her grief, and the
strangeness of her manner; and asks her if she doubts that he will
act in good faith toward their children.
Medea. I was their mother! When I heard thy prayer
Of long life for them, there swept over me
A horror, wondering how these things shall be.[31]
But the gentler mood passes, and when Jason, with characteristic
canniness, counsels her not to send such precious gifts to his bride,
the spirit of vengeance has regained possession of her soul. She
overrules him, and Jason leads the children to the princess, carrying
in their innocent hands the weapon that will slay her. Not until they
are gone does Medea realize fully what the next step must be; and
the realization brings agony. She waits for their return in a storm of
emotion: suspense that almost stops the beating of her heart:
hideous hope that her plot has succeeded and that Glaucé even now
is dying from the poison; and ghastly fear that her children have
been taken for the deed. But when they return at last, in
unconscious gladness that the great lady has been kind to them, it is
something more awful still that robs their mother of power of
utterance. The children’s tutor is amazed at the grief that he sees is
racking her, and asks its cause.
Medea. For bitter need, Old Man! The gods have willed,
And mine own evil mind, that this should come.[31]
And as the man goes in, leaving her alone with her boys, a poignant
scene follows in which every instinct of her nature struggles against
her wrath. Their sweet young faces stir the tenderness that has
hitherto been bound within her; and as it floods her heart it seems
for a few moments to sweep away her evil purpose. But it only
returns in added strength, and as her soul writhes in the conflict,
reason totters, and she implores the vengeance within, as a living
and implacable foe, to spare her babes. Backward and forward she
sways, driven by hatred and love, until the scale is turned at last by
the thought of her own irrevocable act. Glaucé, even at this
moment, is dying from the poison that she has sent.
“Too late, too late!
By all Hell’s living agonies of hate,
They shall not take my little ones alive
To make their mock with! Howso’er I strive
The thing is doomed....
Oh, darling hand! Oh, darling mouth, and eye,
And royal mien, and bright brave faces clear,
May you be blessèd, but not here! What here
Was yours, your father stole....
... I am broken by the wings
Of evil.... Yea, I know to what bad things
I go, but louder than all thought doth cry
Anger, which maketh man’s worst misery.“[31]

But even yet she cannot strike: one thing more is needed to nerve
her hand, and it comes only too soon. A messenger is seen flying
toward them from the palace in frantic haste. As he comes within
hail, he shouts to Medea to flee—both Creon and the princess lie
dead from the effects of her poisoned gift, and she has not a
moment to lose. Her own life will surely be demanded for the crime.
Medea remains immovable, smiling in awful joy at the news. She
makes the man relate every detail of the ghastly scene in the palace;
and for just so long as the story takes to tell, she clasps revenge
complete and satisfying. But a moment later the thing has shrivelled
in her hand; for there is now no hope to save her children.
“Oh, up, and get thine armour on,
My heart!...
Take up thy sword, O poor right hand of mine,
Thy sword: then onward to the thin-drawn line
Where life turns agony.”[31]

She goes into the house; and a moment later the shrieks of the
children are heard. They have hardly ceased when Jason rushes in,
bent on carrying off his sons before the king’s avengers can capture
them. A woman warns him of what is passing within; and as the
agonized father bursts open the door of the house, Medea appears
on the roof, in the dragon-chariot of the Sun, with the poor dead
bodies lying at her feet. There is something weird in this touch of
the supernatural; but there is something symbolic too. For Medea is
a woman no longer: with her own hand, driven by foul wrong and
an untamed heart, she has cast humanity away.
We need not follow to the end the last clash of the two bitter spirits.
Jason pleads piteously for one poor boon: “Give me the dead to
weep and make their grave.” But the fury that has smitten him is
inexorable.
“Never! Myself will lay them in a still
Green sepulchre....
... For thee, behold, death draweth on,
Evil and lonely, like thine heart: the hands
Of thine own Argo, rotting where she stands,
Shall smite thine head in twain, and bitter be
To the last end thy memories of me.“[31]

30. From Dr. A. S. Way’s translation of the Argonautica (Dent and


Sons Ltd.).
31. From Professor Gilbert Murray’s translation of the Medea
(George Allen & Co. Ltd.).
Euripides: Phædra

T he Hippolytus of Euripides, to which we turn for the story of


Phædra, is frequently called the earliest love-tragedy in
European literature. That is to say, it is the first to deal fully and
frankly with the power of love toward tragic issues. This can hardly
be said about the Medea, for that drama is only the last incident of a
story wherein love has been changed to hatred; and the motive is
revenge. But in the Hippolytus the story is unfolded from its
inception; and Phædra’s passion is found to be the force that moves
the whole action of the tragedy. This fact has a peculiar attraction
for the modern mind; but the drama has other claims upon us too.
First, for its sheer beauty, as poetry and as dramatic art of a special
type; then, for its accurate study of character, three people at least
gripping our interest as complete and convincing human creatures;
and again, for its lofty tone and a reflective element which, though
characteristically original, is calm and clear. But the most wonderful
fact of all is the surprising contrast between the nature of the theme
and the austere beauty of the drama which has been built upon it.
The crude facts of the story are almost repulsive on the face of
them. Phædra, the young wife of Theseus, King of Athens and
Trozen, had fallen in love with her husband’s illegitimate son
Hippolytus. That is the initial situation; and the further data of the
old Attic legend do not soften it. For we know that Phædra’s love
was unrequited, a fact which, with curious unreason, seems to
accuse her; and we know too that when her love was betrayed to
Hippolytus, she took her own life in shame and fear, first making a
false charge against him which she knew would bring upon him the
punishment of death.
Such, in harsh outline, is the story of unhappy love and wild impulse
which has been made by this poet who was before all things a
seeker of truth, into a work of supreme spiritual beauty. More
wonderful still, Phædra, who by conventional canons would seem to
have forfeited all claim to respect or sympathy, is found to be a
woman of sweet and gentle purity, cruelly betrayed by forces
without and within, and driven by desperation to a frantic attempt to
save her honour.
The means to such an end are interesting, although behind them all
lies the explanation of them all—the poet’s higher and broader
perception of truth. He has seen the passion which ruled Phædra as
a great world-force, an elemental power which could neither be
escaped nor overcome. This power is personified as Aphrodite or
Cypris, goddess of love; and she is conceived as the mortal enemy
of Hippolytus, because he has scorned her in his spiritual pride and
refused her her need of worship.
The key to the tragedy lies in this conception of Cypris, and in the
mystical, ascetic spirit of Hippolytus against which she has set her
offended godhead. They represent eternally opposing forces, and
warfare between them is inevitable and deadly. For that reason, the
opening monologue of the Drama is of great importance. The scene
is placed before the castle of Theseus at Trozen. A statue of a
goddess stands on either side—that of Artemis, chaste Moon-
goddess, on the one hand, decked with flowers and carefully
tended; and on the other hand, bare and unhonoured, is the statue
of Aphrodite. While beside the latter, musing in evident anger, is the
gleaming form of the goddess herself. We learn the cause of her
anger as she speaks. She is grieved on account of Hippolytus, who
in his excessive devotion to Artemis, despises Aphrodite and looks
upon love as a thing unclean. His arrogance and neglect are an
unbearable insult, and she has determined to punish him, swiftly and
without mercy. She has already prepared the pitfall, long ago in
Athens, when Hippolytus came to be solemnly initiated into the
Mysteries.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebooknice.com

You might also like