• Tidak ada hasil yang ditemukan

ePUB eBook Customization Guide

N/A
N/A
Yejwys Ywuwi

Academic year: 2024

Membagikan "ePUB eBook Customization Guide"

Copied!
2035
0
0

Teks penuh

In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks.

Preface

This book aims to serve those who want to write nontrivial programs for the use of others and to do so. This book is written to help you to understand what it means for code to be beautiful, to help you to master the.

Previous Editions

Acknowledgments

Also, thanks to the many people who contributed to the first and second editions of this book.

Notes to the Reader

The structure of this book

Then, we show how to present numeric data, text, and geometric shapes as graphical output, and how to get input into a program from a graphical user interface (GUI). We also show how to handle errors using exceptions and how to parameterize our classes and functions using templates.

  • General approach
  • Drills, exercises, etc
  • What comes after this book?
  • A philosophy of teaching and learning
    • A note to students
    • A note to teachers
  • ISO standard C++
    • Portability
    • Guarantees
    • A brief history of C++
  • PPP support
    • Web resources
  • Author biography
  • Bibliography

No tool, library, language, or technique is “the solution” to all of the many challenges facing a programmer. We move fast in this initial phase – we want to get you to the point where you can write interesting programs as fast as possible.

Postscript

The Basics

Hello, World!

  • Programs
  • The classic first program
  • Compilation
  • Linking
  • Programming environments

That’s not of much use, though, because it doesn’t do anything. the main function”) of our “Hello, World!” program has two statements in its body:. Unless otherwise specified, we use code to mean “source code” or even “the source code except the.

Drill

Don’t be alarmed if you don’t understand every detail of the syntax you are using, and don’t be afraid to ask for help from instructors or friends. Otherwise, if you plan to use the command line, get a command window, figure out how to use an editor to type in your code, and see §0.3.1.

Review

Now is the time to get a bit better acquainted with your compiler’s error-detection and error-reporting facilities. The basic idea of these review questions is to give you a chance to see if you have noticed and understood the key points of the chapter.

Terms

Exercises

For the benefit of the reader, you may add a short description of the layout of the house you are imagining. Please note that with a bit of help and instruction, most of the people in the world can bake delicious blueberry muffins.

Objects, Types, and Values

Input

An object is a region of memory with a type that specifies what kind of information can be placed in it. The first line of main() simply writes out a message encouraging the user to enter a first name.

Variables

The reason for the name double is historical: double is short for “double- precision floating point.” Floating point is the computer’s approximation to the mathematical concept of a real number. 34;Annemarie" // string: a sequence of characters delimited by double quotes true // bool: either true or false.

Input and type

When we want to write the names to output, we must insert a space between them. Then, modify it to write out the age in number of months: read the input in years and multiply (using the * operator) by 12.

Operations and operators

The compiler knows exactly which operations can be applied to each variable and can therefore prevent many mistakes. We’ll use functions along the way and discuss them in some detail in §3.5 and §7.4.

Assignment and initialization

  • An example: detect repeated words
  • Composite assignment operators
  • An example: find repeated words

This program is not the most helpful since it doesn’t tell where the repeated word occurred in the text, but it’ll do for now. We could improve that by giving an idea of where the repeated word was in the.

Names

You can use names of facilities in the standard library, such as string for your own variables, but you shouldn’t. We use an initial capital letter for types we define, such as Square and Graph.

Types and objects

Think of it this way: computer memory doesn’t know about our types; it’s just memory. The bits of memory get meaning only when we decide how that memory is to be interpreted.

Type safety

The ideal is never to use language features that cannot be proven type-safe before the program starts executing: static type safety. With the obvious exception of code used to illustrate unsafe techniques (e.g the code in this book follows the C++ Core Guidelines [CG] and has been verified to be type safe.

Conversions

By the time we assign x to y we may have forgotten that x was a double, or that a double-to-int conversion truncates (always rounds down, toward zero) rather than using the conventional 4/5 rounding (rounding towards the nearest integer). Also, many such conversions don’t actually cause problems because the values involved happen to be in range, and many.

Type deduction: auto

4] Prompt the user to enter the age of the recipient and assign it to an int. 6] Write a program that prompts the user to enter three integer values, and then outputs the values in numerical sequence separated by commas.

Computation

Computation

The input can come from a keyboard, from a mouse, from a touch screen, from files, from other input devices, from other programs, from other parts of a program. By “parts of a program” we mean entities such as a function producing a result from a set of input.

Objectives and tools

Our main tool for organizing a program – and for organizing our thoughts as we program – is to break up a big computation into many little ones. A good library, supplying useful facilities for expressing ideas, can crucially affect the way we distribute functionality into different parts of a program.

Expressions

  • Constant expressions
  • Operators

Here, as the left-hand operand of the assignment, length means “the object named length,” so that the assignment expression is read “Put 99 into the object named length.” We distinguish between length used on the left-hand side of an assignment or an initialization (the lvalue of length: “the object named length”) and length used on the right-hand side of an assignment or initialization (the rvalue of length: “the value of the object named length,” or just “the value of length”). Sometimes (as an lvalue) length refers to the box (object) and sometimes (as an rvalue) length refers to the value in that box.

Statements

  • Selection
    • if-statements
    • switch-statements
    • Switch technicalities
  • Iteration
    • while-statements
    • Blocks
    • for-statements

The condition (unit == ’i’) would fail and the program would execute the else part. the second alternative), converting from centimeters to inches. In this program the end of the program is next, so we leave the program.

Functions

  • Why bother with functions?
  • Function declarations

Finally, why did we use square(i) rather than simply i*i in the first version of the problem. Why would we want to look at the body of the standard-library sqrt() function.

  • Traversing a vector
  • Growing a vector
  • A numeric example
  • A text example

This is called a range-for-loop because the word range is often used to mean the same as “sequence of elements.” We read for (int x : v) as “for each int x in v” and the meaning of the loop is exactly like the equivalent loop over the subscripts [0:v.size()). In §9.4 we discuss how to terminate input and how to deal with errors in input.

Language features

If it is the largest so far, write the largest so far after the number. 9] Keep track of the sum of values entered (as well as the smallest and the largest) and the number of values entered.

Errors!

Introduction

Link-time errors: Errors found by the linker when it is trying to combine object files into an executable program. In fact, for real-world programs it can be hard to know exactly what “all errors” means.

Sources of errors

Even if we don’t meet that ideal 100% of the time, it must be the ideal. None of these approaches can completely eliminate errors by itself; we have to use all three.

Compile-time errors

  • Syntax errors
  • Type errors

More surprisingly, as you gain experience, you’ll begin to wish that the compiler would reject more code, rather than less. As you gain experience, you’ll learn how to get the most out of the compiler’s ability to detect errors and to dodge its known weaknesses.

Link-time errors

From the compiler’s point of view, area(10,−7) is fine: area() asks for two integers and you gave them to it; nobody said that those arguments had to be positive. Instead, the compiler gives an error immediately when it sees a call to an undeclared function.

Run-time errors

  • The caller deals with errors
  • The callee deals with errors
  • Error reporting

That’s the one we’d have to choose if area() was a function in a library where we couldn’t modify it. For example, that’s the case with area(), where the check also more than doubles the size of the function (that is, the number of machine instructions that need to be executed, not just the length of the source code).

Exceptions

  • Bad arguments
  • Range errors
  • Bad input

Note that a range error is really a special case of the argument errors we discussed in §4.5.2. Note again that the return value from main() is passed to “the system” that invoked the program.

Avoiding and finding errors

  • Estimation
  • Debugging
    • Practical debug advice AA
  • Assertions
    • Preconditions CC
    • expect()
    • Postconditions
  • Testing
  • Random numbers

Here, we are not asking, “What’s the exact answer?” or “What’s the correct answer?” That’s what we are writing the program to tell us. Bulls and Cows.” The program has a vector of four different integers in the range 0 to 9 (e.g., 1234 but not 1122) and it is the user’s task to discover those numbers by repeated guesses.

Writing a Program

A problem

After all, a program that solves the wrong problem is likely to be of little use to you, however elegant it may be. What we want is a program that simply and cleanly solves the problem we decided to solve.

Thinking about the problem

  • Stages of development
  • Strategy

We don’t reach a version of the program that we are happy with until the end of the next chapter. To see if details of the problem statement need changing to make the problem manageable.

Back to the calculator!

  • First attempt

We often think we do (don’t we all know what a calculator program is?), but we don’t. We simply haven’t thought hard enough, but thinking is hard work and – like most programmers – we are anxious to write some code.

Referensi

Dokumen terkait