// *******************************************************************
//
// 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
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 List;
// **************** Node **************
// Generic node, can be added to a list
// ************************************
template
class Node
{
public:
friend class List
Node (T*);
~Node();
void SetNext(Node * node) { itsNext = node; }
Node * GetNext() const;
T * GetObject() const;
private:
T* itsObject;
Node * itsNext;
};
// Node Implementations...
template
Node
itsObject(pObject),
itsNext(0)
{}
template
Node
{
delete itsObject;
itsObject = 0;
delete itsNext;
it