//: C09:Recycle4.cpp // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt //{L} TrashPrototypeInit //{L} fillBin Trash TrashStatics // Adding TrashBins and TrashSorters #include "Trash.h" #include "Aluminum.h" #include "Paper.h" #include "Glass.h" #include "Cardboard.h" #include "fillBin.h" #include "sumValue.h" #include "../purge.h" #include #include using namespace std; ofstream out("Recycle4.out"); class TBin : public vector { public: virtual bool grab(Trash*) = 0; }; template class TrashBin : public TBin { public: bool grab(Trash* t) { TrashType* tp = dynamic_cast(t); if(!tp) return false; // Not grabbed push_back(tp); return true; // Object grabbed } }; class TrashSorter : public vector { public: bool sort(Trash* t) { for(iterator it = begin(); it != end(); it++) if((*it)->grab(t)) return true; return false; } void sortBin(vector& bin) { vector::iterator it; for(it = bin.begin(); it != bin.end(); it++) if(!sort(*it)) cerr << "bin not found" << endl; } ~TrashSorter() { purge(*this); } }; int main() { vector bin; // Fill up the Trash bin: fillBin("Trash.dat", bin); TrashSorter tbins; tbins.push_back(new TrashBin); tbins.push_back(new TrashBin); tbins.push_back(new TrashBin); tbins.push_back(new TrashBin); tbins.sortBin(bin); for(TrashSorter::iterator it = tbins.begin(); it != tbins.end(); it++) sumValue(**it); sumValue(bin); purge(bin); } ///:~