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

📄 ch14rv2.htm

📁 vc的电子书
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->

	<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
	<TITLE>Teach Yourself C++ in 21 Days</TITLE>
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF">

<H1></H1>
<P ALIGN="CENTER"><A HREF="ch14.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch14.htm"><IMG SRC="BLANPREV.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANPREV.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="tppmsgs/msgs0.htm#1" tppabs="http://www.mcp.com/sams"><IMG
SRC="BLANHOME.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="0"></A><A HREF="index.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/index.htm"><IMG SRC="BLANTOC.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch15.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch15.htm"><IMG SRC="BLANNEXT.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A>
<H2 ALIGN="CENTER"><BR>
<A NAME="Heading1"></A><FONT COLOR="#000077">In Review</FONT></H2>
<P>The Week in Review program for Week 2 brings together many of the skills you've
acquired over the past fortnight and produces a powerful program.</P>
<P>This demonstration of linked lists utilizes virtual functions, pure virtual functions,
function overriding, polymorphism, public inheritance, function overloading, forever
loops, pointers, references, and more.</P>
<P>The goal of this program is to create a linked list. The nodes on the list are
designed to hold parts, as might be used in a factory. While this is not the final
form of this program, it does make a good demonstration of a fairly advanced data
structure. The code list is 311 lines. Try to analyze the code on your own before
reading the analysis that follows the output.</P>

