Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- # include <iostream>
- # include <14_5hSTATIC2.hpp>
- // implementation of pure virtual function so that
- // derived classes can chain up
- ULONG&Part::Display(ULONG*)
- {
- cout << "Display function called.\n\n";
- cout << "\nPart Number: " << &Part::itsPartNumber << endl;
- }
- CarPart::CarPart(USHORT year, ULONG PartNumber):
- itsModelYear(year),
- Part(PartNumber)
- { cout << "Derived class copy constructor called.\n" ;
- cout << "Derived class PartNumber = " << PartNumber << "\n\n" ; }
- AirPlanePart::AirPlanePart(USHORT EngineNumber, ULONG PartNumber):
- itsEngineNumber(EngineNumber),
- Part(PartNumber)
- {}
- // 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
- }
- PartsList PartsList::GlobalPartsList;
- // Implementation 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->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(ULONG& (*)(ULONG*))
- {
- if (!pHead)
- return;
- PartNode* pNode = pHead;
- do
- (pNode->GetPart());
- 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;
- }
- }
- void PartsCatalog::Insert(Part * newPart)
- {
- ULONG
- PartNumber = newPart->GetPartNumber();
- ULONG offset;
- if (!thePartsList.Find(offset, PartNumber))
- thePartsList.Insert(newPart);
- else
- {
- cout << PartNumber << " was the ";
- switch (offset)
- {
- case 0: cout << "first "; break;
- case 1: cout << "second "; break;
- case 2: cout << "third "; break;
- default: cout << offset+1 << "th ";
- }
- cout << "entry. Rejected!\n";
- }
- }
- ULONG PartsCatalog::Exists(ULONG PartNumber)
- {
- ULONG offset;
- thePartsList.Find(offset,PartNumber);
- return offset;
- }
- Part * PartsCatalog::Get(ULONG PartNumber)
- {
- ULONG offset;
- Part * thePart = thePartsList.Find(offset, PartNumber);
- return thePart;
- }
- void startof()
- {
- PartsCatalog pc;
- Part * pPart = 0;
- ULONG PartNumber;
- USHORT value;
- ULONG choice;
- while (1)
- {
- cout << "(0)Quit (1)Car (2)Plane: ";
- cout << "\n\n Base class current Part No: " << &Part::GetPartNumber << endl;
- cout << "\n";
- cin >> choice;
- if (!choice)
- break;
- cout << "New part Number?: ";
- cin >> PartNumber;
- if (choice == 1)
- {
- cout << "ModelYear?:";
- cin >> value;
- cout << "\n";
- pPart = new CarPart(value,PartNumber);
- }
- else
- {
- cout << "Engine Number?: ";
- cin >> value;
- pPart = new AirPlanePart(value,PartNumber);
- }
- pc.Insert(pPart);
- }
- pc.ShowAll();
- }
- int main()
- {
- startof();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement