⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6members.html

📁 Visual C++ has been one of most effective tool for the large industrial applications. This book is t
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<table width=50% border=2>
<tr>
<td colspan=3>Arithmetic Binary Operators
<tr>
<td>+        <td>addition            <td>a + b
<tr>
<td>-        <td>subtraction            <td>a - b
<tr>
<td>*        <td>multiplication        <td>a * b
<tr>
<td>/        <td>division            <td>a / b
<tr>
<td>%        <td>division modulo    <td>a % b
<tr>
<td colspan=3>Arithmetic Unary Operators
<tr>
<td>+        <td>plus                <td>+a
<tr>
<td>-        <td>minus                <td>-a
<tr>
<td colspan=3>Assignment Operator
<tr>
<td>=        <td>assignment            <td>a = b
</table>
<p>
Some of the operators require a word of explanation. For instance, why are there two division operators?
<p>First of all, if you are operating on floating point numbers (for instance, of the type double) you should use the regular division operator /. Even if only one of the two numbers or variables is a decimal fraction, like 3.14, or a double, the result of the division will be a number of the type double. However, if both operands are integers--literal numbers without a decimal point or variables defined as <var>int</var> (or other integral types that we'll talk about soon)--applying operator / results in an integer equal to the <i>integral part</i> of the quotient. For instance, 3 / 2 will produce 1, which is the integral part of 1.5. Similarly, 10 / 3 will produce 3, etc.

<p>The % operator, which can only be applied to integral types, yields the <i>reminder</i> from the division. For instance, 3 % 2 (pronounced "3 modulo 2") is equal to 1. Similarly, 11 % 3 yields 2, etc.
<p>
It is always true that
<!-- Definition -->
<p>
<table border=4 cellpadding=10><tr>
    <td bgcolor="#ffffff" class=defTable>

(a / b) * b + a % b is equal to a</table>
<!-- End Definition -->
<p>
However, the results of applying operators / and % when one of the numbers is negative are not well defined (although they still fulfill the equality above). So don't rely on them if you are dealing with numbers of either sign. If you think this sucks, you are right. The arguments for implementing such behavior in C++ are really lame: compatibility with C and efficient implementation in terms of processor instructions. I rest my case.
<p>
The unary plus does nothing. It is there for symmetry with the unary minus, which inverts the sign of a number. So if a = 2 then -a yields -2 and +a yields 2.
<p>
One thing is kind of awkward in the last program. The prompt "Enter number " is always the same. The user has no idea what's going on. Wouldn't it be nice to vary the prompt depending on the context? Ideally we would like to have code like this, in main:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>InputNum num( "Enter number " );
num.AddInput( "Another one  " );
num.AddInput( "One more     " );</pre>
</table>
<!-- End Code -->

<p>
Some of the <var>InputNum</var>'s methods would have to take strings as arguments. No problem. Here's how it's done:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>#include &lt;iostream&gt;
using std::cout;
using std::cin;

class  <span class=method>InputNum</span>
{
public:
    InputNum (char msg [])
    {
        cout &lt;&lt; msg;
        cin &gt;&gt; _num;
    }

    int GetValue () const {  return _num; }

    void AddInput (char msg [])
    {
        InputNum aNum (msg);
        _num = GetValue () + aNum.GetValue ();
    }
        
private:
    int _num;
};

const char SumString [] = "The sum is   ";

int main()
{
    InputNum num ("Enter number ");
    num.AddInput ("Another one  ");
    num.AddInput ("One more     ");
    cout &lt;&lt; SumString &lt;&lt; num.GetValue () &lt;&lt; "\n";
    return 0;
}
</pre>
</table>
<!-- End Code -->

<p>
Several things are going on here. A new built-in type, <var>char</var>, has been introduced. As the name suggests, this type is used to store characters. Unless you work on some really weird machine, you can safely assume that <var>char</var>s are 8-bit quantities. 
<p>
We also learn that a string is an <i>array</i> of characters--that's the meaning of brackets after the name of the string variable. What we don't see here is the fact that literal strings are zero terminated; that is, after the last explicit character, a <i>null</i> char is added by the compiler. For instance <var>"Hi"</var> is really an array of three characters, <var>'H'</var>, <var>'i'</var>, and 0 (or <var>'\0'</var> for purists). The null character is distinctly different from the character representing the digit zero. The latter is written as <var>'0'</var>. See the difference? Null character: <var>0</var> or <var>'\0'</var>, zero: <var>'0'</var>. Since we are at it, <var>"\n"</var> is an array of two characters <var>'\n'</var> and <var>'\0'</var>. This terminating null tells various functions operating on strings, including our friend cout, where the end of the string is. Otherwise it would have no clue. 
<p>
By the way, instead of sending the string "\n" to the output, you can get the same result sending a special thing called <var>endl</var>. The syntax is, <var>cout &lt;&lt; endl;</var>  and it works like magic (meaning, I don't want to explain it right now). Since <var>endl</var> is defined in the standard library, you either have to prefix it with <var>std::</var> or declare, up front, your intent of <var>using std::endl</var>.

<p>

<!-- Sidebar -->
<table width="100%" border=0 cellpadding=5><tr>
<td width=10>
<td bgcolor="#cccccc" class=sidebar>

There is a special <var>string</var> type defined in the standard library. It's extremely useful if you're planning on doing any string manipulations. We'll get to it later.
<td width=10>
</table>
<!-- End Sidebar -->


<p>
Another curious thing about this program is the way we call <var>GetValue()</var> inside <var>AddInput()</var>. We make two calls, one with no object specified, the other targeted at <var>aNum</var>. We know that the second one invokes <var>GetValue</var> on the object <var>aNum</var>. The first one, it turns out, invokes the <var>GetValue()</var> method on the object we are sitting in. The "this" object, as we say in the C++ argot. It simply retrieves its own value of <var>_num</var>. It does it, however, in a way that isolates us from the details of its implementation. Here it may look like an overkill of generality (why don't we just use <var>_num</var> directly?), but many situations it may mean a big win in future maintenance.
<p>
Some of you are probably asking yourselves the question: how expensive it is. Calling a member function instead of retrieving the value of <var>_num</var> directly. Or even, why not make <var>_num</var> public and shortcut our way to it everywhere. The syntax for accessing a public data member is:
<!-- Code -->
<table width="100%" cellspacing=10><tr>
    <td class=codeTable>
<pre>myNum._num</pre>
</table>
<!-- End Code -->

<p>
You won't believe it, but the call is actually free. I'm not kidding, it costs zero additional cycles. Nada, nil, niente, zilch! (Later, I'll explain the meaning of <i>inline</i> functions).
<p class=summary>
To summarize, the body of a member function forms a separate local scope. This is also true about the body of a constructor and of a destructor. One can define objects within this local scope as well as create sub-scopes inside of it.
<p class=summary>
A string is a null terminated array of chars. Arrays are declared with rectangular brackets following their names. Arrays of chars can be initialized with explicit literal strings. Arrays may be defined within global or local scopes and passed as arguments to other functions.


</table>
<!-- End Main Table -->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -