📄 chapter05.html
字号:
return result;</PRE><P>Wrapping that all up in a function, we get:</P><PRE>double fred (double xc, double yc, double xp, double yp) { double radius = distance (xc, yc, xp, yp); double result = area (radius); return result;} </PRE><P>The name of this function is <TT>fred</TT>, which may seem odd. I will explain why in the next section.</P><P>The temporary variables <TT>radius</TT> and <TT>area</TT> are useful for development and debugging, but once the program is working we can make it more concise by composing the function calls:</P><PRE>double fred (double xc, double yc, double xp, double yp) { return area (distance (xc, yc, xp, yp));} </PRE><BR><BR><H3>5.4 Overloading</H3><P>In the previous section you might have noticed that <TT>fred</TT> and <TT>area</TT> perform similar functions---finding the area of a circle---but take different parameters. For <TT>area</TT>, we have to provide the radius; for <TT>fred</TT> we provide two points.</P><P>If two functions do the same thing, it is natural to give them the same name. In other words, it would make more sense if <TT>fred</TT> were called <TT>area</TT>.</P><P>Having more than one function with the same name, which is called <B>overloading</B>, is legal in C++ <I>as long as each version takes different parameters</I>. So we can go ahead and rename <TT>fred</TT>:</P><PRE>double area (double xc, double yc, double xp, double yp) { return area (distance (xc, yc, xp, yp));} </PRE><P>This looks like a recursive function, but it is not. Actually, this versionof <TT>area</TT> is calling the other version. When you call an overloaded function, C++ knows which version you want by looking at the arguments that youprovide. If you write:</P><PRE> double x = area (3.0);</PRE>C++ goes looking for a function named <TT>area</TT> that takes a <TT>double</TT>as an argument, and so it uses the first version. If you write</P><PRE> double x = area (1.0, 2.0, 4.0, 6.0);</PRE><P>C++ uses the second version of <TT>area</TT>.</P> <P>Many of the built-in C++ commands are overloaded, meaning that there are different versions that accept different numbers or types of parameters.</P><P>Although overloading is a useful feature, it should be used with caution. You might get yourself nicely confused if you are trying to debug one version of a function while accidently calling a different one.</P><P>Actually, that reminds me of one of the cardinal rules of debugging: <B>make sure that the version of the program you are looking at is the version of the program that is running!</B> Some time you may find yourself making onechange after another in your program, and seeing the same thing every time you run it. This is a warning sign that for one reason or another you are not running the version of the program you think you are. To check, stick in an output statement (it doesn't matter what it says) and make sure the behavior ofthe program changes accordingly.</P><BR><BR><H3>5.5 Boolean values</H3><P>The types we have seen so far are pretty big. There are a lot of integers in the world, and even more floating-point numbers. By comparison, the set of characters is pretty small. Well, there is another type in C++ that is even smaller. It is called <B>boolean</B>, and the only values in it are <TT>true</TT> and <TT>false</TT>.</P><P>Without thinking about it, we have been using boolean values for the last couple of chapters. The condition inside an <TT>if</TT> statement or a <TT>while</TT> statement is a boolean expression. Also, the result of a comparison operator is a boolean value. For example:</P><PRE> if (x == 5) { // do something }</PRE><P>The operator <TT>==</TT> compares two integers and produces a boolean value.</P><P>The values <TT>true</TT> and <TT>false</TT> are keywords in C++, and can be used anywhere a boolean expression is called for. For example,</P><PRE> while (true) { // loop forever }</PRE><P>is a standard idiom for a loop that should run forever (or until it reaches a <TT>return</TT> or <TT>break</TT> statement).</P><BR><BR><H3>5.6 Boolean variables</H3><P>As usual, for every type of value, there is a corresponding type of variable.In C++ the boolean type is called <B>bool</B>. Boolean variables work just likethe other types:</P><PRE> bool fred; fred = true; bool testResult = false;</PRE><P>The first line is a simple variable declaration; the second line is an assignment, and the third line is a combination of a declaration and as assignment, called an initialization.</P><P>As I mentioned, the result of a comparison operator is a boolean, so you can store it in a <TT>bool</TT> variable</P><PRE> bool evenFlag = (n%2 == 0); // true if n is even bool positiveFlag = (x > 0); // true if x is positive</PRE><P>and then use it as part of a conditional statement later</P><PRE> if (evenFlag) { cout << "n was even when I checked it" << endl; }</PRE><P>A variable used in this way is called a <B>flag</B>, since it flags the presence or absence of some condition.</P><BR><BR><H3>5.7 Logical operators</H3><P>There are three <B>logical operators</B> in C++: AND, OR and NOT,which are denoted by the symbols <TT>&&</TT>, <TT>||</TT> and <TT>!</TT>. The semantics (meaning) of these operators is similar to their meaning in English. For example <TT>x > 0 && x < 10</TT> is true only if <TT>x</TT> is greater than zero AND less than 10.</P><P><TT>evenFlag || n%3 == 0</TT> is true if <I>either</TT> of the conditions is true, that is, if <TT>evenFlag</TT> is true OR the number is divisible by 3.</P><P>Finally, the NOT operator has the effect of negating or inverting a bool expression, so <TT>!evenFlag</TT> is true if <TT>evenFlag</TT> is false; that is, if the number is odd.</P><P>Logical operators often provide a way to simplify nested conditional statements. For example, how would you write the following code using a singleconditional?</P><PRE> if (x > 0) { if (x < 10) { cout << "x is a positive single digit." << endl; } }</PRE><BR><BR><H3>5.8 Bool functions</H3><P>Functions can return <TT>bool</TT> values just like any other type, which isoften convenient for hiding complicated tests inside functions. For example:</P><PRE>bool isSingleDigit (int x){ if (x >= 0 && x < 10) { return true; } else { return false; }}</PRE><P>The name of this function is <TT>isSingleDigit</TT>. It is common to give boolean functions names that sound like yes/no questions. The return type is <TT>bool</TT>, which means that every return statement has to provide a <TT>bool</TT> expression.</P><P>The code itself is straightforward, although it is a bit longer than it needs to be. Remember that the expression <TT>x >= 0 && x < 10</TT> hastype <TT>bool</TT>, so there is nothing wrong with returning it directly, and avoiding the <TT>if</TT> statement altogether:</P><PRE>bool isSingleDigit (int x){ return (x >= 0 && x < 10);}</PRE><P>In <TT>main</TT> you can call this function in the usual ways:</P><PRE> cout << isSingleDigit (2) << endl; bool bigFlag = !isSingleDigit (17);</PRE><P>The first line outputs the value <TT>true</TT> because 2 is a single-digit number. Unfortunately, when C++ outputs <TT>bool</TT>s, it does not display the words <TT>true</TT> and <TT>false</TT>, but rather the integers <TT>1</TT> and <TT>0</TT>. (note: There is a way to fix that using the <TT>boolalpha</TT>flag, but it is too hideous to mention.)</P><P>The second line assigns the value <TT>true</TT> to <TT>bigFlag</TT> only if 17 is <I>not</I> a single-digit number.</P><P>The most common use of <TT>bool</TT> functions is inside conditional statements</P><PRE> if (isSingleDigit (x)) { cout << "x is little" << endl; } else { cout << "x is big" << endl; }</PRE><BR><BR><H3>5.9 Returning from <TT>main</TT></H3><P>Now that we have functions that return values, I can let you in on a secret.<TT>main</TT> is not really supposed to be a <TT>void</TT> function. It's supposed to return an integer:</P><PRE>int main (){ return 0;} </PRE><P>The usual return value from <TT>main</TT> is 0, which indicates that the program succeeded at whatever it was supposed to to. If something goes wrong, it is common to return -1, or some other value that indicates what kind of error occurred.</P><P>Of course, you might wonder who this value gets returned to, since we never call <TT>main</TT> ourselves. It turns out that when the system executes a program, it starts by calling <TT>main</TT> in pretty much the same way it calls all the other functions.</P><P>There are even some parameters that are passed to <TT>main</TT> by the system, but we are not going to deal with them for a little while.</P><BR><BR><H3>5.10 More recursion</H3><P>So far we have only learned a small subset of C++, but you might be interested to know that this subset is now a <B>complete</B> programming language, by which I mean that anything that can be computed can be expressed in this language. Any program ever written could be rewritten using only the language features we have used so far (actually, we would need a few commands to control devices like the keyboard, mouse, disks, etc., but that's all).</P><P>Proving that claim is a non-trivial exercise first accomplished by Alan Turing, one of the first computer scientists (well, some would argue that he was a mathematician, but a lot of the early computer scientists started as mathematicians). Accordingly, it is known as the Turing thesis. If you take a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -