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

📄 apd.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	<BR>	<B>7. What is recursion?</B><BR>	<BR>	Recursion generally refers to the ability of a function to call itself.<BR>	<BR>	<B>8. When should you use global variables?</B><BR>	<BR>	Global variables are typically used when many functions need access to the same data.	Global variables are very rare in C++; once you know how to create static class variables,	you will almost never create global variables.<BR>	<BR>	<B>9. What is function overloading?</B><BR>	<BR>	Function overloading is the ability to write more than one function with the same	name, distinguished by the number or type of the parameters.<BR>	<BR>	<B>10. What is polymorphism?</B><BR>	<BR>	Polymorphism is the ability to treat many objects of differing but related types	without regard to their differences. In C++, polymorphism is accomplished by using	class derivation and virtual functions.</DL><H4 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">Exercises</FONT></H4><DL>	<DD><B>1.</B> Write the prototype for a function named <TT>Perimeter</TT>, which	returns an <TT>unsigned long int</TT> and which takes two parameters, both <TT>unsigned	short</TT> <TT>int</TT>s.<BR>	u<TT>nsigned long int</TT> <TT>Perimeter</TT>(<TT>unsigned short int</TT>, <TT>unsigned	short int</TT>);<BR>	<BR>	<B>2.</B> Write the definition of the function <TT>Perimeter</TT> as described in	Exercise 1. The two parameters represent the length and width of a rectangle and	have the function return the perimeter (twice the length plus twice the width).</DL><PRE><FONT COLOR="#0066FF"><TT>unsigned long int Perimeter</TT>(<TT>unsigned short int length</TT>, <TT>unsigned short int width</TT>)</FONT><FONT COLOR="#0066FF">{  return 2*length + 2*width;}</FONT></PRE><DL>	<DD><B>3</B>. BUG BUSTERS: What is wrong with the function?</DL><PRE><FONT COLOR="#0066FF">#include &lt;iostream.h&gt;void myFunc(unsigned short int x);int main(){    unsigned short int x, y;    y = myFunc(int);    cout &lt;&lt; &quot;x: &quot; &lt;&lt; x &lt;&lt; &quot; y: &quot; &lt;&lt; y &lt;&lt; &quot;\n&quot;;return 0;}void myFunc(unsigned short int x){    return (4*x);}</FONT></PRE><DL>	<DD>The function is declared to return <TT>void</TT> and it cannot return a value.<BR>	<BR>	<B>4.</B> BUG BUSTERS: What is wrong with the function?</DL><PRE><FONT COLOR="#0066FF">#include &lt;iostream.h&gt;int myFunc(unsigned short int x);int main(){    unsigned short int x, y;    y = myFunc(int);    cout &lt;&lt; &quot;x: &quot; &lt;&lt; x &lt;&lt; &quot; y: &quot; &lt;&lt; y &lt;&lt; &quot;\n&quot;;return 0;}int myFunc(unsigned short int x){    return (4*x);}</FONT></PRE><DL>	<DD>This function would be fine, but there is a semicolon at the end of the function	definition's header.<BR>	<BR>	<B>5.</B> Write a function that takes two <TT>unsigned short</TT> <TT>int</TT> arguments	and returns the result of dividing the first by the second. Do not do the division	if the second number is <TT>0</TT>, but do return <TT>-1</TT>.<BR>	<TT>short int Divider</TT>(<TT>unsigned short int valOne</TT>, <TT>unsigned short	int valTwo</TT>)</DL><PRE><FONT COLOR="#0066FF">{    if (valTwo == 0)          return -1;    else          return valOne / valTwo;}</FONT></PRE><DL>	<DD><B>6.</B> Write a program that asks the user for two numbers and calls the function	you wrote in Exercise 5. Print the answer, or print an error message if you get <TT>-1</TT>.</DL><PRE><FONT COLOR="#0066FF">#include &lt;iostream.h&gt;typedef unsigned short int USHORT;typedef unsigned long int ULONG;short int Divider(unsigned short int valone,unsigned short int valtwo);int main(){    USHORT one, two;    short int answer;    cout &lt;&lt; &quot;Enter two numbers.\n Number one: &quot;;    cin &gt;&gt; one;    cout &lt;&lt; &quot;Number two: &quot;;    cin &gt;&gt; two;    answer = Divider(one, two);    if (answer &gt; -1)       cout &lt;&lt; &quot;Answer: &quot; &lt;&lt; answer;    else       cout &lt;&lt; &quot;Error, can't divide by zero!&quot;;return 0;}</FONT></PRE><DL>	<DD><B>7. </B>Write a program that asks for a number and a power. Write a recursive	function that takes the number to the power. Thus, if the number is 2 and the power	is 4, the function will return <TT>16</TT>.</DL><PRE><FONT COLOR="#0066FF">#include &lt;iostream.h&gt;typedef unsigned short USHORT;typedef unsigned long ULONG;ULONG GetPower(USHORT n, USHORT power);int main(){   USHORT number, power;   ULONG answer;   cout &lt;&lt; &quot;Enter a number: &quot;;   cin &gt;&gt; number;   cout &lt;&lt; &quot;To what power? &quot;;   cin &gt;&gt; power;   answer = GetPower(number,power);   cout &lt;&lt; number &lt;&lt; &quot; to the &quot; &lt;&lt; power &lt;&lt; &quot;th power is &quot; &lt;&lt;answer &lt;&lt; endl;return 0;}ULONG GetPower(USHORT n, USHORT power){    if(power == 1)     return n;    else       return (n * GetPower(n,power-1));}</FONT></PRE><H3 ALIGN="CENTER"><FONT COLOR="#0066FF"></FONT></H3><H3><A NAME="Heading18"></A><FONT COLOR="#000077">Day 6</FONT></H3><H4 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">Quiz</FONT></H4><DL>	<DD><B>1. What is the dot operator, and what is it used for?</B><BR>	<BR>	The dot operator is the period (<TT>.</TT>). It is used to access the members of	the class.<BR>	<BR>	<B>2. Which sets aside memory--declaration or definition?</B><BR>	<BR>	Definitions of variables set aside memory. Declarations of classes don't set aside	memory.<BR>	<BR>	<B>3. Is the declaration of a class its interface or its implementation?</B><BR>	<BR>	The declaration of a class is its interface; it tells clients of the class how to	interact with the class. The implementation of the class is the set of member functions	stored--usually in a related CPP file.<BR>	<BR>	<B>4. What is the difference between public and private data members?</B><BR>	<BR>	Public data members can be accessed by clients of the class. Private data members	can be accessed only by member functions of the class.<BR>	<BR>	<B>5. Can member functions be private?<BR>	</B><BR>	Yes. Both member functions and member data can be private.<BR>	<BR>	<B>6. Can member data be public?</B><BR>	<BR>	Although member data can be public, it is good programming practice to make it private	and to provide public accessor functions to the data.<BR>	<BR>	<B>7. If you declare two <TT>Cat</TT> objects, can they have different values in	their <TT>itsAge</TT> member data?</B><BR>	<BR>	Yes. Each object of a class has its own data members.<BR>	<BR>	<B>8. Do class declarations end with a semicolon? Do class method definitions?</B><BR>	<BR>	Declarations end with a semicolon after the closing brace; function definitions do	not.<BR>	<BR>	<B>9. What would the header for a <TT>Cat</TT> function, <TT>Meow</TT>, that takes	no parameters and returns <TT>void</TT> look like?<BR>	</B><BR>	The header for a <TT>Cat</TT> function, <TT>Meow()</TT>, that takes no parameters	and returns <TT>void</TT> looks like this:</DL><PRE><FONT COLOR="#0066FF">void Cat::Meow()</FONT></PRE><DL>	<DD><B>10. What function is called to initialize a class?<BR>	</B><BR>	The constructor is called to initialize a class.</DL><H4 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">Exercises</FONT></H4><DL>	<DD><B>1. </B>Write the code that declares a class called <TT>Employee</TT> with	these data members: <TT>age</TT>, <TT>yearsOfService</TT>, and <TT>Salary</TT>.</DL><PRE><FONT COLOR="#0066FF">class Employee{    int Age;    int YearsOfService;    int Salary;};</FONT></PRE><DL>	<DD><B>2.</B> Rewrite the <TT>Employee</TT> class to make the data members private,	and provide public accessor methods to get and set each of the data members.</DL><PRE><FONT COLOR="#0066FF">class Employee{public:    int GetAge() const;    void SetAge(int age);    int GetYearsOfService()const;    void SetYearsOfService(int years);    int GetSalary()const;    void SetSalary(int salary);private:    int Age;    int YearsOfService;    int Salary;};</FONT></PRE><DL>	<DD><B>3.</B> Write a program with the <TT>Employee</TT> class that makes two <TT>Employee</TT>s;	sets their <TT>age</TT>, <TT>YearsOfService</TT>, and <TT>Salary</TT>; and prints	their values.</DL><PRE><FONT COLOR="#0066FF">main(){    Employee John;    Employee Sally;    John.SetAge(30);    John.SetYearsOfService(5);    John.SetSalary(50000);    Sally.SetAge(32);    Sally.SetYearsOfService(8);    Sally.SetSalary(40000);    cout &lt;&lt; &quot;At AcmeSexist company, John and Sally have the samejob.\n&quot;;    cout &lt;&lt; &quot;John is &quot; &lt;&lt; John.GetAge() &lt;&lt; &quot; years old and he hasbeen with&quot;;    cout &lt;&lt; &quot;the firm for &quot; &lt;&lt; John.GetYearsOfService &lt;&lt; &quot;years.\n&quot;;    cout &lt;&lt; &quot;John earns $&quot; &lt;&lt; John.GetSalary &lt;&lt; &quot; dollars peryear.\n\n&quot;;    cout &lt;&lt; &quot;Sally, on the other hand is &quot; &lt;&lt; Sally.GetAge() &lt;&lt; &quot;years old and has&quot;;    cout &lt;&lt; &quot;been with the company &quot; &lt;&lt; Sally.GetYearsOfService;    cout &lt;&lt; &quot; years. Yet Sally only makes $&quot; &lt;&lt; Sally.GetSalary();    cout &lt;&lt; &quot; dollars per year!  Something here is unfair.&quot;;</FONT></PRE><DL>	<DD><B>4.</B> Continuing from Exercise 3, provide a method of <TT>Employee</TT> that	reports how many thousands of dollars the employee earns, rounded to the nearest	1,000.</DL><PRE><FONT COLOR="#0066FF">float Employee:GetRoundedThousands()const{    return Salary / 1000;}</FONT></PRE><DL>	<DD><B>5. </B>Change the <TT>Employee</TT> class so that you can initialize <TT>age</TT>,	<TT>YearsOfService</TT>, and <TT>Salary</TT> when you create the employee.</DL><PRE><FONT COLOR="#0066FF">class Employee{public:    Employee(int age, int yearsOfService, int salary);    int GetAge()const;    void SetAge(int age);    int GetYearsOfService()const;    void SetYearsOfService(int years);    int GetSalary()const;    void SetSalary(int salary);private:    int Age;    int YearsOfService;    int Salary;};</FONT></PRE><DL>	<DD><B>6.</B> BUG BUSTERS: What is wrong with the following declaration?</DL><PRE><FONT COLOR="#0066FF">class Square{public:    int Side;}</FONT></PRE><DL>	<DD>Class declarations must end with a semicolon.<BR>	<BR>	<B>7</B>. BUG BUSTERS: Why isn't the following class declaration very useful?</DL><PRE><FONT COLOR="#0066FF">class Cat{    int GetAge()const;private:    int itsAge;};</FONT></PRE><DL>	<DD>The accessor <TT>GetAge()</TT> is private. Remember: All class members are private	unless you say otherwise.<BR>	<BR>	<B>8</B>. BUG BUSTERS: What three bugs in this code will the compiler find?</DL><PRE><FONT COLOR="#0066FF">class  TV{public:    void SetStation(int Station);    int GetStation() const;private:    int itsStation;};main(){    TV myTV;    myTV.itsStation = 9;    TV.SetStation(10);    TV myOtherTv(2);}</FONT></PRE><DL>	<DD>You can't access <TT>itsStation</TT> directly. It is private.<BR>	You can't call <TT>SetStation()</TT> on the class. You can call <TT>SetStation()</TT>	only on objects.<BR>	You can't initialize <TT>itsStation</TT> because there is no matching constructor.</DL><H3 ALIGN="CENTER"></H3><H3><A NAME="Heading21"></A><FONT COLOR="#000077">Day 7</FONT></H3><H4 ALIGN="CENTER"><A NAME="Heading22"></A><FONT COLOR="#000077">Quiz</FONT></H4><DL>	<DD><B>1. How do I initialize more than one variable in a <TT>for</TT> loop?</B><BR>	<BR>	Separate the initializations with commas, such as</DL><PRE><FONT COLOR="#0066FF">for (x = 0, y = 10; x &lt; 100; x++, y++)</FONT></PRE><DL>	<DD><B>2. Why is <TT>goto</TT> avoided?<BR>	</B><TT><BR>	goto</TT> jumps in any direction to any arbitrary line of code. This makes for source	code that is difficult to understand and therefore difficult to maintain.<BR>	<BR>	<B>3. Is it possible to write a <TT>for</TT> loop with a body that is never executed?</B><BR>	<BR>	Yes, if the condition is <TT>FALSE</TT> after the initialization, the body of the	<TT>for</TT> loop will never execute. Here's an example:</DL><PRE><FONT COLOR="#0066FF">for (int x = 100; x &lt; 100; x++)</FONT></PRE><DL>	<DD><B>4. Is it possible to nest <TT>while</TT> loops within <TT>for</TT> loops?</B><BR>	<BR>	Yes. Any loop can be nested within any other loop.<BR>	<BR>	<B>5. Is it possible to create a loop that never ends? Give an example.<BR>	</B><BR>	Yes. Following are examples for both a <TT>for</TT> loop and a <TT>while</TT> loop:</DL><PRE><FONT COLOR="#0066FF">for(;;){    // This for loop never ends!}while(1){    // This while loop never ends!</FONT></PRE><PRE><FONT COLOR="#0066FF">}</FONT></PRE><DL>	<DD><B>6. What happens if you create a loop that never ends?<BR>	</B><BR>	Your program hangs, and you usually must reboot the computer.</DL><H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">Exercises</FONT></H4><DL>	<DD><B>1.</B> What is the value of <TT>x</TT> when the <TT>for</TT> loop completes?</DL>

⌨️ 快捷键说明

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