📄 ch21rv3.htm
字号:
<!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://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch20.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"><IMGSRC="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><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 learnedduring the past three weeks of hard work. Week 3 in Review provides a template-basedlinked 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 3in Review listing.</B></FONT><PRE><FONT COLOR="#0066FF">0: // **************************************************1: //2: // Title: Week 3 in Review3: //4: // File: Week35: //6: // Description: Provide a template-based linked list7: // demonstration program with exception handling8: //9: // Classes: PART - holds part numbers and potentially other10: // information about parts. This will be the11: // example class for the list to hold12: // Note use of operator<< to print the13: // information about a part based on its14: // runtime type.15: //16: // Node - acts as a node in a List17: //18: // List - template-based list which provides the19: // mechanisms for a linked list20: //21: //22: // Author: Jesse Liberty (jl)23: //24: // Developed: Pentium 200 Pro. 128MB RAM MVC 5.025: //26: // Target: Platform independent27: //28: // Rev History: 9/94 - First release (jl)29: // 4/97 - Updated (jl)30: // **************************************************31: 32: #include <iostream.h>33: 34: // exception classes35: 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 parts44: class Part45: {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 overridden52: 53: private:54: int itsObjectNumber;55: };56: 57: // implementation of pure virtual function so that58: // derived classes can chain up59: void Part::Display() const60: {61: cout << "\nPart Number: " << itsObjectNumber << endl;62: }63: 64: // this one operator<< will be called for all part objects.65: // It need not be a friend as it does not access private data66: // It calls Display() which uses the required polymorphism67: // We'd like to be able to override this based on the real type68: // of thePart, but C++ does not support contravariance69: ostream& operator<<( ostream& theStream,Part& thePart)70: {71: thePart.Display(); // virtual contravariance!72: return theStream;73: }74: 75: // **************** Car Part ************76: class CarPart : public Part77: {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() const93: {94: Part::Display();95: cout << "Model Year: " << itsModelYear << endl;96: }97: 98: // **************** AirPlane Part ************99: class AirPlanePart : public Part100: {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() const116: {117: Part::Display();118: cout << "Engine No.: " << itsEngineNumber << endl;119: }120:121: // forward declaration of class List122: template <class T>123: class List;124:125: // **************** Node ************126: // Generic node, can be added to a list127: // ************************************128:129: template <class T>130: class Node131: {132: public:133: friend class List<T>;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 <class T>147: Node<T>::Node(T* pOjbect):148: itsObject(pOjbect),149: itsNext(0)150: {}151:152: template <class T>153: Node<T>::~Node()154: {155: delete itsObject;156: itsObject = 0;157: delete itsNext;158: itsNext = 0;159: }160:161: // Returns NULL if no next Node162: template <class T>163: Node<T> * Node<T>::GetNext() const164: {165: return itsNext;166: }167:168: template <class T>169: T * Node<T>::GetObject() const170: {171: if (itsObject)172: return itsObject;173: else174: throw NullNode();175: }176:177: // **************** List ************178: // Generic list template179: // Works with any numbered object180: // ***********************************181: template <class T>182: class List183: {184: public:185: List();186: ~List();187:188: T* Find(int & 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<T> * pHead;195: int itsCount;196: };197:198: // Implementations for Lists...199: template <class T>200: List<T>::List():201: pHead(0),202: itsCount(0)203: {}204:205: template <class T>206: List<T>::~List()207: {208: delete pHead;209: }210:211: template <class T>212: T* List<T>::GetFirst() const213: {214: if (pHead)215: return pHead->itsObject;216: else217: throw EmptyList();218: }219:220: template <class T>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -