-
create a struct for a linked list of parts(partNo, desc, qty)
- struct Part
- {
- string partNo;
- string desc;
- int qty;
- Part * link;
- };
-
Write the spec file for the data members.
- class PartList
- {
- ...
- private:
- Part * firstPtr;
- string productName;
- int noParts;
- };
-
Write spec file for:
constructor that accepts product name
destructor
gets
- class PartList
- {
- public:
- PartList(string /*productName*/);
- ~PartList();
- int getNoParts() const
- { return noParts; }
- string getProductName() const
- { return productName; }
- ...
-
Write the specfile parts for showParts, addParts, removePart for
- class PartList
- {
- public:
- void showParts
- (ostream & /*out*/) const;
- void addPart();
- void addPart
- (string /*partNo*/, string /*desc*/,
- int /*qty*/);
- void removePart
- (string /*partNo*/);
-
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;
- }
-
Write implementation code for PartList constructor
- PartList::PartList
- (string newProductName)
- {
- productName = newProductName
- == "" ? "No Name" :
- newProductName;
- noParts = 0;
- firstPtr = NULL;
- }
-
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
- ...
-
Print the product name by calling the get function.
cout << "Product name is " << crv->getProductName()<< endl;
-
Call the showPart function.
crv->showParts(cout); // remember (cout)
-
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);
-
add animal type and quantity to list
animalPtr->addAnimal("tiger",50);
-
//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;
- }
-
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
- }
-
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;
- }
-
addPart in ascending order
see week 13 and assignment
-
write the destructor for the animal list
- AnimalList::~AnimalList()
- { SpaceStation * walker = firstPtr;
- while (walker!=NULL)
- { walker=walker->link;
- delete firstPtr;
- firstPtr=walker;
- }
- }
-
write an inline function called isEmptyList that returns true if there are no nodes
bool isEmptyList() const {return firstPtr==NULL;}
-
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;
- }
- }
-
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
- }
-
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;
- }
|
|