📄 bicycle.cpp
字号:
//: C21:Bicycle.cpp {O}
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Bicycle implementation
#include <algorithm>
#include <cassert>
#include "Bicycle.h"
using namespace std;
BicyclePart::LeakChecker BicyclePart::lc;
int Bicycle::counter = 0;
Bicycle::Bicycle() : id(counter++) {
BicyclePart *bp[] = {
new Part<Frame>, new Part<Wheels>,
new Part<Seat>, new Part<HandleBars>,
new Part<Sprockets>, new Part<Deraileur>,
};
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();
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;
}
Bicycle::~Bicycle() {
purge();
}
ostream& operator<<(ostream& os, Bicycle* b) {
copy(b->parts.begin(), b->parts.end(),
ostream_iterator<BicyclePart*>(os, "\n"));
os << "--------" << endl;
return os;
}
void Bicycle::print(vector<Bicycle*>& vb,
ostream& os) {
copy(vb.begin(), vb.end(),
ostream_iterator<Bicycle*>(os, "\n"));
cout << "--------" << endl;
}
// Both the Bicycle and the
// generator should provide more variety than
// this. But I hope you get the idea.
Bicycle* BicycleGenerator::operator()() {
return new Bicycle();
} ///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -