VasilM

oop_object_factory

May 13th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class BadShapeCreation {
  7.     string type;
  8. public:
  9.     BadShapeCreation(string type):type(type) {}
  10.     string what() { return "Cannot create type " + type; }
  11. };
  12.  
  13. class Shape {
  14. public:
  15.     virtual void draw() = 0;
  16.     virtual void erase() = 0;
  17.     virtual ostream& ins(ostream& os)const = 0;
  18.     virtual ~Shape() {}
  19.     static Shape* factory(const char* type) throw (BadShapeCreation);
  20. };
  21. ostream& operator<<(ostream& os, const Shape& obj) { return obj.ins(os); }
  22.  
  23. class Circle: public Shape {
  24.     static int counter;
  25.     int no;
  26.     Circle() { no=++counter; }
  27.     friend class Shape;
  28. public:
  29.     void draw() { cout <<"Circle "<< no <<" :draw\n"; }
  30.     void erase() { cout <<"Circle "<< no <<" :erase\n"; }
  31.     ~Circle() { cout <<"Circle "<< no <<" ~Circle\n"; }
  32.     ostream& ins(ostream& os) const { return os <<"Circle "<< no; }
  33. };
  34. int Circle::counter=0;
  35.  
  36. class Triangle: public Shape {
  37.     static int counter;
  38.     int no;
  39.     Triangle() { no=++counter; }
  40.     friend class Shape;
  41. public:
  42.     void draw() { cout <<"Triangle "<< no <<" :draw\n"; }
  43.     void erase() { cout <<"Triangle "<< no <<" :erase\n"; }
  44.     ~Triangle() { cout <<"Triangle "<< no <<" ~Triangle\n"; }
  45.     ostream& ins(ostream& os) const { return os<<"Triangle "<<no; }
  46. };
  47. int Triangle::counter=0;
  48.  
  49. class Square: public Shape {
  50.     static int counter;
  51.     int no;
  52.     Square() { no=++counter; }
  53.     friend Shape* getObjS();
  54. public:
  55.     void draw() { cout <<"Square "<< no <<" :draw\n"; }
  56.     void erase() { cout <<"Square "<< no <<" :erase\n"; }
  57.     ~Square() { cout <<"Square "<< no <<" ~Square\n"; }
  58.     ostream& ins(ostream& os) const { return os<<"Square "<<no; }
  59. };
  60. int Square::counter=0;
  61.  
  62. Shape* getObjS() { return new Square; }
  63.  
  64. Shape* Shape::factory( const char* type ) throw (BadShapeCreation) {
  65.     switch(type[0]) {
  66.     case 'C': return new Circle;
  67.     case 'T': return new Triangle;
  68.     case 'S': return getObjS();
  69.     default: throw BadShapeCreation(type);
  70.     }
  71. }
  72.  
  73. char * askForType() {
  74.     char* s[] = { "Circle", "Square", "Triangle", "None" };
  75.     return s[rand()%4];
  76. }
  77.  
  78. int main() {
  79.     Shape ** shapes;
  80.     cout <<"Enter the number of objects: ";
  81.     int n;
  82.     cin >> n;
  83.     shapes = new Shape *[n];
  84.  
  85.     for(int i=0; i<n; i++) {
  86.         Shape * s;
  87.         try {
  88.             s = Shape::factory(askForType());
  89.         } catch(BadShapeCreation e) {
  90.             cout << e.what() << endl;
  91.             i--;
  92.             continue;
  93.         }
  94.         shapes[i] = s;
  95.     }
  96.  
  97.     cout<<"\n The Objects Are:\n";
  98.     for(int i=0; i<n; i++) {
  99.         cout << i <<"   "<<*shapes[i]<<endl;
  100.     }
  101.  
  102.     cout<<"\n Operating the objects:\n";
  103.     for(int i=0; i<n; i++) {
  104.         shapes[i]->draw();
  105.         shapes[i]->erase();
  106.     }
  107.  
  108.     cout<<"\n Purging the objects:\n";
  109.     for(int i=0; i<n; i++) delete shapes[i];
  110.  
  111.     system("PAUSE");
  112.     return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment