333 Test3

Home > Preview

The flashcards below were created by user slc53 on FreezingBlue Flashcards.


  1. create a struct for a linked list of parts(partNo, desc, qty)
    • struct Part
    • {
    •     string partNo;
    •     string desc;
    •     int qty;
    •     Part * link;
    • };
  2. Write the spec file for the data members.Image Upload
    • class PartList
    • {
    • ...
    • private:
    •     Part * firstPtr;
    •     string productName;
    •     int noParts;
    • };
  3. Write spec file for:
    constructor that accepts product name
    destructor
    gets
    Image Upload
    • class PartList
    • {
    • public:
    •     PartList(string /*productName*/);
    •     ~PartList();
    •     int getNoParts() const
    •         { return noParts; }
    •     string getProductName() const
    •         { return productName; }
    • ...
  4. Write the specfile parts for showParts, addParts, removePart for Image Upload
    • class PartList
    • {
    • public:
    •     void showParts
    •         (ostream & /*out*/) const;
    •     void addPart();
    •     void addPart
    •         (string /*partNo*/, string /*desc*/,
    •         int /*qty*/);
    •     void removePart
    •         (string /*partNo*/);
  5. Write code for showParts
    • void PartList::showParts(ostream & out) const
    • {
    • out << "Product: "<< getProductName;
    • out << setw(10) << left << "Part #"
    •         << setw(15) << "Description"
    •         << setw(10) << right << "Quantity"
    •         << endl << endl;
    • Part * walker = firstPtr;
    • // no need for assert(walker!=0) since walker is not on the heap
    • // only with new, heap(dynamic)
    • while (walker != NULL)
    • {out << walker->partNo<<
    •  walker->desc<<walker->qty<<endl;
    • walker = walker->link;
    • }
    • out <<"# of Parts = " <<
    •         getNoParts() << endl;
    • }
  6. Write implementation code for PartList constructor
    • PartList::PartList
    •     (string newProductName)
    • {
    • productName = newProductName
    •      == "" ? "No Name" :
    •      newProductName;
    • noParts = 0;
    • firstPtr = NULL;
    • }
  7. Create an instance of the class dynamically passing hardcoded values for the part list.
    • int main()
    • {
    • PartList * crv =
    •         new PartList("CRV");
    • assert(crv != 0);    // remember
    • delete crv; // not sure about this
    • ...
  8. Print the product name by calling the get function.
    cout << "Product name is " << crv->getProductName()<< endl;
  9. Call the showPart function.
    crv->showParts(cout); // remember (cout)
  10. create an AnimalList object on heap using default constructor
    • AnimalList * animalPtr =
    •        new AnimalList;
    • // animalPtr is on the stack
    • //- which points to a place on the
    •     //heap
    • assert(animalPtr);
  11. add animal type and quantity to list
    animalPtr->addAnimal("tiger",50);
  12. //Add a node(animal) to the beginning of the list
    • void AnimalList::addAnimalFirst
    •      (string animal, int qty)
    • {
    • SpaceStation* builder =
    •     new SpaceStation;
    • assert(builder != 0);       builder->animal = animal.empty() ?
    •       "no name" : animal;       builder->qty = qty <0 || qty>10 ?
    •      0 : qty;
    • builder->link = firstPtr;
    • firstPtr = builder;
    • }
  13. Add a node(animal) to the end of a list
    • void AnimalList::addAnimal
    •     (string newAn, int newQty)
    • {SpaceStation * builder =
    •         new SpaceStation;
    • assert(builder != 0);
    • builder->animal = newAn=="" ?
    •      "No Name" : newAn;
    • builder->qty =
    •     (newQty <0||newQty>10)?
    •      1 : newQty;
    • builder->link = NULL;
    • if (firstPtr == NULL)
    •     {firstPtr = builder;}
    • else    
    • {  SpaceStation * walker =firstPtr;
    •     SpaceStation * stalker = NULL;
    •     while (walker!=NULL)
    •     {    stalker = walker;
    •           walker = walker->link;   }
    •     stalker->link = builder;
    • } // end else
    • }
  14. count the number of nodes in animal list
    • spec file
    • public:
    •     int countSS() const;
    • implementation file
    • int AnimalList::countSS() const
    • {  int noNodes=0;
    •     if (firstPtr != NULL)
    •     { SpaceStations * walker
    •                 =firstPtr;
    •        while (walker !=NULL)
    •        {    noNodes++;
    •              walker = walker->link;
    •         }
    •     }
    •     return noNodes;
    • }
  15. addPart in ascending order
    see week 13 and assignment
  16. write the destructor for the animal list
    • AnimalList::~AnimalList()
    • {  SpaceStation * walker = firstPtr;
    •     while (walker!=NULL)
    •     {  walker=walker->link;
    •         delete firstPtr;
    •         firstPtr=walker;
    •     }
    • }
  17. write an inline function called isEmptyList that returns true if there are no nodes
    bool isEmptyList() const {return firstPtr==NULL;}
  18. delete the first node of animal list
    • void AnimalList::deleteFirst()
    • {  if(isEmptyList())
    •        cout <<"No nodes"<<endl;
    •     else
    •     {   SpaceStation * walker
    •                 =firstPtr;
    •          firstPtr=walker->link;
    •          delete walker;
    •     }
    • }
  19. friend function that overloads << so that a call in client name
    'cout << object;'
    shows all animals on list
    • ostream & operator<<
    •      (ostream & out,
    •      const AnimalList * ptr)
    • {  SpaceStation * walker =
    •         ptr->firstPtr;
    •        //ptr is on stack but member
    •         // values are on heap
    •     int count = 0;
    •     out << "Animal" << endl;
    •     while (walker != NULL)
    •     {   out << walker->animal
    •               << endl;
    •          count++;
    •          walker = walker->link;
    •     }
    •     out << "# animals " << count
    •            << endl;
    •            //endl is very important here
    •             //endl flushes the buffer
    •     return out;
    •      //because writing to stream
    •      //building a stream and then
    •       //hand it to them
    • }
  20. write partList friend function such that
    out<<Object shows the product name and # of parts
    • spec file
    • friend ostream & operator <<
    •      (ostream & /*out*/,
    •         const PartList * /*object*/);
    • in client file---------------------------
    • ostream & operator <<
    •       (ostream & out,
    •        const PartList * ptr)
    • {   out << ptr->getProductName()
    •            << endl;
    •     out <<  ptr->getNoParts()
    •             << endl;
    •     return out;
    • }

Card Set Information

Author:
slc53
ID:
326732
Filename:
333 Test3
Updated:
2016-12-12 18:48:09
Tags:
333 Test3
Folders:

Description:
333 Test3
Show Answers:

Home > Flashcards > Print Preview