⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recycle4.cpp

📁 Think in C++ 第二版源码
💻 CPP
字号:
//: C25:Recycle4.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// 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 <fstream>

#include <vector>

using namespace std;

ofstream out("Recycle4.out");



class TBin : public vector<Trash*> {

public:

  virtual bool grab(Trash*) = 0;

};



template<class TrashType>

class TrashBin : public TBin {

public:

  bool grab(Trash* t) {

    TrashType* tp = dynamic_cast<TrashType*>(t);

    if(!tp) return false; // Not grabbed

    push_back(tp);

    return true; // Object grabbed

  }

};



class TrashSorter : public vector<TBin*> {

public:

  bool sort(Trash* t) {

    for(iterator it = begin(); it != end(); it++)

      if((*it)->grab(t))

        return true;

    return false;

  }

  void sortBin(vector<Trash*>& bin) {

    vector<Trash*>::iterator it;

    for(it = bin.begin(); it != bin.end(); it++)

      if(!sort(*it))

        cerr << "bin not found" << endl;

  }

  ~TrashSorter() { purge(*this); }

};



int main() {

  vector<Trash*> bin;

  // Fill up the Trash bin:

  fillBin("Trash.dat", bin);

  TrashSorter tbins;

  tbins.push_back(new TrashBin<Aluminum>());

  tbins.push_back(new TrashBin<Paper>());

  tbins.push_back(new TrashBin<Glass>());

  tbins.push_back(new TrashBin<Cardboard>());

  tbins.sortBin(bin);

  for(TrashSorter::iterator it = tbins.begin(); 

    it != tbins.end(); it++)

    sumValue(**it);

  sumValue(bin);

  purge(bin);

} ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -