?? apd.htm
字號:
<PRE><FONT COLOR="#0066FF"> class Node
{
public:
Node ();
Node (int);
~Node();
void SetNext(Node * node) { itsNext = node; }
Node * GetNext() const { return itsNext; }
int GetVal() const { return itsVal; }
void Insert(Node *);
void Display();
private:
int itsVal;
Node * itsNext;
};
</FONT></PRE>
<DL>
<DD><B>4.</B> BUG BUSTERS: What is wrong with this code fragment?
</DL>
<PRE><FONT COLOR="#0066FF">unsigned short SomeArray[5][4];
for (int i = 0; i<4; i++)
for (int j = 0; j<5; j++)
SomeArray[i][j] = i+j;
</FONT></PRE>
<DL>
<DD>The array is 5 elements by 4 elements, but the code initializes 4x5.<BR>
<B><BR>
5.</B> BUG BUSTERS: What is wrong with this code fragment?
</DL>
<PRE><FONT COLOR="#0066FF">unsigned short SomeArray[5][4];
for (int i = 0; i<=5; i++)
for (int j = 0; j<=4; j++)
SomeArray[i][j] = 0;
</FONT></PRE>
<DL>
<DD>You wanted to write <TT>i<5</TT>, but you wrote <TT>i<=5</TT> instead.
The code will run when <TT>i == 5</TT> and <TT>j == 4</TT>, but there is no such
element as <TT>SomeArray[5][4]</TT>.
</DL>
<H3 ALIGN="CENTER"></H3>
<H3><A NAME="Heading36"></A><FONT COLOR="#000077">Day 12</FONT></H3>
<H4 ALIGN="CENTER"><A NAME="Heading37"></A><FONT COLOR="#000077">Quiz</FONT></H4>
<DL>
<DD><B>1. What is a v-table?<BR>
</B><BR>
A v-table, or virtual function table, is a common way for compilers to manage virtual
functions in C++. The table keeps a list of the addresses of all the virtual functions
and, depending on the runtime type of the object pointed to, invokes the right function.<BR>
<BR>
<B>2. What is a virtual destructor?<BR>
</B><BR>
A destructor of any class can be declared to be virtual. When the pointer is deleted,
the runtime type of the object will be assessed and the correct derived destructor
invoked.<BR>
<BR>
<B>3. How do you show the declaration of a virtual constructor?<BR>
</B><BR>
There are no virtual constructors.<BR>
<BR>
<B>4. How can you create a virtual copy constructor?<BR>
</B><BR>
By creating a virtual method in your class, which itself calls the copy constructor.<BR>
<BR>
<B>5. How do you invoke a base member function from a derived class in which you've
overridden that function?</B>
</DL>
<PRE><FONT COLOR="#0066FF">Base::FunctionName();
</FONT></PRE>
<DL>
<DD><B>6. How do you invoke a base member function from a derived class in which
you have not <BR>
overridden that function?</B>
</DL>
<PRE><FONT COLOR="#0066FF">FunctionName();
</FONT></PRE>
<DL>
<DD><B>7. If a base class declares a function to be virtual, and a derived class
does not use the term <TT>virtual</TT> when overriding that class, is it still virtual
when inherited by a third-generation class?</B><BR>
<BR>
Yes, the virtuality is inherited and cannot be turned off.<BR>
<BR>
<B>8. What is the <TT>protected</TT> keyword used for?</B><BR>
<TT><BR>
protected</TT> members are accessible to the member functions of derived objects.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading38"></A><FONT COLOR="#000077">Exercises</FONT></H4>
<DL>
<DD><B>1. </B>Show the declaration of a virtual function taking an integer parameter
and returning <TT>void</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">virtual void SomeFunction(int);
</FONT></PRE>
<DL>
<DD><B>2.</B> Show the declaration of a class <TT>Square</TT>, which derives from
<TT>Rectangle</TT>, which in turn derives from <TT>Shape</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">class Square : public Rectangle
{};
</FONT></PRE>
<DL>
<DD><B>3.</B> If, in Exercise 2, <TT>Shape</TT> takes no parameters, <TT>Rectangle</TT>
takes two (<TT>length</TT> and <TT>width</TT>), and <TT>Square</TT> takes only one
(<TT>length</TT>), show the constructor initialization for <TT>Square</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">Square::Square(int length):
Rectangle(length, length){}
</FONT></PRE>
<DL>
<DD><B>4.</B> Write a virtual copy constructor for the class <TT>Square</TT> (from
the preceding question).
</DL>
<PRE><FONT COLOR="#0066FF">class Square
{
public:
// ...
virtual Square * clone() const { return new Square(*this); }
// ...
};
</FONT></PRE>
<DL>
<DD><B>5.</B> BUG BUSTERS: What is wrong with this code snippet?
</DL>
<PRE><FONT COLOR="#0066FF">void SomeFunction (Shape);
Shape * pRect = new Rectangle;
SomeFunction(*pRect);
</FONT></PRE>
<DL>
<DD>Perhaps nothing. <TT>SomeFunction</TT> expects a <TT>Shape</TT> object. You've
passed it a <TT>Rectangle</TT> "sliced" down to a <TT>Shape</TT>. As long
as you don't need any of the <TT>Rectangle</TT> parts, this will be fine. If you
do need the <TT>Rectangle</TT> parts, you'll need to change <TT>SomeFunction</TT>
to take a pointer or a reference to a <TT>Shape</TT>.<BR>
<B><BR>
6.</B> BUG BUSTERS: What is wrong with this code snippet?
</DL>
<PRE><FONT COLOR="#0066FF">class Shape()
{
public:
Shape();
virtual ~Shape();
virtual Shape(const Shape&);
};
</FONT></PRE>
<DL>
<DD>You can't declare a copy constructor to be virtual.
</DL>
<H3 ALIGN="CENTER"></H3>
<H3><A NAME="Heading39"></A><FONT COLOR="#000077">Day 13</FONT></H3>
<H4 ALIGN="CENTER"><A NAME="Heading40"></A><FONT COLOR="#000077">Quiz</FONT></H4>
<DL>
<DD><B>1. What is a down cast?<BR>
</B><BR>
A down cast (also called "casting down") is a declaration that a pointer
to a base class is to be treated as a pointer to a derived class.<BR>
<BR>
<B>2. What is the v-ptr?<BR>
</B><BR>
The v-ptr, or virtual-function pointer, is an implementation detail of virtual functions.
Each object in a class with virtual functions has a v-ptr, which points to the virtual
function table for that class.<BR>
<BR>
<B>3. If a round rectangle has straight edges and rounded corners, your <TT>RoundRect</TT>
class inherits both from <TT>Rectangle</TT> and from <TT>Circle</TT>, and they in
turn both inherit from <TT>Shape</TT>, how many <TT>Shape</TT>s are created when
you create a <TT>RoundRect</TT>?</B><BR>
<BR>
If neither class inherits using the keyword <TT>virtual</TT>, two <TT>Shapes</TT>
are created: one for <TT>Rectangle</TT> and one for <TT>Shape</TT>. If the keyword
<TT>virtual</TT> is used for both classes, only one shared <TT>Shape</TT> is created.<BR>
<BR>
<B>4. If <TT>Horse</TT> and <TT>Bird</TT> inherit <TT>virtual</TT> <TT>public</TT>
from <TT>Animal</TT>, do their constructors initialize the <TT>Animal</TT> constructor?
If <TT>Pegasus</TT> inherits from both <TT>Horse</TT> and <TT>Bird</TT>, how does
it initialize <TT>Animal</TT>'s constructor?</B><BR>
<BR>
Both <TT>Horse</TT> and <TT>Bird</TT> initialize their base class, <TT>Animal</TT>,
in their constructors. <TT>Pegasus</TT> does as well, and when a <TT>Pegasus</TT>
is created, the <TT>Horse</TT> and <TT>Bird</TT> initializations of <TT>Animal</TT>
are ignored.<BR>
<B><BR>
5. Declare a class <TT>Vehicle</TT> and make it an abstract data type.</B>
</DL>
<PRE><FONT COLOR="#0066FF">class Vehicle
{
virtual void Move() = 0;
}
</FONT></PRE>
<DL>
<DD><B>6. If a base class is an ADT, and it has three pure virtual functions, how
many of these functions must be overridden in its derived classes?</B><BR>
<BR>
None must be overridden unless you want to make the class non-abstract, in which
case all three must be overridden.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading41"></A><FONT COLOR="#000077">Exercises</FONT></H4>
<DL>
<DD><B>1.</B> Show the declaration for a class <TT>JetPlane</TT>, which inherits
from <TT>Rocket</TT> and <TT>Airplane</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">class JetPlane : public Rocket, public Airplane
</FONT></PRE>
<DL>
<DD><B>2. </B>Show the declaration for <TT>747</TT>, which inherits from the <TT>JetPlane</TT>
class described in Exercise 1.
</DL>
<PRE><FONT COLOR="#0066FF">class 747 : public JetPlane
</FONT></PRE>
<DL>
<DD><B>3.</B> Show the declarations for the classes <TT>Car</TT> and <TT>Bus</TT>,
which each derive from the class <TT>Vehicle</TT>. Make <TT>Vehicle</TT> an ADT with
two pure virtual functions. Make <TT>Car</TT> and <TT>Bus</TT> not be ADTs.
</DL>
<PRE><FONT COLOR="#0066FF">class Vehicle
{
virtual void Move() = 0;
virtual void Haul() = 0;
};
class Car : public Vehicle
{
virtual void Move();
virtual void Haul();
};
class Bus : public Vehicle
{
virtual void Move();
virtual void Haul();
};
</FONT></PRE>
<DL>
<DD><B>4. </B>Modify the program in Exercise 1 so that <TT>Car</TT> is an ADT, and
derive <TT>SportsCar</TT> and <TT>Coupe</TT> from <TT>Car</TT>. In the <TT>Car</TT>
class, provide an implementation for one of the pure virtual functions in <TT>Vehicle</TT>
and make it non-pure.
</DL>
<PRE><FONT COLOR="#0066FF">class Vehicle
{
virtual void Move() = 0;
virtual void Haul() = 0;
};
class Car : public Vehicle
{
virtual void Move();
};
class Bus : public Vehicle
{
virtual void Move();
virtual void Haul();
};
class SportsCar : public Car
{
virtual void Haul();
};
class Coupe : public Car
{
virtual void Haul();
};
</FONT></PRE>
<H3 ALIGN="CENTER"><FONT COLOR="#0066FF"></FONT></H3>
<H3><A NAME="Heading42"></A><FONT COLOR="#000077">Day 14</FONT></H3>
<H4 ALIGN="CENTER"><A NAME="Heading43"></A><FONT COLOR="#000077">Quiz</FONT></H4>
<DL>
<DD><B>1. Can static member variables be private?</B><BR>
<BR>
Yes. They are member variables, and their access can be controlled like any other.
If they are private, they can be accessed only by using member functions or, more
commonly, static member functions.<BR>
<B><BR>
2. Show the declaration for a static member variable.</B>
</DL>
<PRE><FONT COLOR="#0066FF">static int itsStatic;
</FONT></PRE>
<DL>
<DD><B>3. Show the declaration for a static function pointer.</B>
</DL>
<PRE><FONT COLOR="#0066FF">static int SomeFunction();
</FONT></PRE>
<DL>
<DD><B>4. Show the declaration for a pointer <TT>to</TT> function returning <TT>long</TT>
and taking an integer parameter.</B>
</DL>
<PRE><FONT COLOR="#0066FF">long (* function)(int);
</FONT></PRE>
<DL>
<DD><B>5. Modify the pointer in Exercise 4 to be a pointer to member function of
class <TT>Car</TT></B>
</DL>
<PRE><FONT COLOR="#0066FF">long ( Car::*function)(int);
</FONT></PRE>
<DL>
<DD><B>6. Show the declaration for an array of 10 pointers as defined in Exercise
5.</B>
</DL>
<PRE><FONT COLOR="#0066FF">(long ( Car::*function)(int) theArray [10];
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading44"></A><FONT COLOR="#000077">Exercises</FONT></H4>
<DL>
<DD><B>1.</B> Write a short program declaring a class with one member variable and
one static member variable. Have the constructor initialize the member variable and
increment the static member variable. Have the destructor decrement the member variable.
</DL>
<PRE><FONT COLOR="#0066FF">1: class myClass
2: {
3: public:
4: myClass();
5: ~myClass();
6: private:
7: int itsMember;
8: static int itsStatic;
9: };
10:
11: myClass::myClass():
12: itsMember(1)
13: {
14: itsStatic++;
15: }
16:
17: myClass::~myClass()
18: {
19: itsStatic--;
20: }
21:
22: int myClass::itsStatic = 0;
23:
24: int main()
25: {}
</FONT></PRE>
<DL>
<DD><B>2. </B>Using the program from Exercise 1, write a short driver program that
makes three objects and then displays their member variables and the static member
variable. Then destroy each object and show the effect on the static member variable.
</DL>
<PRE><FONT COLOR="#0066FF">1: #include <iostream.h>
2:
3: class myClass
4: {
5: public:
6: myClass();
7: ~myClass();
8: void ShowMember();
9: void ShowStatic();
10: private:
11: int itsMember;
12: static int itsStatic;
13: };
14:
15: myClass::myClass():
16: itsMember(1)
17: {
18: itsStatic++;
19: }
20:
21: myClass::~myClass()
22: {
23: itsStatic--;
24: cout << "In destructor. ItsStatic: " << itsStatic << endl;
25: }
26:
27: void myClass::ShowMember()
28: {
29: cout << "itsMember: " << itsMember << endl;
30: }
31:
32: void myClass::ShowStatic()
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -