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

📄 ch12.htm

📁 Why C++ is the emerging standard in software development. The steps to develop a C++ program. How
💻 HTM
📖 第 1 页 / 共 5 页
字号:
28:29:    class Dog : public Mammal30:    {31:    public:32:33:       // Constructors34:       Dog():itsBreed(YORKIE){}35:       ~Dog(){}36:37:       // Accessors38:       BREED GetBreed() const { return itsBreed; }39:       void SetBreed(BREED breed) { itsBreed = breed; }40:41:       // Other methods42:       void WagTail() { cout &lt;&lt; &quot;Tail wagging...\n&quot;; }43:       void BegForFood() { cout &lt;&lt; &quot;Begging for food...\n&quot;; }44:45:    private:46:       BREED itsBreed;47:    };48:49:    int main()50:    {51:       Dog fido;52:       fido.Speak();53:       fido.WagTail();54:       cout &lt;&lt; &quot;Fido is &quot; &lt;&lt; fido.GetAge() &lt;&lt; &quot; years old\n&quot;;55:     return 0;<TT>56: }</TT></FONT><FONT COLOR="#0066FF">Output: Mammal sound!Tail wagging...Fido is 2 years old</FONT></PRE><P><FONT COLOR="#000077"><B><BR>Analysis:</B></FONT><B> </B>On lines 6-27, the <TT>Mammal</TT> class is declared(all of its functions are inline to save space here). On lines 29-47, the <TT>Dog</TT>class is declared as a derived class of <TT>Mammal</TT>. Thus, by these declarations,all <TT>Dog</TT>s have an age, a weight, and a breed.</P><P>On line 51, a <TT>Dog</TT> is declared: <TT>Fido</TT>. <TT>Fido</TT> inheritsall the attributes of a <TT>Mammal</TT>, as well as all the attributes of a <TT>Dog</TT>.Thus, <TT>Fido</TT> knows how to <TT>WagTail()</TT>, but also knows how to <TT>Speak()</TT>and <TT>Sleep()</TT>.<H3 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Constructors andDestructors</FONT></H3><P><TT>Dog</TT> objects are <TT>Mammal</TT> objects. This is the essence of the is-arelationship. When <TT>Fido</TT> is created, his base constructor is called first,creating a <TT>Mammal</TT>. Then the <TT>Dog</TT> constructor is called, completingthe construction of the <TT>Dog</TT> object. Because we gave <TT>Fido</TT> no parameters,the default constructor was called in each case. <TT>Fido</TT> doesn't exist untilhe is completely constructed, which means that both his <TT>Mammal</TT> part andhis <TT>Dog</TT> part must be constructed. Thus, both constructors must be called.</P><P>When <TT>Fido</TT> is destroyed, first the <TT>Dog</TT> destructor will be calledand then the destructor for the <TT>Mammal</TT> part of <TT>Fido</TT>. Each destructoris given an opportunity to clean up after its own part of <TT>Fido</TT>. Rememberto clean up after your <TT>Dog</TT>! Listing 12.3 demonstrates this.</P><P><A NAME="Heading14"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 12.3. Constructorsand destructors called.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:     //Listing 12.3 Constructors and destructors called.2:3:     #include &lt;iostream.h&gt;4:     enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };5:6:     class Mammal7:     {8:     public:9:        // constructors10:       Mammal();11:       ~Mammal();12:13:       //accessors14:       int GetAge() const { return itsAge; }15:       void SetAge(int age) { itsAge = age; }16:       int GetWeight() const { return itsWeight; }17:       void SetWeight(int weight) { itsWeight = weight; }18:19:       //Other methods20:       void Speak() const { cout &lt;&lt; &quot;Mammal sound!\n&quot;; }21:       void Sleep() const { cout &lt;&lt; &quot;shhh. I'm sleeping.\n&quot;; }22:23:24:    protected:25:       int itsAge;26:       int itsWeight;27:    };28:29:    class Dog : public Mammal30:    {31:    public:32:33:       // Constructors34:       Dog();35:       ~Dog();36:37:       // Accessors38:       BREED GetBreed() const { return itsBreed; }39:       void SetBreed(BREED breed) { itsBreed = breed; }40:41:       // Other methods42:       void WagTail() { cout &lt;&lt; &quot;Tail wagging...\n&quot;; }43:       void BegForFood() { cout &lt;&lt; &quot;Begging for food...\n&quot;; }44:45:    private:46:       BREED itsBreed;47:    };48:49:    Mammal::Mammal():50:    itsAge(1),51:    itsWeight(5)52:    {53:       cout &lt;&lt; &quot;Mammal constructor...\n&quot;;54:    }55:56:    Mammal::~Mammal()57:    {58:       cout &lt;&lt; &quot;Mammal destructor...\n&quot;;59:    }60:61:    Dog::Dog():62:    itsBreed(YORKIE)63:    {64:       cout &lt;&lt; &quot;Dog constructor...\n&quot;;65:    }66:67:    Dog::~Dog()68:    {69:       cout &lt;&lt; &quot;Dog destructor...\n&quot;;70:    }71:    int main()72:    {73:       Dog fido;74:       fido.Speak();75:       fido.WagTail();76:       cout &lt;&lt; &quot;Fido is &quot; &lt;&lt; fido.GetAge() &lt;&lt; &quot; years old\n&quot;;77:     return 0;<TT>78: }</TT></FONT><FONT COLOR="#0066FF">Output: Mammal constructor...Dog constructor...Mammal sound!Tail wagging...Fido is 1 years oldDog destructor...Mammal destructor...</FONT></PRE><P><FONT COLOR="#000077"><B><BR>Analysis:</B></FONT><B> </B>Listing 12.3 is just like Listing 12.2, except that theconstructors and destructors now print to the screen when called. <TT>Mammal</TT>'sconstructor is called, then <TT>Dog</TT>'s. At that point the <TT>Dog</TT> fullyexists, and its methods can be called. When <TT>fido</TT> goes out of scope, <TT>Dog</TT>'sdestructor is called, followed by a call to <TT>Mammal</TT>'s destructor.<H4 ALIGN="CENTER"><A NAME="Heading16"></A><FONT COLOR="#000077">Passing Argumentsto Base Constructors</FONT></H4><P>It is possible that you'll want to overload the constructor of <TT>Mammal</TT>to take a specific age, and that you'll want to overload the <TT>Dog</TT> constructorto take a breed. How do you get the age and weight parameters passed up to the rightconstructor in <TT>Mammal</TT>? What if <TT>Dog</TT>s want to initialize weight but<TT>Mammal</TT>s don't?</P><P>Base class initialization can be performed during class initialization by writingthe base class name, followed by the parameters expected by the base class. Listing12.4 demonstrates this.</P><P><A NAME="Heading17"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 12.4. Overloadingconstructors in derived classes.</B></FONT></P><PRE><FONT COLOR="#0066FF">1:     //Listing 12.4 Overloading constructors in derived classes2:3:     #include &lt;iostream.h&gt;4:     enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };5:6:     class Mammal7:     {8:     public:9:        // constructors10:       Mammal();11:       Mammal(int age);12:       ~Mammal();13:14:       //accessors15:       int GetAge() const { return itsAge; }16:       void SetAge(int age) { itsAge = age; }17:       int GetWeight() const { return itsWeight; }18:       void SetWeight(int weight) { itsWeight = weight; }19:20:       //Other methods21:       void Speak() const { cout &lt;&lt; &quot;Mammal sound!\n&quot;; }22:       void Sleep() const { cout &lt;&lt; &quot;shhh. I'm sleeping.\n&quot;; }23:24:25:    protected:26:       int itsAge;27:       int itsWeight;28:    };29:30:    class Dog : public Mammal31:    {32:    public:33:34:       // Constructors35:       Dog();36:       Dog(int age);37:       Dog(int age, int weight);38:       Dog(int age, BREED breed);39:       Dog(int age, int weight, BREED breed);40:       ~Dog();41:42:       // Accessors43:       BREED GetBreed() const { return itsBreed; }44:       void SetBreed(BREED breed) { itsBreed = breed; }45:46:       // Other methods47:       void WagTail() { cout &lt;&lt; &quot;Tail wagging...\n&quot;; }48:       void BegForFood() { cout &lt;&lt; &quot;Begging for food...\n&quot;; }49:50:    private:51:       BREED itsBreed;52:    };53:54:    Mammal::Mammal():55:    itsAge(1),56:    itsWeight(5)57:    {58:       cout &lt;&lt; &quot;Mammal constructor...\n&quot;;59:    }60:61:    Mammal::Mammal(int age):62:    itsAge(age),63:    itsWeight(5)64:    {65:       cout &lt;&lt; &quot;Mammal(int) constructor...\n&quot;;66:    }67:68:    Mammal::~Mammal()69:    {70:       cout &lt;&lt; &quot;Mammal destructor...\n&quot;;71:    }72:73:    Dog::Dog():74:    Mammal(),75:    itsBreed(YORKIE)76:    {77:       cout &lt;&lt; &quot;Dog constructor...\n&quot;;78:    }79:80:    Dog::Dog(int age):81:    Mammal(age),82:    itsBreed(YORKIE)83:    {84:       cout &lt;&lt; &quot;Dog(int) constructor...\n&quot;;85:    }86:87:    Dog::Dog(int age, int weight):88:    Mammal(age),89:    itsBreed(YORKIE)90:    {91:       itsWeight = weight;92:       cout &lt;&lt; &quot;Dog(int, int) constructor...\n&quot;;93:    }94:95:    Dog::Dog(int age, int weight, BREED breed):96:    Mammal(age),97:    itsBreed(breed)98:    {99:       itsWeight = weight;100:      cout &lt;&lt; &quot;Dog(int, int, BREED) constructor...\n&quot;;101:   }102:103:   Dog::Dog(int age, BREED breed):104:   Mammal(age),105:   itsBreed(breed)106:   {107:      cout &lt;&lt; &quot;Dog(int, BREED) constructor...\n&quot;;108:   }109:110:   Dog::~Dog()111:   {112:      cout &lt;&lt; &quot;Dog destructor...\n&quot;;113:   }114:   int main()115:   {116:      Dog fido;117:      Dog rover(5);118:      Dog buster(6,8);119:      Dog yorkie (3,YORKIE);120:      Dog dobbie (4,20,DOBERMAN);121:      fido.Speak();122:      rover.WagTail();123:      cout &lt;&lt; &quot;Yorkie is &quot; &lt;&lt; yorkie.GetAge() &lt;&lt; &quot; years old\n&quot;;124:      cout &lt;&lt; &quot;Dobbie weighs &quot;;125:      cout &lt;&lt; dobbie.GetWeight() &lt;&lt; &quot; pounds\n&quot;;126:     return 0;<TT>127: }</TT></FONT></PRE><BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>The output has been numbered here	so that each line can be referred to in the analysis. <HR></BLOCKQUOTE><PRE><FONT COLOR="#0066FF">Output: 1:  Mammal constructor...2:  Dog constructor...3:  Mammal(int) constructor...4:  Dog(int) constructor...5:  Mammal(int) constructor...6:  Dog(int, int) constructor...7:  Mammal(int) constructor...8:  Dog(int, BREED) constructor....9:  Mammal(int) constructor...10: Dog(int, int, BREED) constructor...

⌨️ 快捷键说明

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