home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / CXX / Bicycle.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.6 KB  |  65 lines

  1. //: CXX:Bicycle.cpp {O}
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Bicycle implementation
  7. #include "Bicycle.h"
  8. #include <map>
  9. #include <algorithm>
  10. #include <cassert>
  11. using namespace std;
  12.  
  13. // Static member definitions:
  14. LeakChecker BicyclePart::lc;
  15. int Bicycle::counter = 0;
  16.  
  17. Bicycle::Bicycle() : id(counter++) {
  18.   BicyclePart *bp[] = {
  19.     new Part<Frame>, 
  20.     new Part<Wheel>, new Part<Wheel>, 
  21.     new Part<Seat>, new Part<HandleBar>,
  22.     new Part<Sprocket>,  new Part<Deraileur>,
  23.   };
  24.   const int bplen = sizeof bp / sizeof *bp;
  25.   parts = VBP(bp, bp + bplen);
  26. }
  27.  
  28. Bicycle::Bicycle(const Bicycle& old) 
  29.   : parts(old.parts.begin(), old.parts.end()) {
  30.   for(int i = 0; i < parts.size(); i++)
  31.     parts[i] = parts[i]->clone();
  32. }
  33.  
  34. Bicycle& Bicycle::operator=(const Bicycle& old) {
  35.   purge(); // Remove old lvalues
  36.   parts.resize(old.parts.size());
  37.   copy(old.parts.begin(), 
  38.     old.parts.end(), parts.begin());
  39.   for(int i = 0; i < parts.size(); i++)
  40.     parts[i] = parts[i]->clone();
  41.   return *this;
  42. }
  43.  
  44. void Bicycle::purge() {
  45.   for(VBP::iterator it = parts.begin();
  46.     it != parts.end(); it++) {
  47.       delete *it;
  48.       *it = 0; // Prevent multiple deletes
  49.   }
  50. }
  51.  
  52. ostream& operator<<(ostream& os, Bicycle* b) {
  53.   copy(b->parts.begin(), b->parts.end(),
  54.     ostream_iterator<BicyclePart*>(os, "\n"));
  55.   os << "--------" << endl;
  56.   return os;
  57. }
  58.  
  59. void Bicycle::print(vector<Bicycle*>& vb, 
  60.   ostream& os) {
  61.   copy(vb.begin(), vb.end(),
  62.     ostream_iterator<Bicycle*>(os, "\n"));
  63.   cout << "--------" << endl;
  64. } ///:~
  65.