<P><A NAME="Heading2"></A><FONT SIZE="4" COLOR="#000077"><B>Listing R2.1. Week 2
in Review listing.</B></FONT>
<PRE><FONT COLOR="#0066FF">0:     // **************************************************
1:     //
2:     // Title:       Week 2 in Review
3:     //
4:     // File:       Week2
5:     //
6:     // Description:   Provide a linked list demonstration program
7:     //
8:     // Classes:      PART - holds part numbers and potentially other
9:     //                     information about parts
10:     //
11:     //               PartNode - acts as a node in a PartsList
12:     //
13:     //               PartsList - provides the mechanisms for a linked list                                                            &#194;of parts
14:     //
15:     // Author:      Jesse Liberty (jl)
16:     //
17:     // Developed:   486/66 32mb RAM  MVC 1.5
18:     //
19:     // Target:      Platform independent
20:     //
21:     // Rev History:  9/94 - First release (jl)
22:     //
23:     // **************************************************
24:   
25:     #include &lt;iostream.h&gt;
26:   
27:     typedef unsigned long ULONG;
28:     typedef unsigned short USHORT;
29:   
30:   
31:     // **************** Part ************
32:   
33:     // Abstract base class of parts
34:     class Part
35:     {
36:     public:
37:        Part():itsPartNumber(1) {}
38:        Part(ULONG PartNumber):itsPartNumber(PartNumber){}
39:        virtual ~Part(){};
40:        ULONG GetPartNumber() const { return itsPartNumber; }
41:        virtual void Display() const =0;  // must be overridden
42:     private:
43:        ULONG itsPartNumber;
44:     };
45:   
46:     // implementation of pure virtual function so that
47:     // derived classes can chain up
48:     void Part::Display() const
49:     {
50:         cout &lt;&lt; &quot;\nPart Number: &quot; &lt;&lt; itsPartNumber &lt;&lt; endl;
51:     }
52:   
53:     // **************** Car Part ************
54:   
55:     class CarPart : public Part
56:     {
57:     public:
58:        CarPart():itsModelYear(94){}
59:        CarPart(USHORT year, ULONG partNumber);
60:        virtual void Display() const 
61:       { 
62:          Part::Display(); cout &lt;&lt; &quot;Model Year: &quot;;
63:          cout &lt;&lt; itsModelYear &lt;&lt; endl;  
64:       }
65:     private:
66:        USHORT itsModelYear;
67:     };
68:   
69:     CarPart::CarPart(USHORT year, ULONG partNumber):
70:        itsModelYear(year),
71:        Part(partNumber)
72:     {}
73:   
74:   
75:     // **************** AirPlane Part ************
76:   
77:     class AirPlanePart : public Part
78:     {
79:     public:
80:        AirPlanePart():itsEngineNumber(1){};
81:        AirPlanePart(USHORT EngineNumber, ULONG PartNumber);
82:        virtual void Display() const
83:       { 
84:          Part::Display(); cout &lt;&lt; &quot;Engine No.: &quot;;
85:          cout &lt;&lt; itsEngineNumber &lt;&lt; endl;  
86:       }
87:     private:
88:        USHORT itsEngineNumber;
89:     };
90:   
91:     AirPlanePart::AirPlanePart(USHORT EngineNumber, ULONG PartNumber):
92:        itsEngineNumber(EngineNumber),
93:        Part(PartNumber)
94:     {}
95:   
96:     // **************** Part Node ************
97:     class PartNode
98:     {
99:     public:
100:        PartNode (Part*);
101:        ~PartNode();
102:        void SetNext(PartNode * node) { itsNext = node; }
103:        PartNode * GetNext() const;
104:        Part * GetPart() const;
105:     private:
106:        Part *itsPart;
107:        PartNode * itsNext;
108:     };
109:   
110:     // PartNode Implementations...
111:   
112:     PartNode::PartNode(Part* pPart):
113:     itsPart(pPart),
114:     itsNext(0)
115:     {}
116:   
117:     PartNode::~PartNode()
118:     {
119:        delete itsPart;
120:        itsPart = 0;
121:        delete itsNext;
122:        itsNext = 0;
123:     }
124:   
125:     // Returns NULL if no next PartNode
126:     PartNode * PartNode::GetNext() const
127:     {
128:           return itsNext;
129:     }
130:   
131:     Part * PartNode::GetPart() const
132:     {
133:        if (itsPart)
134:           return itsPart;
135:        else
136:           return NULL; //error
137:     }
138:   
139:     // **************** Part List ************
140:     class PartsList
141:     {
142:     public:
143:        PartsList();
144:        ~PartsList();
145:        // needs copy constructor and operator equals!
146:        Part*      Find(ULONG &amp; position, ULONG PartNumber)  const;
147:        ULONG      GetCount() const { return itsCount; }
148:        Part*      GetFirst() const;
149:        static     PartsList&amp; GetGlobalPartsList() 
150:      { 
151:         return  GlobalPartsList; 
152:      }
153:        void       Insert(Part *);
154:        void       Iterate(void (Part::*f)()const) const;
155:        Part*      operator[](ULONG) const;
156:     private:
157:        PartNode * pHead;
158:        ULONG itsCount;
159:        static PartsList GlobalPartsList;
160:     };
161:   
162:     PartsList PartsList::GlobalPartsList;
163:   
164:     // Implementations for Lists...
165:   
166:     PartsList::PartsList():
167:        pHead(0),
168:        itsCount(0)
169:        {}
170:   
171:     PartsList::~PartsList()
172:     {
173:        delete pHead;
174:     }
175:   
176:     Part*   PartsList::GetFirst() const
177:     {
178:        if (pHead)
179:           return pHead-&gt;GetPart();
180:        else
181:           return NULL;  // error catch here
182:     }
183:   
184:     Part *  PartsList::operator[](ULONG offSet) const
185:     {
186:        PartNode* pNode = pHead;
187:   
188:        if (!pHead)
189:           return NULL; // error catch here
190:   
191:        if (offSet &gt; itsCount)
192:           return NULL; // error
193:   
194:        for (ULONG i=0;i&lt;offSet; i++)
195:           pNode = pNode-&gt;GetNext();
196:   
197:       return   pNode-&gt;GetPart();
198:     }
199:   
200:     Part*   PartsList::Find(ULONG &amp; position, ULONG PartNumber)  const
201:     {
202:        PartNode * pNode = 0;
203:        for (pNode = pHead, position = 0;
204:              pNode!=NULL;
205:              pNode = pNode-&gt;GetNext(), position++)
206:        {
207:           if (pNode-&gt;GetPart()-&gt;GetPartNumber() == PartNumber)
208:              break;
209:        }
210:        if (pNode == NULL)
211:           return NULL;
212:        else
213:           return pNode-&gt;GetPart();
214:     }
215:   
216:     void PartsList::Iterate(void (Part::*func)()const) const
217:     {
218:        if (!pHead)
219:           return;

⌨️ 快捷键说明

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