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

📄 apd.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	<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&amp; SimpleCircle::operator++(){     ++(itsRadius);     return *this;}// Operator ++(int) postfix. // Fetch then incrementconst 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&amp; operator++();     const SimpleCircle operator++(int);private:     int *itsRadius;};SimpleCircle::SimpleCircle(){itsRadius = new int(5);}SimpleCircle::SimpleCircle(int radius){itsRadius = new int(radius);}const SimpleCircle&amp; SimpleCircle::operator++(){     ++(itsRadius);     return *this;}// Operator ++(int) postfix. // Fetch then incrementconst 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 &amp; 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&amp; SimpleCircle::operator=(const SimpleCircle &amp; rhs){     if (this == &amp;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 &lt;iostream.h&gt;class SimpleCircle{public:      // constructors     SimpleCircle();     SimpleCircle(int);     SimpleCircle(const SimpleCircle &amp;);     ~SimpleCircle() {}// accessor functions     void SetRadius(int);     int GetRadius()const;// operators     const SimpleCircle&amp; operator++();     const SimpleCircle operator++(int);     SimpleCircle&amp; operator=(const SimpleCircle &amp;);private:     int *itsRadius;};SimpleCircle::SimpleCircle(){itsRadius = new int(5);}SimpleCircle::SimpleCircle(int radius){itsRadius = new int(radius);}SimpleCircle::SimpleCircle(const SimpleCircle &amp; rhs){     int val = rhs.GetRadius();     itsRadius = new int(val);}SimpleCircle&amp; SimpleCircle::operator=(const SimpleCircle &amp; rhs){     if (this == &amp;rhs)          return *this;     *itsRadius = rhs.GetRadius();     return *this;}const SimpleCircle&amp; SimpleCircle::operator++(){     ++(itsRadius);     return *this;}// Operator ++(int) postfix. // Fetch then incrementconst 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 &lt;&lt; &quot;CircleOne: &quot; &lt;&lt; CircleOne.GetRadius() &lt;&lt; endl;     cout &lt;&lt; &quot;CircleTwo: &quot; &lt;&lt; CircleTwo.GetRadius() &lt;&lt; endl;     CircleOne = CircleTwo;     cout &lt;&lt; &quot;CircleOne: &quot; &lt;&lt; CircleOne.GetRadius() &lt;&lt; endl;     cout &lt;&lt; &quot;CircleTwo: &quot; &lt;&lt; CircleTwo.GetRadius() &lt;&lt; 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 &amp; 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&amp; 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&amp; 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 &quot;Brad is a nice guy&quot;?<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><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&lt;4; i++)     for (int j = 0; j&lt;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&lt;=5; i++)     for (int j = 0; j&lt;=4; j++)          SomeArray[i][j] = 0;</FONT></PRE><DL>	<DD>You wanted to write <TT>i&lt;5</TT>, but you wrote <TT>i&lt;=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();

⌨️ 快捷键说明

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