📄 6members.html
字号:
<html>
<head>
<title>Member Function Scope</title>
<meta name="description" content="The local scope of a member function">
<meta name="keywords" content="class, member, function, scope, string, array, char">
<link rel="stylesheet" href="rs.css" tppabs="http://www.relisoft.com/book/rs.css">
</head>
<body background="margin.gif" tppabs="http://www.relisoft.com/book/images/margin.gif" bgcolor="#FFFFDC">
<!-- Main Table -->
<table cellpadding="6">
<tr>
<td width="78"> </td>
<td>
<h3>Member function scope</h3>
<p class=topics>
Member function scope, strings as arrays of chars, calling other member functions.
<p>
In the next example we will show how the body of a member function forms its own local scope. We will be able to define objects within that scope, create sub-scopes, and so on.
<p>
<!-- Sidebar -->
<table width="100%" border=0 cellpadding=5><tr>
<td width=10> </td>
<td bgcolor="#cccccc" class=sidebar>
But first, let's make some simplifications. You are probably tired of typing <var>std::</var> in front of <var>cin</var> and <var>cout</var>. It is actually enough to tell the compiler once, before you start using them, that you are about to use them. So, once you say the magic words
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>using std::cout;
</pre>
</table>
<!-- End Code -->
you can omit <var>std::</var> in front of <var>cout</var>. The same goes for <var>cin</var>. There's an even more general way of getting rid of the <var>std::</var> prefixes, by declaring
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>using namespace std;
</pre>
</table>
<!-- End Code -->
That's because the whole standard library sits in a <i>namespace</i> called <var>std</var>. We'll come back to namespaces later.</td></table>
<!-- End Sidebar -->
<p>So here's a new twist in our input object.
<!-- Code -->
<table width="100%" cellspacing=10><tr>
<td class=codeTable>
<pre>#include <iostream>
using std::cout;
using std::cin;
class <span class=method>InputNum</span>
{
public:
InputNum ()
{
cout << "Enter number ";
cin >> _num;
}
int GetValue () const { return _num; }
void AddInput ()
{
InputNum aNum; // get a number from user
_num = _num + aNum.GetValue ();
}
private:
int _num;
};
int main()
{
InputNum num;
cout << "The value is " << num.GetValue() << "\n";
num.AddInput();
cout << "Now the value is " << num.GetValue() << "\n";
return 0;
}
</pre>
</table>
<!-- End Code -->
<p>
A new method was added to <var>InputNum</var>. It uses a local object of type <var>InputNum</var> to prompt the user for a number. This number is then retrieved using <var>GetValue()</var> and added to the original value.
<p>Look at the following series of pictures that visualize the execution of the program.
<p><img src="exec1.gif" tppabs="http://www.relisoft.com/book/lang/scopes/images/exec1.gif" width=516 height=228 border=0 alt=" ">
<p class=caption>Figure 6 While executing <var>main</var>, the constructor of object <var>num</var> of class <var>InputNum</var> is called. It prompts the user to input a number. The user inputs 5. This value is stored in <var>num</var>'s private data member <var>_num</var>.
<p><img src="exec2.gif" tppabs="http://www.relisoft.com/book/lang/scopes/images/exec2.gif" width=516 height=228 border=0 alt=" ">
<p class=caption>Figure 7 The <var>GetValue</var> method of the object <var>num</var> is called. It retrieves and returns the value stored in <var>num</var>'s private data member <var>_num</var>. This value is 5.
<p><img src="exec3.gif" tppabs="http://www.relisoft.com/book/lang/scopes/images/exec3.gif" width=516 height=228 border=0 alt=" ">
<p class=caption>Figure 8 The <var>AddInput</var> method of the object <var>num</var> is called next.
<p><img src="exec4.gif" tppabs="http://www.relisoft.com/book/lang/scopes/images/exec4.gif" width=516 height=300 border=0 alt=" ">
<p class=caption>Figure 9 The <var>AddInput</var> method of num creates the object <var>aNum</var> of the class <var>InputNumber</var>. The constructor of <var>aNum</var> prompts the user to input a number. User enters 11 and this value is stored in <var>aNum</var>'s private data member <var>_num</var>.
<p><img src="exec5.gif" tppabs="http://www.relisoft.com/book/lang/scopes/images/exec5.gif" width=516 height=294 border=0 alt=" ">
<p class=caption>Figure 10 While still executing the <var>AddInput</var> method of num, the method <var>GetValue</var> of the object <var>aNum</var> is called. It retrieves and returns the value stored in <var>aNum</var>'s private data member <var>_num</var>. This value, equal to 11, is then added to the <var>num</var>'s private data member <var>_num</var>. It's value was 5, but after the addition of 11 it changes to 16.
<p><img src="exec6.gif" tppabs="http://www.relisoft.com/book/lang/scopes/images/exec6.gif" width=516 height=228 border=0 alt=" ">
<p class=caption>Figure 11 The method <var>GetValue</var> of the object <var>num</var> is called again. It retrieves and returns the value of <var>num</var>'s private data member <var>_num</var>, which is now equal to 16.
<p>
<!-- Sidebar -->
<table width="100%" border=0 cellpadding=5><tr>
<td width=10> </td>
<td bgcolor="#cccccc" class=sidebar>
You might wonder, how it is done that the program executes a method, then returns to the caller and continues exactly where it left off. Where does it keep the information about what it was doing before it made the call? The answer is, it's using the program (or the execution) stack. Calling a procedure means pushing some data on this (invisible) stack. Returning from the procedure means popping this data from the stack and returning to the previous state.
<p>In particular, a calling sequence might look something like this. First, all the arguments to be passed to the procedure are pushed on the stack. Then the return address is pushed--that's the address of the next instruction to be executed after the return from the call. Then the execution jumps to the actual code of the procedure. The procedure starts by making room on the stack for its local variables. This way, each procedure invocation gets a fresh copy of all local variables. The procedure is executed, then it pops its local variables, the return address and the arguments passed to it, and jumps back to the return address. If there is a return value, it is usually passed in one of the processor's registers.
<p>Take into account that this is only a conceptual model. The actual details depend on the particular <i>calling convention</i> and are both processor- and compiler-dependent.</td></table>
<!-- End Sidebar -->
<p>
<!-- Sidebar -->
<table width="100%" border=0 cellpadding=5><tr>
<td width=10>
<td bgcolor="#cccccc" class=sidebar>
At this point you might want to make a small detour into a short <a href="javascript:if(confirm('http://www.relisoft.com/book/lang/scopes/debug.html \n\nThis file was not retrieved by Teleport Pro, because it is linked too far away from its Starting Address. If you increase the in-domain depth setting for the Starting Address, this file will be queued for retrieval. \n\nDo you want to open it from the server?'))window.location='http://www.relisoft.com/book/lang/scopes/debug.html'" tppabs="http://www.relisoft.com/book/lang/scopes/debug.html">debugging</a> tutorial.<td width=10>
</table>
<!-- End Sidebar -->
<p>
In C++ you can do standard arithmetic operations on numbers and variables. Below is a list of basic arithmetic operators.
<p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -