📄 apd.htm
字号:
</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 myClass2: {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 myClass4: {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()33: {34: cout << "itsStatic: " << itsStatic << endl;35: }36: int myClass::itsStatic = 0;37:38: int main()39: {40: myClass obj1;41: obj1.ShowMember();42: obj1.ShowStatic();43:44: myClass obj2;45: obj2.ShowMember();46: obj2.ShowStatic();47:48: myClass obj3;49: obj3.ShowMember();50: obj3.ShowStatic();51: return 0;52: }</FONT></PRE><DL> <DD><B>3. </B>Modify the program from Exercise 2 to use a static member function to access the static member variable. Make the static member variable private.</DL><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2:3: class myClass4: {5: public:6: myClass();7: ~myClass();8: void ShowMember();9: static int GetStatic();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: int myClass::itsStatic = 0;33:34: void myClass::GetStatic()35: {36: return itsStatic;37: }38:39: int main()40: {41: myClass obj1;42: obj1.ShowMember();43: cout << "Static: " << myClass::GetStatic() << endl;44:45: myClass obj2;46: obj2.ShowMember();47: cout << "Static: " << myClass::GetStatic() << endl;48:49: myClass obj3;50: obj3.ShowMember();51: cout << "Static: " << myClass::GetStatic() << endl;52: return 0;53: }</FONT></PRE><DL> <DD><B>4.</B> Write a pointer to a member function to access the non-static member data in the program in Exercise 3, and use that pointer to print the value of that data.</DL><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2:3: class myClass4: {5: public:6: myClass();7: ~myClass();8: void ShowMember();9: static int GetStatic();10: private:11: int itsMember;12: static int itsStatic;13: };14:15: myClass::myClass():16: itsMember(1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -