?? apd.htm
字號:
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 << "Boots is " << Boots.GetAge() << " years old\n";
19: return 0;
20: }
21:
22: CAT & 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 <iostream.h>
2:
3: class CAT
4: {
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 << "Boots is " << Boots->GetAge() << " years old\n";
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>
<B>2. What is the difference between a declaration and a definition?<BR>
</B><BR>
A definition sets aside memory, but a declaration does not. Almost all declarations
are definitions; the major exceptions are class declarations, function prototypes,
and <TT>typedef</TT> statements.<BR>
<B><BR>
3. When is the copy constructor called?<BR>
</B><BR>
Whenever a temporary copy of an object is created. This happens every time an object
is passed by value.<BR>
<BR>
<B>4. When is the destructor called?<BR>
</B><BR>
The destructor is called each time an object is destroyed, either because it goes
out of scope or because you call <TT>delete</TT> on a pointer pointing to it.<BR>
<B><BR>
5. How does the copy constructor differ from the assignment operator (<TT>=</TT>)?<BR>
</B><BR>
The assignment operator acts on an existing object; the copy constructor creates
a new one.<BR>
<B><BR>
6. What is the <TT>this</TT> pointer?<BR>
</B><BR>
The <TT>this</TT> pointer is a hidden parameter in every member function that points
to the object itself.<BR>
<BR>
<B>7. How do you differentiate between overloading the prefix and postfix increments?<BR>
</B><BR>
The prefix operator takes no parameters. The postfix operator takes a single <TT>int</TT>
parameter, which is used as a signal to the compiler that this is the postfix variant.<BR>
<BR>
<B>8. Can you overload the operator<TT>+</TT> for <TT>short</TT> integers?<BR>
</B><BR>
No, you cannot overload any operator for built-in types.<BR>
<BR>
<B>9. Is it legal in C++ to overload <TT>operator++</TT> so that it decrements a
value in your class?<BR>
</B><BR>
It is legal, but it is a bad idea. Operators should be overloaded in a way that is
likely to be readily understood by anyone reading your code.<BR>
<BR>
<B>10. What return value must conversion operators have in their declaration?<BR>
</B><BR>
None. Like constructors and destructors, they have no return values.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading32"></A><FONT COLOR="#000077">Exercises</FONT></H4>
<DL>
<DD><B>1.</B> Write a <TT>SimpleCircle</TT> class declaration (only) with one member
variable: <TT>itsRadius</TT>. Include a default constructor, a destructor, and accessor
methods for <TT>itsRadius</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">class SimpleCircle
{
public:
SimpleCircle();
~SimpleCircle();
void SetRadius(int);
int GetRadius();
private:
int itsRadius;
};
</FONT></PRE>
<DL>
<DD><B>2.</B> Using the class you created in Exercise 1, write the implementation
of the default constructor, initializing <TT>itsRadius</TT> with the value <TT>5</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">SimpleCircle::SimpleCircle():
itsRadius(5)
{}
</FONT></PRE>
<DL>
<DD><B>3.</B> Using the same class, add a second constructor that takes a value as
its parameter and assigns that value to <TT>itsRadius</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">SimpleCircle::SimpleCircle(int radius):
itsRadius(radius)
{}
</FONT></PRE>
<DL>
<DD><B>4. </B>Create a prefix and postfix increment operator for your <TT>SimpleCircle</TT>
class that increments <TT>itsRadius</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">const SimpleCircle& SimpleCircle::operator++()
{
++(itsRadius);
return *this;
}
// Operator ++(int) postfix.
// Fetch then increment
const SimpleCircle SimpleCircle::operator++ (int)
{
// declare local SimpleCircle and initialize to value of *this
SimpleCircle temp(*this);
++(itsRadius);
return temp;
}
</FONT></PRE>
<DL>
<DD><B>5.</B> Change <TT>SimpleCircle</TT> to store <TT>itsRadius</TT> on the free
store, and fix the existing methods.
</DL>
<PRE><FONT COLOR="#0066FF">class SimpleCircle
{
public:
SimpleCircle();
SimpleCircle(int);
~SimpleCircle();
void SetRadius(int);
int GetRadius();
const SimpleCircle& operator++();
const SimpleCircle operator++(int);
private:
int *itsRadius;
};
SimpleCircle::SimpleCircle()
{itsRadius = new int(5);}
SimpleCircle::SimpleCircle(int radius)
{itsRadius = new int(radius);}
const SimpleCircle& SimpleCircle::operator++()
{
++(itsRadius);
return *this;
}
// Operator ++(int) postfix.
// Fetch then increment
const SimpleCircle SimpleCircle::operator++ (int)
{
// declare local SimpleCircle and initialize to value of *this
SimpleCircle temp(*this);
++(itsRadius);
return temp;
}
</FONT></PRE>
<DL>
<DD><B>6. </B>Provide a copy constructor for <TT>SimpleCircle</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">SimpleCircle::SimpleCircle(const SimpleCircle & rhs)
{
int val = rhs.GetRadius();
itsRadius = new int(val);
}
</FONT></PRE>
<DL>
<DD><B>7.</B> Provide an <TT>operator=</TT> for <TT>SimpleCircle</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">SimpleCircle& SimpleCircle::operator=(const SimpleCircle & rhs)
{
if (this == &rhs)
return *this;
delete itsRadius;
itsRadius = new int;
*itsRadius = rhs.GetRadius();
return *this;
}
</FONT></PRE>
<DL>
<DD><B>8.</B> Write a program that creates two <TT>SimpleCircle</TT> objects. Use
the default constructor on one and instantiate the other with the value <TT>9</TT>.
Call <TT>increment</TT> on each and then print their values. Finally, assign the
second to the first and print its values.
</DL>
<PRE><FONT COLOR="#0066FF">#include <iostream.h>
class SimpleCircle
{
public:
// constructors
SimpleCircle();
SimpleCircle(int);
SimpleCircle(const SimpleCircle &);
~SimpleCircle() {}
// accessor functions
void SetRadius(int);
int GetRadius()const;
// operators
const SimpleCircle& operator++();
const SimpleCircle operator++(int);
SimpleCircle& operator=(const SimpleCircle &);
private:
int *itsRadius;
};
SimpleCircle::SimpleCircle()
{itsRadius = new int(5);}
SimpleCircle::SimpleCircle(int radius)
{itsRadius = new int(radius);}
SimpleCircle::SimpleCircle(const SimpleCircle & rhs)
{
int val = rhs.GetRadius();
itsRadius = new int(val);
}
SimpleCircle& SimpleCircle::operator=(const SimpleCircle & rhs)
{
if (this == &rhs)
return *this;
*itsRadius = rhs.GetRadius();
return *this;
}
const SimpleCircle& SimpleCircle::operator++()
{
++(itsRadius);
return *this;
}
// Operator ++(int) postfix.
// Fetch then increment
const SimpleCircle SimpleCircle::operator++ (int)
{
// declare local SimpleCircle and initialize to value of *this
SimpleCircle temp(*this);
++(itsRadius);
return temp;
}
int SimpleCircle::GetRadius() const
{
return *itsRadius;
}
int main()
{
SimpleCircle CircleOne, CircleTwo(9);
CircleOne++;
++CircleTwo;
cout << "CircleOne: " << CircleOne.GetRadius() << endl;
cout << "CircleTwo: " << CircleTwo.GetRadius() << endl;
CircleOne = CircleTwo;
cout << "CircleOne: " << CircleOne.GetRadius() << endl;
cout << "CircleTwo: " << CircleTwo.GetRadius() << endl;
return 0;
}
</FONT></PRE>
<DL>
<DD><B>9.</B> BUG BUSTERS: What is wrong with this implementation of the assignment
operator?
</DL>
<PRE><FONT COLOR="#0066FF">SQUARE SQUARE ::operator=(const SQUARE & rhs)
{
itsSide = new int;
*itsSide = rhs.GetSide();
return *this;
}
</FONT></PRE>
<DL>
<DD>You must check to see whether <TT>rhs</TT> equals <TT>this</TT>, or the call
to <TT>a = a</TT> will crash your program.<BR>
<BR>
<B>10.</B> BUG BUSTERS: What is wrong with this implementation of <TT>operator+</TT>?
</DL>
<PRE><FONT COLOR="#0066FF">VeryShort VeryShort::operator+ (const VeryShort& rhs)
{
itsVal += rhs.GetItsVal();
return *this;
}
</FONT></PRE>
<DL>
<DD>This <TT>operator+</TT> is changing the value in one of the operands, rather
than creating a new <TT>VeryShort</TT> object with the sum. The right way to do this
is as follows:
</DL>
<PRE><FONT COLOR="#0066FF">VeryShort VeryShort::operator+ (const VeryShort& rhs)
{
return VeryShort(itsVal + rhs.GetItsVal());
}
</FONT></PRE>
<H3 ALIGN="CENTER"><FONT COLOR="#0066FF"></FONT></H3>
<H3><A NAME="Heading33"></A><FONT COLOR="#000077">Day 11</FONT></H3>
<H4 ALIGN="CENTER"><A NAME="Heading34"></A><FONT COLOR="#000077">Quiz</FONT></H4>
<DL>
<DD><B>1. What are the first and last elements in <TT>SomeArray[25]</TT>?<BR>
</B><TT><BR>
SomeArray[0]</TT>,<TT> SomeArray[24]</TT><BR>
<BR>
<B>2. How do you declare a multidimensional array?<BR>
</B><BR>
Write a set of subscripts for each dimension. For example, <TT>SomeArray[2][3][2]</TT>
is a three-dimensional array. The first dimension has two elements, the second has
three, and <BR>
the third has two.<BR>
<BR>
<B>3. Initialize the members of the array in Question 2.</B>
</DL>
<PRE><FONT COLOR="#0066FF">SomeArray[2][3][2] = { { {1,2},{3,4},{5,6} } , { {7,8},{9,10},{11,12} } };
</FONT></PRE>
<DL>
<DD><B>4. How many elements are in the array <TT>SomeArray[10][5][20]</TT>?<BR>
</B><BR>
10x5x20=1,000<BR>
<BR>
<B>5. What is the maximum number of elements that you can add to a linked list?<BR>
</B><BR>
There is no fixed maximum. It depends on how much memory you have available.<BR>
<BR>
<B>6. Can you use subscript notation on a linked list?<BR>
</B><BR>
You can use subscript notation on a linked list only by writing your own class to
contain the linked list and overloading the subscript operator.<BR>
<BR>
<B>7. What is the last character in the string "Brad is a nice guy"?<BR>
</B><BR>
The null character.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading35"></A><FONT COLOR="#000077">Exercises</FONT></H4>
<DL>
<DD><B>1. </B>Declare a two-dimensional array that represents a tic-tac-toe game
board.
</DL>
<PRE><FONT COLOR="#0066FF">int GameBoard[3][3];
</FONT></PRE>
<DL>
<DD><B>2.</B> Write the code that initializes all the elements in the array you created
in Exercise 1 to the value <TT>0</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">int GameBoard[3][3] = { {0,0,0},{0,0,0},{0,0,0} }
</FONT></PRE>
<DL>
<DD><B>3.</B> Write the declaration for a <TT>Node</TT> class that holds <TT>unsigned</TT>
<TT>short</TT> integers.
</DL>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -