100% found this document useful (2 votes)
8 views

Readings from Programming with C++ 1st Edition Kyla Mcmullen pdf download

Ebook installation

Uploaded by

floorinauli90
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 (2 votes)
8 views

Readings from Programming with C++ 1st Edition Kyla Mcmullen pdf download

Ebook installation

Uploaded by

floorinauli90
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/ 71

Readings from Programming with C++ 1st Edition

Kyla Mcmullen install download

https://ebookmeta.com/product/readings-from-programming-
with-c-1st-edition-kyla-mcmullen-2/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

Readings from Programming with C++ 1st Edition Kyla


Mcmullen

https://ebookmeta.com/product/readings-from-programming-
with-c-1st-edition-kyla-mcmullen/

Mnemonics for Radiologists and FRCR 2B Viva Preparation


A Systematic Approach Aug 15 2013 _ 1908911956 _ CRC
Press 1st Edition Yoong

https://ebookmeta.com/product/mnemonics-for-radiologists-and-
frcr-2b-viva-preparation-a-systematic-approach-
aug-15-2013-_-1908911956-_-crc-press-1st-edition-yoong/

Ergodic theory geometry and dynamics 1st Edition C


Mcmullen

https://ebookmeta.com/product/ergodic-theory-geometry-and-
dynamics-1st-edition-c-mcmullen/

Ether War Box Set The Complete Series John Walker Et El

https://ebookmeta.com/product/ether-war-box-set-the-complete-
series-john-walker-et-el/
Race and Transnationalism in the Americas 1st Edition
Benjamin Bryce David M K Sheinin Eds

https://ebookmeta.com/product/race-and-transnationalism-in-the-
americas-1st-edition-benjamin-bryce-david-m-k-sheinin-eds/

Keys to Soil Taxonomy 13th 2022 Soil Survey Staff

https://ebookmeta.com/product/keys-to-soil-
taxonomy-13th-2022-soil-survey-staff/

Taking What’s Necessary 1st Edition Alexa Riley

https://ebookmeta.com/product/taking-whats-necessary-1st-edition-
alexa-riley/

Whose Housing Crisis?: Assets and Homes in a Changing


Economy 1st Edition Nick Gallent

https://ebookmeta.com/product/whose-housing-crisis-assets-and-
homes-in-a-changing-economy-1st-edition-nick-gallent/

Religion and Politics: New Developments Worldwide Roy


C. Amore (Editor)

https://ebookmeta.com/product/religion-and-politics-new-
developments-worldwide-roy-c-amore-editor/
Plastics Application Technology for Lightweight
Automobiles 1st Edition Sudhakar Marur

https://ebookmeta.com/product/plastics-application-technology-
for-lightweight-automobiles-1st-edition-sudhakar-marur/
Programming with

C++ KYLA McMULLEN


Elizabeth matthews
June Jamrich Parsons

Australia • Brazil • Canada • Mexico • Singapore • United Kingdom • United States

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
This is an electronic version of the print textbook. Due to electronic rights restrictions,
some third party content may be suppressed. Editorial review has deemed that any suppressed
content does not materially affect the overall learning experience. The publisher reserves the right
to remove content from this title at any time if subsequent rights restrictions require it. For
valuable information on pricing, previous editions, changes to current editions, and alternate
formats, please visit www.cengage.com/highered to search by ISBN#, author, title, or keyword for
materials in your areas of interest.

Important Notice: Media content referenced within the product description or the product
text may not be available in the eBook version.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Readings from Programming with C++ © 2022 Cengage Learning, Inc.
Kyla McMullen, Elizabeth Matthews, WCN: 02-300
June Jamrich Parsons
Unless otherwise noted, all content is © Cengage.
SVP, Higher Education Product Management:
ALL RIGHTS RESERVED. No part of this work covered by the copyright herein
Erin Joyner
may be reproduced or distributed in any form or by any means, except as
VP, Product Management: Thais Alencar permitted by U.S. copyright law, without the prior written permission of the
copyright owner.
Product Team Manager: Kristin McNary

Associate Product Manager: Tran Pham For product information and technology assistance, contact us at
Cengage Customer & Sales Support, 1-800-354-9706
Product Assistant: Tom Benedetto
or support.cengage.com.
Learning Designer: Mary Convertino

Senior Content Manager: Maria Garguilo For permission to use material from this text or product, submit all requests
online at www.cengage.com/permissions.
Digital Delivery Lead: David O’Connor

Technical Editor: John Freitas Library of Congress Control Number: 2020922802

Developmental Editor: Lisa Ruffolo ISBN: 978-0-357-63775-3

Vice President, Marketing – Science, Technology,


Cengage
& Math: Jason Sakos
200 Pier 4 Boulevard
Senior Director, Marketing: Michele McTighe Boston, MA 02210
USA
Marketing Manager: Cassie L. Cloutier

Marketing Development Manager: Cengage is a leading provider of customized learning solutions with
Samantha Best employees residing in nearly 40 different countries and sales in more than
125 countries around the world. Find your local representative at
Product Specialist: Mackenzie Paine
www.cengage.com.
IP Analyst: Ashley Maynard
To learn more about Cengage platforms and services, register or access
IP Project Manager: Cassidie Parker your online learning solution, or purchase materials for your course, visit
www.cengage.com.
Production Service: SPi Global
Notice to the Reader
Designer: Erin Griffin Publisher does not warrant or guarantee any of the products described

Cover Image Source: echo3005/ShutterStock.com herein or perform any independent analysis in connection with any of the
product information contained herein. Publisher does not assume, and
expressly disclaims, any obligation to obtain and include information other
than that provided to it by the manufacturer. The reader is expressly warned
to consider and adopt all safety precautions that might be indicated by the
activities described herein and to avoid all potential hazards. By following
the instructions contained herein, the reader willingly assumes all risks in
connection with such instructions. The publisher makes no representations or
warranties of any kind, including but not limited to, the warranties of fitness
for particular purpose or merchantability, nor are any such representations
implied with respect to the material set forth herein, and the publisher takes
no responsibility with respect to such material. The publisher shall not be
liable for any special, consequential, or exemplary damages resulting, in
whole or part, from the readers’ use of, or reliance upon, this material.

Printed in Mexico
Print Number: 01 Print Year: 2020

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Brief Contents

prefacexiii Module 17
Polymorphism 309
Module 1
Computational Thinking 1 Module 18
Templates 319
Module 2
Programming Tools 15 Module 19
Linked List Data Structures 333
Module 3
Literals, Variables, and Constants 35 Module 20
Stacks and Queues 353
Module 4
Numeric Data Types and Expressions 49 Module 21
Trees and Graphs 371
Module 5
Character and String Data Types 63 Module 22
Algorithm Complexity and Big-O Notation 395
Module 6
Decision Control Structures 83 Module 23
Search Algorithms 411
Module 7
Repetition Control Structures 103 Module 24
Sorting Algorithms 427
Module 8
Arrays 125 Module 25
Processor Architecture 455
Module 9
Functions 145 Module 26
Data Representation 469
Module 10
Recursion 165 Module 27
Programming Paradigms 491
Module 11
Exceptions 185 Module 28
User Interfaces 507
Module 12
File Operations 205 Module 29
Software Development Methodologies 525
Module 13
Classes and Objects 231 Module 30
Pseudocode, Flowcharts, and Decision Tables 541
Module 14
Methods 245 Module 31
Unified Modeling Language 557
Module 15
Encapsulation 271
GLOSSARY 569
Module 16 Index 583
Inheritance 291

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents

prefacexiii Build Tools 22


The Toolset 22
Module 1 Compilers 23
COMPUTATIONAL THINKING 1 Preprocessors and Linkers 24
Virtual Machines 25
Algorithms2 Interpreters 26
Algorithm Basics 2 Debugging Tools 27
Programming Algorithms 2 Programming Errors 27
“Good” Algorithms 3 Syntax Errors 28
Selecting and Creating Algorithms 4 Runtime Errors 29
Decomposition4 Semantic Errors 29
Decomposition Basics 4 Debugging Utilities 30
Structural Decomposition 5 IDEs and SDKs 32
Functional Decomposition 6 Integrated Development Environments 32
Object-Oriented Decomposition 7 Software Development Kits 32
Dependencies and Cohesion 7
Summary 33
Pattern Identification 8
Key Terms  34
Pattern Identification Basics 8
Repetitive Patterns 8
Module 3
Classification Patterns 9
Abstraction9 LITERALS, VARIABLES, AND
Abstraction Basics 9 CONSTANTS35
Classes and Objects 10
Literals36
Black Boxes 11
Numeric Literals 36
Levels of Abstraction 12
Character and String Literals 37
Summary12 Tricky Literals 38
Key Terms  13 Variables and Constants 38
Variables 38
Module 2 Constants 40
PROGRAMMING TOOLS 15 The Memory Connection 41
Assignment Statements 41
Programming Languages 16 Declaring Variables  41
Hello World! 16 Initializing Variables 42
Programming Language Basics 16 Assigning Variables 43
Syntax and Semantics 17
Input and Output 44
Core Elements 19
Your Toolbox 19 Input to a Variable  44
Output from a Variable 46
Coding Tools 20
Program Editors 20 Summary 46
Basic Structure 21 Key Terms  47

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents v

Module 4 Module 6
Numeric Data Types and Decision Control
Expressions 49 Structures 83
Primitive Data Types 50 If-Then Control Structures 84
Data Types 50 Control Structures 84
Primitive Data Types 50 Decision Logic 85
Composite Data Types 51 If-Then Structures 85
Numeric Data Types 52 Relational Operators 87
Integer Data Types 52 The Equal Operator 87
Floating-Point Data Types 53 Using Relational Operators 88
Mathematical Expressions 54 Boolean Expressions and Data Types 89
Arithmetic Operators 54 Multiple Conditions 91
Order of Operations 56 If-Then-Else Structures 91
Compound Operators 56 Nested-If Structures 93
Numeric Data Type Conversion 58 Else If Structures 96
Convert Integers and Floating-Point Numbers 58 Fall Through 97
Rounding Quirks 59 Conditional Logical Operators 100
Formatting Output 60 The AND Operator 100
Formatted Output 60 The OR Operator 101
Formatting Parameters 60 Summary 102
Summary 62 Key Terms 102
Key Terms 62

Module 7
Module 5 Repetition Control
CHARACTER AND STRING DATA TYPES 63 Structures 103
Character Data Types 64 Count-Controlled Loops 104
Working with Character Data 64 Loop Basics 104
Character Memory Allocation 65 Control Statements 105
Digits 66 For-Loops 105
Character Output Format 67 User-Controlled Loops 108
Character Manipulation 68
Counters and Accumulators 109
String Data Types 69 Loops That Count 109
Working with String Data 69 Loops That Accumulate 111
Escape Characters 70
String Indexes 71
Nested Loops 112
Loops Within Loops 112
String Functions 72
Inner and Outer Loops 113
String Manipulation 72
String Length 72 Pre-Test Loops 116
Change Case 73 While-Loops 116
Find the Location of a Character 74 Infinite Loops 117
Retrieve a Substring 75 Breaking Out of Loops 118
Concatenation and Typecasting 76 Post-Test Loops 120
Concatenated Output 76 Do-Loops 120
Concatenated Variables 77 Test Conditions and Terminating
Coercion and Typecasting 78 Conditions 123
Summary 80 Summary 124
Key Terms 81 Key Terms 124

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
vi Programming with C++

Module 8 Pass by Reference 161


Namespaces 162
Arrays 125
Summary 163
Array Basics 126
Key Terms 163
Magic Rectangles 126
Array Characteristics 127
Array Use Cases 128 Module 10
One-Dimensional Arrays 128 Recursion 165
Initialize Numeric Arrays 128
Key Components of Recursion 165
Initialize String Arrays 130
The Recursive Mindset 165
Array Input and Output 130 Recursion Basics 167
Output an Array Element 130 When to Use Recursion 171
Index Errors 131
Using Recursion to Solve Complex
Traverse an Array 132
Problems 171
Input Array Elements 133
Designing Recursive Structures 171
Array Operations 135 Linear Recursion 174
Change an Array Element 135 Branching Recursion 175
Find an Array Element 135
Managing Memory during Recursion 179
Sum Array Elements 137
Memory Management 179
Two-Dimensional Arrays 137 Stable Recursion 182
Two-Dimensional Array Basics 137
Summary 183
Initialize a Two-Dimensional Array 138
Output a Two-Dimensional Array 139 Key Terms 183
Sum Array Columns and Rows 141
Summary 143 Module 11
Key Terms 144 Exceptions 185
Defining Exceptions 185
Module 9 Errors in Code 185
Functions 145 Exception Types 187

Function Basics 146 Dealing with Exceptions 189


Function Classifications 146 Handling Others’ Exceptions 189
Programmer-Defined Functions 146 Try and Catch Blocks 189
Flow of Execution 147 Using Exceptions 198
Function Advantages 147 Throwing Exceptions 198
Void Functions 148 When to Bail 202
Void Function Basics 148 Summary 203
Function Pseudocode 149
Key Terms 203
Functions with Parameters 150
Function Parameters 150
Module 12
Function Arguments 150
The Handoff 152 File Operations 205
Return Values 153 File Input and Output 206
Return Values 153 The Purpose of Files 206
Return Type 156 Anatomy of a File 210
Function Signature 157 File Usage 212
Scope 157 Processing a File 214
Scope Basics 157 Accessing Files 214
Pass by Value 160 Streaming and Buffering 214

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents vii

Reading from a File 216 Method Cascading and Method Chaining 263
Opening a File for Reading 216 Calling Multiple Methods on the Same Object 263
Reading from a File 218 Using Constructors 266
Closing a File 222 Specifying How to Construct an Object 266
Closing Files after Use 222 Constructing an Object from Another Object 268
Trying to Close a File 222 Summary 269
Creating and Writing New Files 222
Key Terms 269
Creating a File 222
Opening a File for Writing 223 Module 15
Writing to and Appending a File 224
Anticipating Exceptions 228 Encapsulation 271
Summary 229 Components of Class Structure 271
Key Terms 230 Data Hiding 271
Designing Objects 273
Self-Reference Scope 276
Module 13 Accessor and Mutator Context 277
Classes and Objects 231 Viewing Data from an Object 277
Classes in Object-Oriented Programming 232 Changing Data in an Object 278
Representing the Real World with Code 232 Using Constructors 280
Using Classes 232 Parameters and Arguments 280
Class Components 233 Default Parameters and Constructor
Using Objects 236 Overloading 281
Creating Objects 236 Encapsulation Enforcement
Objects as Variables 238 with Access Modifiers 283
Object-Oriented Features and Principles 238 Access Modifiers 283
Using Static Elements in a Class 239 Public Variables and Methods 283
Private Variables and Methods 284
Static Member Variables 239
Static Methods 240 Interfaces and Headers 286
Static Classes 241 Interfaces 286
Characteristics of Objects Programming an Interface 287
in Object-Oriented Programs 242 Summary 290
Object Identity 242 Key Terms 290
Object State 242
Object Behavior 243 Module 16
Summary 244 Inheritance 291
Key Terms 244 Using Inheritance 291
Creating Classes from Other Classes 291
Module 14 Family Trees in OOP 292
Methods 245 Levels of Access 295
Necessary Components for Inheritance 296
Using Methods 245
Defining a Parent Class 296
Why Use Methods? 245 Defining a Child Class 297
Anatomy of a Method 251
Creating a Child Class That Inherits
Using Methods 251
from a Parent Class 298
Changing the Default Behavior
Inheritance Syntax 298
of an Object 255 Customizing Behavior 301
Using Objects as Regular Variables 255
Overloading Methods 258 Summary 307
Setting One Object to Equal Another 262 Key Terms 307

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
viii Programming with C++

Module 17 The Find Method 346


The Insert Method 347
Polymorphism 309
Summary 350
The Purpose of Polymorphism 309
Key Terms 351
Flexibility While Coding 309
Dynamic Binding Under the Hood 314
Polymorphism Basics 314 Module 20
Classes Within Classes 314 Stacks and Queues 353
Objects as Other Objects 315 Stacks 353
Virtual Functions 316 Stack Basics 353
Anticipating Customization 316 Stack Use Cases 355
Abstract Classes 317 Built-in Stacks 356
Summary 318 Code a Stack 357

Key Terms 318


Queues 362
Queue Basics 362
Module 18 Queue Use Cases 363
Code a Queue 364
Templates 319
Summary 369
Template Basics 319
Key Terms 369
Data Abstraction 319
Template Structure and Use 322
Module 21
Tricky Templating 328
Advanced Templating 328
Trees and Graphs 371
Templated Objects as Arguments 330 Nonlinear Data Structures 371
Templates as a Problem-Solving Linear versus Nonlinear Structures 371
Approach 331 Nonlinear Building Blocks 373
Designing a Template 331 Tree Structures 373
When to Use Templates 331 Tree Basics 373
Summary 331 Tree Properties 376
Trees as Recursive Structures 376
Key Terms 332
Solving Problems Using Trees 379
Module 19 Tree Applications 379
Linked List Data Data Storage in Trees 380
Graph Structures 387
Structures 333
Graph Basics 387
Linked List Structures 334 Directed and Undirected Graphs 388
Data Structure Selection 334 Solving Problems with Graphs 388
Data Structure Implementation 335 Graph Applications 388
Linked List Basics 336 Computing Paths 389
Types of Linked Lists 337
Summary 394
Singly Linked Lists 337
Doubly Linked Lists 338 Key Terms 394
Circular Linked Lists 339
Linked List Characteristics 339 Module 22
Code a Linked List 342 Algorithm Complexity
The Node Class 342 and Big-O Notation 395
The LinkedList Class 343
The Append Method 343 Big-O Notation 396
Linked List Traversal 345 Algorithm Complexity 396

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents ix

Asymptotic Analysis 397 Quicksort 438


Asymptotic Notation 398 Defining the Quicksort Algorithm 438
Time Complexity 398 Quicksort Properties 446
Big-O Metrics 398 Merge Sort 447
Constant Time 399 Defining the Merge Sort Algorithm 447
Linear Time 399 Merge Sort Properties 453
Quadratic Time 400
Summary 454
Logarithmic Time 401
Key Terms 454
Space Complexity 403
Memory Space 403 Module 25
Constant Space Complexity 404
Linear Space Complexity 404 Processor Architecture 455
Complexity Calculations 405 Processor Organization 456
Line-by-Line Time Complexity 405 Integrated Circuits 456
Combine and Simplify 406 Moore’s Law 458
A Mystery Algorithm 407 CPUs 458
Low-Level Instruction Sets 459
Summary 409
Microprocessor Instruction Sets 459
Key Terms 409 RISC and CISC 460
Machine Language 460
Module 23 Assembly Language 461

Search Algorithms 411 Microprocessor Operations 462


Processing an Instruction 462
Using Search Algorithms 412 The Instruction Cycle 462
Search Basics 412 High-Level Programming Languages 464
Performing a Linear Search 413 Evolution 464
Looking for a Needle in a Haystack 413 Teaching Languages 465
Evaluating Search Time 416 The C Family 465
Web Programming Languages 466
Performing a Binary Search 416 Characteristics 466
Shrinking the Search Space 416 Advantages and Disadvantages 467
Implementing Binary Search 418
Summary 467
Using Regular Expressions
Key Terms 468
in Search Algorithms 423
Specifying a Search Pattern 423 Module 26
Regular Expression Search Operators 423
Data Representation 469
Summary 426
Bits and Bytes 470
Key Terms 426
Digital Data 470
Bits 471
Module 24 Bytes 472
Sorting Algorithms 427 Binary 474
Binary Numbers 474
Qualities of Sorting Algorithms 428 Binary to Decimal 475
Ordering Items 428 Decimal to Binary 476
Time Complexity in Sorting Algorithms 428 Binary Addition 477
Sorting Properties 430 Negative Numbers 478
Bubble Sort 431 Hexadecimal 480
Defining the Bubble Sort Algorithm 431 Colors 480
Bubble Sort Properties 437 Hexadecimal Numbers 481

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
x Programming with C++

Binary-Hex-Binary Conversions 481 Speech Synthesis 516


Hex-Decimal Conversion 482 Designing Programs for Voice User Interfaces 517
Information Density 483 Virtual Environment Interfaces 517
ASCII and Unicode 483 Virtual Environments 517
ASCII 483 Virtual Environment Interface Components 518
Extended ASCII 484 Programming the Virtual Interface 519
Unicode 485 Accessibility and Inclusion 520
Memory Allocation 486 Accessibility Guidelines 520
Memory and Storage 486 Inclusive Design 521
Storage Devices 487 Summary 524
Memory 487
Key Terms 524
Summary 489
Key Terms 489
Module 29
Software Development
Module 27
Methodologies 525
Programming Paradigms 491
Software Development 526
Imperative and Declarative Paradigms 492
The Software Development Life Cycle 526
Think Outside the Box 492
Efficiency, Quality, and Security 527
The Procedural Paradigm 493 The Waterfall Model 528
Procedural Basics 493
Structured Analysis and Design 528
Characteristics of Procedural Programs 494
Waterfall Advantages and Disadvantages 529
Procedural Paradigm Applications 496
The Agile Model 530
The Object-Oriented Paradigm 497
Incremental Development 530
Objects, Classes, and Methods 497
Agile Methodologies 530
Characteristics of Object-Oriented Programs 499
Agile Advantages and Disadvantages 531
Object-Oriented Applications 501
Coding Principles 532
Declarative Paradigms 501
Efficient Coding 532
Declarative Basics 501
Modularized Code 533
Characteristics of the Declarative
Clean Coding 534
Paradigm 504
Secure Coding 534
Applications for Declarative Paradigms 504
Success Factors 536
Summary 505 Testing 536
Key Terms 505 Levels of Testing 536
Unit Testing 537
Module 28 Integration Testing 538
System Testing 539
User Interfaces 507 Acceptance Testing 539
User Interface Basics 508 Regression Testing 539
UI and UX 508 Summary 540
UI Components 508
Key Terms 540
Selecting a UI 510
Command-Line User Interfaces 510
Module 30
Command-Line Basics 510
Command-Line Program Design 510 Pseudocode, Flowcharts,
Graphical User Interfaces 512 and Decision Tables 541
GUI Basics 512 Pseudocode 542
GUI Program Design 514
From Algorithms to Pseudocode 542
Voice User Interfaces 515 Pseudocode Basics 544
Voice Interface Basics 515 Pseudocode Guidelines 545
Speech Recognition 515 Writing Pseudocode 547

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Table of Contents xi

Flowcharts 548 UML Diagram Parts 558


Flowchart Basics 548 Class Diagram Basics 558
Drawing Flowcharts 548 Use Case Diagram Basics 559
Flowchart Tools 549 Sequence Diagrams 561
Decision Tables 551 Using UML to Structure Programs 562
Decision Table Basics 551 UML Associations 562
List the Conditions 551 Translating UML to Code 564
List All Possible Alternatives 552 Summary 568
Specify Results and Rules 552
Interpret Rules 553 Key Terms 568
Optimize the Rules 554
Check for Completeness and Accuracy 555
GLOSSARY 569
Summary 555
index 583
Key Terms 556

Module 31
Unified Modeling Language 557
Purpose of Unified Modeling
Language (UML) 557
Communicating Ideas to Other Programmers 557

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Preface

Welcome to Readings from Programming with C11.


This text includes the stand-alone lessons and read-
C11 Version
ings from MindTap for Programming with C11 and is We recommend downloading the latest version of
intended to be used in conjunction with the MindTap C11 before beginning this text. C1114 was used to
Reader for a complete learning experience. test all C11 code presented in the module figures.

MindTap Overview MindTap Features


Programming with C 11 presents conceptual, lan- In addition to the readings included within this text,
guage-agnostic narrative with language-specific assets, the MindTap includes the following:
ungraded C11 coding Snippets, language-agnostic test Course Orientation: Custom videos and readings pre-
banks, and additional instructor resources. The goal of pare students for the material and coding experiences
this digital product is to develop content around the they will encounter in their course.
concepts that are essential for understanding Com-
Videos: Animated videos demonstrate new program-
puter Science from a language-agnostic perspective.
ming terms and concepts in an easy-to-understand for-
Learners will gain a foundational understanding of
mat, increasing student confidence and learning.
procedural programming, computer science concepts,
and object-oriented programming. Instructors have Coding Snippets: These short, ungraded coding activities
identified the need for language-agnostic, conceptual are embedded within the MindTap Reader and provide
content that can be paired with hands-on practice in students an opportunity to practice new programming
a specific language. This 31-module text is designed to concepts “in-the-moment.” Additional language-specific
provide that conceptual content paired with language- “bridge content” helps transition the student from con-
specific examples and hands-on learning activities in ceptual understanding to application of C11 code.
C11. Language-specific Examples: Figures within the nar-
rative illustrate the application of general concepts in
C11 code.
Course Objectives:
• Develop a foundational knowledge of
coding principles, vocabulary, and core Instructor & Student Resources
concepts. Additional instructor and student resources for
• Use new foundational knowledge to this product are available online. Instructor assets
learn C11 programming skills. include an Instructor’s Manual, Teaching Online
• Practice emerging coding skills in a low- Guide, ­PowerPoint® slides, and a test bank powered
risk environment. by ­Cognero®. Student assets include source code
• Apply learned concepts and skills to files and coding Snippets ReadMe. Sign up or sign in
assignments/activities that mimic real- at www.cengage.com to search for and access this
world experiences and environments. product and its online resources.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
About the Authors

Dr. Kyla McMullen is a tenure-track faculty member published research in the areas of procedural genera-
in the University of Florida’s Computer & Information tion, video game enjoyment factors, and freshwater
Sciences & Engineering Department, specializing in algae identification with HCI.
Human-Centered Computing. Her research interests June Jamrich Parsons is an educator, digital book
are in the perception, applications, and development pioneer, and co-author of Texty and McGuffey Award-
of 3D audio technologies. Dr. McMullen has authored winning textbooks. She co-developed the first com-
over 30 manuscripts in this line of research and is the mercially successful multimedia, interactive digital
primary investigator for over 2 million dollars’ worth textbook; one that set the bar for platforms now
of sponsored research projects. being developed by educational publishers. Her
Dr. Elizabeth A. Matthews is an Assistant Professor of career includes extensive classroom teaching, prod-
Computer Science at Washington and Lee University. uct design for eCourseware, textbook authoring for
She has taught computer science since 2013 and has Course Technology and Cengage, Creative Strategist
been an active researcher in human–computer inter- for MediaTechnics Corporation, and Director of Con-
action and human-centered computing. Matthews has tent for Veative Virtual Reality Labs.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Acknowledgments

The unique approach for this book required a ­seasoned friends who help me to remain sane, remind me of who
team. Our thanks to Maria Garguilo who ushered the I am, and never let me forget whose I am.
manuscripts through every iteration and kept tight Elizabeth Matthews: I want to thank my parents,
rein on the schedule; to Mary E. Convertino who sup- Drs. Geoff and Robin Matthews, for their support and
plied her expertise in learning design; to Lisa R
­ uffolo understanding in my journey. I would also like to thank
for her excellent developmental edit; to Courtney my advisor, Dr. Juan Gilbert, for seeing my dream to
Cozzy who coordinated the project; to Kristin McNary the end. Finally, I would like to thank my cats, Oreo
for her leadership in Cengage’s computing materials; and Laptop, who made sure that writing this book was
to Rajiv Malkan (Lone Star College) for his instruc- interrupted as often as possible.
tional input; to Wade Schofield (Liberty University) June Jamrich Parsons: Computer programming can
for his reviewing expertise; and to John Freitas for his be a truly satisfying experience. The reward when a
meticulous code review. It was a pleasure to be part program runs flawlessly has to bring a smile even
of this professional and talented team. We hope that to the most seasoned programmers. Working with
instructors and students will appreciate our efforts three programming languages for this project at the
to provide this unique approach to computer science same time was certainly challenging but provided
and programming. insights that can help students understand com-
Kyla McMullen: Above all things, I would like to thank putational thinking. I’ve thoroughly enjoyed work-
God for giving me the gifts and talents that were uti- ing with the team to create these versatile learning
lized to write this book. I would like to thank my amaz- resources and would like to dedicate my efforts to
ing husband Ade Kumuyi for always being my rock, my mom, who has been a steadfast cheerleader for
sounding board, and biggest cheerleader. I thank my me throughout my career. To the instructors and stu-
parents, Rita and James McMullen for all of their sacri- dents who use this book, my hope is that you enjoy
fices to raise me. Last but not least, I thank my spirited programming as much as I do.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
MODULE

1
COMPUTATIONAL
THINKING
LEARNING OBJECTIVES:
1.1 ALGORITHMS 1.2.3 Differentiate the concepts of algorithms and
decomposition.
1.1.1 Define the term “algorithm” as a series of steps for
solving a problem or carrying out a task. 1.2.4 Identify examples of structural decomposition.
1.1.2 State that algorithms are the underlying logic for 1.2.5 Identify examples of functional decomposition.
computer programs. 1.2.6 Identify examples of object-oriented decomposition.
1.1.3 Define the term “computer program.” 1.2.7 Provide examples of decomposition in technology
1.1.4 Provide examples of algorithms used in everyday applications.
technology applications. 1.2.8 Explain how dependencies and cohesion relate to
1.1.5 Confirm that there can be more than one algorithm decomposition.
for a task or problem and that some algorithms 1.3 PATTERN IDENTIFICATION
may be more efficient than others.
1.3.1 Define the term “pattern identification” as a technique
1.1.6 Explain why computer scientists are interested in for recognizing similarities or characteristics among
algorithm efficiency. the elements of a task or problem.
1.1.7 List the characteristics of an effective algorithm. 1.3.2 Identify examples of fill-in-the-blank patterns.
1.1.8 Write an algorithm for accomplishing a simple, 1.3.3 Identify examples of repetitive patterns.
everyday technology application.
1.3.4 Identify examples of classification patterns.
1.1.9 Write an alternate algorithm for an everyday
technology task. 1.3.5 Provide examples of pattern identification in the real
world and in technology applications.
1.1.10 Select the more efficient of the two algorithms you
have written. 1.4 ABSTRACTION
1.2 DECOMPOSITION 1.4.1 Define the term “abstraction” as a technique for
generalization and for simplifying levels of complexity.
1.2.1 Define the term “decomposition” as a technique for
dividing a complex problem or solution into smaller 1.4.2 Explain why abstraction is an important computer
parts. science concept.
1.2.2 Explain why decomposition is an important tool for 1.4.3 Provide an example illustrating how abstraction can
computer scientists. help identify variables.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
2 PROGRAMMING WITH C++

1.4.4 Provide examples of technology applications that 1.4.6 Explain how the black box concept is an
have abstracted or hidden details. implementation of abstraction.
1.4.5 Provide an example illustrating the use of a class as 1.4.7 Identify appropriate levels of abstraction.
an abstraction of a set of objects.

1.1 ALGORITHMS
Algorithm Basics (1.1.1, 1.1.4)
A password might not be enough to protect your online accounts. Two-factor authentication adds an extra layer
of protection. A common form of two-factor authentication sends a personal identification number (PIN) to your
cell phone. To log in, you perform the series of steps shown in Figure 1-1.

Connect to the site’s login page.


Enter your user ID.
Enter your password.
Wait for a text message containing a PIN
to arrive on your smartphone.
On the site’s the login page, enter the PIN.

Figure 1-1 Steps for two-factor authentication

The procedure for two-factor authentication is an example of an algorithm. In a general sense, an algorithm
is a series of steps for solving a problem or carrying out a task.
Algorithms exist for everyday tasks and tasks that involve technology. Here are some examples:

• A recipe for baking brownies


• The steps for changing a tire
• The instructions for pairing a smart watch with your phone
• The payment process at an online store
• The procedure for posting a tweet

Programming Algorithms (1.1.2, 1.1.3, 1.1.5)


Algorithms are also an important tool for programmers. A programming algorithm is a set of steps that speci-
fies the underlying logic and structure for the statements in a computer program. You can think of programming
algorithms as the blueprints for computer programs.
A computer program is a set of instructions, written in a programming language such as C++, Python, or
Java, that performs a specific task when executed by a digital device. A computer program is an implementation
of an algorithm.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 1 Computational Thinking 3

Q Programming algorithms tell the computer what to do. Can you tell which of these algorithms
is a programming algorithm?
Algorithm 1: Algorithm 2:
Connect to the website’s login page. Prompt the user to enter a user ID.
Enter your user ID. Prompt the user to enter a password.
Enter your password. Make sure that the user ID and password match.
Wait for a text message containing a PIN to If the user ID and password match:
arrive on your smartphone. Generate a random PIN.
On the website’s login page, enter the PIN. Send the PIN to user’s phone.
Prompt the user to enter the PIN.
If the PIN is correct:
Allow access.

A Algorithm 1 is not a programming algorithm because it outlines instructions for the user.
Algorithm 2 is a programming algorithm because it specifies what the computer is supposed
to do. When you formulate a programming algorithm, the instructions should be for the
computer, not the user.

There can be more than one programming algorithm for solving a problem or performing a task, but some
algorithms are more efficient than others.

Q Here are two algorithms for summing the numbers from 1 to 10. Which algorithm is more
efficient?
Algorithm 1: Algorithm 2:
Add 1 1 2 to get a total. Get the last number in the series (10).
Repeat these steps nine times: Divide 10 by 2 to get a result.
Get the next number. Add 10 1 1 to get a sum.
Add this number to the total. Multiply the result by the sum.

A Both algorithms contain four instructions, but Algorithm 2 is more efficient. You can use it to
amaze your friends by quickly calculating the total in only four steps. Algorithm 1 is also four
lines long, but two of the instructions are repeated nine times. Counting the first step, that’s
19 steps to complete this task!

“Good” Algorithms (1.1.6, 1.1.7)


Computer scientists are interested in designing what they call “good” algorithms. A good algorithm tends
to produce a computer program that operates efficiently, quickly, and reliably. Good algorithms have these
characteristics:
Input: The algorithm applies to a set of specified inputs.
Output: The algorithm produces one or more outputs.
Finite: The algorithm terminates after a finite number of steps.
Precise: Each step of the algorithm is clear and unambiguous.
Effective: The algorithm successfully produces the correct output.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
4 PROGRAMMING WITH C++

When formulating an algorithm, you can easily check to make sure it satisfies all the criteria for a good algo-
rithm. You can see how these criteria apply to an algorithm in Figure 1-2.

Is there input? Yes.

Get the last number in the series (10).


Divide 10 by 2 to get a RESULT. Are the steps precise and
Add 10 + 1 to get a SUM. unambiguous? Yes.
Multiply the RESULT by the SUM to
Does the algorithm terminate? Does the algorithm produce
produce a total.
Yes, after the fourth step. the correct output?
Yes, it is 55.
Is there output? Yes.

Figure 1-2 Is this a good algorithm?

Selecting and Creating Algorithms (1.1.8, 1.1.9, 1.1.10)


Before coding, programmers consider various algorithms that might apply to a problem. You can come up with
an algorithm in three ways:
Use a standard algorithm. Programmers have created effective algorithms for many computing
tasks, such as sorting, searching, manipulating text, encrypting data, and finding the shortest path.
When you are familiar with these standard algorithms, you can easily incorporate them in programs.
Perform the task manually. When you can’t find a standard algorithm, you can formulate an
algorithm by stepping through a process manually, recording those steps, and then analyzing their
effectiveness.
Apply computational thinking techniques. Computational thinking is a set of techniques designed
to formulate problems and their solutions. You can use computational thinking techniques such as
decomposition, pattern identification, and abstraction to devise efficient algorithms. Let’s take a look
at these techniques in more detail.

1.2 DECOMPOSITION
Decomposition Basics (1.2.1)
A mobile banking app contains many components. It has to provide a secure login procedure, allow users to
manage preferences, display account balances, push out alerts, read checks for deposit, and perform other tasks
shown in Figure 1-3.
The algorithm for such an extensive app would be difficult to formulate without dividing it into smaller parts,
a process called decomposition. When devising an algorithm for a complex problem or task, decomposition can
help you deal with smaller, more manageable pieces of the puzzle.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 1 Computational Thinking 5

Fraud alerts

Change user
Secure login
preferences

Account balance Deposits

iStock.com/vector.S
Pay bills Withdrawals

Transfer money

Figure 1-3 A mobile banking app handles many


interacting tasks

Structural Decomposition (1.2.2, 1.2.3, 1.2.4, 1.2.7)


The first step in decomposition is to identify structural units that perform distinct tasks. Figure 1-4 illustrates
how you might divide a mobile banking app into structural units, called modules.

Mobile banking

Passcode/challenge Account Payments and


Secure login
question changes management transfers

Two-factor
Balances Bill pay
authentication

Fingerprint Mobile check


Alerts
sign-in deposits

Direct Money
deposits transfers

Figure 1-4 Structural decomposition diagram

Structural decomposition is a process that identifies a hierarchy of structural units. At the lowest levels
of the hierarchy are modules, indicated in yellow in Figure 1-4, that have a manageable scope for creating
algorithms.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
6 PROGRAMMING WITH C++

Q Which module of the hierarchy chart is not fully decomposed?


A The module for modifying passwords and challenge questions could be further decomposed
into two modules: one module that allows users to change their passwords and one for
changing their challenge questions.

Here are some tips for creating a structural decomposition diagram:

• Use a top-down approach. The nodes at the top break down into component parts in the nodes below them.
• Label nodes with nouns and adjectives, rather than verbs. For example, “Account management” is the
correct noun phrase, rather than a verb phrase, such as “Manage accounts.”
• Don’t worry about sequencing. Except for the actual login process, the components in a mobile bank-
ing system could be accessed in any order. This is a key difference between an algorithm and decom-
position. An algorithm specifies an order of activities, whereas decomposition specifies the parts of
a task.

Functional Decomposition (1.2.5)


Functional decomposition breaks down modules into smaller actions, processes, or steps. Figure 1-5 illustrates
a functional decomposition of the two-factor authentication module.

Verify login attempts

Verify password Verify PIN

Validate Compare
Generate Send PIN
Prompt for Prompt for user ID and Prompt user user-entered Handle
one-time to
user ID password password for PIN PIN to mismatches
PIN mobile
match generated PIN

Look up Compare Look up Generate


stored entered Handle user text Nodes in green are
user ID and date with mismatches mobile message specific enough to
password stored data number with PIN become steps in an
algorithm.

Nodes in yellow
require further
decomposition.

Figure 1-5 Functional decomposition diagram

Notice how the levels of the functional decomposition diagram get more specific until the nodes in the lowest
levels begin to reveal instructions that should be incorporated in an algorithm.
Here are some tips for constructing functional decomposition diagrams and deriving algorithms from them:

• Label nodes with verb phrases. In contrast to the nodes of a structural decomposition diagram, the
nodes of a functional decomposition are labeled with verb phrases that indicate “what” is to be done.
• Sequence from left to right. Reading left to right on the diagram should correspond to the sequence in
which steps in the algorithm are performed.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 1 Computational Thinking 7

Object-Oriented Decomposition (1.2.6)


Another way to apply decomposition to a module is to look for logical and physical objects that a computer
program will manipulate. Figure 1-6 illustrates an object-oriented decomposition of the two-factor authentica-
tion module.

Each node
represents an
object.
PIN generator
Methods
Login screen Attribute: One-time PIN describe what an
object can do.
Attribute: Entered user ID Method: Generate random PIN
Attribute: Entered password Method: Send PIN to mobile
number
Method: Display login boxes
Method: Verify entered user ID
and password
Method: Verify PIN
Method: Handle mismatches

Valid user

Attribute: Valid user ID


Attribute: Valid password Attributes
Attribute: Mobile number describe an
object.

Figure 1-6 Object-oriented decomposition diagram

An object-oriented decomposition does not produce a hierarchy. Instead it produces a collection of objects
that can represent people, places, or things.
Tips for object-oriented decomposition:

• Node titles are nouns. Each node in the object-oriented decomposition diagram is labeled with a noun.
• Attributes are nouns. A node can contain a list of attributes, which relate to the characteristics of an object.
• Methods are verb phrases. An object can also contain methods, which are actions that an object can
perform. You may need to devise an algorithm for each method.
• Sketch in connection arrows. Connection arrows help you visualize how objects share data.

Dependencies and Cohesion (1.2.8)


You might wonder if there is a correct way to decompose a problem or task. In practice, there may be several
viable ways to apply decomposition, but an effective breakdown minimizes dependencies and maximizes cohe-
sion among the various parts.
The principles of decomposition are:

• Minimize dependencies. Although input and output may flow between nodes, changing the instructions
in one module or object should not require changes to others.
• Maximize cohesion. Each object or module contains attributes, methods, or instructions that perform a
single logical task or represent a single entity.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
8 PROGRAMMING WITH C++

1.3 PATTERN IDENTIFICATION


Pattern Identification Basics (1.3.1, 1.3.2)
The Amaze-Your-Friends math trick for quickly adding numbers from 1 to 10 is very simple:
Get the last number in the series (10).
Divide 10 by 2 to get a result.
Add 10 1 1 to get a sum.
Multiply the result by the sum.

Q Try the algorithm yourself. What is your answer?


A If your math is correct, your answer should be 55.

Now, what if the challenge is to add the numbers from 1 to 200? That algorithm looks like this:
Get the last number in the series (200).
Divide 200 by 2 to get a result.
Add 200 1 1 to get a sum.
Multiply the result by the sum.
Notice a pattern? This fill-in-the-blank algorithm works for any number:
Get the last number in the series (____________________).
Divide ____________________ by 2 to get a result.
Add ____________________ 1 1 to get a sum.
Multiply the result by the sum.
The process of finding similarities in procedures and tasks is called pattern identification. It is a useful com-
putational thinking technique for creating algorithms that can be used and reused on different data sets. By recog-
nizing the pattern in the Amaze-Your-Friends math trick, you can use the algorithm to find the total of any series
of numbers.

Repetitive Patterns (1.3.3)


In addition to fill-in-the-blank patterns, you might also find repetitive patterns as you analyze tasks and problems.
Think about this algorithm, which handles logins to a social media site:
Get a user ID.
Get a password.
If the password is correct, allow access.
If the password is not correct, get the password again.
If the password is correct, allow access.
If the password is not correct, get the password again.
If the password is correct, allow access.
If the password is not correct, get the password again.
If the password is correct, allow access.
If the password is not correct, lock the account.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 1 Computational Thinking 9

Q How many repetition patterns do you recognize?


A Two lines are repeated three times:
If the password is not correct, get the password again.
If the password is correct, allow access.

Recognizing this repetition, you can streamline the algorithm like this:
Get a password.
Repeat three times:
If the password is correct, allow access.
If the password is not correct, get the password again.
If the password is correct, allow access.
If the password is not correct, lock the account.

Classification Patterns (1.3.4, 1.3.5)


Everyone who subscribes to a social media site has a set of login credentials. Here are Lee’s and Priya’s:
Lee’s login credentials: Priya’s login credentials:
Lee’s user ID: [email protected] Priya’s user ID: [email protected]
Lee’s password: MyCat411 Priya’s password: ouY52311v
Lee’s mobile number: 415-999-1234 Priya’s mobile number: 906-222-0987
The series of attributes that define each user’s login credentials have a pattern of similarities. Each user
has three attributes: a user ID, a password, and a mobile number. By recognizing this pattern, you can create a
template for any user’s login credentials like this:
User ID: ____________________
Password: ____________________
Mobile number: ____________________
You can often discover classification patterns in the attributes that describe any person or object. Identify-
ing classification patterns can help you design programs that involve databases because the template identifies
fields, such as User ID, that contain data.
Classification patterns also come in handy if you want to design programs based on the interactions among
a variety of objects, rather than a step-by-step algorithm. In some programming circles, templates are called
classes because they specify the attributes for a classification of objects. For example, people classified as social
media subscribers have attributes for login credentials. Vehicles classified as cars have attributes such as color,
make, model, and VIN number. Businesses classified as restaurants have a name, hours of operation, and a menu.

1.4 ABSTRACTION
Abstraction Basics (1.4.1, 1.4.2, 1.4.3)
Think back to the Amaze-Your-Friends math trick. By identifying a pattern, you formulated a general algorithm
that works for a sequence of any length, whether it is a sequence of 1 to 10 or 1 to 200.
Get the last number in the series (____________________).
Divide ____________________ by 2 to get a result.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
10 PROGRAMMING WITH C++

Add ____________________ 1 1 to get a sum.


Multiply the result by the sum.
In this algorithm, the blank line is an abstraction that represents the last number in the sequence. An abstraction
hides details, simplifies complexity, substitutes a generalization for something specific, and allows an algorithm to
work for multiple inputs.
Abstraction is a key element of computational thinking and helps programmers in a multitude of ways.
If you’ve programmed before, you’ll recognize that in the Amaze-Your-Friends algorithm, the blanks could
become a variable with a name such as last_number. Result and sum are also variables because they represent
values that change depending on the numbers in the sequence
Get the last_number
Divide last_number by 2 to get a result.
Add last_number 1 1 to get a sum.
Multiply the result by the sum.
A variable is an abstraction because rather than representing a specific number, it can be used to represent
many different numbers.

Classes and Objects (1.4.4, 1.4.5)


Abstraction has uses other than identifying variables. It is important for understanding how to represent real-
world and conceptual objects. Remember the pattern you discovered for social media login credentials? With a
little modification, it becomes a template that can be applied to any subscriber:
Class: LoginCredentials
Attribute: user_ID
Attribute: user_password
Attribute: mobile_number
The LoginCredentials class is an abstraction that contains a set of attributes. The class was formed by
abstracting away, or removing, details for any specific subscriber.
Abstractions are handy for any programs that deal with real-world objects in addition to technology objects,
such as login credentials.

Q Can you envision a class that’s an abstraction of the collection of objects shown in Figure 1-7?
Budnyi/Shutterstock.com

12 oz water glass 5 oz martini glass 8 oz wine glass 10 oz water goblet

Figure 1-7 Abstract the details from this collection of glassware

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 1 Computational Thinking 11

A The glassware class could have these attributes:


Class: Glassware
Attribute: Color
Attribute: Capacity
Attribute: Style

Black Boxes (1.4.6)


To drive a car, you don’t have to know exactly what goes on under the hood. The engine is essentially a “black
box” that you control using a few simple inputs such as the gas pedal, brake, and steering wheel. The details of the
engine are hidden. In computer science terminology, these details have been “abstracted away.” See Figure 1-8.

You can drive a car without


knowing the details of what’s
under the hood.

Figure 1-8 The controls for a car are an abstraction of


its detailed mechanics

In concept, a black box is anything that accepts some type of input and performs a specific process to
­produce output without requiring an understanding of its internal workings. See Figure 1-9.

Something goes on inside.

Something goes in.

Something comes out.

Black box

Figure 1-9 Black box abstraction

The black-box concept of abstraction is a fundamental aspect of computer science. Think about it. C
­ omputer
programs are abstractions. For example, you can use a social media app without knowing anything about the
programming that makes it work. The icons that you touch on the screen abstract away the details of the under-
lying programming.
Programmers make extensive use of abstraction within programs by creating a set of instructions that func-
tions like a black box. For example, you could bundle the instructions that handle login attempts into a black
box like the one in Figure 1-10.
Programming languages also have built-in abstractions that perform standard tasks. For example, the built-in
random function generates a random number when given a range, such as 1–100. You can incorporate the random
function in a program without knowing how it works internally.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
12 PROGRAMMING WITH C++

Look up stored user ID and


User ID
password.
password
Compare stored user ID and
password to entered user ID Allow or deny access
and password.
If they match, allow access.

Black box

Figure 1-10 A login abstraction

Levels of Abstraction (1.4.7)


After you get the hang of abstraction, you’ll see examples of this computational thinking concept everywhere.
Applying the correct level of abstraction to your programs may take a little practice.
A level of abstraction relates to the amount of detail that is hidden. Abstracting out too much detail can
make a program too generalized. Neglecting abstraction can produce programs that are too specific to work with
a wide variety of data. See Figure 1-11.

Too specific Just right Too abstract

Figure 1-11 Levels of abstraction

With experience, you’ll be able to identify useful abstractions and gauge the correct level of abstraction
to use.

SUMMARY
• Computational thinking techniques help programmers define problems, find solutions, delineate tasks,
and devise algorithms.
• An algorithm is a series of steps for solving a problem or carrying out a task. Programming algorithms
are the blueprints for computer programs.
• Standard algorithms exist for many computing tasks. When an algorithm does not exist, you can step
through a process manually and record the steps, or apply computational thinking techniques, such as
decomposition, pattern identification, and abstraction.
• Decomposition divides a complex problem or task into manageable units.
• Pattern identification reveals sequences and repetitive tasks that can lead to algorithm efficiencies.
• Abstraction is a key computer science concept that suppresses details, substitutes a generalization for
something specific, and allows an algorithm to work for multiple inputs.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 1 Computational Thinking 13

Key Terms
abstraction computer program objects
algorithm decomposition object-oriented decomposition
attributes Functional decomposition pattern identification
classes level of abstraction programming algorithm
classification patterns methods Structural decomposition
Computational thinking modules

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
MODULE

2
PROGRAMMING TOOLS
LEARNING OBJECTIVES:
2.1 PROGRAMMING LANGUAGES 2.3.5 Explain the purpose of a linker.
2.1.1 Explain the significance of the Hello World! program. 2.3.6 Associate virtual machines with Java and
bytecode.
2.1.2 Reiterate that programming languages are used to
create software. 2.3.7 Explain how an interpreter works.
2.1.3 Name some popular programming languages. 2.3.8 Differentiate between source code, bytecode,
object code, and executable code.
2.1.4 Distinguish between syntax and semantics in the
context of programming languages.
2.4 DEBUGGING TOOLS
2.1.5 Identify the key characteristics common to
2.4.1 Explain the purpose of debugging.
programming languages.
2.4.2 List common syntax errors.
2.1.6 Explain options for accessing programming
language implementations. 2.4.3 List common runtime errors.
2.1.7 Identify programming tools. 2.4.4 List common logic errors.
2.4.5 Classify program errors as syntax errors, logic
2.2 CODING TOOLS
errors, or runtime errors.
2.2.1 Define a program editor as the tool used to enter
2.4.6 Classify a debugger as utility software that allows
program code.
programmers to walk through the code of a target
2.2.2 List the types of editors that can be used for program to find errors.
coding.
2.4.7 List handy features provided by a debugger.
2.2.3 List some handy features of code editors and
explain how they help programmers create 2.5 IDEs AND SDKs
clean code.
2.5.1 List the purpose and typical features of an
2.2.4 Identify the basic structure and syntactical integrated development environment (IDE).
elements for a program written in the
2.5.2 Explain how IDEs support visual programming.
programming language you use.
2.5.3 Confirm that some IDEs are installed locally, while
2.3 BUILD TOOLS other IDEs are accessed online.
2.3.1 Explain the purpose of build tools. 2.5.4 Identify popular IDEs.
2.3.2 Explain the difference between source code and 2.5.5 List the purpose and typical features of a software
object code. development kit (SDK).
2.3.3 Describe how a compiler works. 2.5.6 Provide examples of SDK functionality.
2.3.4 Explain the purpose of a preprocessor. 2.5.7 Identify popular SDKs.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
16 PROGRAMMING WITH C++

2.1 PROGRAMMING LANGUAGES


Hello World! (2.1.1)
Hola! Bonjour! Hi! Ciao! Namaste! Salaam! Nihau! Greetings like these are the first thing you learn when studying
a spoken language. Programmers have a similar starting point. It is the Hello World! program.
Hello World! is without doubt the most famous computer program of all time. It has been written in every one
of the 7001 programming languages. It is typically the first program written by aspiring programmers.

Q Take a look at the Hello World! programs in Figure 2-1. They are written in three popular
programming languages, C11, Java, and Python. What differences can you identify?

Hello World! in C++ Hello World! in Java Hello World! in Python

#include <iostream> class HelloWorld { print("Hello World!")


using namespace std; public static void main(String[] args) {
int main() System.out.println("Hello World!");
{ }
cout <<"Hello World!"; }
return 0;
}

Figure 2-1 Hello World in C11, Java, and Python

A You probably noticed the following differences:


Length: Some Hello World! programs required more instructions than others.
Punctuation: Some programs used lots of curly brackets and semicolons.
Wording: One program used the word cout where the other programs used System.out.
println and print.
Complexity: Some programs seemed easier to interpret than others.

A lot of backstory is bundled into the Hello World! program that can be applied to learning a programming
language. Let’s unbundle this famous program to discover the basics about your programming language.

Programming Language Basics (2.1.2, 2.1.3)


A programming language is a notation for specifying the steps in an algorithm that are supposed to be per-
formed by a digital device. Programming languages such as C11, Java, and Python are classified as high-level
programming languages because they provide a way for you as a programmer to compose human-readable
instructions that direct the operations performed by a computing device.
How does your programming language stack up to other popular languages and where does it shine? Check
out the list in Figure 2-2.
Using a high-level programming language, you can specify a set of instructions that correspond to the steps
in an algorithm. Each instruction is referred to as a statement. The set of statements you generate using a pro-
gramming language is called program code, or simply code. Your code can become a computer program, which
can be distributed as computer software, desktop applications, web apps, and mobile apps.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 2 Programming Tools 17

Logo Programming Where It Shines


Language

Consumer-facing web development, server-side web


JavaScript
development

Server-side web development, scripting, machine learning,


Python
data science, desktop applications, robotics

Server-side web development, desktop applications, mobile


Java
development, Internet of Things

Systems programming, embedded applications, Internet of


C
Things

Systems programming, game development, desktop


C++
applications embedded applications, robotics

PHP Server-side web development

Swift Desktop and mobile applications for Apple devices

Desktop applications, game development, virtual reality,


C#
mobile development

Ruby Server-side web development

Server-side web development, systems programming,


Go
Internet of Things

Figure 2-2 Popular programming languages

Syntax and Semantics (2.1.4)


When learning a programming language, where do you start? Programming languages have two key elements:
semantics and syntax.
Semantics refers to the meaning and validity of program statements. In a natural language, such as English,
it is perfectly fine to say, “Let’s start the game” or “Let’s start the car,” but “Let’s start the pen” doesn’t make any
sense. It is not semantically valid.
Programming languages include keywords, such as print and cout. These words are a subset of a program-
ming language’s reserved words, which are reserved for special purposes. You cannot use reserved words when
you create names for variables, functions, or classes in your programs.
One aspect of learning a programming language is to become familiar with its keywords and their use. When
learning C11, for example, you’ll need to remember that cout sends output to a display device. But if you’re
learning Java, the keyword is System.out.println. In Python the keyword is print. Figure 2-3 lists keywords
that are common to many programming languages.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
18 PROGRAMMING WITH C++

Keyword Purpose
int Define a value as an integer.
float Define a value as a floating-point number.
char Define something as a letter, numeral, punctuation mark, or symbol.
class Define the characteristics of a person, place, or thing.
new Create an object based on the characteristics of a class.
if Execute program statements when a condition is true.
then
else
case Specify several alternative conditions or decisions.
switch
for Begin a section of code that repeats one or more times.
while
do
return Bring a value back from a function.
import Incorporate a prewritten code module in a program.
try Catch errors and handle them gracefully.

Figure 2-3 Keywords common to programming languages

Q Suppose you want to code a program to output “Hello World!” 10 times. What keyword could
you use?

A Figure 2-3 lists the for, while, and do keywords for specifying code that repeats one or
more times.

You’ll acquire a vocabulary of keywords for your programming language gradually. Language references
provide a list of keywords and examples of their use. You can find language references online. In fact, it is often
helpful to create or find a “cheat sheet” of keywords to keep beside your computer as you learn a new language.
The syntax of a programming language is equivalent to the grammar rules of a written language, such as
English or Cyrillic. Syntax defines the order of words and the punctuation you are required to use when compos-
ing statements.
Various programming languages use different punctuation syntax. One of the early steps in learning a
programming language is to get a handle on its syntax.
Remember the different use of punctuation in the C11, Java, and Python Hello World! programs? Take a closer
look in Figure 2-4 and notice how punctuation helps to separate and structure the statements in a C11 program.

C++ Program
Statements end with a
#include <iostream>
semicolon.
using namespace std;
int main()
{
Curly braces for (int i = 1; i <= 10, i++)
separate blocks of {
statements. These cout << "Hello World!" << endl;
braces set off the cout << "Hola Mundo!" << endl;
statements in the }
return 0; Statements between
main() function.
} braces are indented.

Figure 2-4 C++ syntax style

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 2 Programming Tools 19

Like C11, Java also uses lots of punctuation. Figure 2-5 points out how the commonly used style for placing
curly braces in Java is slightly different from C11. However, either style works equally well as long as the braces
are paired. The key is consistency in the project you are working on.

Curly braces separate


blocks of statements.
Java Program Notice how the opening
class HelloWorld { brace is at the end of
public static void main(String[ ] args) { the line. This is an
System.out.println("Hello World!"); alternative style for the
System.out.println("Hola Mundo!"); placement of braces.
}
}
Statements end with a
semicolon.
Statements in a block
are indented.

Figure 2-5 Java syntax style

Python uses quite a different approach to punctuation. See if you can spot the differences in Figure 2-6.

Python Program
The colon indicates
for count in range(0, 10): the beginning of a
print("Hello World!") block of statements.
print("Hola Mundo!") Python does not use
Statements
braces.
in a block
are indented.
Pressing Enter or
Return marks the end
of a statement.

Figure 2-6 Python syntax style

In C11 and Java, statements are enclosed in a structure of curly braces and each statement ends with a semi-
colon. In contrast, Python uses indents to structure statements, and the linefeed that is generated when you press
the Enter key marks the end of a statement. Remember the punctuation style for the language that you are using.

Core Elements (2.1.5)


Although programming languages have differing syntax and semantics, they have common elements. If you look
for the ways that your programming language implements the elements in the following list, you will be well on
your way through the initial learning curve.
Variables that can be assigned values and structures
Arithmetic operators that can be used to perform calculations
Keywords that perform operations such as print or append
Data types that define values and text
Branching controls that change the sequence in which statements are executed
Repetition controls that repeat a series of statements
Syntax rules for constructing valid statements
Terminology for describing the components of a language and their functions

Your Toolbox (2.1.6, 2.1.7)


Before you can write your Hello World! program, you need access to tools for coding instructions and executing
them on a digital device. These tools are sometimes referred to as software development platforms, programming
platforms, or development environments.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
20 PROGRAMMING WITH C++

Programming tools are available as system utility software that can be installed on your computer. Another
option is to use an online programming app.
Your programming toolbox includes the following essentials:
Coding tools provide a way for you to codify an algorithm.
Build tools transform your code into a binary format that a computer can execute.
Debugging tools help you test programs and track down errors.
You can acquire these programming tools as individual components, or you can look for a comprehensive
development environment. Let’s explore these tools in more detail to find out how they can help you develop
brilliant programs.

2.2 CODING TOOLS


Program Editors (2.2.1, 2.2.2, 2.2.3)
Coding tools provide you with a way to express an algorithm using the vocabulary of a programming language.
Just how much of the code you have to manually type depends on your development platform. Here are the
options:
Visual platform: You might have the option of arranging visual elements that represent various
statements, as shown in Figure 2-7.

This column contains


your program. It consists
of visual components,
each representing a
statement.

You can drag Here is the statement


Source: scratch.mit.edu

components from the left that displays “Hello


column into your World!”
program. After dragging,
you can customize the
component, like changing
“Hello!” to “Hello World!”

Figure 2-7 Visual programming

Word processor: Despite the attraction of visual programming tools, you will likely end up typing
some if not all of the statements for your programs. For that task, you could use a word processor, but
it embeds all kinds of codes for formatting and font effects which can’t be included in your high-level
code.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 2 Programming Tools 21

Text editor: A text editor such as Windows Notepad can produce plain ASCII text, but offers no
features designed to help programmers. Using a generic text editor is a bare-bones approach. There
are much better coding tools.
Code editor: A code editor is a type of text editor specially designed for entering high-level
programming code. It can help you correctly use punctuation and indents, as well as remember
variable names and ensure that you use valid keywords. Some handy features of code editors are
highlighted in Figure 2-8.

Color coding helps you


recognize keywords,
directives, and
punctuation.

Punctuation monitoring
ensures that the braces
are paired. Autocomplete displays a
list of keywords as you
begin to type.

Source: Visual Studio


Figure 2-8 Visual Studio code editor

When using a code editor, you can simply type your commands. As you type, pay attention to color coding,
autocomplete, and other helpful cues that the editor offers.

Basic Structure (2.2.4)


A programming language has structural and syntax conventions for writing code. When you become familiar with
these conventions, they will be the underlying foundation for most of the programs you create. Browse through
the following list before you try to identify the conventions in the next figure.
Comments: A comment provides an explanation of your code. At the beginning of a program, you can rou-
tinely include a comment with the program title and your name. Comments can be included anywhere in your
code to annotate aspects of the program that might not be clear to other programmers who test or modify your
code. Computers ignore comments as they are intended for documentation only.
Directives: A directive tells the computer how to handle the program rather than how to perform an algo-
rithm. For example, a directive might specify the name of an external file that is supposed to be incorporated
with the rest of your program code. Directives may begin with keywords such as #include, import, or using.
main() function: Many programming languages use main() to denote the entry point for program execution.
The line of code following main() corresponds to the first step in the algorithm you designed for a problem or
task.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
22 PROGRAMMING WITH C++

Statements: Just as human-readable text is divided into sentences and paragraphs, your code should be
divided into statements and blocks. A program statement is similar to a sentence; it contains one instruction. In
languages such as C11 and Java, statements end with a semicolon. In Python, you end a statement by pressing
the Enter or Return key.
Code blocks: A code block is similar to a paragraph; it contains multiple statements that have a specific
purpose. In C++ and Java, you use braces (also known as curly brackets) to enclose code blocks. In Python, code
blocks are indented.

Q Take a look at Figure 2-9 and see if you can identify comments, directives, the main()
function, statements, and code blocks.

//Hello World! in English and Spanish


#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 10, i++)
{
cout << "Hello World!" << endl;
cout << "Hola Mundo!" << endl;
}
return 0;
}

Figure 2-9 Find the structural and syntax


conventions in this code

A The comment is //Hello World! in English and Spanish.


There are two directives: #include <iostream> and using namespace std.
The main() function is labeled int main().
Every line of the program is a statement.
The for..return statement is one block. The two cout statements form a block within the
for..return block.

2.3 BUILD TOOLS


The Toolset (2.3.1)
The high-level code that you produce using an editor cannot be directly executed by a computer’s micropro-
cessor. Microprocessors have a machine language instruction set based on binary codes. There is no print
instruction in the microprocessor’s instruction set, for example.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 2 Programming Tools 23

Before a program can run, your code has to be converted into machine code consisting of machine language
instructions. This executable code is stored in an executable file that can be run, processed, and executed by
the microprocessor. Build tools convert your code into a program that a computer can execute.
As a programmer, you’ll eventually want to package your programs and distribute them so that they can be
installed on a digital device and run with a click, just like the Hola Mundo! program in Figure 2-10.

Hola Mundo!

Source: Microsoft Corporation


program

Figure 2-10 Hola Mundo! installed on a Windows computer

A little background about build tools can help you understand some of the weird statements, such as
#include<iostream>, that you have to include in programs. Also, a passing knowledge of build tools such as
compilers, preprocessors, linkers, virtual machines, and interpreters will help you hold your own at late-night
parties in Silicon Valley.

Compilers (2.3.2, 2.3.3)


A compiler translates code written in one programming language into another language. The original code is
called source code, and the code a compiler produces is called object code. Object code is stored in a standalone
file that is separate from the file that holds your source code. See Figure 2-11.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
24 PROGRAMMING WITH C++

Your source code Object code

Hello.cpp Hello.o

#include <iostream> 00010010 10100010


using namespace std; 10100101 11110000
int main() 01010100 10101010
Compiler conversion
{ 00101010 11010101
cout << "Hello World!"; 10101010 10001010
return 0; 10101010 10101011
} 00010101 10101011
00000001 10101010
10001010 10101010

Figure 2-11 When the compiler creates machine code, the resulting file is
executable

Q If a compiler is converting C11 code into machine code, what is the source code and what is
the object code?

A The C11 code is the source code. The machine code is the object code.

Preprocessors and Linkers (2.3.4, 2.3.5)


The journey from source code to an executable program doesn’t usually happen in one magical step. Your source
code may be analyzed by a preprocessor and combined with other code by a linker.
A preprocessor is a software utility that performs housekeeping tasks before the compiler gets to work.
Experienced programmers can creatively use preprocessors to make source programs easy to compile for vari-
ous software platforms.
For languages such as C11, even basic programs require preprocessing and preprocessor directives such
as #include<iostream> to be routinely included in the program code. After these directives are carried out
by the preprocessor, they can be ignored by the compiler.
A linker is a software utility that converts one or more object files into a binary executable file. Linking takes
place after compiling. Your source code is the main object file for linking, but you can specify additional object
files that you’ve created or that are available from third parties. When you see the keyword import, it is a clue
that an external file needs to be linked in.
Figure 2-12 can help you visualize the build process when a preprocessor and linker are involved.
Preprocessing and linking are not consistent for all programming languages. Your takeaways are:

• Several steps are required to convert your code into a file containing an executable program.
• You may have to include preprocessor directives in your code.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Module 2 Programming Tools 25

The preprocessor executes


directives, such as
#include<iostream>

Hola.cpp

The compiler
converts the source
code to object code
Your source
code
Hola.cpp

The linker combines


modules and generates
machine code
Source code
plus iostream
Hola.o

Hola.exe

Figure 2-12 Preprocessing, compiling, and linking the Hola Mundo! program

Virtual Machines (2.3.6)


The product of a compiler is not necessarily machine code. Some compilers convert source code into semi-
compiled code called bytecode. Software called a virtual machine converts the bytecode into machine code
as the program executes.
What’s the point of a virtual machine? The Java Virtual Machine (JVM) offers some insight. The backstory is
that an executable file designed to run on a computer with the Microsoft Windows operating system won’t work
on a computer running macOS. You could develop one version of a program for Microsoft Windows and one for
macOS. Or, you can use the Java programming language to create one version of the program and compile it to
bytecode.
You can distribute the same bytecode to Windows and Mac users. Windows users have the Windows version
of the JVM. Mac users have another version. The virtual machine software converts the bytecode into code that
works on the host platform. Figure 2-13 can help you understand how the JVM works and the value of bytecode.

Copyright 2022 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
Discovering Diverse Content Through
Random Scribd Documents
了幾腳,罵道:“賊歪剌骨!我使他來要餅,你如何罵他?你罵他奴
才,你如何不溺泡尿把你自家照照!”雪娥被西門慶踢罵了一頓,敢怒
而不敢言。西門慶剛走出廚房外,孫雪娥對著來昭妻一丈青說道:“你
看,我今日晦氣!早是你在旁聽,我又沒曾說什麼。他走將來凶神似
一般,大吆小喝,把丫頭採的去了,反對主子面前輕事重報,惹的走
來平白地把恁一場兒。我洗著眼兒,看著主子奴才長遠恁硬氣著,只
休要錯了腳兒!”不想被西門慶聽見了,復回來又打了幾拳,罵
道:“賊奴才淫婦!你還說不欺負他,親耳朵聽見你還罵他。”打的雪
娥疼痛難忍,西門慶便往前邊去了。那雪娥氣的在廚房裡兩淚悲流,
放聲大哭。吳月娘正在上房,才起來梳頭,因問小玉:“廚房裡亂些甚
麼?”小玉回道:“爹要餅吃了往廟上去,說姑娘罵五娘房裡春梅來,
被爹聽見了,踢了姑娘幾腳,哭起來。”月娘道:“也沒見他,要餅吃
連忙做了與他去就罷了,平白又罵他房裡丫頭怎的!”於是使小玉走到
廚房,攛掇雪娥和家人媳婦忙造湯水,打發西門慶吃了,往廟上去,
不題。

這雪娥氣憤不過,正走到月娘房裡告訴此事。不妨金蓮驀然走來,
立於窗下潛聽。見雪娥在房裡對月娘、李嬌兒說他怎的霸攔漢子,背
地無所不為:“娘,你還不知淫婦,說起來比養漢老婆還浪,一夜沒漢
子也不成的。背地乾的那繭兒,人乾不出,他乾出來。當初在家,把
親漢子用毒藥擺死了,跟了來。如今把俺們也吃他活埋了。弄的漢子
烏眼雞一般,見了俺們便不待見。”月娘道:“也沒見你,他前邊使了
丫頭要餅,你好好打發與他去便了。平白又罵他怎的?”孫雪娥
道:“我罵他禿也瞎也來?那頃,這丫頭在娘房裡著緊不聽手。俺沒曾
在竈上把刀背打他,娘尚且不言語。可可今日輪到他手裡,便驕貴的
這等了。”正說著,只見小玉走到,說: “五娘在外邊。”少傾,金蓮進
房,望著雪娥說道:“比如我當初擺死親夫,你就不消叫漢子娶我來
家,省得我霸攔著他,撐了你的窩兒。論起春梅,又不是我的丫頭,
你氣不憤,還教他伏侍大娘就是了。省得你和他合氣,把我扯在裡
頭。那個好意死了漢子嫁人?如今也不難的勾當,等他來家,與我一
紙休書,我去就是了。” 月娘道:“我也不曉的你們底事。你們大家省
言一句兒便了。”孫雪娥道:“娘,你看他嘴似淮洪也一般,隨問誰也
辯他不過。明在漢子根前戳舌兒,轉過眼就不認了。依你說起來,除
了娘,把俺們都攆,只留著你罷!”那吳月娘坐著,由著他那兩個你一
句我一句,只不言語。後來見罵起來,雪娥道:“你罵我奴才!你便是
真奴才!”險些兒不曾打起來。月娘看不上,使小玉把雪娥拉往後邊
去。這潘金蓮一直歸到前邊,卸了濃妝,洗了脂粉,烏雲散亂,花容
不整,哭得兩眼如桃,躺在床上。

到日西時分,西門慶廟上來,袖著四兩珠子,進入房中,一見便
問:“怎的來?”婦人放聲號哭起來,問西門慶要休書。如此這般告訴
一遍:“我當初又不曾圖你錢財,自恁跟了你來。如何今日教人這等欺
負?千也說我擺殺漢子,萬也說我擺殺漢子!沒丫頭便罷了,如何要
人房裡丫頭伏侍?吃人指罵!”這西門慶不聽便罷,聽了時,三屍神暴
跳,五臟氣衝天。一陣風走到後邊,採過雪娥頭髮來,儘力拿短棍打
了幾下。多虧吳月娘向前拉住了,說道:“沒得大家省些事兒罷了!好
交你主子惹氣!”西門慶便道:“好賊歪剌骨,我親自聽見你在廚房裡
罵,你還攪纏別人。我不把你下截打下來也不算。”看官聽說:不爭今
日打了孫雪娥,管教潘金蓮從前作過事,沒興一齊來。正是:

自古感恩並積恨,萬年千載不生塵。

當下西門慶打了雪娥,走到前邊,窩盤住了金蓮,袖中取出廟上買
的四兩珠子,遞與他。婦人見漢子與他做主,出了氣,如何不喜。由
是要一奉十,寵愛愈深。

話休饒舌,一日正輪該花子虛家擺酒會茶,這花家就在西門慶緊隔
壁。內官家擺酒,甚是豐盛。眾兄弟都到了。因西門慶有事,約午後
才來,都等他,不肯先坐。少頃,西門慶來到,然後敘禮讓坐,東家
安西門慶居首席。兩個妓女,琵琶箏琴在席前彈唱。端的說不盡梨園
嬌艷,色藝雙全。但見:
羅衣疊雪,寶髻堆雲。櫻桃口,杏臉桃腮;楊柳腰,蘭心蕙
性。歌喉宛轉,聲如枝上流鶯;舞態蹁躚,影似花間鳳轉。腔依古
調,音出天然。舞回明月墜秦樓,歌遏行雲遮楚館。高低緊慢按宮
商,輕重疾徐依格調,箏排雁柱聲聲慢,板拍紅牙字字新。

少頃,酒過三巡,歌吟兩套,兩個唱的放下樂器,向前花枝搖颭般
來磕頭。西門慶呼玳安書袋內取兩封賞賜,每人二錢,拜謝了下去。
因問東家花子虛道:“這位姐兒上姓?端的會唱。”東家未及答應,應
伯爵插口道:“大官人多忘事,就不認的了?這彈箏的是花二哥令翠
──勾欄後巷吳銀兒。這彈琵琶的,就是我前日說的李三媽的女兒、李
桂卿的妹子,小名叫做桂姐。你家中見放著他的親姑娘。如何推不認
的?”西門慶笑道:“元來就是他,我六年不見,不想就出落得恁般成
人了!” 落後酒闌,上席來遞酒。這桂姐殷勤勸酒,情話盤桓。西門
慶因問:“你三媽與姐姐桂卿,在家做甚麼?怎的不來我家看看你姑
娘?”桂姐道:“俺媽從去歲不好了一場,至今腿腳半邊通動不的,只
扶著人走。俺姐姐桂卿被淮上一個客人包了半年,常接到店里住,兩
三日不放來家。家中好不無人,只靠著我逐日出來供唱,好不辛苦!
時常也想著要往宅里看看姑娘,白不得個閑。爹許久怎的也不在裡邊
走走?幾時放姑娘家去看看俺媽也好。”西門慶見他一團和氣,說話兒
乖覺伶變,就有幾分留戀之意,說道:“我今日約兩位好朋友送你家
去。你意下如何?”桂姐道:“爹休哄我。你肯貴人腳兒踏俺賤地?”西
門慶道:“我不哄你。”便向袖中取出汗巾連挑牙與香茶盒兒,遞與桂
姐收了。桂姐道:“多咱去?如今使保兒先家去先說一聲,作個預
備。”西門慶道:“直待人散,一同起身。”少頃,遞畢酒,約掌燈人散
時分,西門慶約下應伯爵、謝希大,也不到家,騾馬同送桂姐,逕進
勾欄往李家去。正是:

陷人坑,土窖般暗開掘;迷魂洞,囚牢般巧砌疊;檢屍場,屠
鋪般明排列。整一味死溫存活打劫。招牌兒大字書者:買俏金,哥哥
休扯;纏頭錦,婆婆自接;賣花錢,姐姐不賒。
西門慶等送桂姐轎子到門首,李桂卿迎門接入堂中。見畢禮數,請
老媽出來拜見。不一時,虔婆扶拐而出,半邊胳膊都動彈不得,見了
西門慶,道了萬福。說道: “天麽,天麽!姐夫貴人,那陣風兒颳得
你到這裡?”西門慶笑道:“一向窮冗,沒曾來得,老媽休怪。”虔婆又
嚮應、謝二人說道:“二位怎的也不來走走?”伯爵道:“便是白不得
閑,今日在花家會茶,遇見桂姐,因此同西門爹送回來。快看酒來,
俺們樂飲三杯。”虔婆讓三位上首坐了。一面點茶,一面打抹春台,收
拾酒菜。少頃,掌上燈燭,酒餚羅列。桂姐從新房中打扮出來,旁邊
陪坐,免不得姐妹兩個金樽滿泛,玉阮同調,歌唱遞酒。正是:

琉璃鐘,琥珀濃,小槽酒滴珍珠紅。烹龍炮鳳玉脂泣,羅幃繡
幙圍香風。吹龍笛,擊鼉鼓。皓齒歌,細腰舞。況是青春莫虛度,銀
缸掩映嬌娥語,不到劉伶墳上去。

當下姊妹兩個唱了一套,席上觥籌交錯飲酒。西門慶向桂卿道:“今
日二位在此,久聞桂姐善舞能歌南曲,何不請歌一詞,奉勸二位一杯
兒酒!”應伯爵道:“我又不當起動,借大官人餘光,洗耳願聽佳
音。”那桂姐坐著只是笑,半晌不動身。原來西門慶有心要梳籠桂姐,
故先索落他唱。那院中婆娘見識精明,早已看破了八九分。桂卿在
旁,就先開口說道:“我家桂姐從小兒養得嬌,自來生得靦腆,不肯對
人胡亂便唱。”於是西門慶便叫玳安書袋內取出五兩一錠銀子來,放在
桌上,說道:“這些不當甚麼,權與桂姐為脂粉之需,改日另送幾套織
金衣服。”桂姐連忙起身謝了。先令丫鬟收去,方纔下席來唱。這桂姐
雖年紀不多,卻色藝過人,當下不慌不忙,輕扶羅袖,擺動湘裙,袖
口邊搭剌著一方銀紅撮穗的落花流水汗巾兒,歌唱道:

【駐雲飛】舉止從容,壓盡勾欄占上風。行動香風送,頻使人
欽重。嗏!玉杵污泥中,豈凡庸?一曲宮商,滿座皆驚動。勝似襄王
一夢中,勝似襄王一夢中。

唱畢,把個西門慶喜歡的沒入腳處。吩咐玳安回馬家去,晚夕就在
李桂卿房裡歇了一宿。緊著西門慶要梳籠這女子,又被應伯爵、謝希
大兩個一力攛掇,就上了道兒。次日,使小廝往家去拿五十兩銀子,
段鋪內討四件衣裳,要梳籠桂姐。那李嬌兒聽見要梳籠他的侄女兒,
如何不喜?連忙拿了一錠大元寶付與玳安,拿到院中打頭面,做衣
服,定桌席,吹彈歌舞,花攢錦簇,飲三日喜酒。應伯爵、謝希大又
約會了孫寡嘴、祝實念、常峙節,每人出五分分子,都來賀他。鋪的
蓋的都是西門慶出。每日大酒大肉,在院中玩耍,不在話下。

舞裙歌板逐時新,散盡黃金只此身。寄語富兒休暴殄,儉如良
藥可醫貧。

第十二回 潘金蓮私僕受辱 劉理星魘勝求財

詩曰:

可憐獨立樹,枝輕根亦搖。雖為露所浥,復為風所飄。
錦衾襞不開,端坐夜及朝。是妾愁成瘦,非君重細腰。

話說西門慶在院中貪戀桂姐姿色,約半月不曾來家。吳月娘使小廝
拿馬接了數次,李家把西門慶衣帽都藏過,不放他起身。丟的家中這
些婦人都閑靜了。別人猶可,惟有潘金蓮這婦人,青春未及三十歲,
欲火難禁一丈高。每日打扮的粉妝玉琢,皓齒朱唇,無日不在大門首
倚門而望,只等到黃昏。到晚來歸入房中,粲枕孤幃,鳳台無伴,睡
不著,走來花園中,款步花苔。看見那月洋水底,便疑西門慶情性難
拿;偶遇著玳瑁貓兒交歡,越引逗的他芳心迷亂。當時玉樓帶來一個
小廝,名喚琴童,年約十六歲,才留起頭髮,生的眉目清秀,乖滑伶
俐。西門慶教他看管花園,晚夕就在花園門首一間小耳房內安歇。金
蓮和玉樓白日里常在花園亭子上一處做針指或下棋。這小廝專一獻小
殷勤,常觀見西門慶來,就先來告報。以此婦人喜他,常叫他入房,
賞酒與他吃。兩個朝朝暮暮,眉來眼去,都有意了。
不想到了七月,西門慶生日將近。吳月娘見西門慶留戀煙花,因使
玳安拿馬去接。這潘金蓮暗暗修了一柬帖,交付玳安,教:“悄悄遞與
你爹,說五娘請爹早些家去罷。”這玳安兒一直騎馬到李家,只見應伯
爵、謝希大、祝實念,孫寡嘴,常峙節眾人,正在那裡伴著西門慶,
摟著粉頭歡樂飲酒。西門慶看見玳安來到,便問: “你來怎麽?家中
沒事?”玳安道:“家中沒事。”西門慶道:“前邊各項銀子,叫傅二叔
討討,等我到家算帳。”玳安道:“這兩日傅二叔討了許多,等爹到家
上帳。”西門慶道:“你桂姨那一套衣服,捎來不曾?”玳安道:“已捎
在此。”便向氈包內取出一套紅衫藍裙,遞與桂姐。桂姐道了萬福,收
了,連忙吩咐下邊,管待玳安酒飯。那小廝吃了酒飯,復走來上邊伺
候。悄悄向西門慶耳邊說道:“五娘使我捎了個帖兒在此。請爹早些家
去。”西門慶才待用手去接,早被李桂姐看見,只道是西門慶那個表子
寄來的情書,一手撾過來,拆開觀看,卻是一幅迴文錦箋,上寫著幾
行墨跡。桂姐遞與祝實念,教念與他聽。這祝實念見上面寫詞一首,
名《落梅風》,念道:

黃昏想,白日思,盼殺人多情不至。因他為他憔悴死,可憐也
繡衾獨自! 燈將殘,人睡也,空留得半窗明月。眠心硬,渾似鐵,這
凄涼怎捱今夜?

下書:“愛妾潘六兒拜。”那桂姐聽畢,撇了酒席,走入房中,倒在
床上,面朝裡邊睡了。西門慶見桂姐惱了,把帖子扯的稀爛,眾人前
把玳安踢了兩腳。請桂姐兩遍不來,慌的西門慶親自進房,抱出他
來,說道:“吩咐帶馬回去,家中那個淫婦使你來,我這一到家,都打
個臭死!”玳安只得含淚回家。西門慶道:“桂姐,你休惱,這帖子不
是別人的,乃是我第五個小妾寄來,請我到家有些事兒計較,再無別
故。”祝實念在旁戲道:“桂姐,你休聽他哄你哩!這個潘六兒乃是那
邊院里新敘的一個表子,生的一表人物。你休放他去。”西門慶笑趕著
打,說道:“你這賤天殺的,單管弄死了人,緊著他恁麻犯人,你又胡
說。”李桂卿道:“姐夫差了,既然家中有人拘管,就不消梳籠人家粉
頭,自守著家裡的便了。才相伴了多少時,便就要拋離了去。”應伯爵
插口道:“說的有理。你兩人都依我,大官人也不消家去,桂姐也不必
惱。今日說過,那個再恁,每人罰二兩銀子,買酒咱大家吃。”於是西
門慶把桂姐摟在懷中陪笑,一遞一口兒飲酒。少傾,拿了七鐘茶來,
馨香可掬,每人面前一盞。應伯爵道:“我有個曲兒,單道這茶好處:

【朝天子】這細茶的嫩芽,生長在春風下。不揪不採葉兒楂,
但煮著顏色大。絕品清奇,難描難畫。口裡兒常時呷,醉了時想他,
醒來時愛他。原來一簍兒千金價。”

謝希大笑道:“大官人使錢費物,不圖這‘一摟兒’,卻圖些甚的?如
今每人有詞的唱詞,不會詞,每人說個笑話兒,與桂姐下酒。”就該謝
希大先說,因說道: “有一個泥水匠,在院中墁地。老媽兒怠慢了
他,他暗把陰溝內堵上塊磚。落後天下雨,積的滿院子都是水。老媽
慌了,尋的他來,多與他酒飯,還秤了一錢銀子,央他打水平。那泥
水匠吃了酒飯,悄悄去陰溝內把那塊磚拿出,那水登時出的罄盡。老
媽便問作頭:‘此是那裡的病?’泥水匠回道:‘這病與你老人家的病一
樣,有錢便流,無錢不流。’”桂姐見把他家來傷了,便道:“我也有個
笑話,回奉列位。有一孫真人,擺著筵席請人,卻教座下老虎去請。
那老虎把客人都路上一個個吃了。真人等至天晚,不見一客到。不一
時老虎來,真人便問:‘你請的客人都那裡去了?’老虎口吐人言:‘告
師父得知,我從來不曉得請人,只會白嚼人。’” 當下把眾人都傷了。
應伯爵道:“可見的俺們只是白嚼,你家孤老就還不起個東道?”於是
向頭上撥下一根鬧銀耳斡兒來,重一錢;謝希大一對鍍金網巾圈,秤
了秤重九分半;祝實念袖中掏出一方舊汗巾兒,算二百文長錢;孫寡
嘴腰間解下一條白布裙,當兩壺半酒;常峙節無以為敬,問西門慶借
了一錢銀子。都遞與桂卿,置辦東道,請西門慶和桂姐。那桂卿將銀
錢都付與保兒,買了一錢豬肉,又宰了一隻雞,自家又陪些小菜兒,
安排停當。大盤小碗拿上來,眾人坐下,說了一聲動箸吃時,說時
遲,那時快,但見:
人人動嘴,個個低頭。遮天映日,猶如蝗蚋一齊來;擠眼掇
肩,好似餓牢才打出。這個搶風膀臂,如經年未見酒和餚;那個連三
筷子,成歲不筵與席。一個汗流滿面,卻似與雞骨禿有冤仇;一個油
抹唇邊,把豬毛皮連唾咽。吃片時,杯盤狼藉;啖頃刻,箸子縱橫。
這個稱為食王元帥,那個號作凈盤將軍。酒壺番曬又重斟,盤饌已無
還去探。正是:珍羞百味片時休,果然都送入五臟廟。

當下眾人吃得個凈光王佛。西門慶與桂姐吃不上兩鐘酒,揀了些菜
蔬,又被這夥人吃去了。那日把席上椅子坐折了兩張,前邊跟馬的小
廝,不得上來掉嘴吃,把門前供養的土地翻倒來,便剌了一泡[禾囗
也]谷都的熱屎。臨出門來,孫寡嘴把李家明間內供養的鍍金銅佛,
塞在褲腰裡;應伯爵推鬥桂姐親嘴,把頭上金琢針兒戲了;謝希大把
西門慶川扇兒藏了;祝實念走到桂卿房裡照面,溜了他一面水銀鏡
子。常峙節借的西門慶一錢銀子,競是寫在嫖賬上了。原來這起人,
只伴著西門慶玩耍,好不快活。有詩為證:

工妍掩袖媚如猱,乘興閑來可暫留。若要死貪無厭足,家中金
鑰教誰收?

按下眾人簇擁著西門慶飲酒不題。單表玳安回馬到家,吳月娘和孟
玉樓、潘金蓮正在房坐的,見了便問玳安:“你去接爹來了不曾?”玳
安哭的兩眼紅紅的,說道:被爹踢罵了小的來了。爹說那個再使人
接,來家都要罵。”月娘便道:“你看恁不合理,不來便了,如何又罵
小廝?”孟玉樓道:“你踢將小廝便罷了,如何連俺們都罵將來?”潘金
蓮道:“十個九個院中淫婦,和你有甚情實!常言說的好:船載的金
銀,填不滿煙花寨。”金蓮只知說出來,不防李嬌兒見玳安自院中來
家,便走來窗下潛聽。見金蓮罵他家千淫婦萬淫婦,暗暗懷恨在心。
從此二人結仇,不在話下。正是:

甜言美語三冬暖,惡語傷人六月寒。
不說李嬌兒與潘金蓮結仇。單表金蓮歸到房中,捱一刻似三秋,盼
一時如半夏。知道西門慶不來家,把兩個丫頭打發睡了,推往花園中
游玩,將琴童叫進房與他酒吃。把小廝灌醉了,掩上房門,褪衣解
帶,兩個就乾做一處。但見:

一個不顧綱常貴賤,一個那分上下高低。一個色膽歪邪,管甚
丈夫利害;一個淫心蕩漾,縱他律法明條。一個氣喑眼瞪,好似牛吼
柳影;一個言驕語澀,渾如鶯轉花間。一個耳畔許雨意雲情,一個枕
邊說山盟海誓。百花園內,翻為快活排場;主母房中,變作行樂世
界。霎時一滴驢精髓,傾在金蓮玉體中。

自此為始,每夜婦人便叫琴童進房如此。未到天明,就打發出來。
背地把金裹頭簪子兩三根帶在頭上,又把裙邊帶的錦香囊葫蘆兒也與
了他。豈知這小廝不守本分,常常和同行小廝街上吃酒耍錢,頗露機
關。常言:若要不知,除非莫為。有一日,風聲吹到孫雪娥、李嬌兒
耳朵內,說道:“賊淫婦,往常假撇清,如何今日也做出來了?”齊來
告月娘。月娘再三不信,說道:“不爭你們和他合氣,惹的孟三姐不
怪?只說你們擠撮他的小廝。”說的二人無言而退。落後婦人夜間和小
廝在房中行事,忘記關廚房門,不想被丫頭秋菊出來凈手,看見了。
次日傳與後邊小玉,小玉對雪娥說。雪娥同李嬌兒又來告訴月娘如此
這般:“他屋裡丫頭親口說出來,又不是俺們葬送他。大娘不說,俺們
對他爹說。若是饒了這個淫婦,非除饒了蝎子!”

此時正值七月二十七日,西門慶從院中來家上壽。月娘道:“他才來
家,又是他好日子,你們不依我,只顧說去!等他反亂將起來,我不
管你。”二人不聽月娘,約的西門慶進入房中,齊來告訴金蓮在家怎的
養小廝一節。這西門慶不聽萬事皆休,聽了怒從心上起,惡向膽邊
生。走到前邊坐下,一片聲叫琴童兒。早有人報與潘金蓮。金蓮慌了
手腳,使春梅忙叫小廝到房中,囑咐千萬不要說出來,把頭上簪子都
拿過來收了。著了慌,就忘解了香囊葫蘆下來。被西門慶叫到前廳跪
下,吩咐三四個小廝,選大板子伺候。西門慶道:“賊奴才,你知罪
麽?”那琴童半日不敢言語。西門慶令左右:“撥下他簪子來,我
瞧!”見沒了簪子,因問:“你戴的金裹頭銀簪子,往那裡去了?”琴童
道:“小的並沒甚銀簪子。”西門慶道:“奴才還搗鬼!與我旋剝了衣
服,拿板子打!”當下兩三個小廝扶侍一個,剝去他衣服,扯了褲子。
見他身底下穿著玉色絹縼兒,縼兒帶上露出錦香囊葫蘆兒。西門慶一
眼看見,便叫:“拿上來我瞧!”認的是潘金蓮裙邊帶的物件,不覺心
中大怒,就問他: “此物從那裡得來?你實說是誰與你的?”唬的小廝
半日開口不得,說道:“這是小的某日打掃花園,在花園內拾的。並不
曾有人與我。”西門慶越怒,切齒喝令: “與我捆起來著實打!”當下把
琴童繃子繃著,打了三十大棍,打得皮開肉綻,鮮血順腿淋漓。又叫
來保:“把奴才兩個鬢毛與我撏了!趕將出去,再不許進門!” 那琴童
磕了頭,哭哭啼啼出門去了。
潘金蓮在房中聽見,如提冷水盆內一般。不一時,西門慶進房來,
嚇的戰戰兢兢,渾身無了脈息,小心在旁扶侍接衣服,被西門慶兜臉
一個耳刮子,把婦人打了一交。吩咐春梅:“把前後角門頂了,不放一
個人進來!”拿張小椅兒,坐在院內花架兒底下,取了一根馬鞭子,拿
在手裡,喝令:“淫婦,脫了衣裳跪著!”那婦人自知理虧,不敢不
跪,真個脫去了上下衣服,跪在面前,低垂粉面,不敢出一聲兒。西
門慶便問:“賊淫婦,你休推夢裡睡里,奴才我已審問明白,他一一都
供出來了。你實說,我不在家,你與他偷了幾遭?”婦人便哭道:“天
那,天那!可不冤屈殺了我罷了!自從你不在家半個來月,奴白日里
只和孟三兒一處做針指,到晚夕早關了房門就睡了。沒勾當,不敢出
這角門邊兒來。你不信,只問春梅便了。有甚和鹽和醋,他有個不知
道的?”因叫春梅:“姐姐你過來,親對你爹說。”西門慶罵道:“賊淫
婦!有人說你把頭上金裹頭簪子兩三根都偷與了小廝,你如何不
認?”婦人道:“就屈殺了奴罷了!是那個不逢好死的嚼舌根的淫婦,
嚼他那旺跳身子。見你常時進奴這屋裡來歇,無非都氣不憤,拿這有
天沒日頭的事壓枉奴。就是你與的簪子,都有數兒,一五一十都在,
你查不是!我平白想起甚麼來與那奴才?好成材的奴才,也不枉說
的,恁一個尿不出來的毛奴才,平空把我篡一篇舌頭!”西門慶
道:“簪子有沒罷了。”因向袖中取出那香囊來,說道:“這個是你的物
件兒,如何打小廝身底下捏出來?你還口強甚麼?”說著紛紛的惱了,
向他白馥馥香肌上,颼的一馬鞭子來,打的婦人疼痛難忍,眼噙粉
淚,沒口子叫道:“好爹爹,你饒了奴罷!你容奴說便說,不容奴說,
你就打死了奴,也只臭爛了這塊地。這個香囊葫蘆兒,你不在家,奴
那日同孟三姐在花園裡做生活,因從木香棚下過,帶兒系不牢,就抓
落在地,我那裡沒尋,誰知這奴才拾了。奴並不曾與他。”只這一句,
就合著琴童供稱一樣的話,又見婦人脫的光赤條條,花朵兒般身子,
嬌啼嫩語,跪在地下,那怒氣早已鑽入爪窪國去了,把心已回動了八
九分,因叫過春梅,摟在懷中,問他:“淫婦果然與小廝有首尾沒有?
你說饒了淫婦,我就饒了罷。”那春梅撒嬌撒痴,坐在西門慶懷裡,說
道:“這個,爹你好沒的說!我和娘成日唇不離腮,娘肯與那奴才?這
個都是人氣不憤俺娘兒們,做作出這樣事來。爹,你也要個主張,好
把醜名兒頂在頭上,傳出外邊去好聽?”幾句把西門慶說的一聲兒沒言
語,丟了馬鞭子,一面叫金蓮起來,穿上衣服,吩咐秋菊看菜兒,放
桌兒吃酒。這婦人滿斟了一杯酒,雙手遞上去,跪在地下,等他鐘
兒。西門慶吩咐道:“我今日饒了你。我若但凡不在家,要你洗心改
正,早關了門戶,不許你胡思亂想。我若知道,並不饒你!”婦人
道:“你吩咐,奴知道了。”又與西門慶磕了四個頭,方纔安坐兒,在
旁陪坐飲酒。潘金蓮平日被西門慶寵的狂了,今日討這場羞辱在身
上。正是:

為人莫作婦人身,百年苦樂由他人。

當下西門慶正在金蓮房中飲酒,忽小廝打門,說:“前邊有吳大舅、
吳二舅、傅伙計、女兒、女婿,眾親戚送禮來祝壽。”方纔撇了金蓮,
出前邊陪待賓客。那時應伯爵、謝希大眾人都有人情,院中李桂姐家
亦使保兒送禮來。西門慶前邊亂著收人家禮物,發柬請人,不在話
下。

且說孟玉樓打聽金蓮受辱,約的西門慶不在房裡,瞞著李嬌兒、孫
雪娥,走來看望。見金蓮睡在床上,因問道:“六姐,你端的怎麼緣
故?告我說則個。”那金蓮滿眼流淚哭道:“三姐,你看小淫婦,今日
在背地裡白唆調漢子,打了我恁一頓。我到明日,和這兩個淫婦冤仇
結得有海深。”玉樓道:“你便與他有瑕玷,如何做作著把我的小廝弄
出去了?六姐,你休煩惱,莫不漢子就不聽俺們說句話兒?若明日他
不進我房裡來便罷,但到我房裡來,等我慢慢勸他。”金蓮道:“多謝
姐姐費心。”一面叫春梅看茶來吃。坐著說了回話,玉樓告回房去了。
至晚,西門慶因上房吳大妗子來了,走到玉樓房中宿歇。玉樓因說
道:“你休枉了六姐心,六姐並無此事,都是日前和李嬌兒、孫雪娥兩
個有言語,平白把我的小廝扎罰了。你不問個青紅皂白,就把他屈
了,卻不難為他了!我就替他賭個大誓,若果有此事,大姐姐有個不
先說的?”西門慶道:“我問春梅,他也是這般說。”玉樓道:“他今在
房中不好哩,你不去看他看去?”西門慶道:“我知道,明日到他房中
去。”當晚無話。

到第二日,西門慶正生日。有周守備、夏提刑、張團練、吳大舅許
多官客飲酒,拿轎子接了李桂姐並兩個唱的,唱了一日。李嬌兒見他
侄女兒來,引著拜見月娘眾人,在上房裡坐吃茶。請潘金蓮見,連使
丫頭請了兩遍,金蓮不出來,只說心中不好。到晚夕,桂姐臨家去,
拜辭月娘。月娘與他一件雲絹比甲兒、汗巾花翠之類,同李嬌兒送出
門首。桂姐又親自到金蓮花園角門首:“好歹見見五娘。”那金蓮聽見
他來,使春梅把角門關得鐵桶相似,說道:“娘吩咐,我不敢開。”這
花娘遂羞訕滿面而回,不題。

單表西門慶至晚進入金蓮房內來,那金蓮把雲鬢不整,花容倦淡,
迎接進房,替他脫衣解帶,伺候茶湯腳水,百般殷勤扶侍。到夜裡枕
席歡娛,屈身忍辱,無所不至,說道:“我的哥哥,這一家誰是疼你
的?都是露水夫妻,再醮貨兒。惟有奴知道你的心,你知道奴的意。
旁人見你這般疼奴,在奴身邊的多,都氣不憤,背地裡駕舌頭,在你
跟前唆調。我的傻冤家!你想起甚麼來,中人的拖刀之計,把你心愛
的人兒這等下無情的折挫!常言道:家雞打的團團轉,野雞打的貼天
飛。你就把奴打死了,也只在這屋裡。就是前日你在院里踢罵了小廝
來,早是有大姐姐、孟三姐在跟前,我自不是說了一聲,恐怕他家粉
頭掏淥壞了你身子,院中唱的一味愛錢,有甚情節?誰人疼你?誰知
被有心的人聽見,兩個背地做成一幫兒算計我。自古人害人不死,天
害人才害死了。往後久而自明,只要你與奴做個主兒便了。”幾句把西
門慶窩盤住了。是夜與他淫欲無度。

過了幾日,西門慶備馬,玳安、平安兩個跟隨,往院中來。卻說李
桂姐正打扮著陪人坐的,聽見他來,連忙走進房去,洗了濃妝,除了
簪環,倒在床上裹衾而卧。西門慶走到,坐了半日,老媽才出來,道
了萬福,讓西門慶坐下,問道:“怎的姐夫連日不進來走走?”西門慶
道:“正是因賤日窮冗,家中無人。”虔婆道:“姐兒那日打攪。”西門
慶道:“怎的那日桂卿不來走走?”虔婆道:“桂卿不在家,被客人接去
店里。這幾日還不放了來。”說了半日話,才拿茶來陪著吃了。西門慶
便問:“怎的不見桂姐?”虔婆道:“姐夫還不知哩,小孩兒家,不知怎
的,那日著了惱,來家就不好起來,睡倒了。房門兒也不出,直到如
今。姐夫好狠心,也不來看看姐兒。”西門慶道:“真個?我通不
知。”因問:“在那邊房裡?我看看去。”虔婆道:“在他後邊卧房裡
睡。”慌忙令丫鬟掀帘子。西門慶走到他房中,只見粉頭烏雲散亂,粉
面慵妝,裹被坐在床上,面朝里,見了西門慶,不動一動兒。西門慶
道:“你那日來家,怎的不好?”也不答應。又問:“你著了誰人惱,你
告我說。”問了半日,那桂姐方開言說道:“左右是你家五娘子。你家
中既有恁好的迎歡賣俏,又來稀罕俺們這樣淫婦做甚麼?俺們雖是門
戶中出身,蹺起腳兒,比外邊良人家不成的貨色兒高好些!我前日又
不是供唱,我也送人情去。大娘到見我甚是親熱,又與我許多花翠衣
服。待要不請他見,又說俺院中沒禮法。聞說你家有五娘子,當即請
他拜見,又不出來。家來同俺姑娘又辭他去,他使丫頭把房門關了。
端的好不識人敬重!”西門慶道:“你到休怪他。他那日本等心中不自
在,他若好時,有個不出來見你的?這個淫婦,我幾次因他咬群兒,
口嘴傷人,也要打他哩!”桂姐反手向西門慶臉上一掃,說道:“沒羞
的哥兒,你就打他?”西門慶道: “你還不知我手段,除了俺家房下,
家中這幾個老婆丫頭,但打起來也不善,著緊二三十馬鞭子還打不下
來。好不好還把頭髮都剪了。”桂姐道:“我見砍頭的,沒見吹嘴的,
你打三個官兒,唱兩個喏,誰見來?你若有本事,到家裡只剪下一柳
子頭髮,拿來我瞧,我方信你是本司三院有名的子弟。”西門慶
道:“你敢與我排手?”那桂姐道:“我和你排一百個手。”當日西門慶
在院中歇了一夜,到次日黃昏時分,辭了桂姐,上馬回家。桂姐
道:“哥兒,你這一去,沒有這物件兒,看你拿甚嘴臉見我!”

這西門慶吃他激怒了幾句話,歸家已是酒酣,不往別房裡去,逕到
潘金蓮房內來。婦人見他有酒了,加意用心伏侍。問他酒飯都不吃。
吩咐春梅把床上枕席拭抹乾凈,帶上門出去。他便坐在床上,令婦人
脫靴。那婦人不敢不脫。須臾,脫了靴,打發他上床。西門慶且不
睡,坐在一隻枕頭上,令婦人褪了衣服,地下跪著。那婦人嚇的捏兩
把汗,又不知因為甚麼,於是跪在地下,柔聲痛哭道:“我的爹爹!你
透與奴個伶俐說話,奴死也甘心。饒奴終日恁提心吊膽,陪著一千個
小心,還投不著你的機會,只拿鈍刀子鋸處我,教奴怎生吃受?”西門
慶罵道:“賤淫婦,你真個不脫衣裳,我就沒好意了!”因叫春梅:“門
背後有馬鞭子,與我取了來!”那春梅只顧不進房來,叫了半日,才慢
條廝禮推開房門進來。看見婦人跪在床地平上,向燈前倒著桌兒下,
由西門慶使他,只不動身。婦人叫道:“春梅,我的姐姐,你救我救
兒,他如今要打我。”西門慶道:“小油嘴兒,你不要管他。你只遞馬
鞭子與我打這淫婦。”春梅道:“爹,你怎的恁沒羞!娘乾壞了你甚麼
事兒?你信淫婦言語,平地里起風波,要便搜尋娘?還教人和你一心
一計哩!你教人有那眼兒看得上你!倒是我不依你。”拽上房門,走在
前邊去了。那西門慶無法可處,倒呵呵笑了,向金蓮道:“我且不打
你。你上來,我問你要椿物兒,你與我不與我?”婦人道:“好親親,
奴一身骨朵肉兒都屬了你,隨要甚麼,奴無有不依隨的。不知你心裡
要甚麼兒?”西門慶道:“我要你頂上一柳兒好頭髮。”婦人道:“好心
肝!奴身上隨你怎的揀著燒遍了也依,這個剪頭髮卻依不的,可不嚇
死了我罷了。奴出娘胞兒,活了二十六歲,從沒乾這營生。打緊我頂
上這頭髮近來又脫了好些,只當可憐見我罷。”西門慶道:“你只怪我
惱,我說的你就不依。”婦人道:“我不依你,再依誰?”因問:“你實
對奴說,要奴這頭髮做甚麼?”西門慶道:“我要做網巾。”婦人
道:“你要做網巾,奴就與你做,休要拿與淫婦,教他好壓鎮我。” 西
門慶道:“我不與人便了,要你發兒做頂線兒。”婦人道:“你既要做頂
線,待奴剪與你。”當下婦人分開頭髮,西門慶拿剪刀,按婦人頂上,
齊臻臻剪下一大柳來,用紙包放在順袋內。婦人便倒在西門慶懷中,
嬌聲哭道:“奴凡事依你,只願你休忘了心腸,隨你前邊和人好,只休
拋閃了奴家!”是夜與他歡會異常。

到次日,西門慶起身,婦人打發他吃了飯,出門騎馬,逕到院里。
桂姐便問:“你剪的他頭髮在那裡?”西門慶道:“有,在此。”便向茄
袋內取出,遞與桂姐。打開看,果然黑油也一般好頭髮,就收在袖
中。西門慶道:“你看了還與我,他昨日為剪這頭髮,好不煩難,吃我
變了臉惱了,他才容我剪下這一柳子來。我哄他,只說要做網巾頂線
兒,逕拿進來與你瞧。可見我不失信。”桂姐道:“甚麼稀罕貨,慌的
恁個腔兒!等你家去,我還與你。比是你恁怕他,就不消剪他的來
了。”西門慶笑道:“那裡是怕他!恁說我言語不的了。”桂姐一面叫桂
卿陪著他吃酒,走到背地裡,把婦人頭髮早絮在鞋底下,每日踹踏,
不在話下。卻把西門慶纏住,連過了數日,不放來家。

金蓮自從頭髮剪下之後,覺道心中不快,每日房門不出,茶飯慵
餐。吳月娘使小廝請了家中常走看的劉婆子來看視,說:“娘子著了些
暗氣,惱在心中,不能迴轉,頭疼噁心,飲食不進。”一面打開藥包
來,留了兩服黑丸子藥兒:“晚上用薑湯吃。”又說:“我明日叫我老公
來,替你老人家看看今歲流年,有災沒災。”金蓮道:“原來你家老公
也會算命?”劉婆道:“他雖是個瞽目人,到會兩三椿本事:第一善陰
陽算命,與人家禳保;第二會針灸收瘡;第三椿兒不可說,──單管與
人家回背。”婦人問道:“怎麼是回背?”劉婆子道:“比如有父子不
和,兄弟不睦,大妻小妻爭鬥,教了俺老公去說了,替他用鎮物安
鎮,畫些符水與他吃了,不消三日,教他父子親熱,兄弟和睦,妻妾
不爭。若人家買賣不順溜,田宅不興旺者,常與人開財門發利市。治
病灑掃,禳星告鬥都會。因此人都叫他做劉理星。也是一家子,新娶
個媳婦兒是小人家女兒,有些手腳兒不穩,常偷盜婆婆家東西往娘家
去。丈夫知道,常被責打。俺老公與他回背,畫了一道符,燒灰放在
水缸下埋著,合家大小吃了缸內水,眼看媳婦偷盜,只象沒看見一
般。又放一件鎮物在枕頭內,男子漢睡了那枕頭,好似手封住了的,
再不打他了。”那金蓮聽見遂留心,便呼丫頭,打發茶湯點心與劉婆
吃。臨去,包了三錢藥錢,另外又秤了五錢,要買紙扎信信物。明日
早飯時叫劉瞎來燒神紙。那婆子作辭回家。

到次日,果然大清早晨,領賊瞎逕進大門往裡走。那日西門慶還在
院中,看門小廝便問:“瞎子往那裡走?”劉婆道:“今日與裡邊五娘燒
紙。”小廝道:“既是與五娘燒紙,老劉你領進去。仔細看狗。”這婆子
領定,逕到潘金蓮卧房明間內,等了半日,婦人才出來。瞎子見了
禮,坐下。婦人說與他八字,賊瞎用手捏了捏,說道:“娘子庚辰年,
庚寅月,乙亥日,己醜時。初八日立春,已交正月算命。依子平正
論,娘子這八字,雖故清奇,一生不得夫星濟,子上有些防礙。乙木
生在正月間,亦作身旺論,不克當自焚。又兩重庚金,羊刃大重,夫
星難為,克過兩個才好。”婦人道:“已克過了。”賊瞎子道:“娘子這
命中,休怪小人說,子平雖取煞印格,只吃了亥中有癸水,醜中又有
癸水,水太多了,衝動了只一重巳土,官煞混雜。論來,男人煞重掌
威權,女子煞重必刑夫。所以主為人聰明機變,得人之寵。只有一
件,今歲流年甲辰,歲運並臨,災殃立至。命中又犯小耗勾絞,兩位
星辰打攪,雖不能傷,卻主有比肩不和,小人嘴舌,常沾些啾唧不寧
之狀。”婦人聽了,說道:“累先生仔細用心,與我回背回背。我這裡
一兩銀子相謝先生,買一盞茶吃。奴不求別的,只願得小人離退,夫
主愛敬便了。”一面轉入房中,拔了兩件首飾遞與賊瞎。賊瞎收入袖
中,說道:“既要小人回背,用柳木一塊,刻兩個男女人形,書著娘子
與夫主生辰八字,用七七四十九根紅線扎在一處。上用紅紗一片,蒙
在男子眼中,用艾塞其心,用針釘其手,下用膠粘其足,暗暗埋在睡
的枕頭內。又硃砂書符一道燒灰,暗暗攪茶內。若得夫主吃了茶,到
晚夕睡了枕頭,不過三日,自然有驗。”婦人道:“請問先生,這四椿
兒是怎的說?”賊瞎道:“好教娘子得知:用紗蒙眼,使夫主見你一似
西施嬌艷;用艾塞心,使他心愛到你;用針釘手,隨你怎的不是,使
他再不敢動手打你;用膠粘足者,使他再不往那裡胡行。”婦人聽言,
滿心歡喜。當下備了香燭紙馬,替婦人燒了紙。到次日,使劉婆送了
符水鎮物與婦人,如法安頓停當,將符燒灰,頓下好茶,待的西門慶
家來,婦人叫春梅遞茶與他吃。到晚夕,與他共枕同床,過了一日
兩,兩日三,似水如魚,歡會異常。看觀聽說:但凡大小人家,師尼
僧道,乳母牙婆,切記休招惹他,背地什麼事不乾出來?古人有四句
格言說得好:

堂前切莫走三婆,後門常鎖莫通和。院內有井防小口,便是禍
少福星多。
第十三回 李瓶姐牆頭密約 迎春兒隙底私窺

詞曰:

繡面芙蓉一笑開,斜飛寶鴨襯香腮。眼波才動被人猜。
一面風情深有韻,半箋嬌恨寄幽懷。月移花影約重來。

話說一日西門慶往前邊走來,到月娘房中。月娘告說:“今日花家使
小廝拿帖來,請你吃酒。”西門慶觀看帖子,寫著:“即午院中吳銀家
一敘,希即過我同往,萬萬!”少頃,打選衣帽,叫了兩個跟隨,騎匹
駿馬,先逕到花家。不想花子虛不在家了。他渾家李瓶兒,夏月間戴
著銀絲鬏髻,金鑲紫瑛墜子,藕絲對衿衫,白紗挑線鑲邊裙,裙邊露
一對紅鴛鳳嘴尖尖趫趫小腳,立在二門裡台基上。那西門慶三不知走
進門,兩下撞了個滿懷。這西門慶留心已久,雖故莊上見了一面,不
曾細玩。今日對面見了,見他生的甚是白凈,五短身才,瓜子面兒,
細灣灣兩道眉兒,不覺魂飛天外,忙向前深深作揖。婦人還了萬福,
轉身入後邊去了。使出一個頭髮齊眉的丫鬟來,名喚繡春,請西門慶
客位內坐。他便立在角門首,半露嬌容說:“大官人少坐一時。他適纔
有些小事出去了,便來也。”丫鬟拿出一盞茶來,西門慶吃了。婦人隔
門說道:“今日他請大官人往那邊吃酒去,好歹看奴之面,勸他早些回
家。兩個小廝又都跟去了,止是這兩個丫鬟和奴,家中無人。”西門慶
便道:“嫂子見得有理,哥家事要緊。嫂子既然吩咐在下,在下一定伴
哥同去同來。”

正說著,只見花子虛來家,婦人便回房去了。花子虛見西門慶敘禮
說道:“蒙哥下降,小弟適有些不得已小事出去,失迎,恕罪!”於是
分賓主坐下,便叫小廝看茶。須臾,茶罷。又吩咐小廝:“對你娘說,
看菜兒來,我和西門爹吃三杯起身。今日六月二十四,是院內吳銀姐
生日,請哥同往一樂。”西門慶道:“二哥何不早說?”即令玳安:“快
家去,討五錢銀子封了來。”花子虛道:“哥何故又費心?小弟到不是
了。”西門慶見左右放桌兒,說道:“不消坐了,咱往裡邊吃去罷。” 花
子虛道:“不敢久留,哥略坐一回。”少傾,就是齊整餚饌拿將上來,
銀高腳葵花鐘,每人三鐘,又是四個捲餅,吃畢收下來與馬上人吃。

少傾,玳安取了分資來,一同起身上馬,逕往吳四媽家與吳銀兒做
生日。到那裡,花攢錦簇,歌舞吹彈,飲酒至一更時分方散。西門慶
留心,把子虛灌得酩酊大醉。又因李瓶兒央浼之言,相伴他一同來
家。小廝叫開大門,扶到他客位坐下。李瓶兒同丫鬟掌著燈燭出來,
把子虛攙扶進去。

西門慶交付明白,就要告回。婦人旋走出來,拜謝西門慶,說
道:“拙夫不才貪酒,多累看奴薄面,姑待來家,官人休要笑話。”那
西門慶忙屈身還喏,說道:“不敢。嫂子這裡吩咐,在下敢不銘心刻
骨,同哥一搭里來家!非獨嫂子耽心,顯的在下幹事不的了。方纔哥
在他家,被那些人纏住了,我強著催哥起身。走到樂星堂兒門首粉頭
鄭愛香兒家,──小名叫做鄭觀音,生的一表人物,哥就要往他家去,
被我再三攔住,勸他說道:‘恐怕家中嫂子放心不下。’方纔一直來家。
若到鄭家,便有一夜不來。嫂子在上,不該我說,哥也糊塗,嫂子又
青年,偌大家室,如何就丟了,成夜不在家?是何道理!”婦人
道:“正是如此,奴為他這等在外胡行,不聽人說,奴也氣了一身病痛
在這裡。往後大官人但遇他在院中,好歹看奴薄面,勸他早早回家。
奴恩有重報,不敢有忘。”這西門慶是頭上打一下腳底板響的人,積年
風月中走,甚麼事兒不知道?今日婦人到明明開了一條大路,教他入
港,豈不省腔!於是滿面堆笑道:“嫂子說那裡話!相交朋友做甚麼?
我一定苦心諫哥,嫂子放心。”婦人又道了萬福,又叫小丫鬟拿了一盞
果仁泡茶來。西門慶吃畢茶,說道:“我回去罷,嫂子仔細門戶。”遂
告辭歸家。

自此西門慶就安心設計,圖謀這婦人,屢屢安下應伯爵、謝希大這
夥人,把子虛掛住在院里飲酒過夜。他便脫身來家,一徑在門首站
立。這婦人亦常領著兩個丫鬟在門首。西門慶看見了,便揚聲咳嗽,
一回走過東來,又往西去,或在對門站立,把眼不住望門裡睃盼。婦
人影身在門裡,見他來便閃進裡面,見他過去了,又探頭去瞧。兩個
眼意心期,已在不言之表。一日,西門慶正站在門首,忽見小丫鬟繡
春來請。西門慶故意問道:“姐姐請我做甚麼?你爹在家裡不在?”繡
春道:“俺爹不在家,娘請西門慶爹問句話兒。”這西門慶得不的一
聲,連忙走過來,到客位內坐下。良久,婦人出來,道了萬福,便
道:“前日多承官人厚意,奴銘刻於心,知感不盡。他從昨日出去,一
連兩日不來家了,不知官人曾會見他來不曾?”西門慶道:“他昨日同
三四個在鄭家吃酒,我偶然有些小事就來了。今日我不曾得進去,不
知他還在那裡沒在。若是我在那裡,恐怕嫂子憂心,有個不催促哥早
早來家的?”婦人道:“正是這般說。奴吃煞他不聽人說、在外邊眠花
卧柳不顧家事的虧。”西門慶道:“論起哥來,仁義上也好,只是有這
一件兒。”說著,小丫鬟拿茶來吃了。西門慶恐子虛來家,不敢久戀,
就要告歸。婦人又千叮萬囑,央西門慶:“不拘到那裡,好歹勸他早來
家,奴一定恩有重報,決不敢忘官人!”西門慶道:“嫂子沒的說,我
與哥是那樣相交!”說畢,西門慶家去了。

到次日,花子虛自院中回家,婦人再三埋怨說道:“你在外邊貪酒戀
色,多虧隔壁西門大官人,兩次三番顧睦你來家。你買分禮兒謝謝
他,方不失了人情。”那花子虛連忙買了四盒禮物,一壇酒,使小廝天
福兒送到西門慶家。西門慶收下,厚賞來人去了。吳月娘便問說:“花
家如何送你這禮?”西門慶道:“花二哥前日請我們在院中與吳銀兒做
生日,醉了,被我攙扶了他來家;又見常時院中勸他休過夜,早早來
家。他娘子兒因此感我的情,想對花二哥說,故買此禮來謝我。”吳月
娘聽了,與他打個問訊,說道:“我的哥哥,你自顧了你罷,又泥佛勸
土佛!你也成日不著個家,在外養女調婦,反勸人家漢子!”又
道:“你莫不白受他這禮?”因問:“他帖上兒寫著誰的名字?若是他娘
子的名字,今日寫我的帖兒,請他娘子過來坐坐,他也只恁要來咱家
走走哩。若是他男子漢名字,隨你請不請,我不管你。”西門慶
道:“是花二哥名字,我明日請他便了。”次日,西門慶果然治酒,請
過花子虛來,吃了一日酒。歸家,李瓶兒說:“你不要差了禮數。咱送
了他一分禮,他到請你過去吃了一席酒,你改日還該治一席酒請他,
只當回席。”

光陰迅速,又早九月重陽。花子虛假著節下,叫了兩個妓者,具柬
請西門慶過來賞菊。又邀應伯爵、謝希大、祝實念、孫天化四人相
陪。傳花擊鼓,歡樂飲酒。有詩為證:

烏兔循環似箭忙,人間佳節又重陽。千枝紅樹妝秋色,三徑
黃花吐異香。 不見登高烏帽客,還思捧酒綺羅娘。秀簾瑣闥私
相覷,從此恩情兩不忘。

當日,眾人飲酒到掌燈之後,西門慶忽下席來外邊解手。不防李瓶
兒正在遮槅子邊站立偷覷,兩個撞了個滿懷,西門慶迴避不及。婦人
走到西角門首,暗暗使繡春黑影里走到西門慶跟前,低聲說道:“俺娘
使我對西門爹說,少吃酒,早早回家。晚夕,娘如此這般要和西門爹
說話哩。”西門慶聽了,歡喜不盡。小解回來,到席上連酒也不吃,唱
的左右彈唱遞酒,只是裝醉不吃。看看到一更時分,那李瓶兒不住走
來廉外,見西門慶坐在上面,只推做打盹。那應伯爵、謝希大,如同
釘在椅子上,白不起身。熬的祝實念、孫寡嘴也去了,他兩個還不
動。把個李瓶兒急的要不的。西門慶已是走出來,被花子虛再不放,
說道:“今日小弟沒敬心,哥怎的白不肯坐?”西門慶道:“我本醉了,
吃不去。”於是故意東倒西歪,教兩個扶歸家去了。應伯爵道:“他今
日不知怎的,白不肯吃酒,吃了不多酒就醉了。既是東家費心,難為
兩個姐兒在此,拿大鐘來,咱每再周四五十輪,散了罷。”李瓶兒在簾
外聽見,罵“涎臉的囚根子”不絕。暗暗使小廝天喜兒請下花子虛來,
吩咐說:“你既要與這夥人吃,趁早與我院里吃去。休要在家裡聒噪。
我半夜三更,熬油費火,我那裡耐煩!”花子虛道:“這咱晚我就和他
們院里去,也是來家不成,你休再麻犯我。”婦人道:“你去,我不麻
犯便了。”這花子虛得不的這一聲,走來對眾人說:“我們往院里
去。”應伯爵道:“真個?休哄我。你去問聲嫂子來,咱好起身。”子虛
道:“房下剛纔已是說了,教我明日來家。”謝希大道:“可是來,自吃

You might also like