• No results found

When you write a C++ program, most of the code the computer executes is not the code you have written yourself, but the library code that you have loaded along with your application. In a way, programs today are like icebergs, in which most of the bulk lies hidden beneath the surface. If you want to become an effective C++ programmer, you need to spend at least as much time learning about the standard libraries as you do learning the language itself.

Every program you have seen in this book—all the way back to the tiny HelloWorld example at the beginning of Chapter 1—includes the <iostream> library, which gives the program access to the cin and cout streams. The details of how those streams are implemented aren’t important to you when you are writing HelloWorld or PowersOfTwo. Those implementations, in fact, are not simply beyond your reach at the moment, but entirely beyond the scope of this book. In your role as a programmer, all that matters is that you know how to use these libraries and that the library implementations do what they’re supposed to do.

For some reason, beginning programmers are sometimes uncomfortable with the idea of calling a function without understanding its underlying implementation. In

fact, you’ve probably been doing precisely that in mathematics for a long time. In high school, you presumably encountered several functions that turned out to be useful even if you had no idea—and probably no interest—in how the values of that function are computed. In your algebra classes, for example, you learned about functions like logarithms and square root. If you took a trigonometry course, you worked with functions like sine and cosine. If you needed to know the value of one of these functions, you didn’t compute the result by hand, but instead looked up the answer in a table or, more likely, typed the appropriate values into a calculator.

When you’re writing programs, you want to follow that same strategy. If you need to invoke some mathematical function, what you want to have is a library function that computes it with no more conceptual effort than it takes to press the right key on a calculator. Fortunately, C++ has an extensive mathematical library called <cmath> that includes all the functions you are ever likely to need. The most common functions in the <cmath> library appear in Table 2-1. Don’t worry if you

T A B L E 2 - 1 Selected functions in the <cmath> library General mathematical functions

abs(x) Returns the absolute value of x sqrt(x) Returns the square root of x.

floor(x) Returns the largest integer less than or equal to x as a floating-point value. ceil(x) Returns the smallest integer greater than or equal to x as a floating-point value. Logarithmic and exponential functions

exp(x) Returns the exponential function of x (ex). log(x) Returns the natural logarithm (base e) of x. log10(x) Returns the common logarithm (base 10) of x. pow(x, y) Returns xy.

Trigonometric functions

cos(theta) Returns the cosine of the angle theta, measured in radians counterclockwise from the +x axis. You can convert from degrees to radians by multiplying by π/180. sin(theta) Returns the sine of the radian angle theta.

tan(theta) Returns the tangent of the radian angle theta.

atan(x) Returns the principal arctangent of x. The result is an angle expressed in radians between –π/2 and +π/2.

atan2(y, x) Returns the radian angle formed between the x-axis and the line extending from the origin through the point (x, y).

have no idea what some of those functions mean. This book requires very little mathematics and will explain any concepts beyond basic algebra as they come up.

Whenever a library makes some service available to the programs that include it, computer scientists say that the library exports that service. The <iostream> library, for example, exports the cin and cout streams; the <cmath> library exports the sqrt function, along with the other functions in Table 2-1.

One of the design goals of any library is to hide the complexity involved in the underlying implementation. By exporting the sqrt function, the designers of the <cmath> library make it far easier to write programs that use it. When you call the sqrt function, you don’t need to have any idea how sqrt works internally. Those details are relevant only to the programmers who implement the <cmath> library in the first place.

Knowing how to call the sqrt function and knowing how to implement it are both important skills. It is important to recognize, however, that those two skills— calling a function and implementing one—are to a large extent independent. Successful programmers often use functions that they wouldn’t have a clue how to write. Conversely, programmers who implement a library function can never anticipate all the potential uses for that function.

To emphasize the difference in perspective between programmers who implement a library and those who use it, computer scientists have assigned names to programmers working in each of these roles. Naturally enough, a programmer who implements a library is called an implementer. Conversely, a programmer who calls functions provided by a library is called a client of that library. As you go through the chapters in this book, you will have a chance to look at several libraries from both of these perspectives, first as a client and later as an implementer.