//: C09:Bicycle.cpp {O} // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt // Bicycle implementation #include "Bicycle.h" #include #include #include using namespace std; // Static member definitions: LeakChecker BicyclePart::lc; int Bicycle::counter = 0; Bicycle::Bicycle() : id(counter++) { BicyclePart *bp[] = { new Part, new Part, new Part, new Part, new Part, new Part, new Part, }; const int bplen = sizeof bp / sizeof *bp; parts = VBP(bp, bp + bplen); } Bicycle::Bicycle(const Bicycle& old) : parts(old.parts.begin(), old.parts.end()) { for(int i = 0; i < parts.size(); i++) parts[i] = parts[i]->clone(); } Bicycle& Bicycle::operator=(const Bicycle& old) { purge(); // Remove old lvalues parts.resize(old.parts.size()); copy(old.parts.begin(), old.parts.end(), parts.begin()); for(int i = 0; i < parts.size(); i++) parts[i] = parts[i]->clone(); return *this; } void Bicycle::purge() { for(VBP::iterator it = parts.begin(); it != parts.end(); it++) { delete *it; *it = 0; // Prevent multiple deletes } } ostream& operator<<(ostream& os, Bicycle* b) { copy(b->parts.begin(), b->parts.end(), ostream_iterator(os, "\n")); os << "--------" << endl; return os; } void Bicycle::print(vector& vb, ostream& os) { copy(vb.begin(), vb.end(), ostream_iterator(os, "\n")); cout << "--------" << endl; } ///:~