1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
#include <iostream>
typedef unsigned long ULONG;
typedef unsigned short USHORT;
// ********* Part *********
// Abstract base of parts
class Part
{
public:
Part():itsPartNumber(1){}
Part(ULONG PartNumber):itsPartNumber(PartNumber){}
virtual ~Part(){};
ULONG GetPartNumber() const { return itsPartNumber; }
virtual void Display() const =0; // must be overidden
private:
ULONG itsPartNumber;
};
// implementation of pure virtual function so that
// derived classes can chain up
void Part::Display() const
{
std::cout<<"\nPart Number: " << itsPartNumber << std::endl;
}
// ******* Car Part ********
class CarPart : public Part
{
public:
CarPart():itsModelYear(94){}
CarPart(USHORT year, ULONG partNumber);
virtual void Display() const { Part::Display(); std::cout <<"Model Year: " << itsModelYear << std::endl; }
private:
USHORT itsModelYear;
};
CarPart::CarPart(USHORT year, ULONG partNumber):
itsModelYear(year),
Part(partNumber)
{}
// ********* Airplane Part *********
class AirPlanePart : public Part
{
public:
AirPlanePart():itsEngineNumber(1){};
AirPlanePart(USHORT EngineNumber, ULONG PartNumber);
virtual void Display() const { Part::Display(); std::cout <<"Engine No.."<< itsEngineNumber<< std::endl;}
private:
USHORT itsEngineNumber;
};
AirPlanePart::AirPlanePart(USHORT EngineNumber, ULONG PartNumber):
itsEngineNumber(EngineNumber),
Part(PartNumber)
{}
// ******** Part Node **********
class PartNode
{
public:
PartNode (Part*);
~PartNode();
void SetNext(PartNode * node) { itsNext = node; }
PartNode * GetNext() const;
Part * GetPart() const;
private:
Part *itsPart;
PartNode * itsNext;
};
// PartNode Implementations...
PartNode::PartNode(Part* pPart):
itsPart(pPart),
itsNext(0)
{}
PartNode::~PartNode()
{
delete itsPart;
itsPart = 0;
delete itsNext;
itsNext = 0;
}
//Returns NULL if no next PartNode
PartNode * PartNode::GetNext()const
{
return itsNext;
}
Part * PartNode::GetPart()const
{
if (itsPart)
return itsPart;
else
return NULL; // error
}
//******** Part List **********
class PartsList
{
public:
PartsList();
~PartsList();
// needs copy constructor and operator equals!
void Iterate(void(Part::*f)()const) const;
Part * Find(ULONG & position, ULONG PartNumber) const;
Part* GetFirst() const;
void Insert(Part*);
Part* operator[] (ULONG) const;
ULONG GetCount() const { return itsCount;}
static PartsList& GetGlobalPartsList() { return GlobalPartsList;}
private:
PartNode * pHead;
ULONG itsCount;
static PartsList GlobalPartsList;
};
PartsList PartsList::GlobalPartsList;
// Implementations for Lists...
PartsList::PartsList():
pHead(0),
itsCount(0)
{}
PartsList::~PartsList()
{
delete pHead;
}
Part* PartsList::GetFirst() const
{
if (pHead)
return pHead->GetPart();
else
return NULL; // error catch here
}
Part * PartsList::operator[](ULONG offSet) const
{
PartNode * pNode = pHead;
if (!pHead)
return NULL; // error catch here
if (offSet > itsCount)
return NULL; // error
for (ULONG i =0; i<offSet; i++)
pNode = pNode->GetNext();
return pNode->GetPart();
}
Part * PartsList::Find(ULONG & position, ULONG PartNumber) const
{
PartNode * pNode = 0;
for (pNode= pHead, position = 0;
pNode!=NULL;
pNode = pNode->GetNext(), position++)
{
if (pNode->GetPart()->GetPartNumber() == PartNumber)
break;
}
if(pNode == NULL)
return NULL;
else
return
pNode->GetPart();
}
void PartsList::Iterate(void (Part::*func)()const) const
{
if(!pHead)
return;
PartNode* pNode = pHead;
do
(pNode->GetPart()->*func)();
while (pNode = pNode->GetNext());
}
void PartsList::Insert(Part * pPart)
{
PartNode * pNode = new PartNode(pPart);
PartNode * pCurrent = pHead;
PartNode * pNext = 0;
ULONG New = pPart->GetPartNumber();
ULONG Next = 0;
itsCount++;
if (!pHead)
{
pHead = pNode;
return;
}
//If this one is smaller than head
//this one is the new head
if (pHead->GetPart()->GetPartNumber() > New)
{
pNode->SetNext(pHead);
pHead = pNode;
return;
}
for (;;)
{
// If there is no next, append this new one
if (!pCurrent->GetNext())
{
pCurrent->SetNext(pNode);
return;
}
// If this goes after this one and before the next
// then insert it here, otherwise get the next
pNext = pCurrent->GetNext();
Next = pNext->GetPart()->GetPartNumber();
if (Next > New)
{
pCurrent->SetNext(pNode);
pNode->SetNext(pNext);
return;
}
pCurrent = pNext;
}
}
class PartsCatalog
{
public:
void Insert(Part *);
ULONG Exists(ULONG PartNumber);
Part * Get(int PartNumber);
operator=(const PartsCatalog &);
void ShowAll() { thePartsList.Iterate(&Part::Display); }
private:
PartsList thePartsList;
};
void PartsCatalog::Insert(Part * newPart)
{
ULONG partNumber = newPart->GetPartNumber();
ULONG offset;
if (!thePartsList.Find(offset, partNumber))
thePartsList.Insert(newPart);
else
{
std::cout << partNumber << " was the ";
switch (offset)
{
case 0: std::cout << "first " ; break;
case 1: std::cout << "second " ;break;
case 2: std::cout << "third " ; break;
default: std::cout << offset+1 << "th ";
}
std::cout <<"entry. Rejected!\n";
}
}
ULONG PartsCatalog::Exists(ULONG PartNumber)
{
ULONG offset;
thePartsList.Find(offset,PartNumber);
return offset;
}
Part * PartsCatalog::Get(int PartNumber)
{
ULONG offset;
Part * thePart = thePartsList.Find(offset, PartNumber);
return thePart;
}
int main()
{
PartsCatalog pc;
Part * pPart = 0;
ULONG PartNumber;
USHORT value;
ULONG choice;
while(1)
{
std::cout<< "(0)Quit (1)Car (2)Plane: ";
std::cin>> choice;
if(!choice)
break;
std::cout <<"New Part Number?: ";
std::cin >> PartNumber;
if(choice == 1)
{
std::cout<<"Model Year?:" ;
std::cin>> value;
pPart = new CarPart(value, PartNumber);
}
else
{
std::cout <<"Engine Number?: ";
std::cin>>value;
pPart = new AirPlanePart(value, PartNumber);
}
pc.Insert(pPart);
}
pc.ShowAll();
}
|