📄 apd.htm
字号:
<BR>
<B>3. If a function doesn't return a value, how do you declare the function?</B><BR>
<BR>
Declare the function to return <TT>void</TT>.<BR>
<BR>
<B>4. If you don't declare a return value, what type of return value is assumed?</B><BR>
<BR>
Any function that does not explicitly declare a return type returns <TT>int</TT>.<BR>
<BR>
<B>5. What is a local variable?</B><BR>
<BR>
A local variable is a variable passed into or declared within a block, typically
a function. It is visible only within the block.<BR>
<BR>
<B>6. What is scope?</B><BR>
<BR>
Scope refers to the visibility and lifetime of local and global variables. Scope
is usually established by a set of braces.<BR>
<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 <iostream.h>
void myFunc(unsigned short int x);
int main()
{
unsigned short int x, y;
y = myFunc(int);
cout << "x: " << x << " y: " << y << "\n";
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 <iostream.h>
int myFunc(unsigned short int x);
int main()
{
unsigned short int x, y;
y = myFunc(int);
cout << "x: " << x << " y: " << y << "\n";
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 <iostream.h>
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 << "Enter two numbers.\n Number one: ";
cin >> one;
cout << "Number two: ";
cin >> two;
answer = Divider(one, two);
if (answer > -1)
cout << "Answer: " << answer;
else
cout << "Error, can't divide by zero!";
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 <iostream.h>
typedef unsigned short USHORT;
typedef unsigned long ULONG;
ULONG GetPower(USHORT n, USHORT power);
int main()
{
USHORT number, power;
ULONG answer;
cout << "Enter a number: ";
cin >> number;
cout << "To what power? ";
cin >> power;
answer = GetPower(number,power);
cout << number << " to the " << power << "th power is " <<
answer << 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 << "At AcmeSexist company, John and Sally have the same
job.\n";
cout << "John is " << John.GetAge() << " years old and he has
been with";
cout << "the firm for " << John.GetYearsOfService << "
years.\n";
cout << "John earns $" << John.GetSalary << " dollars per
year.\n\n";
cout << "Sally, on the other hand is " << Sally.GetAge() << "
years old and has";
cout << "been with the company " << Sally.GetYearsOfService;
cout << " years. Yet Sally only makes $" << Sally.GetSalary();
cout << " dollars per year! Something here is unfair.";
</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 < 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -