stlshape.cpp

来自「Thinking in C++ 2nd edition source code 」· C++ 代码 · 共 52 行

CPP
52
字号
//: C20:Stlshape.cpp
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Simple shapes w/ STL
#include <vector>
#include <iostream>
using namespace std;

class shape {
public:
  virtual void draw() = 0;
  virtual ~shape() {};
};

class circle : public shape {
public:
  void draw() { cout << "circle::draw\n"; }
  ~circle() { cout << "~circle\n"; }
};

class triangle : public shape {
public:
  void draw() { cout << "triangle::draw\n"; }
  ~triangle() { cout << "~triangle\n"; }
};

class square : public shape {
public:
  void draw() { cout << "square::draw\n"; }
  ~square() { cout << "~square\n"; }
};

typedef std::vector<shape*> container;
typedef container::iterator iter;

int main() {
  container shapes;
  shapes.push_back(new circle);
  shapes.push_back(new square);
  shapes.push_back(new triangle);
  for(iter i = shapes.begin();
      i != shapes.end(); i++)
    (*i)->draw();
  // ... Sometime later:
  for(iter i = shapes.begin();
      i != shapes.end(); i++)
    delete *i;
  return 0;
} ///:~

⌨️ 快捷键说明

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