C++ By Example 一pdf

图书网 2017年11月16日17:45:22
评论
2.4K

C++ By Example 一 目录

Overview

I Introduction to C++

1 Welcome to C++ ...........................................................................11

2 What Is a Program? ......................................................................35

3 Your First C++ Program ..............................................................51

4 Variables and Literals ..................................................................69

5 Character Arrays and Strings .....................................................99

6 Preprocessor Directives .............................................................113

7 Simple Input/ Output.................................................................133

II Using C++ Operators

8 Using C++ Math Operators and Precedence..........................163

9 Relational Operators ..................................................................185

10 Logical Operators .......................................................................207

11 Additional C++ Operators ........................................................221

III C++ Constructs

12 The while Loop ...........................................................................245

13 The for Loop ................................................................................273

14 Other Loop Options ...................................................................295

15 The switch and goto Statements...............................................311

16 Writing C++ Functions ..............................................................331

IV Variable Scope and Modular

Program ming

17 Variable Scope.............................................................................353

18 Passing Values ............................................................................379

19 Function Return Values and Prototypes .................................397

20 Default Arguments and Function Overloading .....................415

V Character Input /Output and

String Functions

21 Device and Character Input/ Output.......................................431

22 Character, String, and Numeric Functions .............................449

VI Arrays and Pointers

23 Introducing Arrays.....................................................................473

24 Array Processing.........................................................................493

25 Multidimensional Arrays ..........................................................519

26 Pointers ........................................................................................541

27 Pointers and Arrays ...................................................................557

VII Structures and File Input/Output

28 Structures .....................................................................................583

29 Arrays of Structures ...................................................................605

30 Sequential Files ...........................................................................625

31 Random-Access Files .................................................................645

32 Introduction to Object-Oriented Programming .....................661

VIII References

A Memory Addressing, Binary, and Hexadecimal Review .....679

B Answers to Review Questions..................................................701

C ASCII Table..................................................................................719

D C++ Precedence Table................................................................729

E Keyword and Function Reference............................................733

F The Mailing List Application ....................................................737

Glossary .......................................................................................747

Index .............................................................................................761

Contents

Introduction ...................................................1

Who Should Use This Book ....................................................1

The Book’s Philosophy ............................................................2

Overview of This Book............................................................2

Conventions Used in This Book .................................................5

Index to the Icons .....................................................................5

Margin Graphics (Book Diagrams) .......................................6

Companion Disk Offer ................................................................8

I Introduction to C++

1 Welcome to C++ ...........................................11

What C++ Can Do for You ........................................................12

The Background of C++ ............................................................15

C++ Compared with Other Languages...................................16

C++ and Microcomputers .........................................................17

An Overview of Your Computer .............................................19

Hardware ................................................................................19

Software...................................................................................29

Review Questions.......................................................................33

Summary .....................................................................................34

2 What Is a Program? .....................................35

Computer Programs ..................................................................36

Program Design ..........................................................................38
Using a Program Editor.............................................................40

Using a C++ Compiler ...............................................................42

Running a Sample Program ......................................................44

Handling Errors ..........................................................................46

Review Questions.......................................................................48

Summary .....................................................................................49

Contents o

3 Your First C++ Program ...............................51

Looking at a C++ Program ........................................................52

The Format of a C++ Program .............................................53

Readability Is the Key............................................................54

Uppercase Versus Lowercase...............................................55

Braces and main()...................................................................56

Comments in C++ ..................................................................57

Explaining the Sample Program ..............................................60

Review Questions.......................................................................66

Summary .....................................................................................67

4 Variables and Literals .................................69

Variables ......................................................................................70

Naming Variables ..................................................................70

Variable Types ........................................................................72

Declaring Variables................................................................73

Looking at Data Types ..........................................................75

Assigning Values to Variables .............................................80

Literals..........................................................................................82

Assigning Integer Literals.....................................................83

Assigning String Literals.......................................................85

Assigning Character Literals ................................................89

Constant Variables .....................................................................94

Review Questions.......................................................................95

Review Exercises ........................................................................97

Summary .....................................................................................97

5 Character Arrays and Strings ......................99

Character Arrays ......................................................................100

Character Arrays Versus Strings............................................103

Review Questions.....................................................................110

Review Exercises ......................................................................111

Summary ...................................................................................111

6 Preprocessor Directives ............................113

Understanding Preprocessor Directives ...............................114

The #include Directive ............................................................115

The #define Directive ..............................................................120

Review Questions.....................................................................128

Review Exercises ......................................................................130

Summary ...................................................................................130

7 Simple Input/Output ...................................133

The cout Operator ....................................................................134

Printing Strings.....................................................................134

The cin Operator ..................................................................144

printf() and scanf() ...................................................................149

The printf() Function ................................................................149

Conversion Characters ............................................................151

The scanf() Function .................................................................154

Review Questions.....................................................................157

Review Exercises ......................................................................158

Summary ...................................................................................159

II Using C++ Operators

8 Using C++ Math Operators

and Precedence .........................................163

C++’s Primary Math Operators..............................................164

The Unary Operators...........................................................165

Division and Modulus.........................................................167

The Order of Precedence .........................................................168

Using Parentheses ................................................................170

The Assignment Statements ...................................................174

Multiple Assignments .........................................................175

Compound Assignments ....................................................176

Mixing Data Types in Calculations .......................................178

Type Casting .........................................................................179

Review Questions.....................................................................182

Review Exercises ......................................................................183

Summary ...................................................................................184

9 Relational Operators .................................185

Defining Relational Operators................................................186

The if Statement ........................................................................189

The else Statement ....................................................................199

Review Questions.....................................................................203

Review Exercises ......................................................................204

Summary ...................................................................................205

10 Logical Operators ......................................207

Defining Logical Operators.....................................................207

Logical Operators and Their Uses .........................................209

C++’s Logical Efficiency......................................................211

Logical Operators and Their Precedence ..............................216

Review Questions.....................................................................217

Review Exercises ......................................................................218

Summary ...................................................................................219

11 Additional C++ Operators ..........................221

The Conditional Operator .......................................................222

The Increment and Decrement Operators ............................225

The sizeof Operator ..................................................................230

The Comma Operator ..............................................................232

Bitwise Operators .....................................................................234

Bitwise Logical Operators...................................................235

Review Questions.....................................................................242

Review Exercises ......................................................................243

Summary ...................................................................................243

III C++ Constructs

12 The w hile Loop ..........................................245

The while Statement.................................................................246

The Concept of Loops ..............................................................247

The do-while Loop ...................................................................252

The if Loop Versus the while Loop ........................................255

The exit() Function and break Statement ..............................256

Counters and Totals .................................................................260

Producing Totals ..................................................................265

Review Questions.....................................................................268

Review Exercises ......................................................................269

Summary ...................................................................................270

13 The for Loop ...............................................273

The for Statement .....................................................................274

The Concept of for Loops ........................................................274

Nested for Loops ......................................................................286

Review Questions.....................................................................292

Review Exercises ......................................................................293

Summary ...................................................................................293

14 Other Loop Options ...................................295

Timing Loops ............................................................................296

The break and for Statements .................................................298

The continue Statement ...........................................................303

Review Questions.....................................................................308

Review Exercises ......................................................................308

Summary ...................................................................................309

15 The sw itch and goto Statements ..............311

The switch Statement ...............................................................312

The goto Statement...................................................................321

Review Questions.....................................................................327

Review Exercises ......................................................................328

Summary ...................................................................................328

16 Writing C++ Functions ...............................331

Function Basics .........................................................................332

Breaking Down Problems .......................................................333

More Function Basics ...............................................................335

Calling and Returning Functions...........................................337

Review Questions.....................................................................349

Summary ...................................................................................350

IV Variable Scope and Modular Programming

17 Variable Scope ..........................................353

Global Versus Local Variables................................................354

Defining Variable Scope......................................................355

Use Global Variables Sparingly .........................................362

The Need for Passing Variables .........................................363

Automatic Versus Static Variables.........................................369

Three Issues of Parameter Passing.........................................374

Review Questions.....................................................................375

Review Exercises ......................................................................375

Summary ...................................................................................377

18 Passing Values ..........................................379

Passing by Value (by Copy)....................................................379

Passing by Address (by Reference)........................................385

Variable Addresses ..............................................................385

Sample Program ...................................................................386

Passing Nonarrays by Address..........................................391

Review Questions.....................................................................394

Review Exercises ......................................................................395

Summary ...................................................................................396

19 Function Return Values and Prototypes ...397

Function Return Values...........................................................398

Function Prototypes .................................................................405

Prototype for Safety .............................................................407

Prototype All Functions ......................................................407

Review Questions.....................................................................412

Review Exercises ......................................................................412

Summary ...................................................................................413

20 Default Arguments

and Function Overloading .........................415

Default Argument Lists ...........................................................416

Multiple Default Arguments ..................................................417

Overloaded Functions .............................................................420

Review Questions.....................................................................426

Review Exercises ......................................................................426

Summary ...................................................................................427

V Character Input /Output

and String Functions

21 Device and Character Input/Output ..........431

Stream and Character I/ O ......................................................432

Standard Devices .................................................................434

Redirecting Devices from MS-DOS...................................435

Printing Formatted Output to the Printer.............................436

Character I/ O Functions .........................................................437

The get() and put() Functions.............................................438

The getch() and putch() Functions ....................................444

Review Questions.....................................................................446

Review Exercises ......................................................................447

Summary ...................................................................................448

22 Character, String,

and Numeric Functions .............................449

Character Functions .................................................................450

Character Testing Functions...............................................450

Alphabetic and Digital Testing ..........................................450

Special Character-Testing Functions.................................453

Character Conversion Functions .......................................453

String Functions........................................................................455

Useful String Functions.......................................................456

String I/ O Functions ...........................................................456

Converting Strings to Numbers.........................................460

Numeric Functions...................................................................461

Useful Mathematical Functions .........................................461

Trigonometric Functions.....................................................464

Logarithmic Functions ........................................................465

Random-Number Processing .............................................465

Review Questions.....................................................................467

Review Exercises ......................................................................468

Summary ...................................................................................469

VI Arrays and Pointers

23 Introducing Arrays .....................................473

Array Basics...............................................................................474

Initializing Arrays ....................................................................479

Initializing Elements at Declaration Time ........................479

Initializing Elements in the Program ................................486

Review Questions.....................................................................491

Review Exercises ......................................................................491

Summary ...................................................................................492

24 Array Processing .......................................493

Searching Arrays ......................................................................494

Searching for Values ................................................................496

Sorting Arrays...........................................................................501

Advanced Referencing of Arrays...........................................508

Review Questions.....................................................................515

Review Exercises ......................................................................516

Summary ...................................................................................517

25 Multidimensional Arrays ............................519

Multidimensional Array Basics..............................................520

Reserving Multidimensional Arrays .....................................522

Mapping Arrays to Memory...................................................524

Defining Multidimensional Arrays .......................................526

Tables and for Loops................................................................530

Review Questions.....................................................................537

Review Exercises ......................................................................538

Summary ...................................................................................538

26 Pointers ......................................................541

Introduction to Pointer Variables ..........................................542

Declaring Pointers................................................................543

Assigning Values to Pointers .............................................545

Pointers and Parameters .....................................................546

Arrays of Pointers.....................................................................551

Review Questions.....................................................................553

Summary ...................................................................................555

27 Pointers and Arrays ...................................557

Array Names as Pointers.........................................................558

Pointer Advantages..................................................................560

Using Character Pointers ........................................................563

Pointer Arithmetic....................................................................568

Arrays of Strings.......................................................................574

Review Questions.....................................................................578

Review Exercises ......................................................................579

Summary ...................................................................................580

28 Structures ..................................................583

Introduction to Structures .......................................................584

Defining Structures ..................................................................587

Initializing Structure Data.......................................................591

Nested Structures .....................................................................600

Review Questions.....................................................................603

Review Exercises ......................................................................604

Summary ...................................................................................604

29 Arrays of Structures ..................................605

Declaring Arrays of Structures...............................................606

Arrays as Members ..................................................................615

Review Questions.....................................................................623

Review Exercises ......................................................................624

Summary ...................................................................................624

30 Sequential Files .........................................625

Why Use a Disk?.......................................................................626

Types of Disk File Access ........................................................627

Sequential File Concepts .........................................................628

Opening and Closing Files......................................................629

Writing to a File ........................................................................635

Writing to a Printer ..................................................................637

Adding to a File ........................................................................638

Reading from a File ..................................................................639

Review Questions.....................................................................642

Review Exercises ......................................................................643

Summary ...................................................................................644

31 Random-Access Files ................................645

Random File Records ...............................................................646

Opening Random-Access Files...............................................647

The seekg() Function ................................................................649

Other Helpful I/ O Functions..................................................656

Review Questions.....................................................................658

Review Exercises ......................................................................658

Summary ...................................................................................659

32 Introduction to Object-Oriented

Programming .............................................661

What Is a Class? ........................................................................662

Data Members ......................................................................662

Member Functions ...............................................................662

Default Member Arguments...................................................670

Class Member Visibility ..........................................................674

Review Questions.....................................................................676

Review Exercise ........................................................................676

Summary ...................................................................................676

VIII References

A Memory Addressing, Binary,

and Hexadecimal Review ..........................679

Computer Memory ..................................................................680

Memory and Disk Measurements .....................................680

Memory Addresses..............................................................681

Bits and Bytes ............................................................................682

The Order of Bits ......................................................................686

Binary Numbers .......................................................................686

Binary Arithmetic .....................................................................690

Binary Negative Numbers ......................................................692

Hexadecimal Numbers ............................................................695

Why Learn Hexadecimal?.......................................................697

How Binary and Addressing Relate to C++.........................698

B Answ ers to Review Questions ..................701

C ASCII Table ................................................719

D C++ Precedence Table ..............................729

E Keyw ord and Function Reference ............733

stdio.h ....................................................................................734

ctype.h....................................................................................734

string.h ...................................................................................735

math.h ....................................................................................735

stdlib.h ...................................................................................735

F The Mailing List Application .....................737

Glossary .....................................................747

Index ..........................................................761

C++ By Example 一 精彩文摘

Introduction

Every day, more and more people learn and use the C++ program-ming language. I have taught C to thousands of students in my life.I see many of those students now moving to C++ in their school work or career. The C++ language is becoming an industry-accepted standard programming language, using the solid foundation of C to gain a foothold. C++ is simply a better C than C.

C++ By Example is one of several books in Que’s new line of By Example series. The philosophy of these books is simple: The best way to teach computer programming concepts is with multiple examples. Command descriptions, format syntax, and language references are not enough to teach a newcomer a programming language. Only by looking at numerous examples and by running sample programs can programming students get more than just a “feel” for the language.

Who Should Use This Book

This book teaches at three levels: beginning, intermediate, and advanced. Text and numerous examples are aimed at each level. If you are new to C++, and even if you are new to computers, this book attempts to put you at ease and gradually build your C++ program-ming skills. If you are anexpert at C++, this book provides a few extras for you along the way.

......

图书网:C++ By Example 一pdf

继续阅读

→→→→→→→→→→→→→→→→→→→→查找获取

匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: