Advertisement
GbossMega9

DtoallcppFileV3

Feb 14th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.55 KB | None | 0 0
  1. using namespace std;
  2.  
  3. # include <iostream>
  4. # include <14_5hSTATIC2.hpp>
  5.  
  6. // implementation of pure virtual function so that
  7. // derived classes can chain up
  8.  
  9.  
  10. ULONG&Part::Display(ULONG*)
  11. {
  12.     cout << "Display function called.\n\n";
  13.     cout << "\nPart Number: " << &Part::itsPartNumber << endl;
  14. }
  15.  
  16. CarPart::CarPart(USHORT year, ULONG PartNumber):
  17.     itsModelYear(year),
  18.     Part(PartNumber)
  19.     { cout << "Derived class copy constructor called.\n" ;
  20.       cout << "Derived class PartNumber = " << PartNumber << "\n\n" ;  }
  21.    
  22. AirPlanePart::AirPlanePart(USHORT EngineNumber, ULONG PartNumber):
  23.     itsEngineNumber(EngineNumber),
  24.     Part(PartNumber)
  25.     {}
  26.         // PartNode Implementations...
  27.        
  28.         PartNode::PartNode(Part* pPart):
  29.         itsPart(pPart),
  30.         itsNext(0)
  31.         {}
  32.        
  33.         PartNode::~PartNode()
  34.         {
  35.             delete itsPart;
  36.             itsPart = 0;
  37.             delete itsNext;
  38.             itsNext = 0;
  39.         }
  40.        
  41.         // Returns NULL if no next PartNode
  42.         PartNode * PartNode::GetNext() const
  43.         {
  44.             return itsNext;
  45.         }
  46.            
  47.        
  48.         Part * PartNode::GetPart() const
  49.         {
  50.             if (itsPart)
  51.                 return itsPart;
  52.             else
  53.                 return NULL;    //error
  54.             }
  55.            
  56.            
  57.                
  58.         PartsList PartsList::GlobalPartsList;
  59.        
  60.        
  61.         // Implementation for Lists....
  62.        
  63.         PartsList::PartsList():
  64.         pHead(0),
  65.         itsCount(0)
  66.         {}
  67.        
  68.        
  69.         PartsList::~PartsList()
  70.         {
  71.             delete pHead;
  72.         }
  73.        
  74.         Part* PartsList::GetFirst() const
  75.         {
  76.             if (pHead)
  77.                 return pHead->GetPart();
  78.             else
  79.                 return NULL; // error catch here
  80.         }
  81.        
  82.         Part * PartsList::operator[](ULONG offset) const
  83.         {
  84.             PartNode* pNode = pHead;
  85.            
  86.             if(!pHead)
  87.                 return NULL; // error catch here
  88.            
  89.             if (offset > itsCount)
  90.                 return NULL; //error
  91.            
  92.             for (ULONG i=0;i<offset; i++)
  93.                 pNode->GetNext();
  94.            
  95.             return pNode->GetPart();
  96.         }
  97.        
  98.         Part* PartsList::Find(ULONG & position, ULONG PartNumber) const
  99.         {
  100.             PartNode * pNode = 0;
  101.         for (pNode = pHead, position = 0;
  102.             pNode!=NULL;
  103.             pNode = pNode->GetNext(),position++)
  104.         {
  105.         if (pNode->GetPart() ->GetPartNumber() == PartNumber)
  106.             break;
  107.             }
  108.         if (pNode == NULL)
  109.             return NULL;
  110.         else
  111.             return pNode ->GetPart();
  112.         }
  113.        
  114.         void PartsList::Iterate(ULONG& (*)(ULONG*))
  115.         {
  116.             if (!pHead)
  117.                 return;
  118.             PartNode* pNode = pHead;
  119.             do
  120.                 (pNode->GetPart());
  121.                
  122.  
  123.             while (pNode = pNode->GetNext());
  124.         }
  125.        
  126.         void PartsList::Insert(Part *pPart)
  127.         {
  128.             PartNode * pNode = new PartNode(pPart);
  129.             PartNode * pCurrent = pHead;
  130.             PartNode * pNext = 0;
  131.            
  132.             ULONG New = pPart->GetPartNumber();
  133.             ULONG Next = 0;
  134.             itsCount++;
  135.            
  136.             if (!pHead)
  137.             {
  138.                 pHead = pNode;
  139.                     return;
  140.             }
  141.            
  142.             // if this one is smaller than head
  143.             // this one is the new head
  144.             if (pHead->GetPart() ->GetPartNumber() > New)
  145.             {
  146.                 pNode->SetNext(pHead);
  147.                 pHead = pNode;
  148.                 return;
  149.             }
  150.            
  151.  
  152. for (;;)
  153. {
  154. // if there is no next, append this new one
  155. if (!pCurrent->GetNext())
  156. {
  157.     pCurrent->SetNext(pNode);
  158.     return;
  159.     }
  160.    
  161.     // if this goes after this one and before the next
  162.     // then insert it here, otherwise get the next
  163.     pNext = pCurrent->GetNext();
  164.     Next = pNext->GetPart()->GetPartNumber();
  165.     if (Next > New)
  166.     {
  167.         pCurrent->SetNext(pNode);
  168.         pNode->SetNext(pNext);
  169.         return;
  170.         }
  171.         pCurrent = pNext;
  172.         }
  173. }
  174.        
  175.        
  176.        
  177.             void PartsCatalog::Insert(Part * newPart)
  178.             {
  179.                 ULONG
  180.                 PartNumber = newPart->GetPartNumber();
  181.                 ULONG offset;
  182.                
  183.                 if (!thePartsList.Find(offset, PartNumber))
  184.                 thePartsList.Insert(newPart);
  185.                 else
  186.         {
  187.             cout << PartNumber << " was the ";
  188.             switch (offset)
  189.             {
  190.                 case 0:  cout << "first "; break;
  191.                 case 1:  cout << "second "; break;
  192.                 case 2:  cout << "third "; break;
  193.                 default: cout << offset+1 << "th ";
  194.             }
  195.             cout << "entry. Rejected!\n";
  196.         }
  197.     }
  198.        
  199.         ULONG PartsCatalog::Exists(ULONG PartNumber)
  200.         {
  201.             ULONG offset;
  202.             thePartsList.Find(offset,PartNumber);
  203.             return offset;
  204.         }
  205.        
  206.         Part * PartsCatalog::Get(ULONG PartNumber)
  207.         {
  208.             ULONG offset;
  209.             Part * thePart = thePartsList.Find(offset, PartNumber);
  210.             return thePart;
  211.         }
  212.        
  213.         void startof()
  214.         {
  215.             PartsCatalog pc;
  216.             Part * pPart = 0;
  217.             ULONG PartNumber;
  218.             USHORT value;
  219.             ULONG choice;
  220.            
  221.             while (1)
  222.             {
  223.                 cout << "(0)Quit (1)Car (2)Plane: ";
  224.                 cout << "\n\n Base class current Part No: " << &Part::GetPartNumber << endl;
  225.                 cout << "\n";
  226.                 cin >> choice;
  227.                
  228.                 if (!choice)
  229.                 break;
  230.                
  231.                 cout << "New part Number?: ";
  232.                 cin >> PartNumber;
  233.                
  234.                 if (choice == 1)
  235.                 {
  236.                     cout << "ModelYear?:";
  237.                     cin >> value;
  238.                     cout << "\n";
  239.                     pPart = new CarPart(value,PartNumber);
  240.                 }
  241.                 else
  242.                 {
  243.             cout << "Engine Number?: ";
  244.             cin >> value;
  245.             pPart = new AirPlanePart(value,PartNumber);
  246.         }
  247.         pc.Insert(pPart);
  248.     }
  249.         pc.ShowAll();
  250.     }
  251.    
  252.     int main()
  253.     {
  254.         startof();
  255.         return 0;
  256.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement