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

📄 apd.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<PRE><FONT COLOR="#0066FF">for (int x = 0; x &lt; 100; x++)100</FONT></PRE><DL>	<DD><B>2.</B> Write a nested <TT>for</TT> loop that prints a 10x10 pattern of <TT>0</TT>s.</DL><PRE><FONT COLOR="#0066FF">for (int i = 0; i&lt; 10; i++){    for ( int j = 0; j&lt; 10; j++)       cout &lt;&lt; &quot;0&quot;;    cout &lt;&lt; &quot;\n&quot;;}</FONT></PRE><DL>	<DD><B>3</B>. Write a <TT>for</TT> statement to count from 100 to 200 by 2s.</DL><PRE><FONT COLOR="#0066FF">for (int x = 100; x&lt;=200; x+=2)</FONT></PRE><DL>	<DD><B>4.</B> Write a <TT>while</TT> loop to count from 100 to 200 by 2s.</DL><PRE><FONT COLOR="#0066FF">int x = 100;while (x &lt;= 200)    x+= 2;</FONT></PRE><DL>	<DD><B>5.</B> Write a <TT>do...while</TT> loop to count from 100 to 200 by 2s.</DL><PRE><FONT COLOR="#0066FF">int x = 100;do{    x+=2;} while (x &lt;= 200);</FONT></PRE><DL>	<DD><B>6.</B> BUG BUSTERS: What is wrong with this code?</DL><PRE><FONT COLOR="#0066FF">int counter = 0while (counter &lt; 10){     cout &lt;&lt; &quot;counter: &quot; &lt;&lt; counter;    counter++;}</FONT></PRE><DL>	<DD><TT>counter</TT> is never incremented and the <TT>while</TT> loop will never	terminate.<BR>	<BR>	<B>7.</B> BUG BUSTERS: What is wrong with this code?</DL><PRE><FONT COLOR="#0066FF">for (int counter = 0; counter &lt; 10; counter++);    cout &lt;&lt; counter &lt;&lt; &quot;\n&quot;;</FONT></PRE><DL>	<DD>There is a semicolon after the loop, and the loop does nothing. The programmer	may have intended this, but if <TT>counter</TT> was supposed to print each value,	it won't.<BR>	<BR>	<B>8.</B> BUG BUSTERS: What is wrong with this code?</DL><PRE><FONT COLOR="#0066FF">int counter = 100;while (counter &lt; 10){    cout &lt;&lt; &quot;counter now: &quot; &lt;&lt; counter;    counter--;}</FONT></PRE><DL>	<DD><TT>counter</TT> is initialized to <TT>100</TT>, but the test condition is that	if it is less than 10, the test will fail and the body will never be executed. If	line 1 were changed to <TT>int counter = 5;</TT>, the loop would not terminate until	it had counted down past the smallest possible <TT>int</TT>. Because <TT>int</TT>	is <TT>signed</TT> by default, this would not be what was intended.<BR>	<BR>	<B>9.</B> BUG BUSTERS: What is wrong with this code?</DL><PRE><FONT COLOR="#0066FF">cout &lt;&lt; &quot;Enter a number between 0 and 5: &quot;;cin &gt;&gt; theNumber;switch (theNumber){   case 0:         doZero();   case 1:              // fall through   case 2:              // fall through   case 3:              // fall through   case 4:              // fall through   case 5:          doOneToFive();         break;   default:         doDefault();         break;}</FONT></PRE><DL>	<DD><TT>Case 0</TT> probably needs a <TT>break</TT> statement. If it does not, it	should be documented with a comment.</DL><H3><A NAME="Heading24"></A><FONT COLOR="#000077">Day 8</FONT></H3><H4 ALIGN="CENTER"><A NAME="Heading25"></A><FONT COLOR="#000077">Quiz</FONT></H4><DL>	<DD><B>1. What operator is used to determine the address of a variable?<BR>	</B><BR>	The address of operator (<TT>&amp;</TT>) is used to determine the address of any	variable.<BR>	<B><BR>	2. What operator is used to find the value stored at an address held in a pointer?<BR>	</B><BR>	The dereference operator (<TT>*</TT>) is used to access the value at an address in	a pointer.<BR>	<BR>	<B>3. What is a pointer?<BR>	</B><BR>	A pointer is a variable that holds the address of another variable.<BR>	<BR>	<B>4. What is the difference between the address stored in a pointer and the value	at that </B>address?<BR>	<BR>	The address stored in the pointer is the address of another variable. The value stored	at that address is any value stored in any variable. The indirection operator (<TT>*</TT>)	returns the value stored at the address, which itself is stored in the pointer.<BR>	<BR>	<B>5. What is the difference between the indirection operator and the address of	oper-ator?<BR>	</B><BR>	The indirection operator returns the value at the address stored in a pointer. The	address of operator (<TT>&amp;</TT>) returns the memory address of the variable.<BR>	<BR>	<B>6. What is the difference between <TT>const int * ptrOne</TT> and <TT>int * const	ptrTwo</TT>?<BR>	</B><BR>	The <TT>const int * ptrOne</TT> declares that <TT>ptrOne</TT> is a pointer to a constant	integer. The integer itself cannot be changed using this pointer.<BR>	The <TT>int * const ptrTwo</TT> declares that <TT>ptrTwo</TT> is a constant pointer	to an integer. Once it is initialized, this pointer cannot be reassigned.</DL><H4 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">Exercises</FONT></H4><DL>	<DD><B>1.</B> What do these declarations do?	<DL>		<DD><B><BR>		a.</B> <TT>int * pOne;</TT><BR>		<B>b</B>. <TT>int vTwo;</TT><BR>		<B>c.</B> <TT>int * pThree = &amp;vTwo;</TT><BR>		<B><BR>		a.</B> <TT>int * pOne;</TT> declares a pointer to an integer.<BR>		<B>b.</B> <TT>int vTwo;</TT> declares an integer variable.<BR>		<B>c.</B> <TT>int * pThree = &amp;vTwo;</TT> declares a pointer to an integer and		initializes it with the address of another variable.	</DL>	<DD><B><BR>	2.</B> If you have an <TT>unsigned short</TT> variable named <TT>yourAge</TT>, how	would you declare a pointer to manipulate <TT>yourAge</TT>?</DL><PRE><FONT COLOR="#0066FF">unsigned short *pAge = &amp;yourAge;</FONT></PRE><DL>	<DD><B>3.</B> Assign the value <TT>50</TT> to the variable <TT>yourAge</TT> by using	the pointer that you declared in Exercise 2.</DL><PRE><FONT COLOR="#0066FF">*pAge = 50;</FONT></PRE><DL>	<DD><B>4.</B> Write a small program that declares an integer and a pointer to integer.	Assign the address of the integer to the pointer. Use the pointer to set a value	in the integer variable.</DL><PRE><FONT COLOR="#0066FF">int theInteger;int *pInteger = &amp;theInteger;*pInteger = 5;</FONT></PRE><DL>	<DD><B>5.</B> BUG BUSTERS: What is wrong with this code?</DL><PRE><FONT COLOR="#0066FF">#include &lt;iostream.h&gt;int main(){     int *pInt;     *pInt = 9;     cout &lt;&lt; &quot;The value at pInt: &quot; &lt;&lt; *pInt;return 0;}</FONT></PRE><DL>	<DD><TT>pInt</TT> should have been initialized. More importantly, because it was	not initialized and was not assigned the address of any memory, it points to a random	place in memory. Assigning <TT>9</TT> to that random place is a dangerous bug.<BR>	<BR>	<B>6.</B> BUG BUSTERS: What is wrong with this code?</DL><PRE><FONT COLOR="#0066FF">int main(){    int SomeVariable = 5;    cout &lt;&lt; &quot;SomeVariable: &quot; &lt;&lt; SomeVariable &lt;&lt; &quot;\n&quot;;    int *pVar = &amp; SomeVariable;    pVar = 9;    cout &lt;&lt; &quot;SomeVariable: &quot; &lt;&lt; *pVar &lt;&lt; &quot;\n&quot;;return 0;}</FONT></PRE><DL>	<DD>Presumably, the programmer meant to assign <TT>9</TT> to the value at <TT>pVar</TT>.	Unfortunately, <TT>9</TT> was assigned to be the value of <TT>pVar</TT> because the	indirection operator (<TT>*</TT>) was left off. This will lead to disaster if <TT>pVar</TT>	is used to assign a value.</DL><H3 ALIGN="CENTER"></H3><H3><A NAME="Heading27"></A><FONT COLOR="#000077">Day 9</FONT></H3><H4 ALIGN="CENTER"><A NAME="Heading28"></A><FONT COLOR="#000077">Quiz</FONT></H4><DL>	<DD><B>1. What is the difference between a reference and a pointer?<BR>	</B><BR>	A reference is an alias, and a pointer is a variable that holds an address. References	cannot be null and cannot be assigned to.<BR>	<BR>	<B>2. When must you use a pointer rather than a reference?<BR>	</B><BR>	When you may need to reassign what is pointed to, or when the pointer may be null.<BR>	<BR>	<B>3. What does <TT>new</TT> return if there is insufficient memory to make your	<TT>new</TT> object?<BR>	</B><BR>	A null pointer (<TT>0</TT>).<BR>	<BR>	<B>4. What is a constant reference?<BR>	</B><BR>	This is a shorthand way of saying &quot;a reference to a constant object.&quot;<BR>	<BR>	<B>5. What is the difference between passing by reference and passing a reference?<BR>	</B><BR>	Passing by reference means not making a local copy. It can be accomplished by passing	a reference or by passing a pointer.</DL><H4 ALIGN="CENTER"><A NAME="Heading29"></A><FONT COLOR="#000077">Exercises</FONT></H4><DL>	<DD><B>1</B>. Write a program that declares an <TT>int</TT>, a reference to an <TT>int</TT>,	and a pointer to an <TT>int</TT>. Use the pointer and the reference to manipulate	the value in the <TT>int</TT>.</DL><PRE><FONT COLOR="#0066FF">int main(){int varOne;int&amp; rVar = varOne;int* pVar = &amp;varOne;rVar = 5;*pVar = 7;return 0;}</FONT></PRE><DL>	<DD><B>2.</B> Write a program that declares a constant pointer to a constant integer.	Initialize the pointer to an integer variable, <TT>varOne</TT>. Assign <TT>6</TT>	to <TT>varOne</TT>. Use the pointer to assign <TT>7</TT> to <TT>varOne</TT>. Create	a second integer variable, <TT>varTwo</TT>. Reassign the pointer to <TT>varTwo</TT>.</DL><PRE><FONT COLOR="#0066FF">int main(){   int varOne;   const int * const pVar = &amp;varOne;   *pVar = 7;   int varTwo;   pVar = &amp;varTwo;return 0;}</FONT></PRE><DL>	<DD><B>3.</B> Compile the program in Exercise 2. What produces errors? What produces	warnings?<BR>	You can't assign a value to a constant object, and you can't reassign a constant	pointer.<BR>	<BR>	<B>4.</B> Write a program that produces a stray pointer.</DL><PRE><FONT COLOR="#0066FF">int main(){int * pVar;*pVar = 9;return 0;}</FONT></PRE><DL>	<DD><B>5.</B> Fix the program from Exercise 4.</DL><PRE><FONT COLOR="#0066FF">int main(){int VarOne;int * pVar = &amp;varOne;*pVar = 9;return 0;}</FONT></PRE><DL>	<DD><B>6.</B> Write a program that produces a memory leak.</DL><PRE><FONT COLOR="#0066FF">int FuncOne();int main(){   int localVar = FunOne();   cout &lt;&lt; &quot;the value of localVar is: &quot; &lt;&lt; localVar;return 0;}int FuncOne(){   int * pVar = new int (5);   return *pVar;}</FONT></PRE><DL>	<DD><B>7.</B> Fix the program from Exercise 6.</DL><PRE><FONT COLOR="#0066FF">void FuncOne();int main(){   FuncOne();return 0;}void FuncOne(){   int * pVar = new int (5);   cout &lt;&lt; &quot;the value of *pVar is: &quot; &lt;&lt; *pVar ;}</FONT></PRE><DL>	<DD><B>8.</B> BUG BUSTERS: What is wrong with this program?</DL><PRE><FONT COLOR="#0066FF">1:     #include &lt;iostream.h&gt;2:3:     class CAT4:     {5:        public:6:           CAT(int age) { itsAge = age; }7:           ~CAT(){}8:           int GetAge() const { return itsAge;}9:        private:10:          int itsAge;11:    };12:13:    CAT &amp; MakeCat(int age);14:    int main()15:    {16:       int age = 7;17:       CAT Boots = MakeCat(age);18:       cout &lt;&lt; &quot;Boots is &quot; &lt;&lt; Boots.GetAge() &lt;&lt; &quot; years old\n&quot;;19:     return 0;20:    }21:22:    CAT &amp; MakeCat(int age)23:    {24:       CAT * pCat = new CAT(age);25:       return *pCat;26:    }</FONT></PRE><DL>	<DD><TT>MakeCat</TT> returns a reference to the <TT>CAT</TT> created on the free	store. There is no way to free that memory, and this produces a memory leak.<BR>	<BR>	<B>9.</B> Fix the program from Exercise 8.</DL><PRE><FONT COLOR="#0066FF">1:     #include &lt;iostream.h&gt;2:3:     class CAT4:     {5:        public:6:           CAT(int age) { itsAge = age; }7:           ~CAT(){}8:           int GetAge() const { return itsAge;}9:        private:10:          int itsAge;11:    };12:13:    CAT * MakeCat(int age);14:    int main()15:    {16:       int age = 7;17:       CAT * Boots = MakeCat(age);18:       cout &lt;&lt; &quot;Boots is &quot; &lt;&lt; Boots-&gt;GetAge() &lt;&lt; &quot; years old\n&quot;;19:       delete Boots;20:     return 0;21:    }22:23:    CAT * MakeCat(int age)24:    {25:       return new CAT(age);26:    }</FONT></PRE><H3 ALIGN="CENTER"><FONT COLOR="#0066FF"></FONT></H3><H3><A NAME="Heading30"></A><FONT COLOR="#000077">Day 10</FONT></H3><H4 ALIGN="CENTER"><A NAME="Heading31"></A><FONT COLOR="#000077">Quiz</FONT></H4><DL>	<DD><B>1. When you overload member functions, in what ways must they differ?<BR>	</B><BR>	Overloaded member functions are functions in a class that share a name but differ	in the number or type of their parameters.<BR>

⌨️ 快捷键说明

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