《21天学通C++》第三周复习清单
// *******************************************************************
//
// Title:Week 3 in Review
//
// File:Week3
//
// Description:Provide a template-based linked list
//demonstration program with exception handling
//
// Classes:PART - holds part numbers and potentially other
//information about parts, This will be the
//example class for the list to hold
//Note use of operator<< to print the
//information about a part based on its
//runtime type.
//
//Node - acts as a node in a list
//
//List - template-based list which provides the
//mechanisms for a linked list
//
//
// Author:Jesse Liberty (jl)
//
// Developed:Pentium 200 Pro. 128MB RAM MVC 5.0
//
// Target:Platform independent
//
// Rev History:9/94 - First release (jl)
//4/97 - Updated (jl)
// *******************************************************************
#include <iostream>
using namespace std;
// exception classes
class Exception {};
class OutOfMemory :public Exception{};
class NullNode :public Exception{};
class EmptyList :public Exception{};
class BoundsError :public Exception{};
// **************** Part ************
// Abstract base class of parts
class Part
{
public:
Part():itsObjectNumber(1) {}
Part(int ObjectNumber):itsObjectNumber(ObjectNumber){}
virtual ~Part(){};
int GetObjectNumber() const { return itsObjectNumber; }
virtual void Display() const =0;// must be overridden
private:
int itsObjectNumber;
};
// implementation of pure virtual function so that
// derived classes can chain up
void Part::Display() const
{
cout << " Part Number: " << itsObjectNumber << endl;
}
// this one operator<< will be called for all part objects.
// It need not be a friend as it does not access private data
// It calls Display() which uses the required polymorphism
// Wed like to be able to override this based on the real type
// of thePart, but C++ does not support contravariance
ostream& operator<<( ostream& theStream,Part& thePart)
{
thePart.Display();// virtual contravariance!
return theStream;
}
// **************** Car Part ************
class CarPart : public Part
{
public:
CarPart():itsModelYear(94){}
CarPart(int year, int partNumber);
int GetModelYear() const { return itsModelYear; }
virtual void Display() const;
private:
int itsModelYear;
};
CarPart::CarPart(int year, int partNumber):
itsModelYear(year),
Part(partNumber)
{}
void CarPart::Display() const
{
Part::Display();
cout << "Model Year: " << itsModelYear << endl;
}
// **************** AirPlane Part ************
class AirPlanePart : public Part
{
public:
AirPlanePart():itsEngineNumber(1){};
AirPlanePart(int EngineNumber, int PartNumber);
virtual void Display() const;
int GetEngineNumber() const { return itsEngineNumber; }
private:
int itsEngineNumber;
};
AirPlanePart::AirPlanePart(int EngineNumber, int PartNumber):
itsEngineNumber(EngineNumber),
Part(PartNumber)
{}
void AirPlanePart::Display() const
{
Part::Display();
cout << "Engine No.: " << itsEngineNumber << endl;
}
// forward declaration of class List
template <class T>
class List;
// **************** Node **************
// Generic node, can be added to a list
// ************************************
template <class T>
class Node
{
public:
friend class List<T>;
Node (T*);
~Node();
void SetNext(Node * node) { itsNext = node; }
Node * GetNext() const;
T * GetObject() const;
private:
T* itsObject;
Node * itsNext;
};
// Node Implementations...
template <class T>
Node<T>::Node(T* pObject):
itsObject(pObject),
itsNext(0)
{}
template <class T>
Node<T>::~Node()
{
delete itsObject;
itsObject = 0;
delete itsNext;
itsNext = 0;
}
// Returns NULL if no next Node
template <class T>
Node<T> * Node<T>::GetNext() const
{
return itsNext;
}
template <class T>
T * Node<T>::GetObject() const
{
if (itsObject)
return itsObject;
else
throw NullNode();
}
// **************** List ************
// Generic list template
// Works with any numbered object
// **********************************
template <class T>
class List
{
public:
List();
~List();
T* Find(int & position, int ObjectNumber) const;
T*GetFirst() const;
voidInsert(T *);
T*operator[](int) const;
intGetCount() const { return itsCount; }
private:
Node<T> * pHead;
int itsCount;
};
// Implementations for Lists...
template <class T>
List<T>::List():
pHead(0),
itsCount(0)
{}
template <class T>
List<T>::~List()
{
delete pHead;
}
template <class T>
T*List<T>::GetFirst() const
{
if (pHead)
return pHead->itsObject;
else
throw EmptyList();
}
template <class T>
T*List<T>::operator[](int offset) const
{
Node<T>* pNode = pHead;
if (!pHead)
throw EmptyList();
if (offset > itsCount)
throw BoundsError();
for (int i=0;i<offset;i++)
pNode = pNode->itsNext;
return pNode->itsObject;
}
// find a given object in list based on its unique number (id)
template <class T>
T*List<T>::Find(int & position, int ObjectNumber) const
{
Node<T> * pNode = 0;
for (pNode = pHead, position = 0;
pNode!=NULL;
pNode = pNode->itsNext, position++)
{
if (pNode->itsObject->GetObjectNumber() == ObjectNumber)
break;
}
if (pNode == NULL)
return NULL;
else
return pNode->itsObject;
}
// insert if the number of the object is unique
template <class T>
void List<T>::Insert(T* pObject)
{
Node<T> * pNode = new Node<T>(pObject);
Node<T> * pCurrent = pHead;
Node<T> * pNext = 0;
int New = pObject->GetObjectNumber();
int Next = 0;
itsCount++;
if (!pHead)
{
pHead = pNode;
return;
}
<补充:软件开发 , C++ ,