bicycle.cpp

来自「Thinking_in_C___2.rar」· C++ 代码 · 共 66 行

CPP
66
字号
//: 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 + =
减小字号Ctrl + -
显示快捷键?