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

📄 ch21rv3.htm

📁 good book for learning c++ standard language
💻 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>
<H2 ALIGN="CENTER"><A HREF="ch20.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch20.htm"><IMG SRC="../buttons/BLANPREV.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANPREV.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="javascript:if(confirm('http://www.mcp.com/sams  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://www.mcp.com/sams'" tppabs="http://www.mcp.com/sams"><IMG
SRC="../buttons/BLANHOME.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="0"></A><A HREF="../index.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/index.htm"><IMG SRC="../buttons/BLANTOC.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><BR>
<BR>
<BR>
<A NAME="Heading1"></A><FONT COLOR="#000077">In Review</FONT></H2>
<P>The following program brings together many of the advanced techniques you've learned
during the past three weeks of hard work. Week 3 in Review provides a template-based
linked list with exception handling. Examine it in detail; if you understand it fully,
you are a C++ programmer.


<BLOCKQUOTE>
	<P>
<HR>
<FONT COLOR="#000077"><B>WARNING:</B></FONT><B> </B>If your compiler does not support
	templates, or if your compiler does not support <TT>try</TT> and <TT>catch</TT>,
	you will not be able to compile or run this listing. 
<HR>


</BLOCKQUOTE>

<P><A NAME="Heading2"></A><FONT SIZE="4" COLOR="#000077"><B>Listing R3.1. Week 3
in Review listing.</B></FONT>
<PRE><FONT COLOR="#0066FF">0:   // **************************************************
1:   //
2:   // Title:     Week 3 in Review
3:   //
4:   // File:      Week3
5:   //
6:   // Description:   Provide a template-based linked list
7:   //                demonstration program with exception handling
8:   //
9:   // Classes:    PART - holds part numbers and potentially other
10:   //                      information about parts. This will be the
11:   //                      example class for the list to hold
12:   //                      Note use of operator&lt;&lt; to print the
13:   //                      information about a part based on its
14:   //                      runtime type.
15:   //
16:   //               Node - acts as a node in a List
17:   //
18:   //               List - template-based list which provides the
19:   //                      mechanisms for a linked list
20:   //
21:   //
22:   // Author:    Jesse Liberty (jl)
23:   //
24:   // Developed:   Pentium 200 Pro. 128MB RAM MVC 5.0
25:   //
26:   // Target:    Platform independent
27:   //
28:   // Rev History:  9/94 - First release (jl)
29:   //               4/97 - Updated (jl)
30:   // **************************************************
31: 
32:   #include &lt;iostream.h&gt;
33: 
34:   // exception classes
35:   class Exception {};
36:   class OutOfMemory :   public Exception{};
37:   class NullNode :    public Exception{};
38:   class EmptyList :   public Exception {};
39:   class BoundsError :   public Exception {};
40: 
41: 
42:   // **************** Part ************
43:   // Abstract base class of parts
44:   class Part
45:   {
46:   public:
47:    Part():itsObjectNumber(1) {}
48:    Part(int ObjectNumber):itsObjectNumber(ObjectNumber){}
49:    virtual ~Part(){};
50:    int GetObjectNumber() const { return itsObjectNumber; }
51:    virtual void Display() const =0;  // must be overridden
52: 
53:   private:
54:    int itsObjectNumber;
55:   };
56: 
57:   // implementation of pure virtual function so that
58:   // derived classes can chain up
59:   void Part::Display() const
60:   {
61:     cout &lt;&lt; &quot;\nPart Number: &quot; &lt;&lt; itsObjectNumber &lt;&lt; endl;
62:   }
63: 
64:   // this one operator&lt;&lt; will be called for all part objects.
65:   // It need not be a friend as it does not access private data
66:   // It calls Display() which uses the required polymorphism
67:   // We'd like to be able to override this based on the real type
68:   // of thePart, but C++ does not support contravariance
69:   ostream&amp; operator&lt;&lt;( ostream&amp; theStream,Part&amp; thePart)
70:   {
71:    thePart.Display();  // virtual contravariance!
72:    return theStream;
73:   }
74: 
75:   // **************** Car Part ************
76:   class CarPart : public Part
77:   {
78:   public:
79:    CarPart():itsModelYear(94){}
80:    CarPart(int year, int partNumber);
81:    int GetModelYear() const { return itsModelYear; }
82:    virtual void Display() const;
83:   private:
84:    int itsModelYear;
85:   };
86: 
87:   CarPart::CarPart(int year, int partNumber):
88:    itsModelYear(year),
89:    Part(partNumber)
90:   {}
91: 
92:   void CarPart::Display() const
93:   {
94:       Part::Display();
95:       cout &lt;&lt; &quot;Model Year: &quot; &lt;&lt; itsModelYear &lt;&lt; endl;
96:   }
97: 
98:   // **************** AirPlane Part ************
99:   class AirPlanePart : public Part
100:   {
101:   public:
102:    AirPlanePart():itsEngineNumber(1){};
103:    AirPlanePart(int EngineNumber, int PartNumber);
104:    virtual void Display() const;
105:    int GetEngineNumber()const { return itsEngineNumber; }
106:   private:
107:    int itsEngineNumber;
108:   };
109:
110:   AirPlanePart::AirPlanePart(int EngineNumber, int PartNumber):
111:    itsEngineNumber(EngineNumber),
112:    Part(PartNumber)
113:   {}
114:
115:   void AirPlanePart::Display() const
116:   {
117:    Part::Display();
118:    cout &lt;&lt; &quot;Engine No.: &quot; &lt;&lt; itsEngineNumber &lt;&lt; endl;
119:   }
120:
121:   // forward declaration of class List
122:   template &lt;class T&gt;
123:   class List;
124:
125:   // ****************  Node ************
126:   // Generic node, can be added to a list
127:   // ************************************
128:
129:   template &lt;class T&gt;
130:   class Node
131:   {
132:   public:
133:    friend class List&lt;T&gt;;
134:    Node (T*);
135:    ~Node();
136:    void SetNext(Node * node) { itsNext = node; }
137:    Node * GetNext() const;
138:    T * GetObject() const;
139:   private:
140:    T* itsObject;
141:    Node * itsNext;
142:   };
143:
144:   // Node Implementations...
145:
146:   template &lt;class T&gt;
147:   Node&lt;T&gt;::Node(T* pOjbect):
148:   itsObject(pOjbect),
149:   itsNext(0)
150:   {}
151:
152:   template &lt;class T&gt;
153:   Node&lt;T&gt;::~Node()
154:   {
155:    delete itsObject;
156:    itsObject = 0;
157:    delete itsNext;
158:    itsNext = 0;
159:   }
160:
161:   // Returns NULL if no next Node
162:   template &lt;class T&gt;
163:   Node&lt;T&gt; * Node&lt;T&gt;::GetNext() const
164:   {
165:       return itsNext;
166:   }
167:
168:   template &lt;class T&gt;
169:   T * Node&lt;T&gt;::GetObject() const
170:   {
171:    if (itsObject)
172:       return itsObject;
173:    else
174:       throw NullNode();
175:   }
176:
177:   // ****************  List ************
178:   // Generic list template
179:   // Works with any numbered object
180:   // ***********************************
181:   template &lt;class T&gt;
182:   class List
183:   {
184:   public:
185:    List();
186:    ~List();
187:
188:    T*        Find(int &amp; position, int ObjectNumber)  const;
189:    T*      GetFirst() const;
190:    void      Insert(T *);
191:    T*      operator[](int) const;
192:    int      GetCount() const { return itsCount; }
193:   private:
194:    Node&lt;T&gt; * pHead;
195:    int      itsCount;
196:   };
197:
198:   // Implementations for Lists...
199:   template &lt;class T&gt;
200:   List&lt;T&gt;::List():
201:    pHead(0),
202:    itsCount(0)
203:    {}
204:
205:   template &lt;class T&gt;
206:   List&lt;T&gt;::~List()
207:   {
208:    delete pHead;
209:   }
210:
211:   template &lt;class T&gt;
212:   T*   List&lt;T&gt;::GetFirst() const
213:   {
214:    if (pHead)
215:       return pHead-&gt;itsObject;
216:    else
217:       throw EmptyList();
218:   }
219:
220:   template &lt;class T&gt;

⌨️ 快捷键说明

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